简体   繁体   English

Reactjs Stripe 付款不适用于 Node/Express

[英]Reactjs Stripe payment not working with Node/Express

I am using stripe for the first time in my react project.我第一次在我的反应项目中使用条纹。

I have set up a payment with the test api keys.我已经使用测试 api 密钥设置了付款。 I have been using Javascript and React for a few years now but I have no-to-little knowledge of node.js / express.我已经使用 Javascript 和 React 几年了,但我对 node.js / express 知之甚少。 So I used a boiler plate from sandbox for the backend, which you can see below;所以我在后端使用了来自沙箱的样板,你可以在下面看到;

const cors = require("cors");
const express = require("express");
const stripe = require("stripe")("MY TEST KEY HAS BEEN INSERTED HERE");
const uuid = require("uuid/v4");

const app = express();

app.use(express.json());
app.use(cors());

app.get("/", (req, res) => {
  res.send("Add your Stripe Secret Key to the .require('stripe') statement!");
});

app.post("/checkout", async (req, res) => {
  console.log("Request:", req.body);

  let error;
  let status;
  try {
    const { product, token } = req.body;

    const customer = await stripe.customers.create({
      email: token.email,
      source: token.id
    });

    const idempotency_key = uuid();
    const charge = await stripe.charges.create(
      {
        amount: 8.00,
        currency: "gbp",
        customer: customer.id,
        receipt_email: token.email,
        description: `Purchased the ${product.name}`,
        name: token.card.name,
      },
      {
        idempotency_key
      }
    );
    console.log("Charge:", { charge });
    status = "success";
  } catch (error) {
    console.error("Error:", error);
    status = "failure";
  }

  res.json({ error, status });
});

app.listen(8080);

And here is the function within react js which handles the even once the user has entered the card number etc.这是 react js 中的函数,即使用户输入卡号等,它也会处理。

  async function handleToken(token){

    let whichClass = {
      name: "Bodytone",
      price: 8.00
    }
   
    const response = await axios.post("https://s9mh5.sse.codesandbox.io/checkout", {token, whichClass});

    const { status } = response.data;

    if (status === "success") {
      alert("Successful payment");
    } else {
      alert("failed payment");
    }

  }

What I am finding is that when I click to send the payment after entering details, I see the green tick to suggest everything went smoothly.我发现当我输入详细信息后单击发送付款时,我看到绿色勾号表示一切顺利。 But within the stripe dashboard for this account, nothing shows as a result of the test payment.但是在此帐户的条带仪表板中,测试付款的结果没有任何显示。

In the console I see two different error messages logged, which are detailed below:在控制台中,我看到记录了两个不同的错误消息,详情如下:

StripeCheckout.open: Either 'token' or 'source' is a required option, but neither was found. StripeCheckout.open:'token' 或 'source' 是必需选项,但都没有找到。 You can learn about the available configuration options in the Checkout docs: https://stripe.com/docs/checkout 0.chunk.js:108728:20您可以在 Checkout 文档中了解可用的配置选项: https ://stripe.com/docs/checkout 0.chunk.js:108728:20

As well as.....也.....

**Uncaught TypeError: this.fn is not a function
    trigger https://checkout.stripe.com/checkout.js:3
    bind https://checkout.stripe.com/checkout.js:3
    onToken https://checkout.stripe.com/checkout.js:3
    closed https://checkout.stripe.com/checkout.js:3
    bind https://checkout.stripe.com/checkout.js:3
    processMessage https://checkout.stripe.com/checkout.js:2
    bind https://checkout.stripe.com/checkout.js:2
    message https://checkout.stripe.com/checkout.js:2
    RPC https://checkout.stripe.com/checkout.js:2
checkout.js:3:24013**

Is anyone able to spot the mistake / error?有没有人能够发现错误/错误?

Your help and feedback are greatly appreciated!!非常感谢您的帮助和反馈!!

You should look at using the new Checkout rather than the legacy one you're using now, or possibly Elements to build your own form, for which you could use https://github.com/stripe/react-stripe-js .您应该考虑使用新的 Checkout而不是您现在使用的旧版Checkout ,或者可能使用 Elements 来构建您自己的表单,为此您可以使用https://github.com/stripe/react-stripe-js

That said, you didn't provide enough client-side code to diagnose the issue.也就是说,您没有提供足够的客户端代码来诊断问题。

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

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