Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions localenv/happy-life-bank/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
GRAPHQL_URL: http://happy-life-bank-backend:3001/graphql
WEBHOOK_SIGNATURE_SECRET: iyIgCprjb9uL8wFckR+pLEkJWMB7FJhgkvqhTQR/964=
WEBHOOK_SIGNATURE_VERSION: 1
USE_HTTP: true
depends_on:
- shared-database
healthcheck:
Expand Down
2 changes: 1 addition & 1 deletion packages/point-of-sale/src/card-service-client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function createCardServiceClient({
axios
}: ServiceDependencies): Promise<CardServiceClient> {
const log = logger.child({
service: 'PosDeviceService'
service: 'CardServiceClient'
})
const deps: ServiceDependencies = {
logger: log,
Expand Down
3 changes: 2 additions & 1 deletion packages/point-of-sale/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export const Config = {
webhookSignatureVersion: envInt('WEBHOOK_SIGNATURE_VERSION', 1),
webhookSignatureSecret: envString('WEBHOOK_SIGNATURE_SECRET'),
webhookTimeoutMs: envInt('WEBHOOK_TIMEOUT_MS', 30000),
incomingPaymentExpiryMs: envInt('INCOMING_PAYMENT_EXPIRY_MS', 10000)
incomingPaymentExpiryMs: envInt('INCOMING_PAYMENT_EXPIRY_MS', 10000),
useHttp: envBool('USE_HTTP', false)
}
81 changes: 66 additions & 15 deletions packages/point-of-sale/src/payments/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,33 @@ describe('Payment Routes', () => {
let config: IAppConfig

function mockPaymentService() {
jest.spyOn(paymentService, 'getWalletAddress').mockResolvedValueOnce({
id: 'id',
assetCode: 'USD',
assetScale: 1,
authServer: 'authServer',
resourceServer: 'resourceServer',
cardService: 'cardService'
})
jest.spyOn(paymentService, 'createIncomingPayment').mockResolvedValueOnce({
id: 'incoming-payment-url',
url: faker.internet.url()
})
jest
const getWalletAddressSpy = jest
.spyOn(paymentService, 'getWalletAddress')
.mockResolvedValueOnce({
id: 'id',
assetCode: 'USD',
assetScale: 1,
authServer: 'authServer',
resourceServer: 'resourceServer',
cardService: 'cardService'
})

const createIncomingPaymentSpy = jest
.spyOn(paymentService, 'createIncomingPayment')
.mockResolvedValueOnce({
id: 'incoming-payment-url',
url: faker.internet.url()
})

const getWalletAddressIdByUrlSpy = jest
.spyOn(paymentService, 'getWalletAddressIdByUrl')
.mockResolvedValueOnce(faker.internet.url())

return {
getWalletAddressSpy,
createIncomingPaymentSpy,
getWalletAddressIdByUrlSpy
}
}

beforeAll(async () => {
Expand Down Expand Up @@ -159,6 +171,44 @@ describe('Payment Routes', () => {
}
)
)

test(
'falls back to http for payment request if configured',
withConfigOverride(
() => config,
{ useHttp: true },
async () => {
const senderWalletAddress = 'https://example.com/'

const ctx = createPaymentContext({ senderWalletAddress })

const { getWalletAddressSpy } = mockPaymentService()
jest
.spyOn(cardServiceClient, 'sendPayment')
.mockResolvedValueOnce(Result.APPROVED)

jest
.spyOn(webhookWaitMap, 'setWithExpiry')
.mockImplementationOnce((key, deferred) => {
deferred.resolve({
id: v4(),
type: 'incoming_payment.completed',
data: { id: key, completed: true }
})
return webhookWaitMap
})

await paymentRoutes.payment(ctx)
expect(getWalletAddressSpy).toHaveBeenCalledWith(
'http://example.com/'
)
expect(ctx.response.body).toEqual({
result: { code: Result.APPROVED }
})
expect(ctx.status).toBe(200)
}
)
)
})

describe('get incoming payments', (): void => {
Expand Down Expand Up @@ -328,7 +378,7 @@ describe('Payment Routes', () => {
})
})

function createPaymentContext() {
function createPaymentContext(bodyOverrides?: Record<string, unknown>) {
return createContext<PaymentContext>({
headers: { Accept: 'application/json' },
method: 'POST',
Expand All @@ -339,7 +389,8 @@ function createPaymentContext() {
receiverWalletAddress: faker.internet.url(),
senderWalletAddress: faker.internet.url(),
timestamp: new Date().getTime(),
amount: { assetScale: 2, assetCode: 'USD', value: '100' }
amount: { assetScale: 2, assetCode: 'USD', value: '100' },
...bodyOverrides
}
})
}
Expand Down
22 changes: 15 additions & 7 deletions packages/point-of-sale/src/payments/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,30 @@ async function payment(
const body = ctx.request.body
let incomingPaymentId: string | undefined
try {
const senderWalletAddressUrl = new URL(body.senderWalletAddress)

if (deps.config.useHttp) {
senderWalletAddressUrl.protocol = 'http:'
}

const senderWalletAddress = await deps.paymentService.getWalletAddress(
body.senderWalletAddress.replace(/^https:/, 'http:')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we had this for the local environment, putting this behind a flag

senderWalletAddressUrl.href
)

const receiverWalletAddressId =
await deps.paymentService.getWalletAddressIdByUrl(
body.receiverWalletAddress
)

const incomingPayment = await deps.paymentService.createIncomingPayment(
receiverWalletAddressId,
{
...body.amount,
const incomingPayment = await deps.paymentService.createIncomingPayment({
walletAddressId: receiverWalletAddressId,
incomingAmount: {
assetCode: body.amount.assetCode,
assetScale: body.amount.assetScale,
value: BigInt(body.amount.value)
}
)
},
senderWalletAddress: body.senderWalletAddress
})
const deferred = new Deferred<WebhookBody>()
webhookWaitMap.setWithExpiry(
incomingPayment.id,
Expand Down
17 changes: 12 additions & 5 deletions packages/point-of-sale/src/payments/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ describe('createPaymentService', () => {
const expiresAt = new Date(
now + mockConfig.incomingPaymentExpiryMs
).toISOString()
const senderWalletAddress = faker.internet.url()

const result = await service.createIncomingPayment(
const result = await service.createIncomingPayment({
walletAddressId,
incomingAmount
)
incomingAmount,
senderWalletAddress
})
expect(result).toEqual({ id: uuid, url: expectedUrl })
expect(mockApolloClient.mutate).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -78,7 +80,8 @@ describe('createPaymentService', () => {
idempotencyKey: expect.any(String),
incomingAmount,
isCardPayment: true,
walletAddressId
walletAddressId,
senderWalletAddress
})
})
})
Expand All @@ -97,7 +100,11 @@ describe('createPaymentService', () => {
assetScale: 2
}
await expect(
service.createIncomingPayment(walletAddressId, incomingAmount)
service.createIncomingPayment({
walletAddressId,
incomingAmount,
senderWalletAddress: faker.internet.url()
})
).rejects.toThrow(/Failed to create incoming payment/)
expect(mockLogger.error).toHaveBeenCalledWith(
{ walletAddressId },
Expand Down
28 changes: 16 additions & 12 deletions packages/point-of-sale/src/payments/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
GetWalletAddress,
GetWalletAddressVariables
} from '../graphql/generated/graphql'
import { FnWithDeps } from '../shared/types'
import { v4 } from 'uuid'
import { AxiosInstance, AxiosRequestConfig } from 'axios'
import { GET_WALLET_ADDRESS_BY_URL } from '../graphql/queries/getWalletAddress'
Expand Down Expand Up @@ -44,6 +43,12 @@ export type CreatedIncomingPayment = {
url: string
}

interface CreateIncomingPaymentArgs {
walletAddressId: string
incomingAmount: AmountInput
senderWalletAddress: string
}

type IncomingPaymentPage = Exclude<
Exclude<GetWalletAddress['walletAddressByUrl'], null>,
undefined
Expand All @@ -54,8 +59,7 @@ export type PaymentService = {
options: GetPaymentsQuery
) => Promise<IncomingPaymentPage>
createIncomingPayment: (
walletAddressId: string,
incomingAmount: AmountInput
args: CreateIncomingPaymentArgs
) => Promise<CreatedIncomingPayment>
getWalletAddress: (walletAddressUrl: string) => Promise<WalletAddress>
getWalletAddressIdByUrl: (walletAddressUrl: string) => Promise<string>
Expand All @@ -75,10 +79,8 @@ export function createPaymentService(
return {
getIncomingPayments: (options: GetPaymentsQuery) =>
getIncomingPayments(deps, options),
createIncomingPayment: (
walletAddressId: string,
incomingAmount: AmountInput
) => createIncomingPayment(deps, walletAddressId, incomingAmount),
createIncomingPayment: (args: CreateIncomingPaymentArgs) =>
createIncomingPayment(deps, args),
getWalletAddress: (walletAddressUrl: string) =>
getWalletAddress(deps, walletAddressUrl),
getWalletAddressIdByUrl: (walletAddressUrl: string) =>
Expand Down Expand Up @@ -107,10 +109,11 @@ async function getIncomingPayments(
return data?.walletAddressByUrl?.incomingPayments
}

const createIncomingPayment: FnWithDeps<
ServiceDependencies,
PaymentService['createIncomingPayment']
> = async (deps, walletAddressId, incomingAmount) => {
async function createIncomingPayment(
deps: ServiceDependencies,
args: CreateIncomingPaymentArgs
): Promise<CreatedIncomingPayment> {
const { walletAddressId, incomingAmount, senderWalletAddress } = args
const client = deps.apolloClient
const expiresAt = new Date(
Date.now() + deps.config.incomingPaymentExpiryMs
Expand All @@ -126,7 +129,8 @@ const createIncomingPayment: FnWithDeps<
incomingAmount,
idempotencyKey: v4(),
isCardPayment: true,
expiresAt
expiresAt,
senderWalletAddress
}
}
})
Expand Down
1 change: 1 addition & 0 deletions test/testenv/happy-life-bank/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@ services:
WEBHOOK_SIGNATURE_SECRET: iyIgCprjb9uL8wFckR+pLEkJWMB7FJhgkvqhTQR/964=
WEBHOOK_SIGNATURE_VERSION: 1
WEBHOOK_TIMEOUT_MS: 10_000
USE_HTTP: true
depends_on:
- shared-database
Loading