'Razorpay payment gateway in flutter mobile app

I am trying to integrate Razorpay into my mobile app. I understand the implementation and am able to generate/complete test payments from my app. Now when I read here about the authorization of payment I am a bit confused about it, what I should or need to save in server (apart from paymentId,orderId and signature)for future reference. I can see a tab transaction in the Razorpay dashboard which is showing me the status of payment(authorized/failed) then what is the use of this part. How can I use webhook for authorization? Anyone who has implemented it please help me with this.



Solution 1:[1]

  1. install flutter pub add razorpay_flutter pubdev

  2. import 'package:razorpay_flutter/razorpay_flutter.dart';

  3. create a instance of Razorpay by _razorpay = Razorpay();

it uses event-based communication and emits events like

  • EVENT_PAYMENT_SUCCESS
  • EVENT_PAYMENT_ERROR
  • EVENT_EXTERNAL_WALLET

so we have to listen to these events with respective functions to handle the payments.

  @override
  void initState() {
    super.initState();

    _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) {

    var orderId = response.orderId;
    var signature = response.signature;
    var paymentId = response.paymentId;
    
    //Do your stuff

}

 void _handlePaymentError(PaymentFailureResponse response) {
     var code= response.code;
     var message = response.message ;
  }

 void _handleExternalWallet(ExternalWalletResponse response) {
 
  }


 void openCheckout() {
       //create a Map which details 

       var options = {

      "key": "Use Your API Key Id here",
      "amount":amount, 
      "name": "test",
      "description": "Test Payment",
      "order_id": orderId.toString(),
      "timeout": "60",
      "theme.color": "#5eba7d",
      "currency": "INR",
      "prefill": {"contact": "546338833332", "email": "[email protected]"},
     
    };
    try {
      _razorpay.open(options);
    } catch (e) {
      print(e.toString());
    }
  }

in any click call openCheckout(); this function

at last, dispose of it

  @override
  void dispose() {
    super.dispose();
    _razorpay.clear();
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1