Tom chan
Tom chan

Reputation: 365

Turning the Stripe Checkout into a Synch function

I am trying to use wrapAsync for Stripe.charges call using Stripe Checkout, but I cant seem to get it working

Client code Template.bookingPost2.events({ "click #accept": function(event, template){ event.preventDefault();

    StripeCheckout.open({
      key: 'public_key',
      amount: 5000, // this is equivalent to $50
      name: 'Meteor Tutorial',
      description: 'On how to use Stripe ($50.00)',
      panelLabel: 'Pay Now',
      token: function(res) {
        stripeToken = res.id;
        console.info(res);
        Meteor.call('chargeCard', stripeToken);
      }
    });

  }
});

Server code

Meteor.methods({
    'chargeCard': function(stripeToken) {
      check(stripeToken, String);
      var Stripe = StripeAPI('secret_key');

      Stripe.charges.create({
        source: stripeToken,
        amount: 5000, // this is equivalent to $50
        currency: 'usd'
      }, function(err, charge) {
        console.log(err, charge);
      });
    }
  });

My tried Solution:

  var stripeChargesCreateSync = Meteor.wrapAsync(Stripe.charges.create);

  var result = stripeChargesCreateSync({
     source: stripeToken,
     amount: (info.timeRequired/15)*500, // this is equivalent to $50
     currency: 'gbp'
  });

And how do I handle the returned values namely charge and err?

Upvotes: 0

Views: 69

Answers (1)

Tarang
Tarang

Reputation: 75945

This should work and result should populate with a result as you see in the stripe documentation:

 var result = stripeChargesCreateSync({
     source: stripeToken,
     amount: (info.timeRequired/15)*500, // this is equivalent to $50
     currency: 'gbp'
  });

This is where the error is, use this:

var stripeChargesCreateSync = Meteor.wrapAsync(Stripe.charges.create, Stripe.charges);

instead of

var stripeChargesCreateSync = Meteor.wrapAsync(Stripe.charges.create);

Your stripeChargesCreateSync method needs to bind to the correct context when it runs. Meteor.wrapAsync doesn't know what that is so you need to tell it where the Stripe.charges.create method is.

Upvotes: 1

Related Questions