From 90b5312fc021ed019661f689eadf8e6edb170197 Mon Sep 17 00:00:00 2001 From: rdnasim Date: Thu, 22 Sep 2022 17:46:24 +0600 Subject: [PATCH] modified as sandbox or other as [true/false], and more --- example/lib/main.dart | 23 ++++++++++--------- lib/flutter_bkash.dart | 51 +++++++++++++++++++++++++----------------- lib/src/constants.dart | 6 +++++ 3 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 lib/src/constants.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 55d3e8f..072e6b2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -144,32 +144,33 @@ class HomePageState extends State { // Goto BkashPayment page & pass the params Navigator.of(context).push(MaterialPageRoute( builder: (context) => BkashPayment( - // amount of your bkash payment + isSandbox: true, + /// amount of your bkash payment amount: amount, - // intent would be (sale / authorization) + /// intent would be (sale / authorization) intent: intent, - // accessToken: '', // if the user have own access token for verify payment + // accessToken: '', /// if the user have own access token for verify payment // currency: 'BDT', - // bkash url for create payment, when you implement on you project then it be change as your production create url + /// bkash url for create payment, when you implement on you project then it be change as your production create url, [when you send it on sandbox mode, send it as empty string '' or anything] createBKashUrl: 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/create', - // bkash url for execute payment, , when you implement on you project then it be change as your production create url + /// bkash url for execute payment, , when you implement on you project then it be change as your production create url, [when you send it on sandbox mode, send it as empty string '' or anything] executeBKashUrl: 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/execute', - // for script url, when you implement on production the set it live script js + /// for script url, when you implement on production the set it live script js (https://scripts.pay.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-pay.js) scriptUrl: 'https://scripts.sandbox.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-sandbox.js', - // the return value from the package - // status => 'paymentSuccess', 'paymentFailed', 'paymentError', 'paymentClose' - // data => return value of response + /// the return value from the package + /// status => 'paymentSuccess', 'paymentFailed', 'paymentError', 'paymentClose' + /// data => return value of response paymentStatus: (status, data) { dev.log('return status => $status'); dev.log('return data => $data'); - // when payment success + /// when payment success if (status == 'paymentSuccess') { if (data['transactionStatus'] == 'Completed') { Style.basicToast('Payment Success'); } } - // when payment failed + /// when payment failed else if (status == 'paymentFailed') { if (data.isEmpty) { Style.errorToast('Payment Failed'); diff --git a/lib/flutter_bkash.dart b/lib/flutter_bkash.dart index 25b2829..504a2c4 100644 --- a/lib/flutter_bkash.dart +++ b/lib/flutter_bkash.dart @@ -2,23 +2,37 @@ library flutter_bkash; import 'dart:developer' as dev; import 'package:flutter/material.dart'; +import 'package:flutter_bkash/src/constants.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; typedef PaymentStatus = void Function(A status, B data); class BkashPayment extends StatefulWidget { + + /// default the sandbox is true + final bool isSandbox; + /// amount of the payment through bKash final String amount; + /// intent as sale or authorization final String intent; + /// reference no is order no or any other unique string for payment final String? refNo; + /// BDT final String? currency; + /// if the user have own access token for verify payment final String? accessToken; + /// create bkash url based on sandbox or production final String createBKashUrl; + /// execute bkash url based on sandbox or production final String executeBKashUrl; + /// javascript script url for load modal window for bkash payment final String scriptUrl; + /// return the payment status final PaymentStatus paymentStatus; const BkashPayment({ Key? key, + required this.isSandbox, required this.amount, required this.intent, this.refNo, @@ -38,6 +52,7 @@ class BkashPaymentState extends State { InAppWebViewController? webViewController; bool isLoading = true; + // define the payment empty dynamic variable for payment data var paymentData = {}; // payment handler as payment status @@ -52,14 +67,15 @@ class BkashPaymentState extends State { // payment data create as like below paymentData = { 'paymentRequest': { - 'amount': widget.amount, // amount for payment - 'intent': widget.intent, // sale - 'ref_no': widget.refNo ?? '', // order no - 'currency': widget.currency ?? '' // BDT + 'amount': widget.amount, + 'intent': widget.intent, + 'ref_no': widget.refNo ?? '', + 'currency': widget.currency ?? '', }, 'paymentConfig': { - 'createCheckoutURL': widget.createBKashUrl, - 'executeCheckoutURL': widget.executeBKashUrl, + /// sandbox is sandbox or live mode, change the value depend on it + 'createCheckoutURL': widget.isSandbox ? BkashConstants.sandboxCreateUrlBKash : widget.createBKashUrl, + 'executeCheckoutURL': widget.isSandbox? BkashConstants.sandboxExecuteUrlBKash : widget.executeBKashUrl, 'scriptUrl': widget.scriptUrl, }, 'accessToken': widget.accessToken ?? '', @@ -74,7 +90,7 @@ class BkashPaymentState extends State { // appBar: AppBar(title: const Text('bKash Checkout')), appBar: AppBar( elevation: 0.0, - backgroundColor: Colors.redAccent, + backgroundColor: Colors.pink, automaticallyImplyLeading: true, leading: IconButton( icon: const Icon(Icons.arrow_back), @@ -105,28 +121,20 @@ class BkashPaymentState extends State { ), onWebViewCreated: (controller) { webViewController = controller; - //sending data from dart to js + //sending data from dart to js the data of payment controller.addJavaScriptHandler( handlerName: 'paymentData', callback: (args) { // return data to the JavaScript side! return paymentData; }); - - controller.addJavaScriptHandler( - handlerName: 'accessToken', - callback: (args) { - // return data to the JavaScript side! - return widget.accessToken; - }); - controller.clearCache(); }, onLoadStop: ((controller, url) { // print('url $url'); - // for payment success + /// for payment success controller.addJavaScriptHandler( handlerName: 'paymentSuccess', callback: (success) { @@ -134,7 +142,7 @@ class BkashPaymentState extends State { _paymentHandler('paymentSuccess', success[0]); }); - // for payment failed + /// for payment failed controller.addJavaScriptHandler( handlerName: 'paymentFailed', callback: (failed) { @@ -142,7 +150,7 @@ class BkashPaymentState extends State { _paymentHandler('paymentFailed', failed); }); - // for payment error + /// for payment error controller.addJavaScriptHandler( handlerName: 'paymentError', callback: (error) { @@ -150,7 +158,7 @@ class BkashPaymentState extends State { _paymentHandler('paymentError', error[0]); }); - // for payment failed + /// for payment failed controller.addJavaScriptHandler( handlerName: 'paymentClose', callback: (close) { @@ -158,11 +166,12 @@ class BkashPaymentState extends State { _paymentHandler('paymentClose', close[0]); }); + /// set state is loading or not loading depend on page data setState(() => isLoading = false); }), onConsoleMessage: (controller, consoleMessage) { - // for view the console log as message on flutter side + /// for view the console log as message on flutter side dev.log(consoleMessage.toString()); }, ), diff --git a/lib/src/constants.dart b/lib/src/constants.dart new file mode 100644 index 0000000..63767a2 --- /dev/null +++ b/lib/src/constants.dart @@ -0,0 +1,6 @@ +class BkashConstants { + static const sandboxCreateUrlBKash = 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/create'; + static const sandboxExecuteUrlBKash = 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/execute'; + static const sandboxScriptUrl = 'https://scripts.sandbox.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-sandbox.js'; + static const productionScriptUrl = 'https://scripts.pay.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-pay.js'; +}