1
votes

Try ti integrate Stripe using firebase platform. On a lolalhost (firebase emulators) test payment using cards works. After uploading to a server, got 500

As an example use this code (client on a web, server using node.js) https://github.com/stripe-samples/accept-a-card-payment/tree/master/using-webhooks

Client:

var orderData = {
  items: [{ id: "photo-subscription" }],
  currency: "usd"
};

fetch("/create-payment-intent", {
  method: "POST",
  headers: {
  "Content-Type": "application/json"
},
  body: JSON.stringify(orderData)
})
.then(function(result) {
  console.log(result);
  return result.json();
})
.then(function(data) {
  console.log(data);
  return setupElements(data);
})
.then(function({ stripe, card, clientSecret }) {
  // Handle form submission.
  var form = document.getElementById("payment-form");
  form.addEventListener("submit", function(event) {
    event.preventDefault();
    // Initiate payment when the submit button is clicked
    pay(stripe, card, clientSecret);
  });
});

Server (Cloud Functions)

app.post("/create-payment-intent", async (req, res) => {

  const { items, currency } = req.body;
  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: 1400,
    currency: currency,
    payment_method_types: ['card'],
  });

  // Send publishable key and PaymentIntent details to client
  res.send({
    publishableKey: 'pk_test_...',
    clientSecret: paymentIntent.client_secret
  });
});

Response on a localhost:

Response {type: "basic", url: "http://localhost:5000/jobs/create-payment-intent", redirected: false, status: 200, ok: true, …}

{publishableKey: "pk_test_...", clientSecret: "pi_1Fij2FH..."}

Response on a server:

POST https://pd...b.firebaseapp.com/create-payment-intent 500

Response {type: "basic", url: "https://pd...b.firebaseapp.com/create-payment-intent", redirected: false, status: 500, ok: false, …}

Uncaught (in promise) SyntaxError: Unexpected token E in JSON at position 0

Cloud functions log:

Error: An error occurred with our connection to Stripe.
at /srv/functions/node_modules/stripe/lib/StripeResource.js:212:9
at ClientRequest.req.on 
(/srv/functions/node_modules/stripe/lib/StripeResource.js:467:67)
at ClientRequest.emit (events.js:189:13)
at ClientRequest.EventEmitter.emit (domain.js:441:20)
at TLSSocket.socketErrorListener (_http_client.js:392:9)
at TLSSocket.emit (events.js:189:13)
at TLSSocket.EventEmitter.emit (domain.js:441:20)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
Function execution took 346 ms, finished with status: 'crash'
1

1 Answers

0
votes

This is late, but I believe you have to upgrade to Firebase Blaze plan. Firebase does not allow API requests to non-Google services on the free plan.