1 export const metadata = {
2 title: `Payment Webhook Events`,
3 }
4
5 # {metadata.title}
6
7 In this guide, you’ll learn how you can handle payment webhook events in your Medusa application and using the Payment Module.
8
9 ## What's a Payment Webhook Event?
10
11 A payment webhook event is a request sent from a third-party payment provider to your application. It indicates a change in a payment’s status.
12
13 This is useful in many cases such as:
14
15 - When a payment is processed (authorized or captured) asynchronously.
16 - When a payment is managed on the third-party payment provider's side.
17 - When a payment action on the frontend was interrupted, leading the payment to be processed without an order being created in the Medusa application.
18
19 So, it's essential to handle webhook events to ensure that your application is aware of updated payment statuses and can take appropriate actions.
20
21 ---
22
23 ## How to Handle Payment Webhook Events
24
25 ### Webhook Listener API Route
26
27 The Medusa application has a `/hooks/payment/[identifier]_[id]` API route out-of-the-box that allows you to listen to webhook events from third-party payment providers, where:
28
29 - `[identifier]` is the `identifier` static property defined in the payment provider class. For example, `stripe`.
30 - `[id]` is the `id` field set for the provider in `medusa-config.ts`. For example, `stripe`.
31
32 <Note title="Tip">
33
34 The route segment doesn't need the `pp_` prefix that appears in the full provider ID stored in the database. For a provider registered with `identifier = "myprovider"` and `id: "myprovider"`, the webhook route is `/hooks/payment/myprovider_myprovider`.
35
36 Starting from Medusa v2.21.2, the route also accepts the full provider ID with the `pp_` prefix, such as `/hooks/payment/pp_myprovider_myprovider`, and removes the `pp_` prefix internally. In earlier versions, the prefixed route fails to resolve the provider.
37
38 </Note>
39
40 For example, when integrating basic Stripe payments with the [Stripe Module Provider](../payment-provider/stripe/page.mdx), the webhook listener route is `/hooks/payment/stripe_stripe`.
41
42 You can use this webhook listener when configuring webhook events in your third-party payment provider.
43
44 ### getWebhookActionAndData Method
45
46 The webhook listener API route executes the [getWebhookActionAndData method](/references/payment/getWebhookActionAndData) of the Payment Module's main service. This method delegates handling of incoming webhook events to the relevant payment provider.
47
48 <Note title="Tip">
49
50 Payment providers have a similar [getWebhookActionAndData method](/references/payment/provider) to process the webhook event. So, if you're implementing a custom payment provider, make sure to implement it to handle webhook events.
51
52 </Note>
53
54 ![A diagram showcasing the steps of how the getWebhookActionAndData method words](https://res.cloudinary.com/dza7lstvk/image/upload/v1711567415/Medusa%20Resources/payment-webhook_seaocg.jpg)
55
56 If the `getWebhookActionAndData` method returns an `authorized` or `captured` action, the Medusa application will perform one of the following actions:
57
58 <Note>
59
60 View the full flow of the webhook event processing in the [processPaymentWorkflow](/references/medusa-workflows/processPaymentWorkflow) reference.
61
62 </Note>
63
64 - If the method returns an `authorized` action, Medusa will set the associated payment session to `authorized`.
65 - If the method returns a `captured` action, Medusa will set the associated payment session to `captured`.
66 - In either cases, if the cart associated with the payment session is not completed yet, Medusa will complete the cart.