I’m trying to implement Stripe payment integration in my Flutter mobile app with support for Afterpay. The Stripe payment sheet works fine when I use only the card payment method, but it fails when I add Afterpay as a payment method.
(I'm using this package "flutter_stripe: ^11.0.0" : https://pub.dev/packages/flutter_stripe)
Here is my implementation:
Stripe Service Code:
`
class StripeService {
StripeService._();
static final StripeService instance = StripeService._();
// static final String _stripeSecretKey = dotenv.env['STRIPE_SECRET_KEY'] ?? '';
static const String _stripeSecretKey = "secret key goes here";
Future<void> makePayment() async {
try {
print('Creating payment intent...');
String? paymentIntentClientSecret = await _createPaymentIntent(180, "usd");
print('Payment intent created: $paymentIntentClientSecret');
if (paymentIntentClientSecret == null) return;
print('Initializing payment sheet...');
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: paymentIntentClientSecret,
merchantDisplayName: "Bodyshock",
// Specify payment method types explicitly
paymentMethodOrder: ['card', 'afterpay_clearpay'],
allowsDelayedPaymentMethods: true,
),
);
print('Payment sheet initialized');
print('Presenting payment sheet...');
await _processPayment();
print('Payment sheet presented');
print('Payment Successful');
} catch (e) {
print('Error in makePayment: $e');
}
}
Future<String?> _createPaymentIntent(int amount, String currency) async {
try {
final Map<String, String> data = {
'amount': _calculateAmount(amount),
'currency': currency,
'payment_method_types[]': 'card',
'payment_method_types[]': 'afterpay_clearpay',
};
final response = await http.post(
Uri.parse('https://api.stripe.com/v1/payment_intents'),
body: data,
headers: {
'Authorization': 'Bearer $_stripeSecretKey',
'Content-Type': 'application/x-www-form-urlencoded',
},
);
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
print(responseData);
return responseData['client_secret'];
} else {
print('Failed to create payment intent: ${response.reasonPhrase}');
}
} catch (e) {
print(e);
}
return null;
}
Future<void> _processPayment() async {
try {
await Stripe.instance.presentPaymentSheet();
} catch (e) {
print(e);
}
}
String _calculateAmount(int amount) {
final calculatedAmount = amount * 100;
return calculatedAmount.toString();
}
}`
Issue:
When I include afterpay_clearpay in the payment methods, the Stripe payment sheet loads for a moment and then closes immediately, showing the following error:
I/flutter ( 5034): StripeException(error: LocalizedErrorMessage(code: FailureCode.Failed, localizedMessage: None of the requested payment methods (afterpay_clearpay) are supported., message: None of the requested payment methods (afterpay_clearpay) are supported., stripeErrorCode: null, declineCode: null, type: null))
Observations:
The Stripe form appears and works fine with only the card payment method.
The issue arises only when Afterpay is added as a payment method.
Ensured that Afterpay is enabled and supported in my Stripe account.
Firass Aguech is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.