【问题标题】:Koa and Stripe, wait to render pageKoa 和 Stripe,等待渲染页面
【发布时间】:2016-11-01 21:52:46
【问题描述】:

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

我尝试在回调函数中渲染页面,我得到了一个404 not found。我尝试在stripe.charges.create 函数之后使用.then,因为条纹与承诺兼容,但它给出了相同的结果。我目前的代码如下。

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

【问题讨论】:

    标签: javascript node.js stripe-payments koa


    【解决方案1】:

    如果条带库支持承诺,你最好的选择可能是产生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
        });
    };
    

    现在,当您渲染视图时,charge 将使用您的条带调用结果填充。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多