paddle-node-sdk icon indicating copy to clipboard operation
paddle-node-sdk copied to clipboard

[Feature]: Way to test webhook signature

Open motz0815 opened this issue 1 year ago • 6 comments

Tell us about your feature request

I'd like to test my paddle webhooks locally while using the unmarshal function of the sdk. It would be great to have a way to put that function into test mode, or providing some other way to test handling the event while in development.

What problem are you looking to solve?

In development I have the problem of not really having a way to sign my test requests so that I can use the unmarshal function.

Additional context

No response

How important is this suggestion to you?

Important

motz0815 avatar May 07 '24 17:05 motz0815

My current solution was a python script with the body at body.json in the same folder (if anyone else needs this)

# send a POST request to localhost:3000/api/paddle imitating a paddle webhook notification
# with the JSON body at body.json
import requests
import json
import time
import hmac
import hashlib

url = "http://localhost:3000/api/paddle"

# construct the paddle signature header with timestamp and sha256-hmac body hash

# unix timestamp
timestamp = int(time.time())

# read the body from body.json
with open("body.json", "r") as f:
    body = f.read()

# construct the hmac signature
key = "YOUR_PADDLE_WEBHOOK_SECRET_KEY"

payloadwithtimestamp = str(timestamp) + ":" + body

signature = hmac.new(key.encode(), payloadwithtimestamp.encode(), hashlib.sha256).hexdigest()

headers = {
    "Content-Type": "application/json",
    "paddle-signature": "ts=" + str(timestamp) + ";h1=" + signature
}

# send the POST request
response = requests.post(url, headers=headers, data=body)
print(response.text)

motz0815 avatar May 07 '24 18:05 motz0815

Hi @motz0815, Thank you for raising this feature request.

I can understand the problem you are facing. I am checking with our team on the best way of handling this. I will get back to you once i have a proper solution that will help improve the ways of working with our webhook during local development.

vijayasingam-paddle avatar May 08 '24 18:05 vijayasingam-paddle

Yes, I'm encountering the same problem.

I have a question: Will this package function properly when using Next.js deployed on Vercel's infrastructure?

I'm uncertain about how and where Vercel deploys my backend API endpoint. If they don't deploy it on a Node.js runtime, it wouldn't work, correct?

Waishnav avatar Jul 23 '24 18:07 Waishnav

Hi @Waishnav, I can confirm that you will be able to create an API route in Next.js to receive webhooks, deploy it to Vercel and use our SDK to unmarshal the data.

We have some internal test apps deployed in this pattern.

I'm uncertain about how and where Vercel deploys my backend API endpoint.

Vercel defaults to Node.js runtime and users have an option to configure if they want something else. You can read Vercel's documentation to learn more about this.

Please let us know if you have any other question.

Thank you.

vijayasingam-paddle avatar Jul 24 '24 08:07 vijayasingam-paddle

I did something like this:

import { Environment, Paddle, EventName, EventEntity, SubscriptionCreatedEvent, SubscriptionUpdatedEvent, SubscriptionCanceledEvent } from '@paddle/paddle-node-sdk';

const makePaddleEventObject = (event: any) => {
    if (event.event_type === EventName.SubscriptionCreated) {
        return new SubscriptionCreatedEvent(event);
    }
    if (event.event_type === EventName.SubscriptionUpdated) {
        return new SubscriptionUpdatedEvent(event);
    }
    if (event.event_type === EventName.SubscriptionCanceled) {
        return new SubscriptionCanceledEvent(event);
    }
}

const rawBody = await req.text();
const event = makePaddleEventObject(JSON.parse(rawBody));

dhamaniasad avatar Jul 31 '24 15:07 dhamaniasad

Hi! We're currently working on something that may help with this: a webhook simulator, for firing test events. It should launch within the next few weeks.

StarfallProjects avatar Aug 20 '24 13:08 StarfallProjects

Hi! There is now first class support for simulating webhooks, you can read more about it in the docs: https://developer.paddle.com/webhooks/test-webhooks

danbillson avatar Oct 15 '24 15:10 danbillson