简体   繁体   English

stripe 使用 nodeJS 创建支付意图

[英]stripe create payment intent with nodeJS

I am trying to make a payment with stripe.I make the payment and could see the success response on the browser but when I check in the dashboard it isn't adding there.我正在尝试使用 stripe 进行付款。我进行了付款,并且可以在浏览器上看到成功响应,但是当我检查仪表板时,它并没有添加到那里。

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

response of the above some thing like this上面的回应是这样的

{
    "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"
    },
    ...
}

after checking the docs I saw, to continue I should attach payment method and confirm the payment here .Then to confirm I use this line of code检查完我看到的文档后,要继续我应该附上付款方式并在此处确认付款。然后确认我使用这行代码

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

and it respond an success if I pass the paramater to stripe.paymentIntent.confirm as string, like the one I commented it does work but when I pass the Id as a charged It does throw me an error如果我将参数作为字符串传递给 stripe.paymentIntent.confirm,它会成功响应,就像我评论的那样,它确实有效,但是当我将 Id 作为收费传递时,它确实会给我一个错误

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'

now how could I pass the paymentIntent.id created in the create function to the confirm function with out undefined.现在我如何将在创建函数中创建的 paymentIntent.id 传递给未定义的确认函数。

For typescript对于打字稿

import Stripe from 'stripe';

...

const paymentIntent: Stripe.PaymentIntent = await 
stripeClient.paymentIntents.create({
        amount: amount * 100, 
        currency: 'usd'
    });

finally I figured out The problem was scope as @JimithyPicker stated最后我发现问题是@JimithyPicker 所说的scope

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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