· 5 years ago · Jul 30, 2020, 06:00 AM
1import 'dart:io';
2import 'package:flutter/services.dart';
3import 'package:flutter/material.dart';
4import 'package:hiitburn_user/bloc/global/BlocProvider.dart';
5import 'package:hiitburn_user/bloc/global/GlobalBloc.dart';
6import 'package:hiitburn_user/bloc/cart/Cart.dart';
7import 'package:hiitburn_user/model/sales.dart';
8import 'package:hiitburn_user/screens/cart/Confirmation.dart';
9// import 'package:hiitburn_user/screens/cart/bankCard.dart';
10import 'package:shared_preferences/shared_preferences.dart';
11import 'package:hiitburn_user/theme/style.dart';
12import 'package:intl/intl.dart';
13import 'dart:convert';
14import 'package:hiitburn_user/services/paymentService.dart';
15import 'package:hiitburn_user/services/apiPayments.dart';
16
17import '../../components/widgets/alertDialog.dart';
18
19class PaymentNew extends StatefulWidget {
20 final SalesModel salesdata;
21 PaymentNew({@required this.salesdata});
22 @override
23 _PaymentNewState createState() => _PaymentNewState();
24}
25
26class _PaymentNewState extends State<PaymentNew> {
27 final NumberFormat formatter =
28 NumberFormat.currency(symbol: 'Rp. ', decimalDigits: 0, locale: 'id');
29 CartData cart;
30 int totalItems = 0;
31 PaymentService paymentService = PaymentService();
32 RestPayments api = new RestPayments();
33 bool loadingpay = false;
34 bool isLoading = false;
35 bool isSuccess = false;
36 File _imageFile;
37 final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
38 TextEditingController numberPhone = TextEditingController();
39 String payOption;
40 String userId;
41 bool prosespay = false;
42 void showError(String title, String message) {
43 setState(() {
44 loadingpay = false;
45 });
46 var desc = message;
47 if (desc == null) {
48 desc = 'There has been an error, please try again';
49 } else {
50 desc = message.replaceAll('Exception: ', '');
51 }
52 showDialog(
53 context: context,
54 builder: (BuildContext context) => AlertsDialog(
55 title: title,
56 description: desc,
57 buttonModalText: "OK",
58 ),
59 );
60 }
61
62 void processOrder() async {
63 var now = new DateTime.now();
64 var formatter = new DateFormat('yyyy-mm-dd');
65 String formatted = formatter.format(now);
66 SharedPreferences preferences = await SharedPreferences.getInstance();
67 Map jsonMap = {
68 "id": userId,
69 "amount": widget.salesdata.total,
70 "phone": '081897978665'
71 };
72 try {
73 dynamic res = await paymentService.requestApi('ovo/request', jsonMap);
74 setState(() {
75 if (res['code'] == 200) {
76 preferences.setString('external_id', res['content']['external_id']);
77 print(res.toString());
78 var content = {
79 "name": "",
80 "account": "",
81 // "bankId": _bankControl.text,
82 "bankId": 1,
83 "amount": widget.salesdata.total,
84 "salesId": widget.salesdata.id,
85 "salesCode": widget.salesdata.code,
86 "image": 'ovo.png',
87 "paymentDate": formatted,
88 };
89 print(content);
90 dynamic response = api.createPayments(content, _imageFile);
91 // Navigator.of(context).pushReplacement(
92 // MaterialPageRoute(builder: (BuildContext context) => ProcessOvo()));
93 } else {
94 _key.currentState.showSnackBar(SnackBar(
95 behavior: SnackBarBehavior.floating,
96 backgroundColor: Colors.red,
97 content: Text(
98 'gagal melakukan pembayaran',
99 style: TextStyle(color: Colors.white),
100 ),
101 ));
102 }
103 });
104 } catch (error) {
105 showError(
106 'Failed!', 'Sorry, there has been an error, please try again later');
107 }
108 setState(() {
109 loadingpay = false;
110 });
111 }
112
113 void payNow() async {
114 SharedPreferences preferences = await SharedPreferences.getInstance();
115 var userdata = json.decode(preferences.getString('UserModel'));
116 if (payOption == null) {
117 _key.currentState.showSnackBar(SnackBar(
118 behavior: SnackBarBehavior.floating,
119 duration: Duration(seconds: 1),
120 backgroundColor: Colors.red,
121 content: Text(
122 'Silahkan Pilih Pembayaran Anda',
123 style: TextStyle(color: Colors.white),
124 ),
125 ));
126 } else {
127 setState(() {
128 userId = userdata['id'];
129 prosespay = true;
130 preferences.setString('payOption', payOption);
131 });
132 }
133 }
134
135 @override
136 void initState() {
137 super.initState();
138 }
139
140 @override
141 Widget build(BuildContext context) {
142 BlocProvider.of<GlobalBloc>(context).cartBloc.clearCart();
143 MediaQueryData mediaQD = MediaQuery.of(context);
144 return Scaffold(
145 key: _key,
146 backgroundColor: ColorStyle.darkSlateGray,
147 bottomSheet: Container(
148 color: ColorStyle.darkSlateGray,
149 height: 55.0,
150 child: Row(
151 crossAxisAlignment: CrossAxisAlignment.stretch,
152 children: <Widget>[
153 prosespay
154 ? Expanded(
155 child: FlatButton(
156 color: ColorStyle.secondaryColor,
157 child: Text(
158 'Pay Now',
159 style: TextStyle(color: Colors.white),
160 ),
161 onPressed: () {
162 processOrder();
163 },
164 ),
165 )
166 : Expanded(
167 child: FlatButton(
168 color: ColorStyle.secondaryColor,
169 child: Text(
170 'Submit',
171 style: TextStyle(color: Colors.white),
172 ),
173 onPressed: () {
174 payNow();
175 },
176 ),
177 ),
178 // Expanded(
179 // child: FlatButton(
180 // color: ColorStyle.secondaryColor,
181 // child: Text(
182 // 'OK',
183 // style: TextStyle(color: Colors.white),
184 // ),
185 // onPressed: () {
186 // Navigator.popUntil(context, ModalRoute.withName('home'));
187 // },
188 // ),
189 // )
190 ],
191 ),
192 ),
193 body: Stack(
194 children: <Widget>[
195 SafeArea(
196 child: SingleChildScrollView(
197 child: Container(
198 padding: EdgeInsets.symmetric(horizontal: 20.0),
199 child: Column(
200 crossAxisAlignment: CrossAxisAlignment.start,
201 children: <Widget>[
202 Container(
203 padding: EdgeInsets.only(top: 30.0, bottom: 20.0),
204 width: mediaQD.size.width,
205 child: Row(
206 mainAxisAlignment: MainAxisAlignment.start,
207 children: <Widget>[
208 InkWell(
209 onTap: () {
210 Navigator.popUntil(
211 context, ModalRoute.withName('home'));
212 },
213 child: Container(
214 padding: EdgeInsets.only(
215 right: 2, top: 2, bottom: 2),
216 child: Icon(
217 Platform.isIOS
218 ? Icons.arrow_back_ios
219 : Icons.arrow_back,
220 size: 28.0,
221 color: Colors.white,
222 )),
223 ),
224 Container(
225 padding: EdgeInsets.only(left: 10.0),
226 child: Text(
227 'Order Created',
228 style: TextStyle(
229 color: Colors.white,
230 fontWeight: FontWeight.w600,
231 fontSize: 18),
232 ),
233 )
234 ],
235 ),
236 ),
237 Padding(
238 padding: EdgeInsets.only(top: 20),
239 child: Text(
240 'Order No.',
241 style: TextStyle(
242 fontWeight: FontWeight.w400,
243 color: Colors.white,
244 fontSize: 16.0,
245 letterSpacing: 1),
246 ),
247 ),
248 Text(
249 widget.salesdata.code,
250 style: TextStyle(
251 fontWeight: FontWeight.w800,
252 color: Colors.white,
253 fontSize: 22.0,
254 letterSpacing: 1),
255 ),
256 Padding(
257 padding: EdgeInsets.only(top: 20),
258 child: Text(
259 'Total Amount',
260 style: TextStyle(
261 fontWeight: FontWeight.w400,
262 color: Colors.white,
263 fontSize: 16.0,
264 letterSpacing: 1),
265 )),
266 StreamBuilder(
267 stream: BlocProvider.of<GlobalBloc>(context)
268 .cartBloc
269 .cartStream,
270 builder: (context, snapshot) {
271 if (snapshot.hasData) {
272 cart = snapshot.data;
273 return Text(
274 formatter.format(widget.salesdata.total),
275 style: TxtStyle.cartPriceStyle.copyWith(
276 color: Colors.white, fontSize: 17),
277 );
278 } else {
279 return Container();
280 }
281 }),
282 Stack(
283 children: <Widget>[
284 prosespay
285 ? ListView(
286 shrinkWrap: true,
287 children: <Widget>[
288 Padding(
289 padding: EdgeInsets.only(top: 30),
290 child: Text(
291 'Pay With $payOption',
292 style: TextStyle(
293 fontWeight: FontWeight.w800,
294 color: Colors.white,
295 fontSize: 16.0,
296 letterSpacing: 1),
297 )),
298 ],
299 )
300 : ListView(
301 shrinkWrap: true,
302 children: <Widget>[
303 Padding(
304 padding: EdgeInsets.only(top: 30),
305 child: Text(
306 'Select Payment',
307 style: TextStyle(
308 fontWeight: FontWeight.w800,
309 color: Colors.white,
310 fontSize: 16.0,
311 letterSpacing: 1),
312 )),
313 PaymentOptionMenu(
314 tap: () {
315 setState(() {
316 payOption = 'ovo';
317 });
318 },
319 titleMenu: 'OVO',
320 iconActive:
321 payOption == null ? false : true,
322 icon: payOption == 'ovo'
323 ? Icon(Icons.check,
324 color: Colors.green)
325 : Icon(Icons.check,
326 color: Colors.white),
327 ),
328 PaymentOptionMenu(
329 tap: () {
330 setState(() {
331 payOption = 'linkaja';
332 });
333 },
334 titleMenu: 'LINKAJA',
335 iconActive:
336 payOption == null ? false : true,
337 icon: payOption == 'linkaja'
338 ? Icon(Icons.check,
339 color: Colors.green)
340 : Icon(Icons.check,
341 color: Colors.white),
342 ),
343 PaymentOptionMenu(
344 tap: () {
345 setState(() {
346 payOption = 'dana';
347 });
348 },
349 titleMenu: 'DANA',
350 iconActive:
351 payOption == null ? false : true,
352 icon: payOption == 'dana'
353 ? Icon(Icons.check,
354 color: Colors.green)
355 : Icon(Icons.check,
356 color: Colors.white),
357 ),
358 ],
359 ),
360 loadingpay
361 ? Container(
362 child: Center(
363 child: CircularProgressIndicator()),
364 )
365 : Center()
366 ],
367 ),
368
369 // Column(
370 // children: <Widget>[
371 // Text(
372 // 'Silahkan Lakukan Pembayaran dengan cara transfer ke salah satu nomer rekening dibawah ini:',
373 // style: TxtStyle.descriptionStyle),
374 // BankCard(
375 // bank: 'BCA Klampis a/n Therry Andreas',
376 // account: '5200866969',
377 // color: Colors.indigo,
378 // ),
379 // ],
380 // ),
381 // Padding(
382 // padding: EdgeInsets.only(top: 30),
383 // child: Text(
384 // 'Confirm Payment',
385 // style: TextStyle(
386 // fontWeight: FontWeight.w800,
387 // color: Colors.white,
388 // fontSize: 16.0,
389 // letterSpacing: 1),
390 // )),
391 // Padding(
392 // padding: EdgeInsets.only(bottom: 10.0),
393 // child: Text(
394 // "Setelah melakukan pembayaran, konfirmasi pembayaran Anda dengan masuk ke halaman profile atau click tombol dibawah ini",
395 // style: TextStyle(color: Colors.white),
396 // )),
397 ],
398 ))),
399 ),
400 ],
401 ));
402 }
403}
404
405class PaymentOptionMenu extends StatelessWidget {
406 final Function tap;
407 final String titleMenu;
408 final bool iconActive;
409 final Icon icon;
410 PaymentOptionMenu({this.tap, this.titleMenu, this.iconActive, this.icon});
411
412 @override
413 Widget build(BuildContext context) {
414 return Padding(
415 padding: const EdgeInsets.only(left: 13.0, right: 13.0),
416 child: InkWell(
417 onTap: tap,
418 child: Card(
419 child: Padding(
420 padding: const EdgeInsets.all(8.0),
421 child: Row(
422 mainAxisAlignment: MainAxisAlignment.spaceBetween,
423 children: <Widget>[
424 Text(titleMenu),
425 iconActive
426 ? icon
427 : Icon(
428 Icons.arrow_forward_ios,
429 color: Colors.white,
430 )
431 ],
432 ),
433 ),
434 ),
435 ),
436 );
437 }
438}
439