我正在尝试使用 Flutter Dart 从 Android Java 更新 Google Play 商店上的现有应用程序。在我现有的应用程序中,我有一个使用 google pay 的应用内购买,它工作得很好(使用 android java 编码),但我正在寻找如何使用 flutter dart 实现 google pay 应用内购买。
我发现GooglePay不是支付网关,而是网关的阶梯,因此您必须将支付网关系统与google集成,从列表中我推荐STRIPE。我将在下面解释。
我遇到了两个。实现这一目标的选项。
要求
确保您已设置 Stripe 帐户 - 如果您尚未访问 Stripe Payment Gateway 来设置您的帐户
第一个插件Google Pay插件 确保使用您的条带支付密钥初始化插件
GooglePay.initializeGooglePay("pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN");
这个插件的代码结构是这样的。
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:google_pay/google_pay.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _googlePayToken = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await GooglePay.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
await GooglePay.initializeGooglePay("pk_test_H5CJvRiPfCrRS44bZJLu46fM00UjQ0vtRN");
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
Text('Running on: $_platformVersion\n'),
Text('Google pay token: $_googlePayToken\n'),
FlatButton(
child: Text("Google Pay Button"),
onPressed: onButtonPressed,
)
]
),
),
),
);
}
void onButtonPressed() async{
setState((){_googlePayToken = "Fetching";});
try {
await GooglePay.openGooglePaySetup(
price: "5.0",
onGooglePaySuccess: onSuccess,
onGooglePayFailure: onFailure,
onGooglePayCanceled: onCancelled);
setState((){_googlePayToken = "Done Fetching";});
} on PlatformException catch (ex) {
setState((){_googlePayToken = "Failed Fetching";});
}
}
void onSuccess(String token){
setState((){_googlePayToken = token;});
}
void onFailure(){
setState((){_googlePayToken = "Failure";});
}
void onCancelled(){
setState((){_googlePayToken = "Cancelled";});
}
}
第二个插件Flutter Google Pay利用其他支付网关
您可以在此处查看列表支付网关
这个插件有两种初始化方法 - 使用 stripe 或其他网关
对于 Stripe,请使用此方法并使用您的 Stripe 密钥进行初始化 _makeStripePayment() 异步 { var 环境 = '休息'; // 或“生产”
if (!(await FlutterGooglePay.isAvailable(environment))) {
_showToast(scaffoldContext, 'Google pay not available');
} else {
PaymentItem pm = PaymentItem(
stripeToken: 'pk_test_1IV5H8NyhgGYOeK6vYV3Qw8f',// stripe public key
stripeVersion: "2018-11-08",
currencyCode: "usd",
amount: "0.10",
gateway: 'stripe');
FlutterGooglePay.makePayment(pm).then((Result result) {
if (result.status == ResultStatus.SUCCESS) {
_showToast(scaffoldContext, 'Success');
}
}).catchError((dynamic error) {
_showToast(scaffoldContext, error.toString());
});
}
}
对于其他支付网关
_makeCustomPayment() async {
var environment = 'rest'; // or 'production'
if (!(await FlutterGooglePay.isAvailable(environment))) {
_showToast(scaffoldContext, 'Google pay not available');
} else {
///docs https://developers.google.com/pay/api/android/guides/tutorial
PaymentBuilder pb = PaymentBuilder()
..addGateway("example")
..addTransactionInfo("1.0", "USD")
..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
..addAllowedCardNetworks(
["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"])
..addBillingAddressRequired(true)
..addPhoneNumberRequired(true)
..addShippingAddressRequired(true)
..addShippingSupportedCountries(["US", "GB"])
..addMerchantInfo("Example");
FlutterGooglePay.makeCustomPayment(pb.build()).then((Result result) {
if (result.status == ResultStatus.SUCCESS) {
_showToast(scaffoldContext, 'Success');
} else if (result.error != null) {
_showToast(context, result.error);
}
}).catchError((error) {
//TODO
});
}
}
您在应用程序内使用 g pay 应该没有问题。无需为此编写特定代码。您只需确保在 Google 结算平台中设置您的商家即可。查看本教程 https://medium.com/flutter-community/in-app-purchases-with-flutter-a-compressive-step-by-step-tutorial-b96065d79a21