首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用nodeJS条带化创建付款意向

使用nodeJS条带化创建付款意向
EN

Stack Overflow用户
提问于 2020-08-12 02:07:37
回答 1查看 1.5K关注 0票数 0

我正在尝试使用条纹付款。我正在付款,可以在浏览器上看到成功响应,但当我检查仪表板时,它没有添加在那里。

代码语言:javascript
复制
const express = require("express");
const stripe = require("stripe")("enter your API sk_test");

const app = express();
const port = 8000;

app.post("/charge", async (req, res, next) => {
  try {
    await stripe.paymentIntents.create(
      {
        amount: 200,
        currency: "gbp",
        payment_method_types: ["card"],
        receipt_email: "hadeklte@gmail.com",
      },
      function (err, paymentIntent) {
        if (err) {
          throw new Error("failed to charge");
        }
        res.status(200).send(paymentIntent);
      }
    );
  } catch (err) {
    console.log(err, "error occure");
  }
});

app.listen(port, () => {
  console.log(`Server is up at ${port}`);
});

上面的一些东西的响应类似这样

代码语言:javascript
复制
{
    "id": "pi_1HF19PGz03sGbVedIHyeBeLq",
    "object": "payment_intent",
    "amount": 200,
    "amount_capturable": 0,
    "amount_received": 0,
    "application": null,
    "application_fee_amount": null,
    "canceled_at": null,
    "cancellation_reason": null,
    "capture_method": "automatic",
    "charges": {
        "object": "list",
        "data": [],
        "has_more": false,
        "total_count": 0,
        "url": "/v1/charges?payment_intent=pi_1HF19PGz03sGbVedIHyeBeLq"
    },
    ...
}

检查完我看到的文档后,要继续,我应该附加付款方法并确认付款here.Then以确认我使用了此行代码

代码语言:javascript
复制
app.post("/charge", async (req, res, next) => {
  let charged;
  try {
    await stripe.paymentIntents.create(
      {
        amount: 200,
        currency: "gbp",
        payment_method_types: ["card"],
        receipt_email: "hadeklte@gmail.com",
      },
      function (err, paymentIntent) {
        if (err) {
          console.log(err, "failed payment");
        }
        charged = paymentIntent.id;
      }
    );
  } catch (err) {
    return res.status(500).send(err);
  }

  console.log(charged);
  try {
    await stripe.paymentIntents.confirm(
   // "pi_1HF19PGz03sGbVedIHyeBeLq",
        charged
      { payment_method: "pm_card_visa" },
      function (err, paymentIntent) {
        if (err) {
          console.log(err, "failed confirmation");
        }
        res.status(200).send(paymentIntent);
      }
    );
  } catch (err) {
    return res.status(500).send(err);
  }
});

如果我将参数作为字符串传递给stripe.paymentIntent.confirm,它将响应成功,就像我注释的那样,它确实可以工作,但当我将Id作为charged传递时,它会抛出一个错误

代码语言:javascript
复制
undefined
Error: Stripe: Argument "intent" must be a string, but got: undefined (on API request to `POST /payment_intents/{intent}/confirm`)
    at urlParams.reduce (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:21:13)
    at Array.reduce (<anonymous>)
    at getRequestOpts (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:18:29)
    at Promise (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:69:14)
    at new Promise (<anonymous>)
    at makeRequest (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:66:10)
    at Constructor.confirm (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\StripeMethod.js:31:7)
    at app.post (E:\projects\Stripe\Stripe_API_Docs\app.js:70:33)
    at processTicksAndRejections (internal/process/task_queues.js:86:5) 'failed confirmation'

现在,我如何将在create函数中创建的paymentIntent.id传递给confirm函数,而不是未定义。

EN

回答 1

Stack Overflow用户

发布于 2020-08-12 05:48:21

最后我发现问题出在作用域上,就像@JimithyPicker所说的那样

代码语言:javascript
复制
app.post("/charge", async (req, res, next) => {
  var charged;
  try {
    charged = await stripe.paymentIntents.create({
      amount: 200,
      currency: "gbp",
      payment_method_types: ["card"],
      receipt_email: "hadeklte@gmail.com",
    });
  } catch (err) {
    return res.status(500).send(err);
  }

  console.log(charged);
  try {
    const paymentConfirm = await stripe.paymentIntents.confirm(
      charged.id,
      { payment_method: "pm_card_visa" }
    );
    res.status(200).send(paymentConfirm);
  } catch (err) {
    return res.status(500).send(err);
  }

});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63363993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档