Skip to content

Commit

Permalink
modified as sandbox or other as [true/false], and more
Browse files Browse the repository at this point in the history
  • Loading branch information
rdnasim committed Sep 22, 2022
1 parent 361c99a commit 90b5312
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
23 changes: 12 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,32 +144,33 @@ class HomePageState extends State<HomePage> {
// 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');
Expand Down
51 changes: 30 additions & 21 deletions lib/flutter_bkash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<A, B> = 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<String, dynamic> paymentStatus;

const BkashPayment({
Key? key,
required this.isSandbox,
required this.amount,
required this.intent,
this.refNo,
Expand All @@ -38,6 +52,7 @@ class BkashPaymentState extends State<BkashPayment> {
InAppWebViewController? webViewController;

bool isLoading = true;
// define the payment empty dynamic variable for payment data
var paymentData = {};

// payment handler as payment status
Expand All @@ -52,14 +67,15 @@ class BkashPaymentState extends State<BkashPayment> {
// 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 ?? '',
Expand All @@ -74,7 +90,7 @@ class BkashPaymentState extends State<BkashPayment> {
// 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),
Expand Down Expand Up @@ -105,64 +121,57 @@ class BkashPaymentState extends State<BkashPayment> {
),
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) {
// print("bkashSuccess $success");
_paymentHandler('paymentSuccess', success[0]);
});

// for payment failed
/// for payment failed
controller.addJavaScriptHandler(
handlerName: 'paymentFailed',
callback: (failed) {
// print("bkashFailed $failed");
_paymentHandler('paymentFailed', failed);
});

// for payment error
/// for payment error
controller.addJavaScriptHandler(
handlerName: 'paymentError',
callback: (error) {
// print("paymentError => $error");
_paymentHandler('paymentError', error[0]);
});

// for payment failed
/// for payment failed
controller.addJavaScriptHandler(
handlerName: 'paymentClose',
callback: (close) {
// print("paymentClose => $close");
_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());
},
),
Expand Down
6 changes: 6 additions & 0 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit 90b5312

Please sign in to comment.