-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Remon <>
- Loading branch information
Showing
5 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
example/lib/screens/regional_payment_methods/cash_app_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Map<String, dynamic>> _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<void> _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', | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters