Issue with stripe connection

I am getting “Error: An error occurred with our connection to Stripe.” when I create customer in the worker. It seems something wrong with the connection. This code works when I was using AWS lambda function.

const Stripe = require('stripe');
const stripe = Stripe('stripekey'); //stripe secret test key

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
/**
 * Respond with hello worker text
 * @param {Request} request
 */
async function handleRequest(request) {
  
  var token = '';
  var emailAddress = '';
  var uid = '';
  const json = await request.json();

  //check query params
  if(json.token){
    token = json.token;
  }
  if(json.emailAddress){
    emailAddress = json.emailAddress;
  }
  if(json.uid){
    uid = json.uid;
  }

  if(uid != ''){

    //First Create customer
    console.log(stripe);
    const customer = await stripe.customers.create({
      email: emailAddress,
      description: uid,
      source: token
    }).then(function(customer) {
      console.log(customer);
      return customer;
    }).catch(function(err) {
      console.log(err);
      console.log("[email protected]@#!!!");

      return err;
    });

    return customerStripeId;

  }else{
    new Response('uid is empty', {
      status: 400,
      statusText: 'Bad Request',
    })
  }

  return new Response('Hello worker!', {
    headers: { 'content-type': 'text/plain' },
  })
}

Stripe uses Node APIs. Workers do not use NodeJS, it’s a V8 isolate with full support of browser APIs. Stripe does not currently support Workers. They’re working on it though - Stripe connection errors in Cloudflare workers · Issue #1233 · stripe/stripe-node · GitHub

2 Likes

okay thank you for replying! I will try to use alterative way.

Stripes Node.js library might not, but Stripe have no issue with Workers. You just have to do the work yourself. Several examples are available via your preferred search tool.

2 Likes

thank you it seems I cant use the node library api. looks like i have to use vanilla JS

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.