KBZPay payment with Flutter
- You need KBZPay Merchant Account and related account and app information. So, contact KBZ bank
- After contacting KBZ bank, they will give you UAT account, UAT app for android and ios, app_id and merchant information.
- Here is the official KBZPay payment integration documentation. You should read
In App Payment
section. - In KBZPay docs, they will say
Merchant Key
,Sign Key
orApp Key
. Don't get confused and please remember, they are the same. ButMerchant Code
is difference withMerchant Key
. 🥹
Download the current KBZPay SDK in the Frontend SDK section of SDK & Demo tab as here. Sometimes download link fails, please contact the bank at this time or get the SDK(v1.1.0) in the project of example.
Since Android Studio 4.2 you need to manually perform these steps in order to add .jar/.aar files
-
Open the android folder of your flutter project
-
In the android root folder create a single folder for
kbz-pay-app
, place the corresponding Android Archive(aar) file and create an emptybuild.gradle
file, like on the folder structure below:. └── flutter_project/ └── android/ ├── app ├── gradle └── kbz-pay-app/ ├── kbzsdk_x.x.x.aar └── build.gradle
In
kbzsdk_x.x.x.aar
, x.x.x means the SDK version. -
Content of the
android/kbz-pay-app/build.gradle
file:configurations.maybeCreate("default") artifacts.add("default", file('kbzsdk_x.x.x.aar'))
Please careful the file name of Android Archive file. It must be the same of the kbz SDK file name. There are two KBZPay SDKs archive file for UAT and Production. Please config the correct SDK in order of your development process.🎯
-
In the android root folder find
settings.gradle
file, open it and add this":kbz-pay-app"
// you will already have like this or any more include ":app" // After adding, may be like that include ":app", ":kbz-pay-app"
-
A payment completion message is sent by calling back the activity in the app. Create an activity in the project of
AndroidManifest.xml
file to receive payment results.<application> <!-- ... other config --> <activity android:name="com.kbzbank.payment.sdk.callback.CallbackResultActivity" android:exported="true" /> </application>
If the preceding procedure is not performed, the payment completion message may not be received in production but works on development. 👨🏻💻
- There are two KBZPay SDKs for UAT and Production. Please config the correct SDK in order of your development process.🎯
We'll need to configure our Info.plist
to support the iOS SDK. There are two things we need to
add:
-
Add
kbzpay
toLSApplicationQueriesSchemes
We'll need this to check if the KBZPay main application is installed. The
LSApplicationQueriesSchemes
key inInfo.plist
allows your application to perform this check. To set this up, add this to yourInfo.plist
:<key>LSApplicationQueriesSchemes</key> <array> <string>kbzpay</string> </array>
-
Add a
App Bundle Identifier
inCFBundleURLTypes
as URL SchemaIn order for
KBZ
to send usersback to your application
with payment result status, we need to set up aURI scheme
in ourInfo.plist
. To do this, we'll need our Bundle ID and Redirect URI. It will serve as aDeep Linking
in flutter. You should learn about this topic.<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>[ANY_URL_NAME]</string> <key>CFBundleURLSchemes</key> <array> <string>[YOUR_APP_BUNDLE_IDENTIFIER]</string> </array> </dict> </array>
Notice: If there are any update of KBZPay iOS SDK, you need to download current Customer SDK
(Version 1.3.0)
in here
And then, re-place production framework and simulator framework
in kbz_pay/ios/Frameworks/KBZPayAPPPay.xcframework
and if you need to re-config, research and do it again.
final _flutterKbzPayPlugin = KbzAppPayment();
void pay({required String prePayId, required String merchantKey}) {
final order = _buildOrderInfo(prePayId, merchantKey);
/// In iOS, you require appScheme because KBZPay callback to openUrl of your app for payment status
_flutterKbzPayPlugin.startPay(
orderInfo: order.$1,
sign: order.$2,
appScheme: "com.flutter.kbzpay.jackwill",
);
}
/// Payment Status (Works only Android. In iOS, you can listen status from deep link)
Stream<dynamic> onPayStatus() {
return _flutterKbzPayPlugin.onPayStatus();
}
/// Actually, you don't need this method. Required infos are given by server-side. This is just for demo.
(String, String) _buildOrderInfo(String prePayId, String merchantKey) {
final orderInfo =
"appid=${info.appId}&merch_code=${info.merchCode}&nonce_str=${RandomGen.I.nonceStr(
20)}&prepay_id=$prePayId×tamp=${RandomGen.I.timeStamp()}";
final sign = SHA.I.getSHA256Str("$orderInfo&key=$merchantKey");
return (orderInfo, sign);
}
In iOS, onPayStatus
can't work so, listen by the deeplink.
Example of deeplink
is com.flutter.kbzpay.jackwill://?EXTRA_RESULT=0&EXTRA_ORDER_ID=01003203060026798916
.
EXTRA_RESULT
of the status -
- 0:Pay for success,
- 3:Payment failed, the remaining fields are reserved by KBZPay for later addition
You can also read my research, How to write flutter plugin. These content of my research may be wrong, just a research!
Feel Free to Code
Happy Coding ...
Jack Will