import 'dart:convert'; import 'package:cathaypay_mobile/Home/HomePage.dart'; import 'package:cathaypay_mobile/Numpad.dart'; import 'package:cathaypay_mobile/PinCodeVadidate.dart'; import 'package:cathaypay_mobile/main.dart'; import 'package:cathaypay_mobile/utils/utils.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:mask_text_input_formatter/mask_text_input_formatter.dart'; import '../api/api.dart'; import '../utils/color_custom.dart'; import 'PayQrDialogV2.dart'; class PayPromptPay extends StatefulWidget { const PayPromptPay({Key? key}) : super(key: key); @override State createState() => _TransferPromptPayState(); } class _TransferPromptPayState extends State { TextEditingController _phone = TextEditingController(); bool get hasFocus => false; @override Widget build(BuildContext context) { final keyboardHeight = MediaQuery.of(context).viewInsets.bottom; final maxHeight = MediaQuery.of(context).size.height - keyboardHeight; return Container( height: 300, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.arrow_back_ios, color: Colors.black, )), Text( "Transfer money via PromptPay".tr(), style: TextStyle( color: Color(0xff050505), fontSize: 20, ), ) ], ), SizedBox( height: 20, ), Container( child: TextField( keyboardType: TextInputType.number, inputFormatters: [ FilteringTextInputFormatter.allow(RegExp(r'[0-9]')), ], controller: _phone, decoration: InputDecoration( hintText: 'Mobilephone number'.tr(), border: InputBorder.none, contentPadding: EdgeInsets.symmetric(horizontal: 16), ), ), margin: EdgeInsets.symmetric(horizontal: 20), height: 48, decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xfff2f2f2), ), ), Spacer(), InkWell( onTap: () async { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(16), ), ), builder: (BuildContext context) { return TransferPromptPayDetail( phone: _phone.text, ); }); }, child: Container( width: double.infinity, margin: const EdgeInsets.symmetric(horizontal: 15), child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), child: Container( padding: EdgeInsets.all(10), child: Text( "Next".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: ColorCustom.greyBorder, fontSize: 20, fontWeight: FontWeight.w500, ), ), ), ), ), ), SizedBox( height: 20, ) ], ), ); } } class TransferPromptPayDetail extends StatefulWidget { const TransferPromptPayDetail({ Key? key, required this.phone, }) : super(key: key); final String phone; @override State createState() => _TransferPromptPayDetailState(); } class _TransferPromptPayDetailState extends State { TextEditingController price = TextEditingController(); String priceTemp = ""; setValue(String val) { if (priceTemp.contains(".") && priceTemp.split(".")[1].length == 2) { return; } setState(() { priceTemp += val; if (val == ".") { price.text = "฿ " + Utils.moneyFormat(priceTemp) + "."; } else { price.text = "฿ " + Utils.moneyFormat(priceTemp); } // price.text = decimalFormat.formatString(priceTemp); }); } backspace() { if (priceTemp.length > 0) { setState(() { priceTemp = priceTemp.substring(0, priceTemp.length - 1); price.text = "฿ " + Utils.moneyFormat(priceTemp); // price.text = decimalFormat.formatString(priceTemp); }); } } initPromptPay(BuildContext context) { // var param = jsonEncode({"IDCardOrMobileDeviceNo": widget.phone, "Amount": priceTemp}); var param = jsonEncode( {"requestTransactionID": Utils.getDateInitPromptPay() + widget.phone.lastChars(4), "iDCardorMobileNo": widget.phone, "amount": "${(double.parse(priceTemp).toStringAsFixed(2)).replaceAll(".", "")}"}); Api.post(context, Api.payPromptPayInitial, param).then((value) => { if (value != null) { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.vertical( top: Radius.circular(16), ), ), builder: (BuildContext context) { return TransferBankConfirm( json: value, phone: widget.phone, price: priceTemp, ); }) } else {} }); } var maskFormatter = MaskTextInputFormatter(mask: '#,###,###', filter: {"#": RegExp('[0-9]')}); // CurrencyTextInputFormatter decimalFormat = CurrencyTextInputFormatter.currency(symbol: "", decimalDigits: 2); @override Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Container( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [ Row( children: [ IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.arrow_back_ios, color: Colors.black, )), Expanded( child: Text( "Transfer money via PromptPay".tr(), maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xff050505), fontSize: 20, ), ), ), IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.close, color: Colors.black, )), ], ), Text( "Mobilephone number".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Container( child: Row( children: [ Spacer(), Text( widget.phone, textAlign: TextAlign.right, style: TextStyle( color: Color(0xff65676b), fontSize: 20, ), ), SizedBox( width: 20, ) ], ), height: 48, decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xfff2f2f2), ), ), Text( "Amount transferred".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Container( width: double.infinity, decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xfff2f2f2), ), child: TextField( showCursor: true, readOnly: true, textAlign: TextAlign.end, keyboardType: TextInputType.number, inputFormatters: [ TextInputFormatter.withFunction((TextEditingValue oldValue, TextEditingValue newValue) { var formatter = NumberFormat('#,###,###.##', 'en_US'); var value = formatter.parse(newValue.text) ?? 0; // If a decimal point was just added to the end of the value, keep it. if (newValue.text.endsWith('.') && '.'.allMatches(newValue.text).length == 1) { return newValue; } // Otherwise, format the value correctly. return TextEditingValue( text: formatter.format(value), ); }) ], controller: price, style: TextStyle(fontSize: 24), decoration: InputDecoration( hintText: 'Amount transferred'.tr(), border: InputBorder.none, contentPadding: EdgeInsets.symmetric(horizontal: 16), ), ), ), Container( // padding: EdgeInsets.symmetric(horizontal: 50.0), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ NumpadButton( text: '1', onPressed: () => setValue('1'), ), NumpadButton( text: '2', onPressed: () => setValue('2'), ), NumpadButton( text: '3', onPressed: () => setValue('3'), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ NumpadButton( text: '4', onPressed: () => setValue('4'), ), NumpadButton( text: '5', onPressed: () => setValue('5'), ), NumpadButton( text: '6', onPressed: () => setValue('6'), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ NumpadButton( text: '7', onPressed: () => setValue('7'), ), NumpadButton( text: '8', onPressed: () => setValue('8'), ), NumpadButton( text: '9', onPressed: () => setValue('9'), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ // Visibility( // child: NumpadButton( // text: '0', // onPressed: () {}, // ), // maintainSize: true, // maintainAnimation: true, // maintainState: true, // visible: false, // ), NumpadButton( text: '.', onPressed: () => setValue('.'), ), NumpadButton( text: '0', onPressed: () => setValue('0'), ), NumpadButton( haveBorder: false, icon: Icons.backspace, onPressed: () => backspace(), ), ], ) ], ), ), InkWell( onTap: () async { if (double.parse(priceTemp) >= 1) { initPromptPay(context); } else { Utils.showAlertDialog(context, "Price must more than 10 Baht"); } }, child: SizedBox( width: double.infinity, child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), child: Container( padding: EdgeInsets.all(10), child: Text( "Confirm".tr(), textAlign: TextAlign.center, style: GoogleFonts.kanit( color: ColorCustom.greyBorder, fontSize: 20, fontWeight: FontWeight.w500, ), ), ), ), ), ), SizedBox( height: 10, ), ], ), ), ), )); } } class TransferBankConfirm extends StatefulWidget { const TransferBankConfirm({Key? key, this.json, required this.phone, required this.price}) : super(key: key); final dynamic json; final String phone; final String price; @override State createState() => _TransferBankConfirmState(); } class _TransferBankConfirmState extends State { var reqTrans = ""; var responseTrans = ""; @override void initState() { reqTrans = widget.json["requestTransactionID"]; responseTrans = widget.json["responseTransactionID"]; super.initState(); } confirmPromptPay(BuildContext context) { Utils.loadingProgress(context); // { // "IDCardOrMobileDeviceNo": "string", // "TransactionID": "string", // "Amount": 0, // "Note": "string" // } var param = jsonEncode({"requestTransactionID": reqTrans, "responseTransactionID": responseTrans, "confirmPayment": "Y", "note": ""}); Api.post(context, Api.payPromptPayConfirm, param).then((value) => { if (value != null) { Navigator.pop(context), showDialog( context: context, builder: (BuildContext context) { return PayQrDialogV2( price: value["slip"]["slipAmount"] ?? "", bill: value["requestTransactionID"] ?? "", phone: value["slip"]["promptpayID"] ?? "", name: value["slip"]["receiverNameTH"] ?? "", name_en: value["slip"]["receiverNameEN"] ?? "", bankName: value["slip"]["bankName"] ?? "", referenceNo: value["slip"]["referenceNo"] ?? "", slipDateTime: value["slip"]["slipDateTime"] ?? "", transactionId: value["slip"]["lookref"] ?? "", senderCom: value["slip"]["senderCompanyEN"] ?? "", ); }) } else {} }); } @override Widget build(BuildContext context) { return Column(children: [ Row(children: [ IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.arrow_back_ios, color: Colors.black, )), Text( "Transfer money via PromptPay".tr(), style: TextStyle( color: Color(0xff050505), fontSize: 20, ), ), Spacer(), IconButton( onPressed: () { Navigator.of(context).pop(); }, icon: Icon( Icons.close, color: Colors.black, )), ]), Container( width: 200, child: Image( image: AssetImage('images/neopay_logo.png'), ), ), Expanded( child: SingleChildScrollView( child: Column(mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Card( margin: EdgeInsets.all(10), color: Color(0xfffbfbfb), elevation: 5, child: Container( padding: const EdgeInsets.all(10.0), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text( "เลขที่บิล".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Expanded( child: Text( reqTrans, style: TextStyle( color: Color(0xff65676b), fontSize: 14, fontWeight: FontWeight.w300, ), textAlign: TextAlign.end, )) ], ), SizedBox( height: 15, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "วันที่ทำรายการ".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Text( Utils.getDateTimeCreate(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ) ], ), SizedBox( height: 30, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "จาก".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Text( profile?.fullName ?? "", style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w600, ), ) ], ), SizedBox( height: 15, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "", style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Text( profile?.personalCardId ?? "", style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ) ], ), SizedBox( height: 35, ), Row( children: [ Text( "ถึง".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Spacer(), Text( widget.phone, style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w600, ), ) ], ), SizedBox( height: 15, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "", style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Text( widget.phone, style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ) ], ), Padding( padding: const EdgeInsets.symmetric(vertical: 20), child: Divider(color: Color.fromRGBO(101, 103, 107, 1), thickness: 0.4000000059604645), ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "จำนวนเงิน".tr(), style: TextStyle( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), Text( "฿ ${Utils.moneyFormat(widget.price.replaceAll(",", ""))}", style: TextStyle( color: Color(0xff65676b), fontSize: 20, fontWeight: FontWeight.w800, ), ) ], ), SizedBox( height: 20, ), ], ), ), ), SizedBox( height: 20, ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ InkWell( onTap: () { Navigator.of(context).pop(); }, child: Container( margin: EdgeInsets.only(bottom: 15), width: MediaQuery.of(context).size.width / 4, height: 46, decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Color(0xff2d2d2d), ), child: Center( child: Text( "ยกเลิก".tr(), textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 18, fontWeight: FontWeight.w300, ), )), )), InkWell( onTap: () async { final result = await Navigator.push( context, MaterialPageRoute(builder: (context) => PinCodeValidatePage()), ); if (result == true) { confirmPromptPay(context); } }, child: Container( margin: EdgeInsets.only(bottom: 15), width: MediaQuery.of(context).size.width / 4, height: 46, decoration: BoxDecoration( borderRadius: BorderRadius.circular(100), color: Colors.white, boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 4, offset: Offset(0, 2), // changes position of shadow ), ], ), child: Center( child: Text( "Confirm".tr(), textAlign: TextAlign.center, style: TextStyle( color: ColorCustom.greyBorder, fontSize: 18, fontWeight: FontWeight.w300, ), )), )), ], ) ]), )) ]); } }