How to do a Razorpay integration in Flutter

516 Views Asked by At

I am able to deploy payment transaction but the server part is very hard. How can I create a orderid and how can we find the payment is done by a specific user?

1

There are 1 best solutions below

0
Tuhin On

Hope you set up all the necessary things.

Step 1: creating Order using Razorpay official Order Api:

     void createOrder() async {
    String username = 'xxxxxxxxxx';// razorpay pay key
    String password = "xxxxxxxxxxxxxxxx";// razoepay secret key
    String basicAuth =
        'Basic ${base64Encode(utf8.encode('$username:$password'))}';

    Map<String, dynamic> body = {
      "amount": 1 * 100, 
      "currency": "INR",
      "receipt": "rcptid_11"
    };
    var res = await http.post(
      Uri.https(
          "api.razorpay.com", "v1/orders"), //https://api.razorpay.com/v1/orders // Api provided by Razorpay Official 
      headers: <String, String>{
        "Content-Type": "application/json",
        'authorization': basicAuth,
      },
      body: jsonEncode(body),
    );

    if (res.statusCode == 200) {
      openCheckout(jsonDecode(res.body)['id']); // 
    }
    print(res.body);
  }
  //*#################################################################

Step 2: Open Razorpay checkout interface.

After getting orderId from Razorpay official Api, pass the id when calling openCheckout(jsonDecode(res.body)['id']); function

     void openCheckout(String orderId) async {
    var options = {
      'key': 'xxxxxxxxxxxxxxxx',
      "amount": 1 * 100,
      'order_id': orderId,
      'name': 'main.co.in',
      // 'prefill': {'contact': '', 'email': '[email protected]'},
      'external': {
        'wallets': ['paytm']
      }
    };

    try {
      razorpay.open(options);
    } catch (e) {
      debugPrint('Error: e');
    }
  }

3rd Step: Signature verification.

This is important if you automatically wanna transfer your amount to your bank account.

for Hmac SHA key , install this package: crypto:

  handlerPaymentSuccess(PaymentSuccessResponse response) {
    final key = utf8.encode('NgDLPyiDRPuQpcXy1E3GKTDv');
    final bytes = utf8.encode('${response.orderId}|${response.paymentId}');
    final hmacSha256 = Hmac(sha256, key);
    final  generatedSignature = hmacSha256.convert(bytes);
    if (generatedSignature.toString() == response.signature) {
      log("Payment was successful!");
      //Handle what to do after a successful payment.
      showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Success : payment successful"),
          // content: const Text("Are you sure you wish to delete this item?"),
          actions: <Widget>[
            ElevatedButton(
                onPressed: () {
                  Navigator.of(context).pop(true);
                  // PlaceOrderPrepaid();
                },
                child: Text("OK"))
            // ),
          ],
        );
      },
    );
    } else {
      log("The payment was unauthentic!");
    }
    

  }

enter image description here