Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Using stripe-node outside of Node.js (e.g. Cloudflare Workers or Deno) #997

Closed
brillout opened this issue Sep 9, 2020 · 29 comments
Assignees
Labels

Comments

@brillout
Copy link

brillout commented Sep 9, 2020

I'm trying to setup a Stripe service as a serverless function using the Cloudflare Workers platform.

But the stripe-node package seems to depend on Node.js APIs. Is it possible to use a subset of stripe-node's functionalities for Cloudflare Workers?

Is supporting Cloudlfare Workers something you are interested in? Using Stripe in a no-cold-start serverless environment would be quite lovely.

Would it be possible to extract the business logic that is agnostic to Node.js and expose it to users? So that we can use all kinds of platforms beyond Node.js, such as the Deno or Cloudflare Workers. It could be a new package, e.g. stripe-js.

Thanks for what you are doing at Stripe, it's such a lovely product.

Related tickets: #871 #773.

@richardm-stripe
Copy link
Contributor

Hi @brillout! Thanks for the feature request.

Cloudflare or Deno isn't something we've investigated in depth yet but it would be interesting to learn more. You might be able to help us out a little bit by elaborating a little bit about your use case -- how you would interact with with Stripe from a Cloudflare worker and what value this would unlock for you.

We did a little bit of work towards decoupling the library from the 'child_process' API in #775, which we were just using incidentally. If I understand correctly, I think in order to meet your need we would need to decouple from the 'http' Node API as well, that we use in StripeResource, and we are unfortunately quite far from today.

@brillout
Copy link
Author

Hi @richardm-stripe !

I want to be able to accept donations with credit cards on https://www.clocktab.com/ and https://www.timer-tab.com/. These two website are static/serverless; I don't need to maintain any server which is super convenient. I'd like to keep the serverless architecture.

I could use AWS lambda instead and that would just work. But I'm intrigued by the Cloudflare Workers Webassembly approach and avoiding cold start would be a neat thing. So my use case is not crucial; I'm just checking out options.

Wouldn't it be possible to "simply" (I don't know the details here) to expose functions that take an object holding the HTTP request url, path, headers, and body in a platform agnostic format and likewise return a platform independent object holding the HTTP response headers and payload? You could then expose these platform independent functions as well as expose Node.js, Cloudflare, and Deno wrappers.

@kristianfreeman
Copy link

Hey @richardm-stripe @brillout — I'm on the Workers team 👋

I'm super interested in helping this land! I've been playing around with the Stripe API inside of Workers and it seems like the http API is particularly the issue. Interestingly, I'm able to do some stuff that doesn't require requests (e.g. validating webhook signatures), but as soon as I try to make a request, it errors out! Ideally, the Stripe SDK could use fetch (it's provided by default in the Workers environment), though I understand that's easier said than done. Let me know how I can help!

@dplewis
Copy link

dplewis commented Oct 27, 2020

Can we allow Fetch to be passed into configuration for custom implementation that matches WebAPI spec?

Something like

const stripe = require('stripe')('<KEY>', { Fetch: myCustomWebAPIfetch }]

goshippo/shippo-node-client#50

@richardm-stripe
Copy link
Contributor

Hi all, we'd be excited about adding support for fetch, but don't have immediate plans to prioritize the implementation; however if anybody is interested in putting together a proof-of-concept, that could be a lot of help.

What I'm envisioning is that instead of calling http.request/https.request and using the req object directly as we do here, we would configure an httpClient property on the stripe client that implemented a similar interface, and permit the user to provide their own implementation of that interface & configure the Stripe client to use it.

@brand-on-fire
Copy link

We'd like to be able to onboard Stripe Connect users via CloudFlare workers - is something like that currently possible? We'd prefer not to host the connect/payment functionality and have some way of ensuring uptime...

@maxnordlund
Copy link

I see two ways to do this:

  1. Use http in node and https://github.com/browserify/http-browserify in the browser/Workers
  2. Use fetch in the browser/Workers and polyfill it in node using https://github.com/node-fetch/node-fetch

While I personally would prefer the latter as it means first class support in the browser, the first might be easier to pull off without refactoring too much. What do you think?

@mdingena
Copy link

mdingena commented Dec 4, 2020

Following this thread because I'm trying to setup https://stripe.com/docs/billing/subscriptions/checkout in Cloudflare Workers and bumping into this exact issue.

I can't configure Webpack to target: 'node' because Workers require a 'webworker' target.

Not targeting 'node' means Webpack complains with:

WARNING in ./node_modules/stripe/lib/utils.js 17:9-38
Module not found: Error: Can't resolve 'child_process' in '/<path-to-repo>/node_modules/stripe/lib'
 @ ./node_modules/stripe/lib/stripe.js 28:14-32
 @ ./src/index.js 12:13-30

But targeting 'node' means workers-preview < dist/worker.js explodes with:

Uncaught ReferenceError: require is not defined
    at Object.http (worker.js:5218)
    at __webpack_require__ (worker.js:5269)
    at Object../node_modules/stripe/lib/StripeResource.js (worker.js:1157)
...etc

I would absolutely love a solution for this because it means I don't have to spin up an expensive server just to host my payment integration. My app is completely serverless otherwise.

@leesus
Copy link

leesus commented Dec 11, 2020

Interestingly, I'm able to do some stuff that doesn't require requests (e.g. validating webhook signatures)

Sorry to hijack this issue, but @signalnerve do you mind sharing how you are validating signatures (or more specifically, how you are getting the raw request body)? I get No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? no matter which request method I use in my worker (.text(), .json(), .arrayBuffer() and converting to string etc).

@MaxOrelus
Copy link

+1

I too would love to see this implemented. I'm running up on the same situation, where I'm trying to create a session and I get an error from the Stripe API indicating that it cannot read property "protocol" of undefined.

@rysolv-bot
Copy link

apowerful1 has contributed $70.00 to this issue on Rysolv.

The total bounty is now $70.00. Solve this issue on Rysolv to earn this bounty.

@kristianfreeman
Copy link

Interestingly, I'm able to do some stuff that doesn't require requests (e.g. validating webhook signatures)

Sorry to hijack this issue, but @signalnerve do you mind sharing how you are validating signatures (or more specifically, how you are getting the raw request body)? I get No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? no matter which request method I use in my worker (.text(), .json(), .arrayBuffer() and converting to string etc).

https://github.com/cloudflare/ecommerce-bundles-workers-example/blob/main/api/src/handlers/webhook.ts#L142-L151 :)

@rhaksw
Copy link

rhaksw commented Dec 28, 2020

I would absolutely love a solution for this because it means I don't have to spin up an expensive server just to host my payment integration. My app is completely serverless otherwise.

I'd love a Cloudflare solution too. @mdingena fyi you can currently do this for free with Netlify's Lambda integration. I followed this guide when implementing donations on reveddit.com. If Stripe worked with workers I would look at moving deployment from Netlify to Cloudflare. The site currently uses Cloudflare DNS and Netlify for deployment.

@lgs
Copy link

lgs commented Mar 27, 2021

+1 Stripe on Cloudflare workers

@rysolv-bot
Copy link

An anonymous user has contributed $10.00 to this issue on Rysolv.

The total bounty is now $80.00. Solve this issue on Rysolv to earn this bounty.

@aegooby
Copy link

aegooby commented Apr 1, 2021

I would like to be able to use Stripe in Deno so +1 to any solution compatible with fetch() since Deno uses that too.

There is $80 bounty on this, which is great, is there any progress or updates on if/when it might be implemented?

@rysolv-bot
Copy link

An anonymous user has contributed $30.00 to this issue on Rysolv.

The total bounty is now $110.00. Solve this issue on Rysolv to earn this bounty.

@bsears90
Copy link

bsears90 commented Apr 12, 2021

+1 for this feature - would love to move some of our workloads off of gcloud functions and onto cloudflare

@hmnd
Copy link

hmnd commented Apr 16, 2021

Cloudflare is now working on polyfills to support Node libraries in workers! Stripe and Twilio SDKs are first in line and you can vote here on libraries you'd like to be supported.

@rysolv-bot
Copy link

An anonymous user has contributed $10.00 to this issue on Rysolv.

The total bounty is now $120.00. Solve this issue on Rysolv to earn this bounty.

@EduM22
Copy link

EduM22 commented Apr 17, 2021

Hey I have been trying to build a stripe sdk that can run on workers.dev, but as @hmnd say Cloudflare is going to start supporting stripe-node with polyfills but that will not help Deno users or other that do not want to use Cloudflare workers.

If anyone wants to help to develop and test the repo is: => https://github.com/TrinaryLabs/stripe-workers
This package is not Production ready!!!!

@dcr-stripe
Copy link
Contributor

Hey everyone - thanks for your patience on this issue!

I have some good news on the Cloudflare Workers front. You should now be able to run on Cloudflare Workers out of the box using the following Stripe client instantiation:

const Stripe = require("stripe");

const stripe = Stripe('<API key>', {
  // Cloudflare Workers use the Fetch API for their API requests.
  httpClient: Stripe.createFetchHttpClient()
});

We've put together a template at https://github.com/stripe-samples/stripe-node-cloudflare-worker-template which can be used with wrangler.

You can also use this in Deno as well as any other JS environment which uses fetch, or create your own implementation of HttpClient. Full Deno support (specifically Webhooks) is still a work in progress however - stay tuned for more.

Please let us know if you experience any issues or have feedback for the team.

Thanks!

@dcr-stripe dcr-stripe self-assigned this Nov 18, 2021
@kristianfreeman
Copy link

So happy to see this launch! Thanks team and excited to collab on Workers stuff with you all in the future ❤️

@saibotsivad
Copy link

Hey all, one thing I have not been able to make work (honestly not real sure where to start) is using this with Cloudflare Pages (specifically the Functions feature).

This might be an issue that the Cloudflare team needs to resolve? @signalnerve

I don't see in the docs a way to specify a Webpack configuration for a Pages build, and without it the Stripe SDK is effectively unusable.

(My use-case: single product, only a few pages, static HTML overall with the shopping cart a JS library entirely in the browser, using Stripe to handle the actual purchase point.)

@bradfrosty
Copy link

I don't see in the docs a way to specify a Webpack configuration for a Pages build

@saibotsivad you specify a custom a build command. By default it uses npm, so if you are using a different package manager (yarn or pnpm) you would need to install dependencies in your build command.

@dcr-stripe
Copy link
Contributor

👋 You can now also use stripe-node in Deno.

Here are some resources to get started:

With that we should be able to close this issue out. Thanks for the patience!

@aslakhellesoy
Copy link

aslakhellesoy commented May 3, 2022

@dcr-stripe I'm unable to run the Deno samples. See stripe-samples/stripe-node-deno-samples#1

Should I rather create a new ticket in this repo? AFAICT there is no CI verifying that this library actually works on Deno.

@aslakhellesoy
Copy link

Done! #1428

@beppek
Copy link

beppek commented Nov 3, 2022

Not sure if this is related to this, but I'm getting Buffer is not defined errors when verifying webhook signatures using Stripe.createSubtleCryptoProvider() and stripe.webhooks.constructEventAsync in Remix on Cloudflare Pages. Buffer is not supported in workers so seems like there are still some dependencies that clash with worker environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests