问题描述
条带支付 API 调用返回状态 200,但未收取金额。
我有一个 Flutter 应用程序使用 Stripe 支付。
我已经完成了:
- 已创建认证。
- 商家 ID 已创建。
按照stripe_payment
的示例,我在下面有我的本地付款方式:
void _handleNativePayment(String amount) async {
Token token = await StripePayment.paymentRequestWithNativePay(
androidPayOptions: AndroidPayPaymentRequest(
totalPrice: amount,currencyCode: _currencyCode,),applePayOptions: ApplePayPaymentOptions(
countryCode: _countryCode,items: [
ApplePayItem(
type: 'final',label: _paymentLabel,amount: amount,)
],);
await StripePayment.completeNativePayRequest();
}
当我在模拟器上运行它时,我可以看到付款已完成,并且可以在 Stripe 仪表板上查看请求,不幸的是付款仍然为 0。:
解决方法
在查看代码之前,请确保您已完成以下步骤: 0.增加了stripe_payment。
- 已创建商家 ID。
- Apple Pay 已在 Xcode 中启用。
- 条带“标准密钥”和“受限密钥”已生成。
- 标准密钥包含可发布密钥和秘密密钥
- Flutter 项目中可发布的关键用途。
- 在服务器端限制密钥使用。
我为本地支付做了什么:
- 调用 paymentRequestWithNativePay() 以获取令牌。
- 使用自己实施的收费方法收费(在服务器端,确保您使用受限密钥并且它具有收费写入权限)。
- 调用 paymentRequestWithNativePay()
void handleNativePayment(BuildContext context,String amountInStr) async {
// I used environment configurations for ANDROID_PAY_MODE,use "test" in test mode
// and uses "production" in production mode
StripePayment.setOptions(StripeOptions(
publishableKey: _publishableKey,merchantId: _merchantId,androidPayMode: EnvironmentConfig.ANDROID_PAY_MODE,));
// this is my service class talks to server side API
PaymentService paymentService = Provider.of<PaymentService>(
context,listen: false,);
Token token = await StripePayment.paymentRequestWithNativePay(
androidPayOptions: AndroidPayPaymentRequest(
totalPrice: amountInStr,currencyCode: _currencyCode,),applePayOptions: ApplePayPaymentOptions(
countryCode: _countryCode,items: [
ApplePayItem(
type: 'final',label: paymentLabel,amount: amountInStr,)
],);
// converts amount from string to int,and it is in cents
dynamic chargeResult = await paymentService.charge({
"token": token.tokenId,"amount": double.parse(amountInStr) * 100,"currency": _currencyCode,});
if (chargeResult is ChargeResult) {
_buildSnackBar(context,"Payment success",Colors.green);
} else {
_buildSnackBar(context,"Payment fail",Colors.red);
}
await StripePayment.completeNativePayRequest();
}
_buildSnackBar(BuildContext context,String content,Color color) {
final snackBar = SnackBar(
content: Text(content),backgroundColor: color,);
Scaffold.of(context).showSnackBar(snackBar);
}
这是 Flutter 中的 PaymentService。您需要使用 Stripe 受限密钥实现服务器端收费 API 调用。
class PaymentService {
var logger = Logger(printer: PrettyPrinter());
Future<dynamic> charge(Map<String,dynamic> data) async {
Map<String,dynamic> charge = {
"source": data["token"],"amount": data["amount"],"currency": data["currency"],};
final response = await DioHttp.post('/stripe/payment/charge',charge);
if (response.containsKey("results")) {
return ChargeResult.fromMap(response["results"]);
} else {
String errorMessage = response["errors"][0]["description"];
return errorMessage;
}
}
}
还有一件事需要您注意:
使用Android手机测试时,不要使用Stripe测试卡号,它总是让你请求失败(错误代码OR-CCSEH-21)。您需要做的是:
- 将安卓支付模式设置为“测试”。
- 用你自己的/真实的卡支付,它会通过(在下面附加的支付记录中,你可以看到最后一次支付包含我的名字,那是因为我用我自己的卡进行了android google pay测试)
希望它能帮助需要它的开发者。