微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter stripe_payment Native pay 不收费Apple Pay 和 Google Pay

如何解决Flutter stripe_payment Native pay 不收费Apple Pay 和 Google Pay

条带支付 API 调用返回状态 200,但未收取金额。

我有一个 Flutter 应用程序使用 Stripe 支付。

我已经完成了:

  1. 已创建认证。
  2. 商家 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。:

enter image description here

enter image description here

enter image description here

解决方法

在查看代码之前,请确保您已完成以下步骤: 0.增加了stripe_payment。

  1. 已创建商家 ID。
  2. Apple Pay 已在 Xcode 中启用。
  3. 条带“标准密钥”和“受限密钥”已生成。
    • 标准密钥包含可发布密钥和秘密密钥
    • Flutter 项目中可发布的关键用途。
    • 服务器端限制密钥使用。

我为本地支付做了什么:

  1. 调用 paymentRequestWithNativePay() 以获取令牌。
  2. 使用自己实施的收费方法收费(在服务器端,确保您使用受限密钥并且它具有收费写入权限)。
  3. 调用 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)。您需要做的是:

  1. 将安卓支付模式设置为“测试”。
  2. 用你自己的/真实的卡支付,它会通过(在下面附加的支付记录中,你可以看到最后一次支付包含我的名字,那是因为我用我自己的卡进行了android google pay测试)

希望它能帮助需要它的开发者。

enter image description here enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。