Skip to content

Instantly share code, notes, and snippets.

@appcypher
Last active September 29, 2021 06:54
Show Gist options
  • Select an option

  • Save appcypher/46bf53357eef17188302ae116b67fb59 to your computer and use it in GitHub Desktop.

Select an option

Save appcypher/46bf53357eef17188302ae116b67fb59 to your computer and use it in GitHub Desktop.
JOMI

(1.) Stripe supports signing requests made to your webhook with a signature set in the Stripe-Signature header.

Stripe provides multiple official libraries in different languages for verifying signatures and you also need an API key for a successful verification.

You can create new API keys at https://dashboard.stripe.com/apikeys.

const stripe = require('stripe')('sk_test_26PHem9AhJZvU623DfE1x4sd'); // Set API Key.

try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret); // Verify signature.
}
    catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
}

(2.) After creating the products in the dashboard or CLI, you also have to create the prices.

In this case a recurring (say monthly) $30 subscription.

stripe prices create \
    -d product=prod_H94k5odtwJXMtQ \
    -d unit_amount=30 \
    -d currency=usd \
    -d "recurring[interval]"=month

Note the price id as you are going to need it to create recurring subscriptions.

You can programmatically create and add billable customers to your stripe account.

try {
    await fetch('/create-customer', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            email: billingEmail,
        }),
    })
} catch err {
    ... // Handle errors.
}

Collecting a payment info is equally trivial. The next step is saving payment method and creating a subscription.

stripe
    .createPaymentMethod({
        type: 'card',
        card: card,
        billing_details: {
            name: billingName,
        },
    })
    .then((result) => {
        if (result.error) {
            displayError(result);
        } else {
            createSubscription({
                customerId: customerId,
                paymentMethodId: result.paymentMethod.id,
                priceId: priceId,
            });
        }
    });
function createSubscription({ customerId, paymentMethodId, priceId }) {
  return (
    fetch('/create-subscription', {
      method: 'post',
      headers: {
        'Content-type': 'application/json',
      },
      body: JSON.stringify({
        customerId: customerId,
        paymentMethodId: paymentMethodId,
        priceId: priceId,
      }),
    })
      ... // Handle response and errors.
  );
}

(3.)

db.inventory.find( { country: "india" } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment