flutter - 用于 Google Pay 的 Flutter 应用内购买工具
问题描述
我正在尝试使用 Flutter Dart 从 Android Java 更新我在 google play store 上的现有应用程序。在我现有的应用程序中,我有一个使用谷歌支付的应用内购买,它运行良好(使用 android java 编码),但我正在寻找如何使用颤振飞镖实现谷歌支付应用内购买。
解决方案
我发现 GooglePay 不是支付网关,而是通往网关的阶梯,所以你必须将支付网关系统与谷歌集成,从列表中我推荐 STRIPE。我将在下面解释。
我遇到了两个。实现这一目标的选项。
- Google Pay 插件- 这会自动使用 STRIPE 支付网关。
- Flutter Google Pay - 您可以使用多个自定义支付网关。
要求
确保您已设置 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 密钥初始化 _makeStripePayment() async { var environment = 'rest'; // 或“生产”
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
});
}
}
推荐阅读
- python - Virtualenvwrapper - 正确设置 .bash_profile
- javascript - 由于 element.style 无法自定义 Rev Slider 尺寸
- ssh - 为什么使用 `-t rsa -b 4096` 和 ssh-keygen?
- algorithm - 获取运行时间的渐近符号
- jenkins - 如何将参数作为环境变量从上游作业传递到詹金斯的下游作业?没有对下游作业进行参数化
- go - 我有一个公钥和一个 JWT,如何检查它在 Go 中是否有效?
- javascript - 我使用一位茉莉记者后显示“未找到规格”
- c# - 如何对一个集合进行分组并将相同的分组应用于其他相同长度的集合?
- java - Kafka反序列化嵌套的泛型类型
- python - 如何为 python eve 应用程序动态更新 mongo_prefix