Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): add incoming payment created webhook event #1435

Merged
merged 6 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -6,6 +6,7 @@ import { apolloClient } from './apolloClient'
import { v4 as uuid } from 'uuid'

export enum EventType {
IncomingPaymentCreated = 'incoming_payment.created',
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to also add a handler in mock-account-servicing-entity/app/routes/webhooks.ts otherwise we return 400 from the MASE and the webhook keeps retrying

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. Thanks!

IncomingPaymentCompleted = 'incoming_payment.completed',
IncomingPaymentExpired = 'incoming_payment.expired',
OutgoingPaymentCreated = 'outgoing_payment.created',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@interledger/open-payments'

export enum IncomingPaymentEventType {
IncomingPaymentCreated = 'incoming_payment.created',
IncomingPaymentExpired = 'incoming_payment.expired',
IncomingPaymentCompleted = 'incoming_payment.completed'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ describe('Incoming Payment Service', (): void => {
${undefined} | ${false} | ${undefined} | ${undefined} | ${undefined}
${faker.internet.url({ appendSlash: false })} | ${true} | ${new Date(Date.now() + 30_000)} | ${'Test incoming payment'} | ${'#123'}
`('An incoming payment can be created', async (options): Promise<void> => {
await expect(
IncomingPaymentEvent.query(knex).where({
type: IncomingPaymentEventType.IncomingPaymentCreated
})
).resolves.toHaveLength(0)
const incomingPayment = await incomingPaymentService.create({
paymentPointerId,
...options,
Expand All @@ -84,6 +89,11 @@ describe('Incoming Payment Service', (): void => {
asset,
processAt: new Date(incomingPayment.expiresAt.getTime())
})
await expect(
IncomingPaymentEvent.query(knex).where({
type: IncomingPaymentEventType.IncomingPaymentCreated
})
).resolves.toHaveLength(1)
})

test('Cannot create incoming payment for nonexistent payment pointer', async (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ async function createIncomingPayment(
})
.withGraphFetched('[asset, paymentPointer]')

await IncomingPaymentEvent.query(trx || deps.knex).insert({
type: IncomingPaymentEventType.IncomingPaymentCreated,
data: incomingPayment.toData(BigInt(0))
})

return await addReceivedAmount(deps, incomingPayment, BigInt(0))
}

Expand Down Expand Up @@ -234,7 +239,7 @@ async function handleDeactivated(
: IncomingPaymentEventType.IncomingPaymentCompleted
deps.logger.trace({ type }, 'creating incoming payment webhook event')

await IncomingPaymentEvent.query(deps.knex).insertAndFetch({
await IncomingPaymentEvent.query(deps.knex).insert({
type,
data: incomingPayment.toData(amountReceived),
withdrawal: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const sendWebhookEvent = async (
amount: balance
}
: undefined
await PaymentEvent.query(deps.knex).insertAndFetch({
await PaymentEvent.query(deps.knex).insert({
type,
data: payment.toData({ amountSent, balance }),
withdrawal
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/openapi/webhooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ components:
type:
type: string
enum:
- incoming_payment.created
- incoming_payment.completed
- incoming_payment.expired
data:
Expand Down
7 changes: 7 additions & 0 deletions packages/documentation/docs/integration/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ The endpoint accepts a `POST` request with

| Value | Description |
| ---------------------------------- | --------------------------------------------------------------------------- |
| `incoming_payment.created` | Incoming payment has been created. |
| `incoming_payment.completed` | Incoming payment is complete and doesn't accept any incoming funds anymore. |
| `incoming_payment.expired` | Incoming payment is expired and doesn't accept any incoming funds anymore. |
| `outgoing_payment.created` | Outgoing payment was created. |
Expand All @@ -91,6 +92,12 @@ The `backend` package requires an environment variable called `WEBHOOK_URL` whic

### Event Handlers

#### `incoming_payment.created`

An [Open Payments](./glossary#open-payments) Incoming Payment was created. This is for information purposes only and does not require an action.
sabineschaller marked this conversation as resolved.
Show resolved Hide resolved

- Action: None

#### `incoming_payment.completed`

An [Open Payments](../reference/glossary.md#open-payments) Incoming Payment was completed, either manually or programmatically, i.e. it does not accept any incoming funds anymore. The Account Servicing Entity SHOULD withdraw all funds received and deposit them into the payee's account.
Expand Down