2016-11-25 90 views
0

流星和条纹API的新功能我试图使用Meteor和stripe应用此优惠券代码。这是一次性使用优惠券付款。然而,handleCharge方法在处理付款方法之前触发。我希望Stripe.coupons.retrieve在付款处理之前先返回一个结果。流星和条纹在收费前首先应用优惠券

服务器方法

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; 
    } 
}); 

我也试着优惠券被传递到processPayment之前返回结果。然后当我尝试console.log结果它总是未定义的。

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"); 
     } 
    }); 

任何帮助将不胜感激。

+0

您已经在'Stripe.charges.create'中使用'Meteor.wrapAsync',为什么不将它用于'Stripe.coupons.retrieve'呢? – Khang

回答

0

好吧,这里有一件事情,在条纹api中的优惠券键的参数需要一个字符串,看起来像你已经有,因为你通过coupons.retrieve api传递,所以你会从这个api得到的是优惠券对象对你没用。所以通常在Stripe中我们已经有了优惠券ID,然后才能创建订阅,并在Stripe API中通过折扣。

但正如您所说,在运行另一种方法之前获得响应时遇到问题,所以在这里我可以建议您使用流星的Async.runSync。

另一件事是,你不能使用优惠券charge.create API,其订阅。所以这里是我的方法是如何与订阅:

在这里我从coupon_id retreiving优惠券对象,然后击中订阅API。

客户端:

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

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

在服务器:

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; 
    } 
}); 

希望这有助于你随便问在评论什么,我会很乐意回答更多。