ย 
Znote (recipes)
ย ย Get Znote ย 

๐Ÿ’ณ Payment Integration with Stripe

Deploy a Stripe webhook server to manage post-payment actions.

ย 

๐Ÿ’ณ Payment Integration with Stripe

This template helps you:

  • Set up Stripe in development and production
  • Create a product and payment link
  • Handle checkout.session.completed events via webhook
  • Generate a license or trigger actions after payment

๐Ÿ“ฆ 1. Install Required Packages

npm install -S stripe express cors body-parser node-fetch

๐Ÿงช 2. Stripe Setup for Development

โœ… Step-by-step:

  1. Create a Product & Price (Test mode) ๐Ÿ‘‰ Stripe Test Products Copy the test price ID: price_XXXXX
  2. Get your Test API Key ๐Ÿ‘‰ Test API Keys Copy the stripe_key: sk_test_XXXXX
  3. Install Stripe CLI ๐Ÿ‘‰ Stripe CLI Docs In your terminal:Copy the webhook secret: whsec_XXXXX
  4. Create a Payment Link ๐Ÿ‘‰ Create Payment Link Add metadata โ†’ key: product, value: (e.g. pro-plan) Use test cards below to simulate success/failure.

๐Ÿ’ณ Test Card Numbers

Card TypeNumberResult
Success4242 4242 4242 4242Payment successful
Insufficient Funds4000 0000 0000 9995Payment failed

Expiration: any future date, CVC: 000

๐Ÿ‘‰ Manually trigger event if needed:

stripe trigger checkout.session.completed

๐Ÿš€ 3. Stripe Setup for Production

  1. Get Production API Key ๐Ÿ‘‰ Live API Keys
  2. Create Webhook Endpoint ๐Ÿ‘‰ Webhooks
    1. Leave URL blank until deployment (Znote job will provide it)
    2. Add events:
      1. checkout.session.completed
      2. checkout.session.async_payment_succeeded
      3. checkout.session.async_payment_failed
  3. Create Product & Payment Link As in development, add metadata.product field.

๐Ÿ” 4. Credentials File

Create stripe-properties.json with your keys:

{
  "stripe_key": "sk_test_XXXXX",
  "stripe_ws_secret": "whsec_XXXXX",
  "url_domain": "http://localhost:4242",
  "product1_price": "price_XXXXX"
}
const credentials = loadBlock('stripe');
_fs.writeFileSync(`${__dirname}/stripe-properties.json`, credentials);

๐Ÿง  5. Stripe Webhook Backend

//exec: node

const properties = require("./stripe-properties.json");

const fetch = require('node-fetch');
const stripe = require('stripe')(properties.stripe_key);
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');

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

const YOUR_DOMAIN = properties.url_domain;
const endpointSecret = properties.stripe_ws_secret;

app.get('/', (req, res) => res.sendStatus(200));

app.post('/webhook', bodyParser.raw({ type: 'application/json' }), (req, res) => {
  const sig = req.headers['stripe-signature'];
  const payload = req.body;

  let event;
  try {
    event = stripe.webhooks.constructEvent(payload, sig, endpointSecret);
  } catch (err) {
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  const session = event.data.object;
  console.log(`๐Ÿ“ฆ Received event: ${event.type}`);

  if (event.type === 'checkout.session.completed') {
    const product = session.metadata.product;
    const email = session.customer_details.email;

    console.log(`โœ… Payment success: ${email} bought ${product}`);
    // TODO: Generate license or deliver product here
  }

  res.sendStatus(200);
});

app.listen(4242, () => console.log('๐Ÿš€ Stripe backend running on http://localhost:4242'));

โœ… Next steps:

  • Export or deploy this job in Znote
  • Replace test credentials with live ones in production
  • Handle subscriptions or advanced product logic in the switch(event.type)

Related recipes