GitXplorerGitXplorer
s

stripe-node-cloudflare-worker-template

public
87 stars
17 forks
0 issues

Commits

List of commits on branch main.
Verified
c1278d24ab6c50f49f40b71fcacf91e83c032bd1

Merge pull request #15 from stripe-samples/use-hoon-middleware

hhideokamoto-stripe committed 8 months ago
Verified
d9f034d28ff2df1793b46630b2d2814ecfddc236

Merge pull request #13 from stripe-samples/docs/troubleshooting/miniflare

hhideokamoto-stripe committed 8 months ago
Verified
f0be59ab201bc37940f833b9cc9b419fc393e5f1

Merge pull request #14 from stripe-samples/dependencies/20240527

hhideokamoto-stripe committed 8 months ago
Unverified
7551f7e5af025e57cf3614ed2d93f711eafa8044

chore: load webhook secret from env adaptor

hhideokamoto-stripe committed 8 months ago
Unverified
7602b3ea853fd8736b941e19bd5105fa33693c40

chore: use the Hono middleware to setup the Stripe SDK

hhideokamoto-stripe committed 8 months ago
Unverified
bf6f5b8fc56611b91d20316f1b2ffbd308aa4abc

chore: update dependencies

hhideokamoto-stripe committed 8 months ago

README

The README file for this repository.

stripe-node Cloudflare Worker Template

This is a template for setting up a Cloudflare Worker which uses stripe-node. The wrangler CLI tool is used for publishing.

Generating

To generate using wrangler

wrangler generate projectname https://github.com/stripe-samples/stripe-node-cloudflare-worker-template
cd projectname
npm install

Further documentation for Wrangler can be found here.

How to run locally

To add your STRIPE_API_KEY as a plaintext environment variable via wrangler:

Rename and move the .dev.vars.example file into a file named .dev.vars. For example:

cp .dev.vars.example .dev.vars

Example .env file:

STRIPE_API_KEY='sk_test_xxx'

You will need a Stripe account in order to run the demo. Once you set up your account, go to the Stripe developer dashboard to find your API keys.

Finally, you can run this example by the following command:

npm run dev

You will get the local application URL like the following:

[mf:inf] Ready on http://0.0.0.0:51219 
[mf:inf] - http://127.0.0.1:51219
[mf:inf] - http://192.168.86.21:51219
[mf:inf] - http://172.18.96.89:51219

[Optional] Run a webhook locally

You can use the Stripe CLI to easily spin up a local webhook.

First install the CLI and link your Stripe account.

stripe listen --forward-to http://{REPLACE_TO_YOUR_LOCAL_APPLICATION_URL}/webhook

The CLI will print a webhook secret key to the console. Set STRIPE_WEBHOOK_SECRET to this value in your .env file.

You should see events logged in the console where the CLI is running.

When you are ready to create a live webhook endpoint, follow our guide in the docs on configuring a webhook endpoint in the dashboard.

Publishing

To add your STRIPE_API_KEY as a plaintext environment variable via wrangler:

[vars]
STRIPE_API_KEY = "<YOUR API KEY HERE>"

Now, you can build and publish your worker using:

wrangler publish

This will build your application and publish it. If this is your first time publishing, it will create a new worker named my-stripe-worker under your account. If all goes well, the command will output a URL that looks something like https://my-stripe-worker.<your user name>.workers.dev where your worker is available.

Cloudflare Secrets

Cloudflare Workers offer a secrets infrastructure for storing an encrypted secret. You can use this instead of relying on an environment variable.

  1. Publish your worker at least once using the above instructions to bootstrap the worker.
  2. Run wrangler secret put STRIPE_API_KEY. This will prompt you to enter your secret (ie. your Stripe API key).

This only needs to be done the first time you are configuring up your new worker. It will persist across deployments. Once set up, you can publish using:

wrangler publish

Troubleshooting

MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

Depending on the settings of your local machine, there may be failures when attempting to start the service in a local environment using Wrangler. According to a GitHub issue, it could be related to the certificate file or network settings.

To avoid using an invalid certificate file, please try starting the application with the following command:

NODE_EXTRA_CA_CERTS="" npm run dev

If it still does not work, you may need to change the /etc/hosts/ file and add the following settings:

127.0.0.1       localhost