-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add boilerplate payment webhook handlers
- Loading branch information
Showing
2 changed files
with
117 additions
and
35 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/pages/api/webhooks/payment-gateway-initialize-session.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { SaleorSyncWebhook } from "@saleor/app-sdk/handlers/next"; | ||
import { gql } from "urql"; | ||
import { PaymentGatewayInitializeSessionWebhookPayloadFragment } from "../../../../generated/graphql"; | ||
import { createClient } from "../../../lib/create-graphq-client"; | ||
import { saleorApp } from "../../../saleor-app"; | ||
|
||
/** | ||
* Example payload of the webhook. It will be transformed with graphql-codegen to Typescript type: PaymentGatewayInitializeSessionWebhookPayloadFragment | ||
*/ | ||
const PaymentGatewayInitializeSessionWebhookPayload = gql` | ||
fragment PaymentGatewayInitializeSessionWebhookPayload on PaymentGatewayInitializeSession { | ||
amount | ||
} | ||
`; | ||
|
||
/** | ||
* Top-level webhook subscription query, that will be attached to the Manifest. | ||
* Saleor will use it to register webhook. | ||
*/ | ||
const PaymentGatewayInitializeSession = gql` | ||
# Payload fragment must be included in the root query | ||
${PaymentGatewayInitializeSessionWebhookPayload} | ||
subscription PaymentGatewayInitializeSession { | ||
event { | ||
...PaymentGatewayInitializeSessionWebhookPayload | ||
} | ||
} | ||
`; | ||
|
||
/** | ||
* Create abstract Webhook. It decorates handler and performs security checks under the hood. | ||
* | ||
* paymentGatewayInitializeSessionWebhook.getWebhookManifest() must be called in api/manifest too! | ||
*/ | ||
export const paymentGatewayInitializeSessionWebhook = | ||
new SaleorSyncWebhook<PaymentGatewayInitializeSessionWebhookPayloadFragment>({ | ||
name: "Payment Gateway Initialize Session", | ||
webhookPath: "api/webhooks/payment-gateway-initialize-session", | ||
event: "PAYMENT_GATEWAY_INITIALIZE_SESSION", | ||
apl: saleorApp.apl, | ||
query: PaymentGatewayInitializeSession, | ||
}); | ||
|
||
/** | ||
* Export decorated Next.js handler, which adds extra context | ||
*/ | ||
export default paymentGatewayInitializeSessionWebhook.createHandler((req, res, ctx) => { | ||
const { | ||
/** | ||
* Access payload from Saleor - defined above | ||
*/ | ||
payload, | ||
/** | ||
* Saleor event that triggers the webhook (here - ORDER_CREATED) | ||
*/ | ||
event, | ||
/** | ||
* App's URL | ||
*/ | ||
baseUrl, | ||
/** | ||
* Auth data (from APL) - contains token and saleorApiUrl that can be used to construct graphQL client | ||
*/ | ||
authData, | ||
} = ctx; | ||
|
||
/** | ||
* Create GraphQL client to interact with Saleor API. | ||
*/ | ||
const client = createClient(authData.saleorApiUrl, async () => ({ token: authData.token })); | ||
|
||
/** | ||
* Now you can fetch additional data using urql. | ||
* https://formidable.com/open-source/urql/docs/api/core/#clientquery | ||
*/ | ||
|
||
// const data = await client.query().toPromise() | ||
|
||
/** | ||
* Inform Saleor that webhook was delivered properly. | ||
*/ | ||
return res.status(200).end(); | ||
}); | ||
|
||
/** | ||
* Disable body parser for this endpoint, so signature can be verified | ||
*/ | ||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters