首页 > 解决方案 > 如何在 Node.js 中更新 Stripe paymentIntent?

问题描述

我正在为非营利组织构建一个捐赠表格,并且正在努力更新发送到我的 Node.js Stripe 后端的更新付款金额。

前端是 React,我正在使用 useEffect() 挂钩来创建我的 paymentIntent。似乎使用新的 Stripe API 需要返回 clientSecret 的金额。否则,我想在用户选择捐赠金额后创建 paymentIntent(就像我使用类似的实现所做的那样)。

我正在使用“stripe.paymentIntents.update()”方法来更新金额。这显示在 console.log 中已更新,但并未更新发送到 Stripe 的 paymentIntent。

如何更新发送到 confirmCardPayment() 的 paymentIntent?

服务器.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

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函数

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

标签: node.jsreactjsstripe-payments

解决方案


第一个参数update应该是支付意图 ID。看来您有amount两次输入错误/错误:

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

应该:

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

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


推荐阅读