简体   繁体   中英

Meteor and Stripe apply coupon first before charge happens

New to meteor and stripes API I am trying to apply this coupon code using Meteor and stripe.This is for a one time payment with a coupon. However the handleCharge method fires before the process payment method. And I want the Stripe.coupons.retrieve to return a result first before the payment is processed.

Server Method

Meteor.methods({
  processPayment( charge, coupon ) {
    Stripe.coupons.retrieve(
      coupon,
      function(err, result) {
        if( result ) {
          charge.amount = parseInt(charge.amount) - parseInt( charge.amount * coupon.percent_off );
        }
      }
    );

    let handleCharge = Meteor.wrapAsync( Stripe.charges.create, Stripe.charges ),
        payment      = handleCharge( charge );

    return payment;
  }
});

I've also tried to return a result before the coupon is passed into the processPayment. Then when i try to console.log the result it is always undefined.

checkForCoupon( couponCode ) {
      let result = false;
      Stripe.coupons.retrieve(
        couponCode,
        function(err, coupon) {
          if( err ) {
            result = false;
          } else {
            result =  true;
          }
        }
      );
      return result;
    }

 Meteor.call( 'checkForCoupon', coupon, ( error, response ) => {
       if ( error ) {
         console.log( error );
       } else {
         console.log( "Success");
       }
     });

Any help would be greatly appreciated.

Ok here's a thing, the argument for coupon key in stripe api takes a string which looks like you already have because you are passing that in coupons.retrieve api, so what you'll get from this api is coupon object which is no use to you. So usually in Stripe we already have coupon id before creating subscription which gets passed in Stripe API for discount.

But as you said you are having problem in getting response before running another method, so here I can suggest you to use Async.runSync of meteor.

And another thing is that you can't use coupon in charge.create api, its for subscriptions. So here's how my approach would be with subscription:

Here I'm retreiving coupons object from coupon_id and and then hitting subscriptions API.

On Client:

var data = {
   source: "source",
   plan: "plan"
}

Meteor.call('processPayment', data, function(err, res) {
  if(err)
     return;
  console.log("Subscription added with coupon");
});

On Server:

Meteor.methods({
  var coupon;
  'processPayment': function(data) {
    check( data, {
      source: String,
      plan: String
    });
    Async.runSync(function(done) {
      var coupon = Stripe.coupons.retrieve("coupon_id", function(err, coupon) {
        if(err) {
          return done(null);   //done is callback
        }
        return done(null, coupon);
      });
    });
    //this code will only run when coupon async block is completed
    if(coupon !== null) {
      data.coupon = coupon.id;
    }
    var subscription = Async.runSync(function(done) {
        Stripe.subscriptions.create(data, function(err, subscription) {
            if(err) {
              console.log(err);
              return done(null, err);
            }
            return done(null, subscription);
          }
        );
      });
      return subscription;
   }
});

Hope this helps you and feel free to ask anything in comments, I'll be more than happy to answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM