Stripe says API is synchronous, returns promise

As many others have here, I’m trying to integrate Stripe payments into my app. Fortunately, no issues getting at Stripe Checkout on the client or Stripe on the server via NPM.

The Stripe documentation states:

Synchronicity
Charging cards with Stripe is a synchronous process. Your code will immediately receive the result of the request: either a Charge object upon success or an exception upon failure. No asynchronous webhook or IPN script is required.

However, when I run their example code on a server-side method (protected using the served-files method from Meteor Guide) as follows…

chargeOrder({orderId, token}) {
    var stripe = require("stripe")("sk_test_...");
    const order = Orders.findOne(orderId);
    if(order) {
      const charge = stripe.charges.create({
        amount: order.total * 100,
        currency: 'usd',
        description: 'Example Charge',
        source: order.token.id,
      });
      console.log(charge);
      return charge;
    }
}

…I get back promises:

I20180731-07:40:42.841(-4)? Promise { <pending> }
I20180731-07:40:42.841(-4)? Promise { <pending> }

Clearly I’m taking too simplistic an approach here. Is Meteor automatically wrapping the API call from within the Stripe API library in a promise? What do I need to do with the promise to access the contents of the charge object returned by Stripe?

Should I add a .then() to the promise, in which I persist the charge ID, before returning it? Or should I use async/await? Been a while since I’ve had to use wrapAsync, and the guide snippet on promises is pretty barebones.

Thanks,
Dave

This doesn’t mean that the API request is synchronous.

async chargeOrder(...) {
   const charge = await stripe.charges.create({ ... })
}
3 Likes

Ahhhhhh gotcha, for some reason I spaced on their specific callout of “webhooks or IPN script” since that would be something their server would call at some point in the (in computational terms) very distant future.

DERP!

Thanks for the pointer on async/await.