stripePay method

Future<void> stripePay({
  1. required int amount,
  2. required String currency,
  3. required String secretKey,
  4. required String merchantCountryCode,
  5. required String merchantDisplayName,
  6. String paymentGatewayName = "Stripe",
  7. String? description,
  8. dynamic onShowToast()?,
  9. dynamic onPaymentSuccess()?,
  10. dynamic onPaymentFailure()?,
})

Opens Stripe Payment flow

Parameters:

  • amount: Payment amount in smallest currency unit (e.g. cents for USD)
  • currency: Currency code (e.g. "USD", "INR")
  • description: Payment description
  • paymentGatewayName: Gateway name for default toast/log (default: "Stripe")

Callbacks:

  • onShowToast: Optional custom toast/snackbar/dialog
  • onPaymentSuccess: Called when payment succeeds
  • onPaymentFailure: Called when payment fails

Implementation

Future<void> stripePay({
  required int amount,
  required String currency,
  required String secretKey,
  required String merchantCountryCode,
  required String merchantDisplayName,
  String paymentGatewayName = "Stripe",
  String? description,
  Function()? onShowToast,
  Function()? onPaymentSuccess,
  Function()? onPaymentFailure,
}) async {
  try {
    // Step 1: Create PaymentIntent via Stripe API
    Map<String, dynamic> body = {
      'amount': amount.toString(), // in cents (e.g. 500 = $5.00)
      'currency': currency,
      'description': description ?? '',
    };

    log("Start Payment Intent Http Request...");

    var response = await http.post(
      Uri.parse("https://api.stripe.com/v1/payment_intents"),
      body: body,
      headers: {"Authorization": "Bearer $secretKey", "Content-Type": 'application/x-www-form-urlencoded'},
    );

    log("Payment Intent Http Response => ${response.body}");

    if (response.statusCode == 200) {
      StripePayModel result = StripePayModel.fromJson(jsonDecode(response.body));

      // Step 2: Init Payment Sheet
      SetupPaymentSheetParameters setupPaymentSheetParameters = SetupPaymentSheetParameters(
        paymentIntentClientSecret: result.clientSecret,
        appearance: const PaymentSheetAppearance(
          colors: PaymentSheetAppearanceColors(primary: Colors.blue),
        ),
        googlePay: PaymentSheetGooglePay(merchantCountryCode: merchantCountryCode, testEnv: isTest),
        applePay: PaymentSheetApplePay(merchantCountryCode: merchantCountryCode),
        merchantDisplayName: merchantDisplayName,
      );

      await Stripe.instance.initPaymentSheet(paymentSheetParameters: setupPaymentSheetParameters);

      // Step 3: Present Payment Sheet
      await Stripe.instance.presentPaymentSheet().then((_) {
        final successResponse = IncodesPaymentResponse(
          "Payment Successful",
          true,
          result.id ?? "", // PaymentIntent ID
        );

        if (onShowToast != null) {
          onShowToast();
        } else {
          IncodesListener().onSuccess(
            response: successResponse,
            paymentGatewayName: paymentGatewayName,
            isShowToast: true,
          );
        }

        onPaymentSuccess?.call();
      }).catchError((e) {
        final failureResponse = IncodesPaymentResponse(
          "Payment Cancelled or Failed",
          false,
          result.id ?? "",
        );

        if (onShowToast != null) {
          onShowToast();
        } else {
          IncodesListener().onFailure(
            response: failureResponse,
            paymentGatewayName: paymentGatewayName,
            isShowToast: true,
          );
        }

        onPaymentFailure?.call();
      });
    } else {
      final failureResponse = IncodesPaymentResponse(
        "Stripe Payment Error",
        false,
        "",
      );

      if (onShowToast != null) {
        onShowToast();
      } else {
        IncodesListener().onFailure(
          response: failureResponse,
          paymentGatewayName: paymentGatewayName,
          isShowToast: true,
        );
      }

      onPaymentFailure?.call();
    }
  } catch (e) {
    log('Error in Stripe Payment: ${e.toString()}');

    final failureResponse = IncodesPaymentResponse(
      e.toString(),
      false,
      "",
    );

    if (onShowToast != null) {
      onShowToast();
    } else {
      IncodesListener().onFailure(
        response: failureResponse,
        paymentGatewayName: paymentGatewayName,
        isShowToast: true,
      );
    }

    onPaymentFailure?.call();
  }
}