Skip to content

Commit 7733ac2

Browse files
committed
modified the example and more on package
1 parent 8061693 commit 7733ac2

File tree

5 files changed

+201
-213
lines changed

5 files changed

+201
-213
lines changed

README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,34 @@
44

55
<h1 align="center">bKash(BD) Mobile Finance Payment Gateway for Flutter</h1>
66
<p align="center" >
7-
<img src="#" />
8-
<img src="#" />
9-
</p>
10-
11-
This is a Flutter package for [bKash](https://www.bkash.com/)) BD Payment Gateway. This package can be used in flutter project. We created this package while working for a project and thought to made it release for all so that it helps.
12-
<!--
137

8+
[//]: # (<img src="#" />)
9+
[//]: # (<img src="#" />)
1410

15-
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
11+
</p>
1612

17-
For general information about developing packages, see the Dart guide for
18-
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
19-
and the Flutter guide for
20-
[developing packages and plugins](https://flutter.dev/developing-packages).
21-
-->
13+
[![Pub](https://img.shields.io/pub/v/flutter_bkash.svg)](https://pub.dartlang.org/packages/flutter_bkash)
14+
[![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
2215

23-
TODO: Put a short description of the package here that helps potential users
24-
know whether this package might be useful for them.
16+
This is a Flutter package for [bKash](https://www.bkash.com/) BD Payment Gateway. This package can be used in flutter project. We created this package while working for a project and thought to made it release for all so that it helps.
2517

2618
## Features
2719

28-
TODO: List what your package can do. Maybe include images, gifs, or videos.
20+
List what your package can do. Maybe include images, gifs, or videos.
2921

3022
## Getting started
3123

32-
TODO: List prerequisites and provide or point to information on how to
24+
List prerequisites and provide or point to information on how to
3325
start using the package.
3426

3527
## Usage
3628

37-
TODO: Include short and useful examples for package users. Add longer examples
29+
Include short and useful examples for package users. Add longer examples
3830
to `/example` folder.
3931

40-
```dart
41-
const like = 'sample';
42-
```
4332

4433
## Additional information
4534

46-
TODO: Tell users more about the package: where to find more information, how to
35+
Tell users more about the package: where to find more information, how to
4736
contribute to the package, how to file issues, what response they can expect
4837
from the package authors, and more.

example/lib/main.dart

Lines changed: 184 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import 'package:flutter/material.dart';
1+
import 'dart:convert';
22

3-
import 'pages/home_page.dart';
3+
import 'dart:developer' as dev;
4+
import 'package:example/utils/style.dart';
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_bkash/flutter_bkash.dart';
47

58
void main() {
69
// it should be the first line in main method
@@ -23,4 +26,183 @@ class MyApp extends StatelessWidget {
2326
home: const HomePage(title: 'bKash Demo'),
2427
);
2528
}
29+
}
30+
31+
enum Intent { sale, authorization }
32+
33+
class HomePage extends StatefulWidget {
34+
final String title;
35+
36+
const HomePage({Key? key, required this.title}) : super(key: key);
37+
38+
@override
39+
HomePageState createState() => HomePageState();
40+
}
41+
42+
class HomePageState extends State<HomePage> {
43+
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
44+
final TextEditingController _amountController = TextEditingController();
45+
46+
Intent _intent = Intent.sale;
47+
FocusNode? focusNode;
48+
49+
@override
50+
void initState() {
51+
super.initState();
52+
53+
focusNode = FocusNode();
54+
}
55+
56+
@override
57+
void dispose() {
58+
// Clean up the focus node when the Form is disposed.
59+
focusNode!.dispose();
60+
super.dispose();
61+
}
62+
63+
@override
64+
Widget build(BuildContext context) {
65+
return Scaffold(
66+
key: _scaffoldKey,
67+
appBar: AppBar(title: Text(widget.title)),
68+
body: SingleChildScrollView(
69+
padding: const EdgeInsets.fromLTRB(80, 40, 80, 40),
70+
child: Column(
71+
crossAxisAlignment: CrossAxisAlignment.start,
72+
children: [
73+
const Text(
74+
'Amount :',
75+
style: TextStyle(fontWeight: FontWeight.bold),
76+
),
77+
Container(
78+
margin: const EdgeInsets.only(top: 8),
79+
child: TextFormField(
80+
focusNode: focusNode,
81+
controller: _amountController,
82+
decoration: const InputDecoration(
83+
hintText: "1240",
84+
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
85+
border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(5)), borderSide: BorderSide(color: Colors.grey)),
86+
focusedBorder: OutlineInputBorder(
87+
borderSide: BorderSide(color: Colors.pink, width: 2.0),
88+
),
89+
// hintText: reviewTitle,
90+
),
91+
keyboardType: TextInputType.number,
92+
maxLines: 1,
93+
minLines: 1,
94+
),
95+
),
96+
const SizedBox(height: 40.0),
97+
const Divider(),
98+
ListTile(
99+
title: const Text('Immediate'),
100+
leading: Radio(
101+
value: Intent.sale,
102+
groupValue: _intent,
103+
onChanged: (value) {
104+
setState(() => _intent = value!);
105+
},
106+
),
107+
dense: true,
108+
),
109+
ListTile(
110+
title: const Text('Auth and Capture'),
111+
leading: Radio(
112+
value: Intent.authorization,
113+
groupValue: _intent,
114+
onChanged: (value) {
115+
setState(() => _intent = value!);
116+
},
117+
),
118+
dense: true,
119+
),
120+
const Divider(),
121+
Center(
122+
child: TextButton(
123+
style: TextButton.styleFrom(
124+
shape: RoundedRectangleBorder(
125+
borderRadius: BorderRadius.circular(3.0),
126+
),
127+
backgroundColor: Colors.pink),
128+
child: const Text(
129+
"Checkout",
130+
style: TextStyle(color: Colors.white),
131+
),
132+
onPressed: () {
133+
String amount = _amountController.text.trim();
134+
String intent = _intent == Intent.sale ? "sale" : "authorization";
135+
136+
if (amount.isEmpty) {
137+
// if the amount is empty then show the snack-bar
138+
ScaffoldMessenger.of(context)
139+
.showSnackBar(const SnackBar(content: Text("Amount is empty. Without amount you can't pay. Try again")));
140+
return;
141+
}
142+
// remove focus from TextField to hide keyboard
143+
focusNode!.unfocus();
144+
// Goto BkashPayment page & pass the params
145+
Navigator.of(context).push(MaterialPageRoute(
146+
builder: (context) => BkashPayment(
147+
// amount of your bkash payment
148+
amount: amount,
149+
// intent would be (sale / authorization)
150+
intent: intent,
151+
// accessToken: '', // if the user have own access token for verify payment
152+
// currency: 'BDT',
153+
// bkash url for create payment, when you implement on you project then it be change as your production create url
154+
createBKashUrl: 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/create',
155+
// bkash url for execute payment, , when you implement on you project then it be change as your production create url
156+
executeBKashUrl: 'https://merchantserver.sandbox.bka.sh/api/checkout/v1.2.0-beta/payment/execute',
157+
// for script url, when you implement on production the set it live script js
158+
scriptUrl: 'https://scripts.sandbox.bka.sh/versions/1.2.0-beta/checkout/bKash-checkout-sandbox.js',
159+
// the return value from the package
160+
// status => 'paymentSuccess', 'paymentFailed', 'paymentError', 'paymentClose'
161+
// data => return value of response
162+
paymentStatus: (status, data) {
163+
dev.log('return status => $status');
164+
dev.log('return data => $data');
165+
// when payment success
166+
if (status == 'paymentSuccess') {
167+
if (data['transactionStatus'] == 'Completed') {
168+
Style.basicToast('Payment Success');
169+
}
170+
}
171+
172+
// when payment failed
173+
else if (status == 'paymentFailed') {
174+
if (data.isEmpty) {
175+
Style.errorToast('Payment Failed');
176+
} else if (data[0]['errorMessage'].toString() != 'null'){
177+
Style.errorToast("Payment Failed ${data[0]['errorMessage']}");
178+
} else {
179+
Style.errorToast("Payment Failed");
180+
}
181+
}
182+
183+
// when payment on error
184+
else if (status == 'paymentError') {
185+
Style.errorToast(jsonDecode(data['responseText'])['error']);
186+
}
187+
188+
// when payment close on demand closed the windows
189+
else if (status == 'paymentClose') {
190+
if (data == 'closedWindow') {
191+
Style.errorToast('Failed to payment, closed screen');
192+
} else if (data == 'scriptLoadedFailed') {
193+
Style.errorToast('Payment screen loading failed');
194+
}
195+
}
196+
// back to screen to pop()
197+
Navigator.of(context).pop();
198+
},
199+
)));
200+
},
201+
),
202+
)
203+
],
204+
),
205+
),
206+
);
207+
}
26208
}

0 commit comments

Comments
 (0)