Skip to content

Commit

Permalink
feat(backend): add local payment method to payment method handler
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Aug 15, 2024
1 parent 2e76ee2 commit 4fab5d8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ export function initIocContainer(
return createPaymentMethodHandlerService({
logger: await deps.use('logger'),
knex: await deps.use('knex'),
ilpPaymentService: await deps.use('ilpPaymentService')
ilpPaymentService: await deps.use('ilpPaymentService'),
localPaymentService: await deps.use('localPaymentService')
})
})

Expand Down
61 changes: 61 additions & 0 deletions packages/backend/src/payment-method/handler/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ import { createReceiver } from '../../tests/receiver'
import { IlpPaymentService } from '../ilp/service'
import { truncateTables } from '../../tests/tableManager'
import { createOutgoingPaymentWithReceiver } from '../../tests/outgoingPayment'
import { LocalPaymentService } from '../local/service'

describe('PaymentMethodHandlerService', (): void => {
let deps: IocContract<AppServices>
let appContainer: TestContainer
let paymentMethodHandlerService: PaymentMethodHandlerService
let ilpPaymentService: IlpPaymentService
let localPaymentService: LocalPaymentService

beforeAll(async (): Promise<void> => {
deps = initIocContainer(Config)
appContainer = await createTestApp(deps)

paymentMethodHandlerService = await deps.use('paymentMethodHandlerService')
ilpPaymentService = await deps.use('ilpPaymentService')
localPaymentService = await deps.use('localPaymentService')
})

afterEach(async (): Promise<void> => {
Expand Down Expand Up @@ -64,6 +67,30 @@ describe('PaymentMethodHandlerService', (): void => {

expect(ilpPaymentServiceGetQuoteSpy).toHaveBeenCalledWith(options)
})
test('calls lcaolPaymentService for local payment type', async (): Promise<void> => {
const asset = await createAsset(deps)
const walletAddress = await createWalletAddress(deps, {
assetId: asset.id
})

const options: StartQuoteOptions = {
walletAddress,
receiver: await createReceiver(deps, walletAddress),
debitAmount: {
assetCode: 'USD',
assetScale: 2,
value: 100n
}
}

const localPaymentServiceGetQuoteSpy = jest
.spyOn(localPaymentService, 'getQuote')
.mockImplementationOnce(jest.fn())

await paymentMethodHandlerService.getQuote('LOCAL', options)

expect(localPaymentServiceGetQuoteSpy).toHaveBeenCalledWith(options)
})
})

describe('pay', (): void => {
Expand Down Expand Up @@ -101,5 +128,39 @@ describe('PaymentMethodHandlerService', (): void => {

expect(ilpPaymentServicePaySpy).toHaveBeenCalledWith(options)
})
test('calls localPaymentService for local payment type', async (): Promise<void> => {
const asset = await createAsset(deps)
const walletAddress = await createWalletAddress(deps, {
assetId: asset.id
})
const { receiver, outgoingPayment } =
await createOutgoingPaymentWithReceiver(deps, {
sendingWalletAddress: walletAddress,
receivingWalletAddress: walletAddress,
method: 'ilp',
quoteOptions: {
debitAmount: {
assetCode: walletAddress.asset.code,
assetScale: walletAddress.asset.scale,
value: 100n
}
}
})

const options: PayOptions = {
receiver,
outgoingPayment,
finalDebitAmount: outgoingPayment.debitAmount.value,
finalReceiveAmount: outgoingPayment.receiveAmount.value
}

const localPaymentServicePaySpy = jest
.spyOn(localPaymentService, 'pay')
.mockImplementationOnce(jest.fn())

await paymentMethodHandlerService.pay('LOCAL', options)

expect(localPaymentServicePaySpy).toHaveBeenCalledWith(options)
})
})
})
13 changes: 9 additions & 4 deletions packages/backend/src/payment-method/handler/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WalletAddress } from '../../open_payments/wallet_address/model'
import { Receiver } from '../../open_payments/receiver/model'
import { BaseService } from '../../shared/baseService'
import { IlpPaymentService } from '../ilp/service'
import { LocalPaymentService } from '../local/service'

export interface StartQuoteOptions {
walletAddress: WalletAddress
Expand Down Expand Up @@ -33,7 +34,7 @@ export interface PaymentMethodService {
pay(payOptions: PayOptions): Promise<void>
}

export type PaymentMethod = 'ILP'
export type PaymentMethod = 'ILP' | 'LOCAL'

export interface PaymentMethodHandlerService {
getQuote(
Expand All @@ -45,24 +46,28 @@ export interface PaymentMethodHandlerService {

interface ServiceDependencies extends BaseService {
ilpPaymentService: IlpPaymentService
localPaymentService: LocalPaymentService
}

export async function createPaymentMethodHandlerService({
logger,
knex,
ilpPaymentService
ilpPaymentService,
localPaymentService
}: ServiceDependencies): Promise<PaymentMethodHandlerService> {
const log = logger.child({
service: 'PaymentMethodHandlerService'
})
const deps: ServiceDependencies = {
logger: log,
knex,
ilpPaymentService
ilpPaymentService,
localPaymentService
}

const paymentMethods: { [key in PaymentMethod]: PaymentMethodService } = {
ILP: deps.ilpPaymentService
ILP: deps.ilpPaymentService,
LOCAL: deps.localPaymentService
}

return {
Expand Down

0 comments on commit 4fab5d8

Please sign in to comment.