Search code examples
flutterdartpayment-gatewayrazorpay

razorpay refund process flutter


I am using razor pay flutter package to make payment in flutter in test mode . I have options to handle the events and also save the payment id in my database . I want to refund the amount based on the payment id . I dont have much clarification on how to do the same . Here is my code for sending payment request

 Razorpay? _razorpay = Razorpay();
 void openPaymentPortal() async {
var options = {
  'key': 'My_key',
  'amount': 100000,
  'name': 'test',
  'description': 'Payment for Pro account',
  'prefill': {'contact': '9999999999', 'email': '[email protected]'},
  'external': {
    'wallets': ['paytm']
  }
};
try {
  _razorpay?.open(options);
} catch (e) {
  debugPrint(e.toString());
}
}

void _handlePaymentSuccess(PaymentSuccessResponse response) {
Fluttertoast.showToast(
    msg: "SUCCESS PAYMENT: ${response.paymentId}", timeInSecForIosWeb: 5);
onSubmit(false, response.paymentId.toString());
}

void _handlePaymentError(PaymentFailureResponse response) {
Fluttertoast.showToast(
    msg: "ERROR HERE: ${response.code} - ${response.message}",
    timeInSecForIosWeb: 4);
}

void _handleExternalWallet(ExternalWalletResponse response) {
Fluttertoast.showToast(
    msg: "EXTERNAL_WALLET IS : ${response.walletName}",
    timeInSecForIosWeb: 4);
}

How to process refund of the amount to the user ? I followed something like the below code but it is not working

void processRefund(String paymentId) async {
var url =
    Uri.parse('https://api.razorpay.com/v1/payments/$paymentId/refund');

try {
  var response = await http.post(
    url,
    headers: {
      'Authorization': 'Bearer My_Key',
      'Content-Type': 'application/json'
    },
  );

  if (response.statusCode == 200) {
    // Refund processed successfully
    print('Refund processed successfully');
  } else {
    // Refund failed
    print('Refund failed with status code: ${response.statusCode}');
    print('Response body: ${response.body}');
  }
} catch (e) {
  // Error occurred while making the refund request
  print('Error occurred during refund process: $e');
}

}


Solution

  • I solved my case as follows

        Future<void> processRefund() async {
        String keyId = 'My_Key';
        String keySecret = 'My_Secret';
        String paymentId = 'My_payment_id';
    
        String url = 'https://api.razorpay.com/v1/payments/$paymentId/refund';
    
        Map<String, dynamic> refundRequest = {
          'amount': 1000,
          'speed': 'normal',
          'notes': {
            'notes_key_1': 'Tea, Earl Grey, Hot',
            'notes_key_2': 'Tea, Earl Grey... decaf.',
          },
          'receipt': 'Receipt No. #31',
        };
    
        String basicAuth =
            'Basic ${base64Encode(utf8.encode('$keyId:$keySecret'))}';
    
        try {
          final response = await http.post(
            Uri.parse(url),
            headers: {
              'Content-Type': 'application/json',
              'Authorization': basicAuth,
            },
            body: jsonEncode(refundRequest),
          );
    
          if (response.statusCode == 200) {
            // Refund request successful
            var responseData = jsonDecode(response.body);
            print('Refund success: $responseData');
          } else {
            // Refund request failed
            print('Refund request failed with status code ${response.statusCode}');
          }
        } catch (error) {
          // Handle any exceptions
          print('Error creating refund request: $error');
        }
     }