logoPay4SaaS
Core Concepts

Webhook

Webhooks are real-time notifications sent from payment providers to your server. Successful payments, subscription status changes, renewal failures — all these events are communicated to your backend via Webhooks.

How It Works

User pays → Payment provider processes → Sends Webhook → Your /api/webhooks/{provider} → Updates database

Pay4SaaS has all Webhook receiving and processing logic built in — you just need to configure the Webhook URLs in your payment provider's dashboard.

Webhook Endpoints

ProviderEndpointConfiguration Location
Stripe/api/webhooks/stripeStripe Dashboard → Webhooks
PayPal/api/webhooks/paypalPayPal Developer → Webhooks
Creem/api/webhooks/creemCreem Dashboard → Webhooks
Alipay/api/webhooks/alipayAlipay Open Platform → App Gateway

Production Webhook URL format: https://yourdomain.com/api/webhooks/{provider}

Supported Events

Stripe

EventPurpose
checkout.session.completedCredits purchase, lifetime purchase, first subscription
customer.subscription.createdSubscription created (active or trial)
customer.subscription.updatedSubscription status change
customer.subscription.deletedSubscription canceled/expired
invoice.paidSubscription renewal success
invoice.payment_failedSubscription renewal failure

PayPal

EventPurpose
BILLING.SUBSCRIPTION.ACTIVATEDSubscription activated
BILLING.SUBSCRIPTION.CANCELLEDSubscription canceled
BILLING.SUBSCRIPTION.EXPIREDSubscription expired
BILLING.SUBSCRIPTION.SUSPENDEDSubscription suspended
PAYMENT.SALE.COMPLETEDPayment completed (renewal)
CHECKOUT.ORDER.APPROVEDOne-time purchase completed

Creem

EventPurpose
checkout.completedCheckout completed (credits/lifetime)
subscription.activeSubscription activated
subscription.trialingTrial started
subscription.paidRenewal success
subscription.canceledSubscription canceled
subscription.expiredSubscription expired

Webhook Signature Verification

Each payment provider has a signature verification mechanism to prevent forged requests:

  • Stripe: Uses STRIPE_WEBHOOK_SECRET to verify the stripe-signature header
  • PayPal: Uses PAYPAL_WEBHOOK_ID to verify via the PayPal API
  • Creem: Uses CREEM_WEBHOOK_SECRET to verify the HMAC signature
  • Alipay: Uses certificates to verify request signatures

All of these are already implemented in their respective Webhook handlers — you just need to configure the keys.

Local Debugging

During local development, payment providers cannot directly access localhost. You need to use a tunneling tool to create a public URL.

Use ngrok — a tunneling tool that temporarily exposes your local service to the public internet so payment providers can reach your Webhook endpoint. The free tier includes 20k requests/month, more than enough for development.

Important! If you're testing payments, you must have ngrok running. Without it, the payment flow won't work — providers can't send Webhook callbacks to localhost.

Download it here: https://ngrok.com/download/.

Sign up and log in, then go to https://dashboard.ngrok.com/get-started/your-authtoken to get your token.

Unzip and run: ngrok config add-authtoken your-token, then ngrok http + your project's localhost port. You'll see something like this:

Copy the forwarding URL and use it to configure Webhooks in your payment provider's dashboard.

Using ngrok

ngrok http 3000

This gives you a public URL (e.g., https://xxxx.ngrok.io), then:

  1. Set the Webhook URL in the payment provider's dashboard to https://xxxx.ngrok.io/api/webhooks/{provider}
  2. Start the project locally with npm run dev
  3. Trigger a payment and check the terminal logs

Important Notes

  • Remember to change the Webhook URL to your production domain when going live

On this page