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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export async function handleOutgoingPaymentCreated(
input: {
outgoingPaymentId: payment.id,
idempotencyKey: uuid(),
dataToTransmit: 'sample kyc data'
dataToTransmit: acc.name
}
}
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/backend/src/graphql/generated/graphql.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/backend/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions packages/backend/src/graphql/resolvers/liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3528,19 +3528,11 @@ describe('Liquidity Resolvers', (): void => {
assert.ok(outgoingPayment.debitAmount)
await expect(depositSpy).toHaveBeenCalledWith({
id: eventId,
account: expect.any(OutgoingPayment),
account: expect.objectContaining({
dataToTransmit
}),
amount: outgoingPayment.debitAmount.value
})
await expect(
accountingService.getBalance(outgoingPayment.id)
).resolves.toEqual(outgoingPayment.debitAmount.value)
await expect(
OutgoingPayment.query(knex).findById(outgoingPayment.id)
).resolves.toEqual(
expect.objectContaining({
dataToTransmit
})
)
})

test("Can't deposit for non-existent outgoing payment id", async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ input DepositOutgoingPaymentLiquidityInput {
outgoingPaymentId: String!
"Unique key to ensure duplicate or retried requests are processed only once. For more information, refer to [idempotency](https://rafiki.dev/apis/graphql/admin-api-overview/#idempotency)."
idempotencyKey: String!
"Data to be encrypted and sent to the receiver."
"Data to transmit to the recipient during payment."
dataToTransmit: String
}

Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/open_payments/payment/incoming/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { IncomingPaymentInitiationReason } from './types'
export enum IncomingPaymentEventType {
IncomingPaymentCreated = 'incoming_payment.created',
IncomingPaymentExpired = 'incoming_payment.expired',
IncomingPaymentCompleted = 'incoming_payment.completed'
IncomingPaymentCompleted = 'incoming_payment.completed',
IncomingPaymentPartialPaymentReceived = 'incoming_payment.partial_payment_received'
}

export enum IncomingPaymentState {
Expand Down
108 changes: 108 additions & 0 deletions packages/backend/src/open_payments/payment/incoming/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createDecipheriv, randomBytes } from 'node:crypto'
import assert from 'assert'
import { faker } from '@faker-js/faker'
import { Knex } from 'knex'
Expand Down Expand Up @@ -1118,6 +1119,113 @@ describe('Incoming Payment Service', (): void => {
})
})

describe('processPartialPayment', (): void => {
let incomingPayment: IncomingPayment

const dbEncryptionOverride: Partial<IAppConfig> = {
dbEncryptionSecret: randomBytes(32).toString('base64')
}

beforeEach(async (): Promise<void> => {
incomingPayment = await createIncomingPayment(deps, {
walletAddressId,
tenantId,
initiationReason: IncomingPaymentInitiationReason.Admin
})
})

afterEach(() => {
jest.restoreAllMocks()
})

test(
'can process partial payment for incoming payment',
withConfigOverride(
() => config,
dbEncryptionOverride,
async (): Promise<void> => {
const dataToTransmit = JSON.stringify({
data: faker.internet.email()
})
const processedPayment =
await incomingPaymentService.processPartialPayment(
incomingPayment.id,
dataToTransmit
)
assert.ok(!isIncomingPaymentError(processedPayment))

const webhookEvent = await IncomingPaymentEvent.query(knex)
.where({
incomingPaymentId: processedPayment.id,
type: IncomingPaymentEventType.IncomingPaymentPartialPaymentReceived
})
.withGraphFetched('webhooks')
.first()
assert.ok(webhookEvent)
assert.ok(webhookEvent.data.dataToTransmit)

const webhookDataToTransmit = JSON.parse(
webhookEvent.data.dataToTransmit as string
)
const decipher = createDecipheriv(
'aes-256-gcm',
Uint8Array.from(
Buffer.from(config.dbEncryptionSecret as string, 'base64')
),
webhookDataToTransmit.iv
)
decipher.setAuthTag(
Uint8Array.from(Buffer.from(webhookDataToTransmit.tag, 'base64'))
)
let decrypted = decipher.update(
webhookDataToTransmit.cipherText,
'base64',
'utf8'
)
decrypted += decipher.final('utf8')

expect(decrypted).toEqual(dataToTransmit)
expect(webhookEvent.webhooks).toHaveLength(1)
}
)
)

test(
'does not encrypt transmitted data without configured encryption secret',
withConfigOverride(
() => config,
{
...dbEncryptionOverride,
dbEncryptionSecret: undefined
},
async (): Promise<void> => {
const dataToTransmit = JSON.stringify({
data: faker.internet.email()
})

const processedPayment =
await incomingPaymentService.processPartialPayment(
incomingPayment.id,
dataToTransmit
)
assert.ok(!isIncomingPaymentError(processedPayment))

const webhookEvent = await IncomingPaymentEvent.query(knex)
.where({
incomingPaymentId: processedPayment.id,
type: IncomingPaymentEventType.IncomingPaymentPartialPaymentReceived
})
.withGraphFetched('webhooks')
.first()
assert.ok(webhookEvent)

expect(webhookEvent.data.dataToTransmit).toEqual(dataToTransmit)
expect(webhookEvent.webhooks).toHaveLength(1)
}
)
)
})

describe('getPage', (): void => {
let receiverWalletAddress: MockWalletAddress
let assetId: string
Expand Down
47 changes: 45 additions & 2 deletions packages/backend/src/open_payments/payment/incoming/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { Amount } from '../../amount'
import { IncomingPaymentError } from './errors'
import { IAppConfig } from '../../../config/app'
import { poll } from '../../../shared/utils'
import { encryptDbData, poll } from '../../../shared/utils'
import { AssetService } from '../../../asset/service'
import { finalizeWebhookRecipients } from '../../../webhook/service'
import { Pagination, SortOrder } from '../../../shared/baseModel'
Expand Down Expand Up @@ -78,6 +78,10 @@ export interface IncomingPaymentService
update(
options: UpdateOptions
): Promise<IncomingPayment | IncomingPaymentError>
processPartialPayment(
id: string,
dataToTransmit?: string
): Promise<IncomingPayment | IncomingPaymentError>
}

export interface ServiceDependencies extends BaseService {
Expand Down Expand Up @@ -107,7 +111,9 @@ export async function createIncomingPaymentService(
getWalletAddressPage: (options) => getWalletAddressPage(deps, options),
processNext: () => processNextIncomingPayment(deps),
update: (options) => updateIncomingPayment(deps, options),
getPage: (options) => getPage(deps, options)
getPage: (options) => getPage(deps, options),
processPartialPayment: (id, dataToTransmit) =>
processPartialPayment(deps, id, dataToTransmit)
}
}

Expand Down Expand Up @@ -549,6 +555,43 @@ async function addReceivedAmount(
return payment
}

async function processPartialPayment(
deps: ServiceDependencies,
id: string,
dataToTransmit?: string
): Promise<IncomingPayment | IncomingPaymentError> {
const { config, knex } = deps

const incomingPayment = await IncomingPayment.query(knex)
.findById(id)
.withGraphFetched('asset')
if (!incomingPayment) return IncomingPaymentError.UnknownPayment

await IncomingPaymentEvent.query(knex).insertGraph({
incomingPaymentId: incomingPayment.id,
type: IncomingPaymentEventType.IncomingPaymentPartialPaymentReceived,
data: {
...incomingPayment.toData(0n),
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, we need to add a new property to the data that specifies the amount that was only sent in the "partial" payment, so the receiving ASE can make a decision whether to action it or not. The receivedAmount should remain unchanged, ie, it will include the cumulative amount received thus far for the incoming payment.

I will encapsulate this in a separate issue.

dataToTransmit:
dataToTransmit && config.dbEncryptionSecret
? encryptDbData(dataToTransmit, config.dbEncryptionSecret)
: dataToTransmit
},
tenantId: incomingPayment.tenantId,
webhooks: finalizeWebhookRecipients(
{
tenantIds: [incomingPayment.tenantId],
sendToPosService:
incomingPayment.initiatedBy === IncomingPaymentInitiationReason.Card
},
deps.config,
deps.logger
)
})

return incomingPayment
}

async function getPage(
deps: ServiceDependencies,
options?: GetPageOptions
Expand Down
2 changes: 1 addition & 1 deletion packages/card-service/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/frontend/app/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/mock-account-service-lib/src/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/point-of-sale/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/test-lib/src/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading