Skip to content

Instantly share code, notes, and snippets.

@ologunB
Created December 24, 2021 14:10
Show Gist options
  • Save ologunB/f42089c331675b67b5f9e3393ff93e81 to your computer and use it in GitHub Desktop.
Save ologunB/f42089c331675b67b5f9e3393ff93e81 to your computer and use it in GitHub Desktop.
confirmPayment() async {
try {
setState(() {
isLoading = true;
});
HttpsCallable callable =
FirebaseFunctions.instance.httpsCallable('stripePayment');
var serverData = await callable.call({'amount': 1000});
Logger().d(serverData.data);
if (serverData?.data == null) {
showSnackBar(context, null, 'Error Processing Payment');
setState(() {
isLoading = false;
});
return;
}
String payIntent = serverData.data['client_secret'];
await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters(
applePay: false,
googlePay: true,
style: ThemeMode.light,
testEnv: true,
merchantCountryCode: 'CA',
merchantDisplayName: AppCache.getUser.name,
customerId: AppCache.getUser.uid,
paymentIntentClientSecret: payIntent,
));
await Stripe.instance.presentPaymentSheet();
Logger().d('a');
if (payIntent != null) {
setState(() {
isLoading = false;
});
Logger().d('payment made');
} else {
setState(() {
isLoading = false;
});
showSnackBar(context, null, serverData.data['message']);
}
} on FirebaseFunctionsException catch (e) {
Logger().d(e);
setState(() {
isLoading = false;
});
showSnackBar(context, null, e.message);
} on PlatformException catch (e) {
Logger().d(e);
setState(() {
isLoading = false;
});
showSnackBar(context, null, e.message);
} on StripeException catch (e) {
Logger().d(e);
setState(() {
isLoading = false;
});
showSnackBar(context, null, e.error.message);
} catch (e) {
Logger().d(e);
setState(() {
isLoading = false;
});
showSnackBar(context, null, e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment