cashfree_pg 0.0.3+1 cashfree_pg: ^0.0.3+1 copied to clipboard
A Flutter plugin for Cashfree PG integration. It opens the payment page in a flutter_webview.
Cashfree PG Flutter SDK #
Integration Steps #
Step 1: Add Dependency #
Open the pubspec.yaml file located inside the app folder, and add cashfree_pg: under dependencies.
cashfree_pg: 0.0.3+1
Step 2: Add permissions (Android) #
The Cashfree PG SDK requires that you add the INTERNET permission in your Android Manifest
file.
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application ...>
Step 3: Generate Token (From Backend) #
You will need to generate a token from your backend and pass it to app while initiating payments. For generating token you need to use our token generation API. Please take care that this API is called only from your as it uses secretKey. Thus this API should never be called from App.
Request Description #
You need to send orderId, orderCurrency and orderAmount as a JSON object to the API endpoint and in response a token will received. Please see the description of request below.
curl -XPOST -H 'Content-Type: application/json'
-H 'x-client-id: <YOUR_APP_ID>'
-H 'x-client-secret: <YOUR_SECRET_KEY>'
-d '{
"orderId": "<ORDER_ID>",
"orderAmount":<ORDER_AMOUNT>,
"orderCurrency": "INR"
}' 'https://test.cashfree.com/api/v2/cftoken/order'
Request Example #
Replace YOUR_APP_ID and YOUR_SECRET_KEY with actual values.
curl -XPOST -H 'Content-Type: application/json' -H 'x-client-id: YOUR_APP_ID' -H 'x-client-secret: YOUR_SECRET_KEY' -d '{
"orderId": "Order0001",
"orderAmount":1,
"orderCurrency":"INR"
}' 'https://test.cashfree.com/api/v2/cftoken/order'
Response Example #
{
"status": "OK",
"message": "Token generated",
"cftoken": "v79JCN4MzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye.s79BTM0AjNwUDN1EjOiAHelJCLiIlTJJiOik3YuVmcyV3QyVGZy9mIsEjOiQnb19WbBJXZkJ3biwiIxADMwIXZkJ3TiojIklkclRmcvJye.K3NKICVS5DcEzXm2VQUO_ZagtWMIKKXzYOqPZ4x0r2P_N3-PRu2mowm-8UXoyqAgsG"
}
The "cftoken" is the token that is used authenticate your payment request that will be covered in the next step.
Step 4: Initiate Payment #
- App passes the order info and the token to the SDK
- Customer is shown the payment screen where he completes the payment
- Once the payment is complete SDK verifies the payment
- App receives the response from SDK and handles it appropriately
NOTE #
- The order details passed during the token generation and the payment initiation should match. Otherwise you'll get a"invalid order details" error.
- Wrong appId and token will result in "Unable to authenticate merchant" error. The token generated for payment is valid for 5 mins within which the payment has to be initiated. Otherwise you'll get a "Invalid token" error.
Payment modes #
Web Checkout Integration #
How to integrate #
For both the modes (normal and seamless) you need to invoke the doPayment() method. However, there are a few extra parameters you need to pass incase of seamless mode.
doPayment #
Future<dynamic> doPayment(Map<String, dynamic> inputs)
Initiates the payment in a webview. The customer will be taken to the payment page on cashfree server where they will have the option of paying through any payment option that is activated on their account. Once the payment is done the webview will close and the response will be delivered in the callback.
Parameters:
-
context
BuildContext object of the widget. -
params
A map of all the relevant parameters described in the Request Params section below. -
token
The token generated from Step 4. -
stage
Value should be either "TEST" or "PROD" for testing server or production server respectively.
Sample Code #
CashfreePGSDK().doPayment(inputs).then((val){
print("SDK Result : ${(val)}");
// DO something with the result
});
<br/>
# Request Parameters
| Parameter | Required | Description |
|-------------------------------------|-----------|----------------------------------------------------|
| <code>stage</code> | Yes | Environment - TEST or PROD |
| <code>appId</code> | Yes | Your app id |
| <code>orderId</code> | Yes | Order/Invoice Id |
| <code>orderAmount</code> | Yes | Bill amount of the order |
| <code>orderNote</code> | No | A help text to make customers know more about the order |
| <code>orderCurrency</code> | Yes | Currency code of the order. Default is INR. |
| <code>customerName</code> | No | Name of the customer |
| <code>customerPhone</code> | Yes | Phone number of customer |
| <code>customerEmail</code> | Yes | Email id of the customer |
| <code>tokenData</code> | Yes | Token generated from step 4 |
| <code>notifyUrl</code> | No | Notification URL for server-server communication. Useful when user’s connection drops after completing payment. |
| <code>paymentModes</code> | No | Allowed payment modes for this order. Available values: cc, dc, nb, paypal, upi, wallet. <strong>Leave it blank if you want to display all modes</strong> |
# Response parameters
These parameters are sent as extras to the onActivityResult(). They contain the details of the transaction.
| Parameter | Description |
|------------------------------------------------|----------------------------------------------------|
| <code>orderId</code> | Order id for which transaction has been processed. Ex: GZ-212 |
| <code>orderAmount</code> | Amount of the order. Ex: 256.00 |
| <code>paymentMode</code> | Payment mode of the transaction. |
| <code>referenceId</code> | Cashfree generated unique transaction Id. Ex: 140388038803 |
| <code>txStatus</code> | Payment status for that order. Values can be : SUCCESS, FLAGGED, PENDING, FAILED, CANCELLED. |
| <code>paymentMode</code> | Payment mode used by customer to make the payment. Ex: DEBIT_CARD, MobiKwik, etc |
| <code>txMsg</code> | Message related to the transaction. Will have the reason, if payment failed |
| <code>txTime</code> | Time of the transaction |
| <code>type</code> | Fixed value : <b>CashFreeResponse</b>. To identify the response is from cashfree SDK. |
| <code>signature</code> | Response signature, more [here.](https://docs.cashfree.com/pg/cf-checkout/#response-verification) |
### NOTE
- There can be scenarios where the SDK is not able to verify the payment within a short period of time. The status of such orders will be <code><b>PENDING</b></code>.
- If the WebView is closing as soon as it opens then most probably something is wrong with the input passed to the SDK. Please check your inputs and if you still need further help reach out to us at techsupport@cashfree.com.