有谁知道如何在 Flutter 的移动应用中实现
MOBILE MONEY PAYMENT SYSTEM
(仅在非洲部分地区使用的支付系统)?欢迎任何付款方式。
我找了很长一段时间,但只有一个,但对我来说根本不起作用。
这是我到目前为止所拥有的,它给了我错误。这是使用
PayStack Getway
class ApiKey {
// Paystack Test API Key
static String secretKey = "xxxxxxxx"; //My secrete key from Paystack
static const String payStackPublicKey =
"xxxxxxxx"; //My public key from Paystack
}
class Transaction {
final String amount;
final String reference;
final String currency;
final String email;
Transaction(
{required this.amount,
required this.reference,
required this.currency,
required this.email});
factory Transaction.fromJson(Map<String, dynamic> json) {
return Transaction(
amount: json['amount'],
reference: json['reference'],
currency: json['currency'],
email: json['email']);
}
Map<String, dynamic> toJson() {
return {
'amount': amount,
'reference': reference,
'currency': currency,
'email': email,
};
}
}
class PayStackAuthResponse {
final String authorization_url;
final String access_code;
final String reference;
PayStackAuthResponse({
required this.authorization_url,
required this.access_code,
required this.reference,
});
factory PayStackAuthResponse.fromJson(Map<String, dynamic> json) {
return PayStackAuthResponse(
authorization_url: json['authorization_url'],
access_code: json['access_code'],
reference: json['reference'],
);
}
Map<String, dynamic> toJson() {
return {
'authorization_url': authorization_url,
'access_code': access_code,
'reference': reference,
};
}
}
//on PaymentPage
class _GhanaMomoPaymentState extends State<GhanaMomoPayment> {
// final _webViewKey = UniqueKey();
// late WebViewController _webViewController;
Future<PayStackAuthResponse> createTransaction(
Transaction transaction) async {
const String url = 'https://api.paystack.co/transaction/initialize';
final data = transaction.toJson();
try {
final response = await http.post(
Uri.parse(url),
headers: {
'Authorization': 'Bearer ${ApiKey.secretKey}',
'Content-Type': 'application/json',
},
body: jsonEncode(data),
);
if (response.statusCode == 200) {
// Payment initialization successful
final responseData = jsonDecode(response.body);
return PayStackAuthResponse.fromJson(responseData['data']);
} else {
throw 'Payment unsuceesful';
}
} on Exception {
throw 'Payment Unsuceesful';
}
}
final adId = DateTime.now().microsecondsSinceEpoch;
// Future<bool> verifyTransaction(String reference) async {
// final String url = 'https://api.paystack.co/transaction/verify/$reference';
// try {
// final response = await http.get(Uri.parse(url), headers: {
// 'Authorization': 'Bearer ${ApiKey.secretKey}',
// 'Content-Type': 'application/json'
// });
// if (response.statusCode == 200) {
// final responseData = jsonDecode(response.body);
// if (responseData['data']['gateway_response'] == 'Approved') {
// return true;
// } else {
// return false;
// }
// } else {
// return false;
// }
// } on Exception {
// return false;
// }
// }
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
//These data are from the firestore databse
final serviceFee = widget.selectedRide["ServiceFee"];
int amount = int.parse(serviceFee);
final adsID = widget.selectedRide["Ads-ID"];
final email = widget.selectedRide["PassEmail"];
final String reference = adsID;
Future<String> initializeTransaction() async {
try {
final price = double.parse(serviceFee);
final transaction = Transaction(
amount: (amount * 100).toString(),
reference: reference,
currency: 'GHS',
email: email,
);
final authResponse = await createTransaction(transaction);
return authResponse.authorization_url;
} catch (e) {
Utils().awesomeDialogFailNavigate(
context,
"Error initializing transaction: $e",
"Failed",
const GhanaHomeScreen());
print('Error initializing transaction: $e');
return e.toString();
}
}
return Scaffold(
appBar: Utils().appBar(context),
body: Container(
child: FutureBuilder(
future: initializeTransaction(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final url = snapshot.data;
return WebViewWidget(
controller: WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
if (request.url
.startsWith('https://www.youtube.com/')) {
return NavigationDecision.prevent;
}
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse(url!)), //The error occurs here. It says : //I/flutter ( 4807): Another exception was thrown: Invalid argument(s): Missing scheme in uri: Payment%20unsuceesful
);
} else {
return const Center(child: CircularProgressIndicator());
}
})),
);
}
}
从依赖项 webview_flutter 到我们拥有
..loadRequest(Uri.parse(url!))
的控制器,url 是 ..loadRequest(Uri.parse('https://flutter.dev'))
。但从教程中它被替换为url!
。我尝试了这两种情况,但它仍然给我交易错误。该错误来自未来的构建者,但不知道如何解决它。
任何人都可以帮忙吗?我确实需要在我的应用程序上实施移动货币支付系统。它不一定来自 Paystack。任何支持此功能的网关都可以。谢谢。
class _GhanaMomoPaymentState extends State<GhanaMomoPayment> {
// ... (your existing code)
Future<String> initializeTransaction() async {
try {
final price = double.parse(serviceFee);
final transaction = Transaction(
amount: (amount * 100).toString(),
reference: reference,
currency: 'GHS',
email: email,
);
final authResponse = await createTransaction(transaction);
print('Auth Response: $authResponse'); // Print the response
if (authResponse.authorization_url != null) {
return authResponse.authorization_url;
} else {
throw 'Invalid authorization URL';
}
} catch (e) {
Utils().awesomeDialogFailNavigate(
context,
"Error initializing transaction: $e",
"Failed",
const GhanaHomeScreen());
print('Error initializing transaction: $e');
return e.toString();
}
}
// ... (rest of your code)
}
此外,请考虑检查 Paystack API 文档,以确保您使用正确的端点和参数。如果问题仍然存在,您可能需要探索在您所定位的特定区域支持移动货币支付的其他支付网关。