From 79b23ede9390ec8ac4cff90d51b5044cc636cbbf Mon Sep 17 00:00:00 2001 From: Remon <> Date: Mon, 17 Jul 2023 20:03:52 +0100 Subject: [PATCH] Fix #1313 accept cash app payments --- .../cash_app_screen.dart | 89 +++++++++++++++++++ example/lib/screens/screens.dart | 6 ++ .../lib/src/models/payment_methods.dart | 4 +- .../src/models/payment_methods.freezed.dart | 4 +- 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 example/lib/screens/regional_payment_methods/cash_app_screen.dart diff --git a/example/lib/screens/regional_payment_methods/cash_app_screen.dart b/example/lib/screens/regional_payment_methods/cash_app_screen.dart new file mode 100644 index 000000000..fa5f419c8 --- /dev/null +++ b/example/lib/screens/regional_payment_methods/cash_app_screen.dart @@ -0,0 +1,89 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_stripe/flutter_stripe.dart'; +import 'package:http/http.dart' as http; +import 'package:stripe_example/widgets/example_scaffold.dart'; +import 'package:stripe_example/widgets/loading_button.dart'; + +import '../../config.dart'; + +class CashAppScreen extends StatelessWidget { + const CashAppScreen({Key? key}) : super(key: key); + + Future> _createPaymentIntent() async { + final url = Uri.parse('$kApiUrl/create-payment-intent'); + final response = await http.post( + url, + headers: { + 'Content-Type': 'application/json', + }, + body: json.encode({ + 'currency': 'usd', + 'payment_method_types': ['cashapp'], + 'amount': 6000 + }), + ); + + return json.decode(response.body); + } + + Future _pay(BuildContext context) async { + // Precondition: + //Make sure to have set a custom URI scheme in your app and add it to Stripe SDK + // see file main.dart in this example app. + // 1. on the backend create a payment intent for payment method and save the + // client secret. + final result = await _createPaymentIntent(); + final clientSecret = await result['clientSecret']; + + // 2. use the client secret to confirm the payment and handle the result. + try { + await Stripe.instance.confirmPayment( + paymentIntentClientSecret: clientSecret, + data: PaymentMethodParams.cashAppPay( + paymentMethodData: PaymentMethodData(), + ), + ); + + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Payment succesfully completed'), + ), + ); + } on Exception catch (e) { + if (e is StripeException) { + print('blaat $e'); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'), + ), + ); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Unforeseen error: ${e}'), + ), + ); + } + } + } + + @override + Widget build(BuildContext context) { + return ExampleScaffold( + title: 'Cashapp', + tags: ['Payment method'], + padding: EdgeInsets.all(16), + children: [ + LoadingButton( + onPressed: () async { + await _pay(context); + }, + text: 'Pay', + ), + ], + ); + } +} diff --git a/example/lib/screens/screens.dart b/example/lib/screens/screens.dart index 1e9fac18e..b6a27baff 100644 --- a/example/lib/screens/screens.dart +++ b/example/lib/screens/screens.dart @@ -5,6 +5,7 @@ import 'package:stripe_example/screens/payment_sheet/payment_sheet_screen.dart'; import 'package:stripe_example/screens/payment_sheet/payment_sheet_screen_custom_flow.dart'; import 'package:stripe_example/screens/regional_payment_methods/ali_pay_screen.dart'; import 'package:stripe_example/screens/regional_payment_methods/aubecs_debit.dart'; +import 'package:stripe_example/screens/regional_payment_methods/cash_app_screen.dart'; import 'package:stripe_example/screens/regional_payment_methods/fpx_screen.dart'; import 'package:stripe_example/screens/regional_payment_methods/ideal_screen.dart'; import 'package:stripe_example/screens/regional_payment_methods/klarna_screen.dart'; @@ -221,6 +222,11 @@ class Example extends StatelessWidget { builder: (context) => AliPayScreen(), platformsSupported: [DevicePlatform.android, DevicePlatform.ios], ), + Example( + title: 'Cash app Pay', + builder: (context) => CashAppScreen(), + platformsSupported: [DevicePlatform.android, DevicePlatform.ios], + ), Example( title: 'Ideal', leading: Image.asset( diff --git a/packages/stripe_platform_interface/lib/src/models/payment_methods.dart b/packages/stripe_platform_interface/lib/src/models/payment_methods.dart index 0ad158ded..72cd426e4 100644 --- a/packages/stripe_platform_interface/lib/src/models/payment_methods.dart +++ b/packages/stripe_platform_interface/lib/src/models/payment_methods.dart @@ -358,9 +358,9 @@ class PaymentMethodParams with _$PaymentMethodParams { }) = _PaymentMethodParamsAlipay; @JsonSerializable(explicitToJson: true) - @FreezedUnionValue('CashAppPay') + @FreezedUnionValue('CashApp') - /// Config parameters for Alipay card payment method. + /// Config parameters for cashapp payment method. const factory PaymentMethodParams.cashAppPay({ /// Paymentmethod data for this paymentmethod. required PaymentMethodData paymentMethodData, diff --git a/packages/stripe_platform_interface/lib/src/models/payment_methods.freezed.dart b/packages/stripe_platform_interface/lib/src/models/payment_methods.freezed.dart index 9d3a45431..c47b978ea 100644 --- a/packages/stripe_platform_interface/lib/src/models/payment_methods.freezed.dart +++ b/packages/stripe_platform_interface/lib/src/models/payment_methods.freezed.dart @@ -2859,7 +2859,7 @@ PaymentMethodParams _$PaymentMethodParamsFromJson(Map json) { return _PaymentMethodParamsCardWithMethodId.fromJson(json); case 'Alipay': return _PaymentMethodParamsAlipay.fromJson(json); - case 'CashAppPay': + case 'CashApp': return _PaymentMethodParamsCashAppPay.fromJson(json); case 'Ideal': return _PaymentMethodParamsIdeal.fromJson(json); @@ -4389,7 +4389,7 @@ class _$_PaymentMethodParamsCashAppPay implements _PaymentMethodParamsCashAppPay { const _$_PaymentMethodParamsCashAppPay( {required this.paymentMethodData, final String? $type}) - : $type = $type ?? 'CashAppPay'; + : $type = $type ?? 'CashApp'; factory _$_PaymentMethodParamsCashAppPay.fromJson( Map json) =>