I’m trying to set up Stripe payments in my React Native application with a Python Flask backend, but I keep running into an issue. The error message says “You must provide paymentMethodType” even though I think I configured everything correctly.
Here’s my Python backend endpoint:
@app.route("/process-stripe-payment", methods=["POST"])
def handle_payment_request():
stripe.api_key = stripe_secret_key
try:
request_data = json.loads(request.data)
if "total" in request_data:
try:
payment_intent = stripe.PaymentIntent.create(
amount=int(request_data["total"] * 100),
currency=request_data["currency_code"],
payment_method_types=['card'],
)
return jsonify({"secret": payment_intent["client_secret"]})
except ValueError as error:
return jsonify(error=str(error))
else:
return jsonify(error="Missing payment amount")
except Exception as error:
return jsonify(error=str(error))
And here’s how I’m calling it from React Native:
const getClientSecret = async () => {
const apiResponse = await fetch(`https://myapi.example.com/process-stripe-payment`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
currency_code: 'USD',
total: orderAmount,
}),
});
const { secret } = await apiResponse.json();
return secret;
};
const { confirmPayment, loading } = useConfirmPayment();
const processPayment = async () => {
const secret = await getClientSecret();
const customerInfo = {
email: 'customer@example.com',
phone: '+1234567890',
addressCity: 'New York',
addressCountry: 'US',
addressLine1: '123 Main Street',
addressLine2: 'Apt 4B',
addressPostalCode: '10001',
};
const { error, paymentIntent } = await confirmPayment(secret, {
type: 'Card',
billingDetails: customerInfo,
});
if (error) {
console.log(error)
} else if (paymentIntent) {
console.log(paymentIntent)
}
};
I can’t figure out what’s causing this paymentMethodType error. Any ideas what I might be missing?