简体   繁体   English

如何更新 Node.js 中的 Stripe paymentIntent?

[英]How do I update a Stripe paymentIntent in Node.js?

I'm building a donation form for a non-profit and I'm struggling with updating the updated payment amount sent to my Node.js Stripe backend.我正在为一个非营利组织建立一个捐赠表格,我正在努力更新发送到我的 Node.js Stripe 后端的更新付款金额。

Frontend is React and I'm using the useEffect() hook to create my paymentIntent.前端是 React,我正在使用 useEffect() 挂钩来创建我的 paymentIntent。 It appears that with the new Stripe API the amount is required to return the clientSecret.看来,使用新的 Stripe API 需要返回 clientSecret 的金额。 Otherwise, I would want to create the paymentIntent after the user chooses the donation amount (as I've done with a similar implementation).否则,我想在用户选择捐赠金额后创建 paymentIntent(就像我使用类似的实现所做的那样)。

I'm using the method "stripe.paymentIntents.update()" to update the amount.我正在使用“stripe.paymentIntents.update()”方法来更新金额。 This shows updated in the console.log, but it's not updating the paymentIntent sent to Stripe.这显示在 console.log 中已更新,但并未更新发送到 Stripe 的 paymentIntent。

How do I update the paymentIntent to send to confirmCardPayment()?如何更新发送到 confirmCardPayment() 的 paymentIntent?

server.js服务器.js

  app.post("/create-payment-intent", async (req, res) => {
  let amount = req.body.amount;
  const name = req.body.name;
  
  const options = {
    description: 'Teton Valley Aquatics Donation',
    amount,
    currency: "USD",
    name: name,
  };

  console.log(req.body.amount) //returns my default amount {1000}

  try {
    const paymentIntent = await stripe.paymentIntents.create(options);
    res.json(paymentIntent);
  }
   catch (err) {
    res.json(err);
  }

app.post("/create-payment-intent/update-amount", async (req, res) => {
  const amount = req.body;
  console.log(req.body) //returns {amount: 25}
  try {
    const paymentIntent = await stripe.paymentIntents.update(amount, {
      amount,
    });
    console.log(`Updated amount: ${amount}`);
    res.json(paymentIntent);
  }
  catch (err) {
    res.json(err)
  }
})
});

CheckoutForm.jsx CheckoutForm.jsx

useEffect(() => {
api
.createPaymentIntent({
  amount
})
.then((clientSecret) => {
  setClientSecret(clientSecret);
})
.catch((err) => {
  setError(err.message);
});

console.log(`Amount: ${amount}`)
}, []);


  const handleSubmit = async (ev) => {
    ev.preventDefault();
    setProcessing(true);
    
    api
      .updatePaymentIntent({
        amount,
      })
      .catch((err) => {
        setError(err.message);
      });

    const payload = await stripe.confirmCardPayment(clientSecret, {
      payment_method: {
        card: elements.getElement(CardElement),
        billing_details: {
          name: ev.target.name.value,
          email: ev.target.email.value
        },
      },
    });

    if (payload.error) {
      setError(`Payment failed: ${payload.error.message}`);
      setProcessing(false);
      console.log("[error]", payload.error);
    } else {
      setError(null);
      setSucceeded(true);
      setProcessing(false);
      setMetadata(payload.paymentIntent);
      console.log("[PaymentIntent]", payload.paymentIntent);
    }
  };

api functions api 功能

const createPaymentIntent = options => {
  return window
    .fetch(`http://localhost:4242/create-payment-intent`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(options)
    })
    .then(res => {
      if (res.status === 200) {
        return res.json();
      } else {
        return null;
      }
    })
    .then(data => {
      if (!data || data.error) {
        console.log("API error:", { data });
        throw new Error("PaymentIntent API Error");
      } else {
        return data.client_secret;
      }
    });
};

const updatePaymentIntent = options => {
  return window
    .fetch(`http://localhost:4242/create-payment-intent/update-amount`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(options)
    })
    .then(res => {
      if (res.status === 200) {
        return res.json();
      } else {
        return null;
      }
    })
    .then(data => {
      if (!data || data.error) {
        console.log("API error:", { data });
        throw new Error("PaymentIntent API Error");
      } else {
        return data.client_secret;
      }
    });
}

The first parameter to update should be a payment intent id.update的第一个参数应该是支付意图 ID。 It seems you have a typo/bug putting the amount in twice:看来您有一个错字/错误,将amount输入了两次:

const paymentIntent = await stripe.paymentIntents.update(**amount**, {
  amount,
});

should be:应该:

const paymentIntent = await stripe.paymentIntents.update(**paymentIntentId**, {
  amount,
});

See the snippet here: https://stripe.com/docs/api/payment_intents/update?lang=node请参阅此处的代码段: https://stripe.com/docs/api/payment_intents/update?lang=node

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

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