Files
Neo_wallet/neowallet_mobile/lib/Pay/PayQr.dart
nutchayut 97ec586a44 bug fixed
2025-01-07 22:38:57 +07:00

521 lines
19 KiB
Dart

import 'dart:convert';
import 'package:cathaypay_mobile/Home/HomePage.dart';
import 'package:cathaypay_mobile/Pay/PayQrDialog.dart';
import 'package:cathaypay_mobile/PinCodeVadidate.dart';
import 'package:cathaypay_mobile/utils/utils.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import '../TransferMoney/TransferPromtptPayDialog.dart';
import '../api/api.dart';
import '../utils/color_custom.dart';
import 'PayQrDialogV2.dart';
class PayQrPage extends StatefulWidget {
const PayQrPage({Key? key, required this.code}) : super(key: key);
final List code;
@override
State<PayQrPage> createState() => _PayQrPageState();
}
class _PayQrPageState extends State<PayQrPage> {
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
Barcode? result;
// In order to get hot reload to work we need to pause the camera if the platform
// is android, or resume the camera if the platform is iOS.
var transactionID = "";
var payPhone = "";
var myQRID = "";
var name = "";
var type = "";
@override
void initState() {
type = widget.code[0];
if (type == "TRANSFERFROM") {
payPhone = widget.code[5];
name = widget.code[6];
price.text = widget.code[4];
}
if (type == "PAYMENTC2B") {
payPhone = widget.code[4];
name = widget.code[5];
price.text = widget.code[9];
} else {
payPhone = widget.code[4];
name = widget.code[5];
}
transactionID = widget.code[1];
myQRID = widget.code[3];
super.initState();
}
checkTypePay() {
if (type == "TRANSFERFROM") {
confirmPayQrAmount();
} else if (type == "TRANSFERTO") {
confirmPayQr();
} else if (type == "PAYMENTC2B") {
initC2B();
}
}
String removeTextPrice() {
return price.text.replaceAll(",", "").replaceAll("฿", "").trim();
}
confirmPayQr() {
var param = jsonEncode(<dynamic, dynamic>{"MobileDeviceNo": profile?.phoneNumber ?? "", "Note": "", "Amount": double.parse(removeTextPrice()), "TransactionID": transactionID});
Api.postGetMessage(context, Api.myQrCodeConfirm, param).then((value) => {
if (value != null)
{
showDialog(
context: context,
builder: (BuildContext context) {
return TransferPromtptPayDialog(
phone: payPhone,
price: removeTextPrice(),
name: name,
bill: value["data"]["_TranID"],
);
/* return PayQrDialogV2(
price: value["slipAmount"]??"",
bill: value["requestTransactionID"]??"",
phone: payPhone,
name: value["slipTitleTH"]??"",
name_en: value["slipTitleEN"]??"",
bankName: value["bankName"]??"",
referenceNo:value["referenceNo"]??"",
slipDateTime: value["slipDateTime"]??"",
transactionId: "",
);*/
})
}
else
{}
});
}
confirmPayQrAmount() {
var param = jsonEncode(<dynamic, dynamic>{"MobileDeviceNo": profile?.phoneNumber ?? "", "Note": "", "TransactionID": transactionID});
Api.post(context, Api.qrCodeTransferConfirm, param).then((value) => {
if (value != null)
{
showDialog(
context: context,
builder: (BuildContext context) {
return PayQrDialog(
phone: payPhone,
price: price.text.replaceAll(",", ""),
name: name,
transactionId: value["data"]["_TranID"],
);
})
}
else
{}
});
}
initC2B() {
// var param = jsonEncode(<dynamic, dynamic>{"MobileDeviceNo": payPhone ?? "", "Note": "", "TransactionID": transactionID});
var param = jsonEncode(<dynamic, dynamic>{
"paymentTransactionId": transactionID,
"InitStatus": "INITIAL",
"respondTime": Utils.getResponseTime(),
"MobileDeviceID": profile?.phoneNumber,
"toKen": "Pay${profile?.phoneNumber}",
"RespontURLToMobile": Api.callBackURL
});
Api.post(context, Api.c2bInitial, param).then((value) => {
if (value != null) {confirmPayQrC2BAmount(value["data"]["_TranID"])} else {}
});
}
confirmPayQrC2BAmount(String id) {
// var param = jsonEncode(<dynamic, dynamic>{"MobileDeviceNo": payPhone ?? "", "Note": "", "TransactionID": transactionID});
var param = jsonEncode(<dynamic, dynamic>{
"paymentTransactionId": id,
"transactionStatus": "CONFIRM",
"respondTime": Utils.getResponseTime(),
"MobileDeviceID": profile?.phoneNumber,
"toKen": "${profile?.phoneNumber} confirm C2B"
});
Api.post(context, Api.c2bConfirm, param).then((value) => {
if (value != null)
{
showDialog(
context: context,
builder: (BuildContext context) {
return PayQrDialog(
phone: payPhone,
price: price.text.replaceAll(",", ""),
name: name,
transactionId: id,
);
})
}
else
{}
});
}
// CurrencyTextInputFormatter decimalFormat = CurrencyTextInputFormatter.currency(symbol: "", decimalDigits: 2,);
TextEditingController price = new TextEditingController();
String priceTemp = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
actions: [
CupertinoButton(
onPressed: () {
Navigator.pop(context);
},
child: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: const Icon(
Icons.clear,
color: Colors.grey,
),
),
)
],
elevation: 0,
title: Text(
"Pay".tr(),
textAlign: TextAlign.center,
style: GoogleFonts.kanit(
color: Colors.black,
fontSize: 20,
),
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
"ชื่อ".tr(),
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
fontWeight: FontWeight.w300,
),
),
SizedBox(
height: 20,
),
Container(
height: 48,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xfff2f2f2),
),
child: Row(
children: [
Spacer(),
Text(
name,
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff65676b),
fontSize: 20,
),
),
SizedBox(
width: 20,
)
],
),
),
SizedBox(
height: 10,
),
Row(
children: [
Text(
"เบอร์โทรศัพท์".tr(),
style: TextStyle(
color: Color(0xff65676b),
fontSize: 14,
fontWeight: FontWeight.w300,
),
),
Spacer(),
Text(
payPhone,
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
),
)
],
),
SizedBox(
height: 10,
),
Text(
"จำนวนเงินที่โอน / จ่าย".tr(),
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
fontWeight: FontWeight.w300,
),
),
SizedBox(
height: 10,
),
Container(
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xfff2f2f2),
),
child: TextField(
textAlign: TextAlign.end,
keyboardType: TextInputType.numberWithOptions(decimal: true),
// inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[0-9.]"))],
inputFormatters: [
TextInputFormatter.withFunction((TextEditingValue oldValue, TextEditingValue newValue) {
print("newValue " + newValue.text);
if (".".allMatches(newValue.text).length > 1) {
print("case1 " + newValue.text);
return oldValue;
} else if (newValue.text.contains(".") && newValue.text.split(".")[1].length == 1) {
return newValue;
} else {
if (oldValue.text.contains(".") && oldValue.text.split(".")[1].length == 2) {
print("case2 " + newValue.text);
return oldValue;
} else {
// 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;
}
}
}
print("out case");
var formatter = NumberFormat('฿ #,###,###.##', 'en_US');
var value = formatter.parse(newValue.text) ?? 0;
// Otherwise, format the value correctly.
return TextEditingValue(
text: formatter.format(value),
);
})
],
// onTap: (){
// Utils.popupNum(context, price);
// },
controller: price,
style: TextStyle(fontSize: 24),
decoration: InputDecoration(
hintText: "จำนวนเงินที่โอน / จ่าย".tr(),
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 16),
),
),
),
SizedBox(
height: 10,
),
Text(
"ใช้แต้ม NEO PAY แทนเงินสด".tr(),
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
fontWeight: FontWeight.w300,
),
),
Text(
"${"คะแนนของคุณ".tr()} : ${Utils.moneyFormat(profile?.maxCardMembers.first.pointBalance)} ${"แต้ม".tr()}",
style: TextStyle(
color: Color(0xff9d001b),
fontSize: 14,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xfff2f2f2),
),
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 6,
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"10 ${"แต้ม".tr()}",
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff65676b),
),
),
],
),
),
),
Text(
"=",
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff65676b),
fontSize: 24,
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xff9d001b),
),
padding: const EdgeInsets.symmetric(
horizontal: 20,
vertical: 6,
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"100 ${"Baht".tr()}",
textAlign: TextAlign.right,
style: TextStyle(
color: Colors.white,
),
),
],
),
))
],
),
Center(
child: Text(
"1 แต้ม = 10 สตางค์".tr(),
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xff65676b),
fontSize: 14,
fontWeight: FontWeight.w300,
),
),
),
Container(
height: 136,
color: Colors.transparent,
child: Column(
children: [
SizedBox(
height: 20,
),
Row(
children: [
Text(
"ยอดเงินทั้งหมด".tr(),
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
),
),
Spacer(),
Text(
price.text,
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff9d001b),
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
Text(
" ${"Baht".tr()}",
textAlign: TextAlign.right,
style: TextStyle(
color: Color(0xff65676b),
fontSize: 16,
),
)
],
),
SizedBox(
height: 20,
),
InkWell(
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => PinCodeValidatePage()),
);
if (result == true) {
checkTypePay();
}
},
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,
),
],
),
),
SizedBox(
height: 10,
),
],
),
),
),
));
}
}