Giter VIP home page Giter VIP logo

example-stripe-integration's Introduction

Example Keygen + Stripe integration

The following web app is written in Node.js and shows how to integrate Keygen and Stripe together using webhooks. Much more could be done to automate e.g. license revocation when a subscription is canceled, etc.

This example application is not 100% production-ready, but it should get you 90% of the way there. You may need to add additional logging, error handling, as well as listening for additional webhook events.

๐Ÿšจ Don't want to host your own webhook server? Check out our Zapier integration.

Running the app

First up, configure a few environment variables:

# Stripe publishable key
export STRIPE_PUBLISHABLE_KEY="YOUR_STRIPE_PUBLISHABLE_KEY"

# Stripe secret key (don't share this!)
export STRIPE_SECRET_KEY="YOUR_STRIPE_SECRET_KEY"

# The Stripe plan to subscribe new customers to
export STRIPE_PLAN_ID="YOUR_STRIPE_PLAN_ID"

# Keygen product token (don't share this!)
export KEYGEN_PRODUCT_TOKEN="YOUR_KEYGEN_PRODUCT_TOKEN"

# Your Keygen account ID
export KEYGEN_ACCOUNT_ID="YOUR_KEYGEN_ACCOUNT_ID"

# The Keygen policy to use when creating licenses for new users
# after they successfully subscribe to a plan
export KEYGEN_POLICY_ID="YOUR_KEYGEN_POLICY_ID"

You can either run each line above within your terminal session before starting the app, or you can add the above contents to your ~/.bashrc file and then run source ~/.bashrc after saving the file.

Next, install dependencies with yarn:

yarn

Then start the app:

yarn start

Configuring the webhooks

For local development, create an ngrok tunnel to your local development server:

ngrok http 8080

Next up, add the generated ngrok URL to your Stripe and Keygen accounts to listen for webhooks.

  1. Stripe: add https://{YOUR_NGROK_URL}/stripe-webhooks to https://dashboard.stripe.com/account/webhooks
  2. Keygen: add https://{YOUR_NGROK_URL}/keygen-webhooks to https://app.keygen.sh/webhook-endpoints

In a production environment, you would use your actual server info in place of the ngrok URLs above.

Testing the integration

Visit the following url: http://localhost:8080 and fill out the purchase form.

Common Issues

Incorrect ENV variables

In case of errors, please double check all of your environment variables. If one of the variables are incorrect, it may cause API authentication issues.

Protected account

Please note that this example requires that your Keygen account is set to unprotected, because this example handles user creation on the front-end. You can update this setting on your account's settings page. If you would prefer to keep your account protected, the logic for user creation would need to be moved to a server-side URL.

Other issues

Here's a few things to double check when a problem arises:

  1. Make sure you're using the correct account ID (find yours here)
  2. Make sure you're using a product token or admin token (the token should start with prod- or admi-)
  3. Make sure you're using the correct policy ID (it should be a UUID)
  4. Make sure that your Stripe environment variables are correct
  5. Make sure all dependencies have been installed via yarn install
  6. Make sure you have correctly configured webhooks for both Keygen and Stripe
  7. Make sure that the webhook URL is accessible from the public internet via ngrok

Questions?

Reach out at [email protected] if you have any questions or concerns!

example-stripe-integration's People

Contributors

ezekg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

example-stripe-integration's Issues

Call out where to find ENV variable values

The docs have this, which is helpful:

# Stripe publishable key
export STRIPE_PUBLISHABLE_KEY="YOUR_STRIPE_PUBLISHABLE_KEY"

# Stripe secret key (don't share this!)
export STRIPE_SECRET_KEY="YOUR_STRIPE_SECRET_KEY"

# The Stripe plan to subscribe new customers to
export STRIPE_PLAN_ID="YOUR_STRIPE_PLAN_ID"

# Keygen product token (don't share this!)
export KEYGEN_PRODUCT_TOKEN="YOUR_KEYGEN_PRODUCT_TOKEN"

# Your Keygen account ID
export KEYGEN_ACCOUNT_ID="YOUR_KEYGEN_ACCOUNT_ID"

# The Keygen policy to use when creating licenses for new users
# after they successfully subscribe to a plan
export KEYGEN_POLICY_ID="YOUR_KEYGEN_POLICY_ID"

But links to where to find those values, or what a sample value might look like, would be super helpful. I have what I think are the right variables, but I keep getting

An error occurred during user creation: You do not have permission to complete the request (ensure a token or license is present and valid) (double check your ENV vars)

Which leads me to believe that one (or more?) of those values are wrong. But which one?

A STRIPE_PUBLISHABLE_KEY, I think, looks like pk_test_51O...kN and can be found at https://dashboard.stripe.com/test/apikeys , as can the STRIPE_SECRET_KEY

The STRIPE_PLAN_ID is a bit harder to figure out. I think you go to https://dashboard.stripe.com/subscriptions/products/ and choose a product, but then there are a lot of IDs on that page. I have tried the one at the top-right, prod_PTH...k4 but that doesn't actually work?

I got the KEYGEN_PRODUCT_TOKEN from https://app.keygen.sh/products then clicking on the product., and I get 5X3XY89X-1234-4XYX-1234-8925XY07YZ7X (It's referred to as the ID there, though).

I got the KEYGEN_ACCOUNT_ID from https://app.keygen.sh/settings which seems like the obvious choice.

The KEYGEN_POLICY_ID from https://app.keygen.sh/policies seemed like the right choice,

In my proxy, I'm seeing

HTTP Requests                                                                                                         
-------------                                                                                                         
                                                                                                                      
POST /stripe-webhooks          200 OK                                                                                 
POST /stripe-webhooks          200 OK                                                                                 
POST /stripe-webhooks          200 OK                                                                                 
POST /stripe-webhooks          200 OK               

but you'll notice that there are no attempts from keygen. For the subscriptions, I used * but still ... nothing?

What am I missing here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.