You can update the code as follows to prevents the payment screen opening multiple times. Please let me know if this works.
bool _isPaymentProcessing = false;
void openCheckout(int varTotalValue, String? razorpayOrderId) async {
if (_isPaymentProcessing) return; // Prevent multiple triggers
_isPaymentProcessing = true; // Set flag early
try {
String varUserLoginValue =
await Utils().funcGetSession(ConstantSession.KeyUserLoginValue);
log("varTotalValue ==: $varTotalValue");
bool isUserLoggedInThroughEmailId =
Validations.isValidEmail(varUserLoginValue);
String emailId = _userController.userResponseModel.data?.email ?? varUserLoginValue;
String name = _userController.userResponseModel.data?.firstName ?? varUserLoginValue;
String contactNo = _userController.userResponseModel.data?.contactNo ?? varUserLoginValue;
var options = {
'key': varWebRazorPayKey,
'amount': ((varTotalValue) * 100).toInt(),
'send_sms_hash': true,
'name': name.capitalizeFirstofEach,
'description': '',
'order_id': razorpayOrderId,
'theme': {'color': '#FFC300'},
'prefill': {'contact': contactNo, 'email': emailId},
"notify": {"sms": true, "email": true},
};
_razorpay?.open(options);
log('_razorpayPayments');
} catch (e) {
debugPrint('Error: $e');
_isPaymentProcessing = false; // Reset flag if Razorpay fails
}
}
// Reset flag in event handlers
void _handlePaymentSuccess(PaymentSuccessResponse response) {
log("Payment Successful: ${response.paymentId}");
_isPaymentProcessing = false;
}
void _handlePaymentError(PaymentFailureResponse response) {
log("Payment Failed: ${response.message}");
_isPaymentProcessing = false;
}
void _handleExternalWallet(ExternalWalletResponse response) {
log("External Wallet Selected: ${response.walletName}");
_isPaymentProcessing = false;
}
_isPaymentProcessing
is set at the very beginning.initState()
.Razorpay.open()
fails, _isPaymentProcessing
is reset._isPaymentProcessing = false;
is handled in event callbacks.