Home > Mobile >  React Native, Stripe Integration with python backend
React Native, Stripe Integration with python backend

Time:12-21

I want to integrate Stripe with my react native app however i keep getting the error "localizedMessage": "You must provide paymentMethodType", even though i added the payementMethod this is my backend:

@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))

and this is how i'm send the request:

 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 protected]',
    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)
  }
};

i'm not sure what i'm doing wrong

CodePudding user response:

You are specifying type in the your call to confirmPayment. The expected parameter is paymentMethodType. Also you will need to nest the billingDetails inside a paymentMethodData parameter.

You can see the expected function signature in the code snippet here

  • Related