Skip to content

Commit

Permalink
fix(backend): throw error in pay if incoming payment is not pending
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Oct 16, 2024
1 parent 63011ae commit bd12e30
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
44 changes: 43 additions & 1 deletion packages/backend/src/payment-method/local/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { AccountingService } from '../../accounting/service'
import { truncateTables } from '../../tests/tableManager'
import { createOutgoingPaymentWithReceiver } from '../../tests/outgoingPayment'
import { OutgoingPayment } from '../../open_payments/payment/outgoing/model'
import { IncomingPayment } from '../../open_payments/payment/incoming/model'
import {
IncomingPayment,
IncomingPaymentState
} from '../../open_payments/payment/incoming/model'
import { IncomingPaymentService } from '../../open_payments/payment/incoming/service'
import { errorToMessage, TransferError } from '../../accounting/errors'
import { PaymentMethodHandlerError } from '../handler/errors'
Expand Down Expand Up @@ -451,6 +454,45 @@ describe('LocalPaymentService', (): void => {
}
})

test('throws error if incoming payment state is not pending', async (): Promise<void> => {
const { receiver, outgoingPayment } =
await createOutgoingPaymentWithReceiver(deps, {
sendingWalletAddress: walletAddressMap['USD'],
receivingWalletAddress: walletAddressMap['USD'],
method: 'ilp',
quoteOptions: {
debitAmount: {
value: 100n,
assetScale: walletAddressMap['USD'].asset.scale,
assetCode: walletAddressMap['USD'].asset.code
}
}
})

jest.spyOn(incomingPaymentService, 'get').mockResolvedValueOnce({
state: IncomingPaymentState.Processing
} as IncomingPayment)

expect.assertions(4)
try {
await localPaymentService.pay({
receiver,
outgoingPayment,
finalDebitAmount: 100n,
finalReceiveAmount: 100n
})
} catch (err) {
expect(err).toBeInstanceOf(PaymentMethodHandlerError)
expect((err as PaymentMethodHandlerError).message).toBe(
'Bad Incoming Payment State'
)
expect((err as PaymentMethodHandlerError).description).toBe(
`Incoming Payment state should be ${IncomingPaymentState.Pending}`
)
expect((err as PaymentMethodHandlerError).retryable).toBe(false)
}
})

test('throws InsufficientBalance when balance is insufficient', async (): Promise<void> => {
const { receiver, outgoingPayment } =
await createOutgoingPaymentWithReceiver(deps, {
Expand Down
44 changes: 23 additions & 21 deletions packages/backend/src/payment-method/local/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,31 @@ async function pay(
retryable: false
})
}
if (incomingPayment.state !== IncomingPaymentState.Pending) {
throw new PaymentMethodHandlerError('Bad Incoming Payment State', {
description: `Incoming Payment state should be ${IncomingPaymentState.Pending}`,
retryable: false
})
}

// TODO: remove incoming state check? perhaps only applies ilp account middleware where its checking many different things
if (incomingPayment.state === IncomingPaymentState.Pending) {
try {
await deps.accountingService.createLiquidityAccount(
incomingPayment,
LiquidityAccountType.INCOMING
try {
await deps.accountingService.createLiquidityAccount(
incomingPayment,
LiquidityAccountType.INCOMING
)
} catch (err) {
if (!(err instanceof AccountAlreadyExistsError)) {
deps.logger.error(
{ incomingPayment, err },
'Failed to create liquidity account for local incoming payment'
)
throw new PaymentMethodHandlerError(
'Received error during local payment',
{
description: 'Unknown error while trying to create liquidity account',
retryable: false
}
)
} catch (err) {
if (!(err instanceof AccountAlreadyExistsError)) {
deps.logger.error(
{ incomingPayment, err },
'Failed to create liquidity account for local incoming payment'
)
throw new PaymentMethodHandlerError(
'Received error during local payment',
{
description:
'Unknown error while trying to create liquidity account',
retryable: false
}
)
}
}
}

Expand Down

0 comments on commit bd12e30

Please sign in to comment.