# Testing Stripe webhook

{% hint style="info" %}
This tutorial assumes that you have created Stripe account & have access to your dashboard.
{% endhint %}

## 1. Creating a simple webhook receiver

Here we use flask to create a simple webhook receiver, If the webhook receives a POST it will respond with a message. The flask application is set to run in port 8080.&#x20;

{% hint style="danger" %}
Ideally, you would want to write statement for handling the events and exceptions with return codes.
{% endhint %}

```python
from flask import Flask, jsonify, request 
app = Flask(__name__)

@app.route('/', methods=['POST'])      #You may add other route eg: '/webhook'
def webhook():
    data = request.get_json(silent=True)
    print(data)                        #Prints payload
    return jsonify('Yey! Success')     #Returns happy message

if __name__ == '__main__': 
    app.run(port=8080)                 #Running on port 8080

```

You can then run your code.

```bash
python stripe_webhook.py
```

![](https://349678347-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LoSLso3EDTO5AwTtGuJ%2F-M7ERbNhypH_Ki5QHK4b%2F-M7EXEPvFkewKioDsvW8%2Fwebhook.png?alt=media\&token=25a4bc50-fd2e-4a31-9d73-42f2acdb4150)

{% hint style="danger" %}
this flask webhook app is meant for test use only, in production you would want to follow [best-practices](https://stripe.com/docs/webhooks/best-practices).
{% endhint %}

## 2. Exposing your stripe webhook

Since our webhook is running on port 8080, we can now expose this port with LocalXpose. If you want to use custom domain please visit ([creating a custom domain](https://docs.localxpose.io/tutorials/create-a-custom-domain-name)).

```bash
loclx tunnel http  --basic-auth stripeuser:stripepwd
# also using in-built basic authentication feature (Optional)
```

![](https://349678347-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LoSLso3EDTO5AwTtGuJ%2F-M7ERbNhypH_Ki5QHK4b%2F-M7Eavg5s0yLFaArI-kV%2Floclx.gif?alt=media\&token=7653c8fb-2c1e-462e-b529-68840238a679)

LocalXpose should provide you with http/https tunnel endpoints.

## 3. Register a webhook

Head over to [Stripe Dashboard](https://dashboard.stripe.com/) and click on **Developers** > **Webhooks**.

* Click on **'Add endpoint**'.
* Enter your LocalXpose URL in format *https\://\<username>:\<password>@\<localxposeurl>*
* Select event/s you want to generate.
* Save by clicking '**Add endpoin**t'.

![](https://349678347-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LoSLso3EDTO5AwTtGuJ%2F-M7ERbNhypH_Ki5QHK4b%2F-M7Ej-Z6XpqMQG2mB8s1%2Fezgif-5-e20fc4caa631.gif?alt=media\&token=d5b4f1cd-0d07-4ac7-8ef6-75529b37dae0)

## 4. Triggering Events (Dashboard)

In order to test the working of your setup, we can trigger webhook events.

* Click on '**Send test webhoo**k'
* Select the event you want to trigger.
* Confirm by clicking '**Send test webhook**'

![](https://349678347-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LoSLso3EDTO5AwTtGuJ%2F-M7ERbNhypH_Ki5QHK4b%2F-M7Enynj1eki4MshDPag%2Fevent.gif?alt=media\&token=637a654c-e686-436a-b566-b8cbca8a8311)

You should see the return message from our webhook '**Yey! Success'**.

## 5. Triggering Events (CLI)

Follow [these ](https://stripe.com/docs/stripe-cli#install)instructions to setup Stripe CLI in your machine.

### i) Login to your stripe CLI.

```bash
stripe login
> Your pairing code is: gems-pretty-holy-honor
> Press Enter to open up the browser (^C to quit)
```

### ii) Create a forwarding listener

```bash
stripe listen --forward-to https://stripeuser:stripepwd@hawlmbpb6j19.loclx.io
# Ideally you would want to setup a listner in server, but if you can't, you can
# forward using localxpose's url to forward webhook events like such.
```

### iii) Create a webhook event

```bash
stripe trigger payment_intent.created
```

You should receive the event and it will be forwarded to your webhook.

In this way you can expose your webhook and test against with Stripe's events. For developer's documentation refer to [stripe official docs](https://dashboard.stripe.com/).
