Flutter stripe 如何从付款单中获取拒绝代码

Flutter stripe how can I get decline code from paymentsheet

提问人:Aabhash Rai 提问时间:11/16/2023 更新时间:11/16/2023 访问量:17

问:

我正在使用flutter_stripe包在我的应用程序中实现 stripe pay。PaymentSheet 用于付款。我想从 paymentSheet 获取拒绝代码或错误消息。当我像这样取消付款时,我可能会遇到条纹异常。

StripeException(error: LocalizedErrorMessage(code: FailureCode.Canceled, localizedMessage: 付款已取消, message: 付款已取消, stripeErrorCode: null, declineCode: null, type: null))

我希望在卡被拒绝或资金不足或任何其他问题时得到相同的结果。 正如您在屏幕截图中看到的那样,它显示“您的卡被拒绝”。我怎样才能在代码上捕捉到它,这样我就可以得到decline_code,这样我就可以把它发送到服务器?

As you can see in the screenshot it displays "your card was declined". How can I catch this on code so I can get decline_code so that I can send it to the server.

我的代码

Future<void> makePayment(
  {String? clientSecret,
  String? sessionId,
  int? donationId,
  Currency? currency}) async {
try {
  var gpay = PaymentSheetGooglePay(
      merchantCountryCode: currency!.code.substring(0, 2),
      currencyCode: currency.code,
      testEnv: true);
  var apay = PaymentSheetApplePay(
      merchantCountryCode: currency.code.substring(0, 2));
  //STEP 2: Initialize Payment Sheet

  await Stripe.instance.initPaymentSheet(
      paymentSheetParameters: SetupPaymentSheetParameters(
    billingDetailsCollectionConfiguration:
        const BillingDetailsCollectionConfiguration(
            address: AddressCollectionMode.never),
    paymentIntentClientSecret: clientSecret, //Gotten from payment intent

    merchantDisplayName: "Merchant",
    googlePay: gpay,
    applePay: apay,
  ));

  displayPaymentSheet(sessionId: sessionId, donationId: donationId);
} catch (err) {
  if (err is StripeException) {
    // Handle canceled payment
    log("${err.error.localizedMessage}");
    // You can show a message to the user or navigate back to the previous screen
  } else {
    // Handle other errors
    log('Error during payment: $err');
    // You might want to show a generic error message or log the error for debugging
  }
    log("payment error $err");
 }
}

displayPaymentSheet({String? sessionId, int? donationId}) async {
try {
  await Stripe.instance.presentPaymentSheet().then((value) async {
    try {} catch (e) {
      log("error $e");
    }
  }).onError((error, stackTrace) {
    throw Exception(error);
  });
} on PlatformException catch (exception) {
  log(exception.message ?? 'Something went wrong');
} catch (e) {
  log("error $e");
  if (e is StripeException) {
    // Handle canceled payment
    log("${e.error.localizedMessage}");
    // You can show a message to the user or navigate back to the previous screen
   } else {
    // Handle other errors
       log('Error during payment: $e');
    // You might want to show a generic error message or log the error for debugging
     }
    log('payment error 2 $e');
  }
 }
flutter 错误处理 stripe-payments 自定义错误处理 flutter-stripe

评论

0赞 alex 11/16/2023
为什么要将拒绝代码发送到您的服务器?如果要保存拒绝异步,可以侦听事件的 Webhook 并查看last_payment_errorpayment_intent.payment_failed

答: 暂无答案