React本机,与python后端的条带集成

63lcw9qa  于 2022-12-24  发布在  React
关注(0)|答案(1)|浏览(98)

我想将Stripe与我的React原生应用集成,但我一直收到错误“localizedMessage”:“您必须提供paymentMethodType”,尽管我添加了paymentMethod,但这是我的后端:

@app.route("/create-payment-intent-native", methods=["POST"])
def create_payment_native():
    stripe.api_key = key
    try:
        data = json.loads(request.data)
        if "amount" in data:
            try:
                intent = stripe.PaymentIntent.create(
                    amount=int(data["amount"]* 100),
                    currency=data["currency"],
                    payment_method_types=['card'],
                )
                return jsonify({"clientSecret": intent["client_secret"]})
            except ValueError as e:
                return jsonify(error=str(e))
        else:
            return jsonify(error="No amount to pay in request")
    except Exception as e:
        return jsonify(error=str(e))

这就是我发送请求的方式

const fetchPaymentIntentClientSecret = async () => {
  const response = await fetch(`https://api.click-n.com/create-payment-intent-native`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      currency: 'CAD',
      amount: totalPrice,
    }),
  });
  const { clientSecret } = await response.json();
  return clientSecret;
};

const { confirmPayment, loading } = useConfirmPayment();
const handlePayPress = async () => {
    const clientSecret = await fetchPaymentIntentClientSecret();
    const billingDetails = {
    email: 'email@stripe.com',
    phone: '+48888000888',
    addressCity: 'Houston',
    addressCountry: 'US',
    addressLine1: '1459  Circle Drive',
    addressLine2: 'Texas',
    addressPostalCode: '77063',
  };
    const { error, paymentIntent } = await confirmPayment(clientSecret, {
    type: 'Card',
    billingDetails,
  });
    if (error) {
    console.log(error)
  } else if (paymentIntent) {
    console.log(paymentIntent)
  }
};

我不知道我做错了什么

dz6r00yl

dz6r00yl1#

您正在调用confirmPayment时指定type。预期参数为paymentMethodType。此外,您还需要将billingDetails嵌套在paymentMethodData参数中。
您可以在此处的代码片段中看到预期的函数签名

相关问题