我是编码新手。开发一个应用程序,通过它我可以在移动设备中打开 UPI 应用程序并使用 upi_india 包 flutter 进行支付。
在电话支付中,我收到无法启动交易的回复,请重试。
在 gpay 中,我收到了回复,因为金额没有被扣除。
一切正常,但每次付款状态都失败。
在下面我将所有代码供参考
提前致谢
import 'package:flutter/material.dart';
import 'package:upi_india/upi_india.dart';
import 'package:upi_payment_app/success_screen.dart';
class UPIPaymentScreen extends StatefulWidget {
@override
_UPIPaymentScreenState createState() => _UPIPaymentScreenState();
}
class _UPIPaymentScreenState extends State<UPIPaymentScreen> {
Future<UpiResponse> _transaction;
UpiIndia _upiIndia = UpiIndia();
List<UpiApp> apps;
@override
void initState() {
_upiIndia.getAllUpiApps(mandatoryTransactionId: false).then((value) {
setState(() {
apps = value;
});
}).catchError((e) {
apps = [];
});
super.initState();
}
Future<UpiResponse> initiateTransaction(UpiApp app) async {
return _upiIndia.startTransaction(
app: app,
receiverUpiId: "UPI id",
receiverName: 'UPI user name',
transactionRefId: 'TestingUpiIndiaPlugin',
transactionNote: 'Not actual. Just an example.',
amount: 2.00,
);
}
Widget displayUpiApps() {
if (apps == null)
return Center(child: CircularProgressIndicator());
else if (apps.length == 0)
return Center(
child: Text(
"No apps found to handle transaction.",
style: header,
),
);
else
return Align(
alignment: Alignment.topCenter,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Wrap(
children: apps.map<Widget>((UpiApp app) {
return GestureDetector(
onTap: () {
_transaction = initiateTransaction(app);
setState(() {});
},
child: Container(
height: 100,
width: 100,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.memory(
app.icon,
height: 60,
width: 60,
),
Text(app.name),
],
),
),
);
}).toList(),
),
),
);
}
String _upiErrorHandler(error) {
switch (error) {
case UpiIndiaAppNotInstalledException:
return 'Requested app not installed on device';
case UpiIndiaUserCancelledException:
return 'You cancelled the transaction';
case UpiIndiaNullResponseException:
return 'Requested app didn\'t return any response';
case UpiIndiaInvalidParametersException:
return 'Requested app cannot handle the transaction';
default:
return 'An Unknown error has occurred';
}
}
void _checkTxnStatus(String status) {
switch (status) {
case UpiPaymentStatus.SUCCESS:
print('Transaction Successful');
break;
case UpiPaymentStatus.SUBMITTED:
print('Transaction Submitted');
break;
case UpiPaymentStatus.FAILURE:
print('Transaction Failed');
break;
default:
print('Received an Unknown transaction status');
}
}
Widget displayTransactionData(title, body) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("$title: ", style: header),
Flexible(
child: Text(
body,
style: value,
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("UPI Payment"),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: displayUpiApps(),
),
// _transaction == null ? SizedBox.shrink() : _transaction.
Expanded(
child: FutureBuilder(
future: _transaction,
builder:
(BuildContext context, AsyncSnapshot<UpiResponse> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return Center(
child: Text(
_upiErrorHandler(snapshot.error.runtimeType),
style: header,
), // Print's text message on screen
);
}
// If we have data then definitely we will have UpiResponse.
// It cannot be null
UpiResponse _upiResponse = snapshot.data;
// Data in UpiResponse can be null. Check before printing
String txnId = _upiResponse.transactionId ?? 'N/A';
String resCode = _upiResponse.responseCode ?? 'N/A';
String txnRef = _upiResponse.transactionRefId ?? 'N/A';
String status = _upiResponse.status ?? 'N/A';
String approvalRef = _upiResponse.approvalRefNo ?? 'N/A';
_checkTxnStatus(status);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
displayTransactionData('Transaction Id', txnId),
displayTransactionData('Response Code', resCode),
displayTransactionData('Reference Id', txnRef),
displayTransactionData('Status', status.toUpperCase()),
displayTransactionData('Approval No', approvalRef),
],
),
);
} else
return Center(
child: Text(''),
);
},
),
)
],
),
);
}
}