taxgilde/lib/services/razorpay_service.dart

71 lines
1.8 KiB
Dart

import 'package:razorpay_flutter/razorpay_flutter.dart';
import 'package:flutter/foundation.dart';
class RazorpayService {
late Razorpay _razorpay;
final Function(PaymentSuccessResponse)? onSuccess;
final Function(PaymentFailureResponse)? onFailure;
final Function(ExternalWalletResponse)? onExternalWallet;
RazorpayService({
this.onSuccess,
this.onFailure,
this.onExternalWallet,
}) {
_razorpay = Razorpay();
_razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
_razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError);
_razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);
}
void _handlePaymentSuccess(PaymentSuccessResponse response) {
debugPrint("✅ Razorpay Payment Success: ${response.paymentId}");
onSuccess?.call(response);
}
void _handlePaymentError(PaymentFailureResponse response) {
debugPrint("❌ Razorpay Payment Error: ${response.code} - ${response.message}");
onFailure?.call(response);
}
void _handleExternalWallet(ExternalWalletResponse response) {
debugPrint("💰 Razorpay External Wallet: ${response.walletName}");
onExternalWallet?.call(response);
}
void openCheckout({
required String key,
required dynamic amount,
required String orderId,
required String description,
String? contact,
String? email,
}) {
var options = {
'key': key,
'amount': amount,
'name': 'Taxglide',
'order_id': orderId,
'description': description,
'theme': {'color': '#61277A'},
'prefill': {
'contact': contact ?? '',
'email': email ?? '',
},
'external': {
'wallets': ['paytm']
}
};
try {
_razorpay.open(options);
} catch (e) {
debugPrint("❌ Error opening Razorpay: $e");
}
}
void dispose() {
_razorpay.clear();
}
}