From 4fab5d89fb35d1cf2fe7db424cb4b6c4a1a1eeb8 Mon Sep 17 00:00:00 2001 From: Blair Currey <12960453+BlairCurrey@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:50:11 -0400 Subject: [PATCH] feat(backend): add local payment method to payment method handler --- packages/backend/src/index.ts | 3 +- .../payment-method/handler/service.test.ts | 61 +++++++++++++++++++ .../src/payment-method/handler/service.ts | 13 ++-- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 1f429f2262..db0b4515af 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -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') }) }) diff --git a/packages/backend/src/payment-method/handler/service.test.ts b/packages/backend/src/payment-method/handler/service.test.ts index 2a80feed5c..a28e20b2b4 100644 --- a/packages/backend/src/payment-method/handler/service.test.ts +++ b/packages/backend/src/payment-method/handler/service.test.ts @@ -15,12 +15,14 @@ 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 let appContainer: TestContainer let paymentMethodHandlerService: PaymentMethodHandlerService let ilpPaymentService: IlpPaymentService + let localPaymentService: LocalPaymentService beforeAll(async (): Promise => { deps = initIocContainer(Config) @@ -28,6 +30,7 @@ describe('PaymentMethodHandlerService', (): void => { paymentMethodHandlerService = await deps.use('paymentMethodHandlerService') ilpPaymentService = await deps.use('ilpPaymentService') + localPaymentService = await deps.use('localPaymentService') }) afterEach(async (): Promise => { @@ -64,6 +67,30 @@ describe('PaymentMethodHandlerService', (): void => { expect(ilpPaymentServiceGetQuoteSpy).toHaveBeenCalledWith(options) }) + test('calls lcaolPaymentService for local payment type', async (): Promise => { + 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 => { @@ -101,5 +128,39 @@ describe('PaymentMethodHandlerService', (): void => { expect(ilpPaymentServicePaySpy).toHaveBeenCalledWith(options) }) + test('calls localPaymentService for local payment type', async (): Promise => { + 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) + }) }) }) diff --git a/packages/backend/src/payment-method/handler/service.ts b/packages/backend/src/payment-method/handler/service.ts index 670d46ac4a..c7bb2dfc0e 100644 --- a/packages/backend/src/payment-method/handler/service.ts +++ b/packages/backend/src/payment-method/handler/service.ts @@ -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 @@ -33,7 +34,7 @@ export interface PaymentMethodService { pay(payOptions: PayOptions): Promise } -export type PaymentMethod = 'ILP' +export type PaymentMethod = 'ILP' | 'LOCAL' export interface PaymentMethodHandlerService { getQuote( @@ -45,12 +46,14 @@ export interface PaymentMethodHandlerService { interface ServiceDependencies extends BaseService { ilpPaymentService: IlpPaymentService + localPaymentService: LocalPaymentService } export async function createPaymentMethodHandlerService({ logger, knex, - ilpPaymentService + ilpPaymentService, + localPaymentService }: ServiceDependencies): Promise { const log = logger.child({ service: 'PaymentMethodHandlerService' @@ -58,11 +61,13 @@ export async function createPaymentMethodHandlerService({ 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 {