I am encountering an issue with the Razorpay Flutter integration where the Razorpay payment window opens and closes within a second during the second transaction attempt. Initially, the integration was working correctly, but subsequent transactions are failing with this behavior.
Terminal:
Payment Error: Error in opening checkout
I/flutter (13709): Payment Error: {reason: payment_error, metadata: {}, code: BAD_REQUEST_ERROR, contact: +919000090000, description: Error in opening checkout, step: payment_authentication, source: customer, email: [email protected]}
W/ContentCatcher(13709): Failed to notify a WebView
E/com.razorpay.checkout(13709): Webview JS Error: Uncaught TypeError: CheckoutBridge.sendAnalyticsData is not a function
I/chromium(13709): [INFO:CONSOLE(1)] "Uncaught TypeError: CheckoutBridge.sendAnalyticsData is not a function", source: https://api.razorpay.com/v1/checkout/public?version=1.6.39&library=checkoutjs&platform=android&customer_id=CID1717393257417&session_token=3C2730A481106A990650F5CC724578BD4B01A14F808D62E6BCD93EDB50683C7AD789CD1F252FAC15C9886E59C6377254C66FA1B679920E275D5456E0F28EC2FE551431A7E4BE4B5FD5DCF1ED1B4797BC545327B2C64DE9A256ACA3A4F8BC8AC110C0F50394179365D31E75733F4B82FB9260053D820DBC5993118627FE49AB4D98CC5260CF34DEF3BDF23E9769DAA01EA84C29&country_code=IN&traffic_env=canary (1)
E/com.razorpay.checkout(13709): Webview JS Error: Uncaught (in promise) #<Object>
Future<void> initiatePayment() async {
try {
orderID = await generateOrderId("rzp_live_njtuvWcruSxxxx", "Kn3yyNEj99hss0J7VJvpxxxx", int.parse(amount));
print("Order ID inside initiate payment: $orderID");
var options = {
'key': 'rzp_live_njtuvWcruSxxxx', // Your Razorpay Key ID
'amount': int.parse(amount), // Amount in the smallest currency sub-unit
'name': userName,
'currency' : "INR",
'order_id': orderID,
'customer_id': customerID,
'description': 'Pursue',
'timeout': 180, // in seconds
'prefill': {
'contact': '9009009009',
'email': userEmail,
},
'external': {
'wallets': ['paytm']
},
};
// Clear previous transaction data
razorpay.clear();
razorpay = Razorpay();
razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError);
razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);
razorpay.open(options);
} catch (e) {
debugPrint("Error: ${e.toString()}");
Fluttertoast.showToast(msg: "Error initiating payment");
}
}
Future<String> generateOrderId(String key, String secret, int amount) async {
var authn = 'Basic ${base64Encode(utf8.encode('$key:$secret'))}';
var headers = {
'content-type': 'application/json',
'Authorization': authn,
};
var data =
'{ "amount": $amount, "currency": "INR" }';
var url = Uri.parse('https://api.razorpay.com/v1/orders');
var res = await http.post(url, headers: headers, body: data);
if (res.statusCode != 200) {
throw Exception('http.post error: statusCode= ${res.statusCode}');
}
print('ORDER ID response => ${res.body}');
print(json.decode(res.body)['id'].toString());
return json.decode(res.body)['id'].toString();
}
I’ve tried the following things:
- Validate Amount
- Correct Key and Order ID
- Mandatory Fields:
Ensure all mandatory fields are included in the options. For Razorpay, the mandatory fields typically include:
key
amount
name
order_id
prefill (with contact and email) - Prefill Details:
Ensure the contact and email fields in the prefill section are correctly formatted. - Error Handling and Logging:
Add detailed logging to capture more context about the error.
New contributor
Avanish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.