Skip to main content

Webhooks

This article teaches you how to use webhooks and consume them

Chain avatar
Written by Chain
Updated over 2 weeks ago

A webhook is an HTTP endpoint that receives events from Pocketsflow.

Webhooks allow you to be notified about payment events that happen in Pocketsflow and allow you to connect your own services to it.

Webhooks help you scale your business to new heights beyond Pocketsflow.

Creating your endpoint

To add you webhook, go to Pocketsflow and open Developers page and click New endpoint. Fill out the form and enter the URL you want Pocketsflow to call and the events it should to be called at:

The types of events

1. order.completed

{
"webhookId": "8479823987483902",
"product": {
"id": "prod_1234567890",
"variantId": "var_9876543210" // or null
},
"order": {
"id": "ord_5f4e3d2c1b"
},
"currency": "usd",
"customer": {
"email": "[email protected]"
},
"tax": {
"amount": 4.99, // $4.99
"rate": 0.08, // 8%
"ToCountry": "US",
"ToZip": "94105",
"ToState": "CA",
"percentage": 8.00,
"vatNumber": "EU123456789" // or null
},
"amount": 64.99, // $64.99 total
"amountBeforeTax": 60.00 // $60.00 before tax
}

2. order.refunded

const webhookData = { webhookId: "8479823987483902", product: { id: "prod_5432167890", variantId: null, // example with null variant }, order: { id: "ord_abc123def456", }, currency: "usd", customer: { email: "[email protected]", }, tax: { amount: 1.99, // $1.99 rate: 0.05, // 5% ToCountry: "GB", ToZip: "SW1A 1AA", ToState: null, // UK doesn't use states percentage: 5.00, vatNumber: "GB123456789", // UK VAT number }, amount: 41.99, // $41.99 total amountBeforeTax: 40.00, // $40.00 before tax };

3. customer.created

{
"webhookId": "8479823987483902",
"product": {
"id": "prod_5432167890",
"variantId": null // example with null variant
},
"order": {
"id": "ord_abc123def456"
},
"currency": "usd",
"customer": {
"email": "[email protected]"
},
"tax": {
"amount": 1.99, // $1.99
"rate": 0.05, // 5%
"ToCountry": "GB",
"ToZip": "SW1A 1AA",
"ToState": null, // UK doesn't use states
"percentage": 5.00,
"vatNumber": "GB123456789" // UK VAT number
},
"amount": 41.99, // $41.99 total
"amountBeforeTax": 40.00 // $40.00 before tax
}

4. product.created

{
"webhookId": "8479823987483902",
"_id": "65834f1b2c89d347abcd1234",
"name": "Digital Marketing Guide 2024",
"subtitle": "Complete Guide for Beginners",
"price": 29.99,
"payWant": false,
"minPrice": 19.99,
"maxPrice": 49.99,
"description": "Learn digital marketing from scratch with this comprehensive guide.",
"file": "path/to/guide.pdf",
"isFile": true,
"url": "",
"thumbnail": "https://storage.pocketsflow.com/thumbnails/guide-cover.jpg",
"images": [
"https://storage.pocketsflow.com/images/preview1.jpg",
"https://storage.pocketsflow.com/images/preview2.jpg"
],
"published": true,
"design": 1,
"hasProductPage": "yes",
"colors": [
{
"id": "title",
"name": "Title",
"value": "#000000"
},
{
"id": "buttons",
"name": "Buttons background",
"value": "#6772e5"
},
{
"id": "buttonsText",
"name": "Buttons text",
"value": "#FFFFFF"
},
{
"id": "background",
"name": "Background",
"value": "#FFFFFF"
},
{
"id": "text",
"name": "Text",
"value": "#000000"
},
{
"id": "borders",
"name": "Borders",
"value": "#cfd7df"
},
{
"id": "heroBackground",
"name": "Hero background",
"value": "#6772e5"
},
{
"id": "cardBackground",
"name": "Card background",
"value": "#FFFFFF"
}
],
"ctaText": "buyForPrice",
"checkoutText": "Complete your purchase",
"thankYouText": "Thank you for buying! Check your email for the download link.",
"userId": "user_9876543210",
"showSales": true,
"showReviews": true,
"refundPolicy": "30-day money-back guarantee",
"createdAt": "2024-01-15T10:30:45.123Z",
"updatedAt": "2024-01-15T10:30:45.123Z"
}

5. product.updated

{
"webhookId": "8479823987483902",
"_id": "65834f1b2c89d347abcd1234",
"name": "Digital Marketing Guide 2024",
"subtitle": "Complete Guide for Beginners",
"price": 29.99,
"payWant": false,
"minPrice": 19.99,
"maxPrice": 49.99,
"description": "Learn digital marketing from scratch with this comprehensive guide.",
"file": "path/to/guide.pdf",
"isFile": true,
"url": "",
"thumbnail": "https://storage.pocketsflow.com/thumbnails/guide-cover.jpg",
"images": [
"https://storage.pocketsflow.com/images/preview1.jpg",
"https://storage.pocketsflow.com/images/preview2.jpg"
],
"published": true,
"design": 1,
"hasProductPage": "yes",
"colors": [
{
"id": "title",
"name": "Title",
"value": "#000000"
},
{
"id": "buttons",
"name": "Buttons background",
"value": "#6772e5"
},
{
"id": "buttonsText",
"name": "Buttons text",
"value": "#FFFFFF"
},
{
"id": "background",
"name": "Background",
"value": "#FFFFFF"
},
{
"id": "text",
"name": "Text",
"value": "#000000"
},
{
"id": "borders",
"name": "Borders",
"value": "#cfd7df"
},
{
"id": "heroBackground",
"name": "Hero background",
"value": "#6772e5"
},
{
"id": "cardBackground",
"name": "Card background",
"value": "#FFFFFF"
}
],
"ctaText": "buyForPrice",
"checkoutText": "Complete your purchase",
"thankYouText": "Thank you for buying! Check your email for the download link.",
"userId": "user_9876543210",
"showSales": true,
"showReviews": true,
"refundPolicy": "30-day money-back guarantee",
"createdAt": "2024-01-15T10:30:45.123Z",
"updatedAt": "2024-01-15T10:30:45.123Z"
}

6. review.created

{
"webhookId": "8479823987483902",
"_id": "65834f1b2c89d347def9876",
"userId": "user_9876543210",
"productId": "prod_1234567890",
"buyerEmail": "[email protected]",
"buyerId": "buyer_5678901234",
"saleId": "sale_abcdef123456",
"rating": 5,
"review": "Excellent digital product! The content was well-organized and very helpful. Exactly what I needed.",
"createdAt": "2024-01-15T14:22:15.123Z",
"updatedAt": "2024-01-15T14:22:15.123Z"
}

Other webhooks are:

"customer.created"

"customer.subscription.updated"

"customer.subscription.deleted"

"invoice.payment_succeeded"

"invoice.payment_failed"

"invoice.created"

"invoice.upcoming"

"payment_intent.succeeded"

"payment_intent.payment_failed"

"customer.subscription.trial_will_end"

"customer.subscription.pause"

"customer.subscription.resumed"

To view the body data of each of these other webhooks, log req.body object and use the appropriate values that you need.

Verifying the Webhook

To verify the webhook, use the secret that you got when creating it, and do the same as the following:

import express from 'express';
import crypto from 'crypto';

const app = express();
app.use(express.json());

// Your webhook secret from Pocketsflow
const WEBHOOK_SECRET = '${secret}';

app.post('/webhooks', (req, res) => {
// Get signature from headers
const receivedSignature = req.headers['x-pocketsflow-signature'];

// Generate expected signature
const expectedSignature = crypto
.createHmac('sha256', WEBHOOK_SECRET)
.update(JSON.stringify(req.body))
.digest('hex');

if (receivedSignature === expectedSignature) {
// Valid webhook from Pocketsflow
console.log('Valid webhook received:', req.body);
res.json({ status: 'success' });
} else {
// Invalid signature
console.error('Invalid webhook signature');
res.status(401).json({ error: 'Invalid signature' });
}
});

app.listen(3000, () => {
console.log('Webhook server listening on port 3000');
});

Note:

If you add any URL params to your product/checkout page, they will be sent inside the object metadata in the webhook data object.

For more programming languages please check the Add endpoint page and switch in the dropdown.

Did this answer your question?