diff --git a/apps/simplex-store-service/src/StoreService/Providers.hs b/apps/simplex-store-service/src/StoreService/Providers.hs index 5031335f14..cc4a7beb04 100644 --- a/apps/simplex-store-service/src/StoreService/Providers.hs +++ b/apps/simplex-store-service/src/StoreService/Providers.hs @@ -38,12 +38,14 @@ data WebhookError = WebhookError Text | WebhookStale Text deriving (Eq, Show) -- rcvAmount is the total received on the invoice so far, not the amount of one payment. -- rcvDue is the provider's figure for what is still owed. -- rcvUnits is the crypto received in integer atomic units, Nothing for card money or crypto that could not be counted in them. +-- rcvInvoicedUnits is the crypto the invoice asks in the same units, with any network fee the provider added to it. -- rcvRate is the invoice's rate for crypto, as the provider wrote it. data Received = Received { rcvAmount :: CurrencyAmount, rcvCrypto :: Maybe Text, rcvDue :: Maybe Text, rcvUnits :: Maybe Int64, + rcvInvoicedUnits :: Maybe Int64, rcvRate :: Maybe Text, rcvConfirmed :: Confirmed, rcvUncreditable :: Maybe Uncreditable diff --git a/apps/simplex-store-service/src/StoreService/Providers/BTCPay.hs b/apps/simplex-store-service/src/StoreService/Providers/BTCPay.hs index 5ae3cbb07f..2bf94db221 100644 --- a/apps/simplex-store-service/src/StoreService/Providers/BTCPay.hs +++ b/apps/simplex-store-service/src/StoreService/Providers/BTCPay.hs @@ -437,13 +437,13 @@ paymentMethodsSignal now ref status additional policy body = -- Complete is the old API's status name and never matches, so it has no case here. invoiceSignal :: UTCTime -> Text -> Text -> Maybe Text -> Maybe Text -> [GPaymentMethod] -> Either ProviderError (Maybe PaymentSignal) invoiceSignal now ref status additional policy ms = do - (m@GPaymentMethod {gpmRate, gpmPaid, gpmDue, gpmPayments, gpmSettledThreshold}, cc) <- chooseMethod ref ms + (m@GPaymentMethod {gpmAmount, gpmRate, gpmPaid, gpmDue, gpmPayments, gpmSettledThreshold}, cc) <- chooseMethod ref ms rate <- priced "rate" gpmRate paid <- priced "paymentMethodPaid" gpmPaid let decimals = unitDecimals cc confirming = maybe False (`elem` map speedPolicyName confirmingPolicies) policy shown = cc /= CCXmr || maybe False (>= 1) gpmSettledThreshold - received = receivedOf decimals confirming shown (status == "Settled") rate paid gpmDue gpmPayments + received = receivedOf decimals confirming shown (status == "Settled") rate paid gpmAmount gpmDue gpmPayments -- BTCPay never settles an invoice it has closed, so only these tell that the whole amount reached it closedFunded | maybe False (`elem` closedInFullStatuses) additional = PaidInFull @@ -490,13 +490,15 @@ findMethod what ref methodId body = do -- | The confirmed crypto shares paymentMethodPaid's basis, whatever it nets out: all of it once the invoice or every payment is Settled, -- else the Settled payments' value capped at it. An amount with a non-zero digit past its unit leaves every figure in units out. -receivedOf :: Int -> Bool -> Bool -> Bool -> WireNum -> WireNum -> Maybe WireNum -> [GPayment] -> Received -receivedOf decimals confirming shown invoiceSettled rate paid due payments = +-- The invoiced units are the method's amount, with the network fee BTCPay adds to it. +receivedOf :: Int -> Bool -> Bool -> Bool -> WireNum -> WireNum -> Maybe WireNum -> Maybe WireNum -> [GPayment] -> Received +receivedOf decimals confirming shown invoiceSettled rate paid invoiced due payments = Received { rcvAmount = toMinorUnits (wnValue paid * wnValue rate), rcvCrypto = if wnValue paid > 0 then Just (wnText paid) else Nothing, rcvDue = nonNegative <$> due, rcvUnits = fst <$> counted, + rcvInvoicedUnits = toUnits . wnValue =<< invoiced, rcvRate = Just (wnText rate), rcvConfirmed = case uncreditable of Nothing -> Confirmed {cfCents = toMinorUnits (scientific (maybe 0 (toInteger . snd) counted) (negate decimals) * wnValue rate), cfUnits = snd <$> counted} diff --git a/apps/simplex-store-service/src/StoreService/Providers/Stripe.hs b/apps/simplex-store-service/src/StoreService/Providers/Stripe.hs index 52d387f3a5..0177245a3c 100644 --- a/apps/simplex-store-service/src/StoreService/Providers/Stripe.hs +++ b/apps/simplex-store-service/src/StoreService/Providers/Stripe.hs @@ -285,6 +285,7 @@ signalOf now IntentRead {irStatus, irAmountReceived, irChargeCreated} = rcvCrypto = Nothing, rcvDue = Nothing, rcvUnits = Nothing, + rcvInvoicedUnits = Nothing, rcvRate = Nothing, rcvConfirmed = Confirmed {cfCents = amountFrom confirmed, cfUnits = Nothing}, rcvUncreditable = Nothing diff --git a/apps/simplex-store-service/src/StoreService/Store/Invoices.hs b/apps/simplex-store-service/src/StoreService/Store/Invoices.hs index 7145a85ba1..2c787654ae 100644 --- a/apps/simplex-store-service/src/StoreService/Store/Invoices.hs +++ b/apps/simplex-store-service/src/StoreService/Store/Invoices.hs @@ -284,21 +284,23 @@ largerOf = "GREATEST" largerOf = "MAX" #endif --- Once a row is settled it stays settled, so a later pending update cannot overwrite it. The confirmed figures and received units --- only rise, so a settled sum that falls (a payment turned invalid, a reorg) leaves them; the invoice's rate is written once. +-- Once a row is settled it stays settled, so a later pending update cannot overwrite it. The confirmed figures and the received and +-- invoiced units only rise, so a settled sum that falls (a payment turned invalid, a reorg) leaves them; the invoice's rate is written once. qUpsertPayment :: Query qUpsertPayment = mkQuery $ "INSERT INTO @payments " <> "(payment_id, invoice_id, provider, provider_ref, amount, currency, crypto_paid, crypto_due, paid_in_full, status, " - <> "received_units, confirmed_amount, confirmed_units, invoice_rate, created_at, updated_at) " - <> "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " + <> "received_units, invoiced_units, confirmed_amount, confirmed_units, invoice_rate, created_at, updated_at) " + <> "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) " <> "ON CONFLICT (payment_id) DO UPDATE SET " <> "amount = " <> largerOf <> "(COALESCE(@payments.amount, 0), excluded.amount), " <> "received_units = " <> largerOfNullable "received_units" + <> ", invoiced_units = " + <> largerOfNullable "invoiced_units" <> ", confirmed_amount = " <> largerOf <> "(@payments.confirmed_amount, excluded.confirmed_amount), " @@ -665,11 +667,11 @@ upsertPayment db InvoiceRow {irInvoiceId, irProvider, irProviderRef, irCurrency} qUpsertPayment ( (invId, invId, textEncode irProvider, irProviderRef, amount) :. (irCurrency, rcvCrypto, rcvDue, if paidInFull then 1 :: Int else 0, paymentStatusText status) - :. (rcvUnits, confirmedCents, cfUnits, rcvRate, at, at) + :. (rcvUnits, rcvInvoicedUnits, confirmedCents, cfUnits, rcvRate, at, at) ) where InvoiceId invId = irInvoiceId - Received {rcvAmount = CurrencyAmount amount, rcvCrypto, rcvDue, rcvUnits, rcvRate, rcvConfirmed = Confirmed {cfCents = CurrencyAmount confirmedCents, cfUnits}} = received + Received {rcvAmount = CurrencyAmount amount, rcvCrypto, rcvDue, rcvUnits, rcvInvoicedUnits, rcvRate, rcvConfirmed = Confirmed {cfCents = CurrencyAmount confirmedCents, cfUnits}} = received at = truncateToSecond at' -- | Takes the invoice row's lock for the rest of the transaction; SQLite's transactions hold the whole database already. diff --git a/tests/Bots/StoreService/BTCPayTests.hs b/tests/Bots/StoreService/BTCPayTests.hs index b35002a1ab..7d10b89158 100644 --- a/tests/Bots/StoreService/BTCPayTests.hs +++ b/tests/Bots/StoreService/BTCPayTests.hs @@ -293,6 +293,7 @@ btcReceived cents crypto due units confirmedUnits confirmedCents = rcvCrypto = crypto, rcvDue = Just due, rcvUnits = Just units, + rcvInvoicedUnits = Just 50000, rcvRate = Just "108000.12", rcvConfirmed = Confirmed {cfCents = CurrencyAmount confirmedCents, cfUnits = Just confirmedUnits}, rcvUncreditable = Nothing @@ -665,6 +666,7 @@ xmrReceived = rcvCrypto = Just "0.32095000", rcvDue = Just "0.00000000", rcvUnits = Just 320950000000, + rcvInvoicedUnits = Just 320950000000, rcvRate = Just "168.25", rcvConfirmed = Confirmed {cfCents = CurrencyAmount 0, cfUnits = Just 0}, rcvUncreditable = Just UCNoConfirmationShown diff --git a/tests/Bots/StoreService/FakeBTCPay.hs b/tests/Bots/StoreService/FakeBTCPay.hs index 839198d5d3..9a20526cf6 100644 --- a/tests/Bots/StoreService/FakeBTCPay.hs +++ b/tests/Bots/StoreService/FakeBTCPay.hs @@ -107,6 +107,8 @@ data InvoiceState = InvoiceState isAdditionalStatus :: Text, isPaid :: Maybe Text, isDue :: Maybe Text, + -- | The method's amount, when a test sets one other than the fixture's. + isAmount :: Maybe Text, isSpeedPolicy :: Maybe Text, isPayments :: Maybe J.Value } @@ -239,14 +241,15 @@ patchInvoice invId InvoiceState {isStatus, isAdditionalStatus, isSpeedPolicy} v -- | The fixtures' payments were written for a settled invoice, so the payments follow the state instead. patchMethods :: InvoiceState -> J.Value -> J.Value -patchMethods InvoiceState {isStatus, isPaid, isDue, isPayments} = \case +patchMethods InvoiceState {isStatus, isPaid, isDue, isAmount, isPayments} = \case J.Array vs -> J.Array (fmap one vs) other -> other where paidStatus = if isStatus == "Settled" then "Settled" else "Processing" :: Text one = \case - J.Object o -> - let paid = fromMaybe (fixturePaid o) isPaid + J.Object fixture -> + let o = maybe fixture (\a -> KM.insert "amount" (J.String a) fixture) isAmount + paid = fromMaybe (fixturePaid o) isPaid -- With nothing received the whole amount is owed. The fixtures were written for a -- settled invoice, so without this a freshly created one would serve their `due` of -- zero and the fake would report nothing left to send on an unpaid invoice. @@ -314,6 +317,7 @@ fakeApp stv req respond = do isAdditionalStatus = fromMaybe (isAdditionalStatus inv) (textField "additionalStatus" o), isPaid = maybe (isPaid inv) Just (textField "paymentMethodPaid" o), isDue = maybe (isDue inv) Just (textField "due" o), + isAmount = maybe (isAmount inv) Just (textField "amount" o), isPayments = maybe (isPayments inv) Just (KM.lookup "payments" o) } writeTVar stv st {fsInvoices = M.insert invId inv' (fsInvoices st)} @@ -465,6 +469,7 @@ fakeApp stv req respond = do isAdditionalStatus = "None", isPaid = Just "0.00000000", isDue = Nothing, + isAmount = Nothing, isSpeedPolicy = J.decode body >>= objectField "checkout" >>= objectField "speedPolicy" >>= asText, isPayments = Nothing } @@ -484,7 +489,7 @@ intQuery :: ByteString -> Query -> Maybe Int intQuery k q = join (lookup k q) >>= readMaybe . B8.unpack stateKeys :: [Key] -stateKeys = ["status", "additionalStatus", "paymentMethodPaid", "due", "payments"] +stateKeys = ["status", "additionalStatus", "paymentMethodPaid", "due", "amount", "payments"] unknownKeys :: [Key] -> J.Object -> Maybe Text unknownKeys known o = case filter (`notElem` known) (KM.keys o) of diff --git a/tests/Bots/StoreService/StripeTests.hs b/tests/Bots/StoreService/StripeTests.hs index a3d5bdc48f..7934708884 100644 --- a/tests/Bots/StoreService/StripeTests.hs +++ b/tests/Bots/StoreService/StripeTests.hs @@ -96,6 +96,7 @@ cardReceived cents confirmed = rcvCrypto = Nothing, rcvDue = Nothing, rcvUnits = Nothing, + rcvInvoicedUnits = Nothing, rcvRate = Nothing, rcvConfirmed = Confirmed {cfCents = CurrencyAmount confirmed, cfUnits = Nothing}, rcvUncreditable = Nothing diff --git a/tests/Bots/StoreService/WebTests.hs b/tests/Bots/StoreService/WebTests.hs index 9686cfc46a..fa3d19552e 100644 --- a/tests/Bots/StoreService/WebTests.hs +++ b/tests/Bots/StoreService/WebTests.hs @@ -423,7 +423,7 @@ testMigrationOrder = do [(a, b) | (a, b) <- zip declared (drop 1 declared), a >= b] `shouldBe` [] let (badges, ours) = span (`elem` badgeMigrations) (map name storeServiceSchemaMigrations) badges `shouldBe` badgeMigrations - ours `shouldBe` ["20261013_names", "20261022_drop_taken_codes", "20261022_name_balances", "20261022_name_late_wait", "20261023_name_terms"] + ours `shouldBe` ["20261013_names", "20261022_drop_taken_codes", "20261022_name_balances", "20261022_name_late_wait", "20261023_name_terms", "20261027_name_invoiced_units"] where badgeMigrations = ["20260915_badge_service_schema", "20260918_badge_group_ops", "20260925_badge_store_receipts", "20261012_badge_subscriptions"] @@ -2440,6 +2440,7 @@ rcv amount crypto = rcvCrypto = crypto, rcvDue = Nothing, rcvUnits = Nothing, + rcvInvoicedUnits = Nothing, rcvRate = Nothing, rcvConfirmed = Confirmed {cfCents = CurrencyAmount 0, cfUnits = Nothing}, rcvUncreditable = Nothing