Koa和Stripe,等待呈现页面

我正在使用koa和stripe来处理一次付款。 它的工作原理应该是这样,但是在data被分配之前它会呈现页面。 我希望能够在分配数据之后呈现页面,以便id将显示在页面视图中,而不是未定义的。

我试图在callback函数中呈现页面,我404 not found 。 我试过在stripe.charges.create函数后面使用。因为stripe和promises是兼容的,但是它给出了相同的结果。 我现在的代码如下。

  module.exports.payment = function* payment() { const params = this.request.body; let data; if (!params.stripeToken) { this.throw(400, "Sorry, something has gone awry."); } if (!params.amount) { this.throw(400, "A purchase amount must be supplied."); } const chargeAmount = params.amount * 100; const charge = stripe.charges.create({ amount: chargeAmount, currency: "USD", source: params.stripeToken, description: `${config.site.name} order#: ${this.session.id}` }, (err, charge) => { data = charge.id; } ); yield this.render("payment/payment_success", { id: data }); }; 

如果条纹库支持承诺,最好的办法就是产生stripe.charges.create

 module.exports.payment = function* payment() { const params = this.request.body; if (!params.stripeToken) { this.throw(400, "Sorry, something has gone awry."); } if (!params.amount) { this.throw(400, "A purchase amount must be supplied."); } const chargeAmount = params.amount * 100; const charge = yield stripe.charges.create({ amount: chargeAmount, currency: "USD", source: params.stripeToken, description: `${config.site.name} order#: ${this.session.id}` }; yield this.render("payment/payment_success", { id: charge.id }); }; 

现在当你渲染你的视图时,充电将被填充你的条纹调用的结果。