Skip to content

Commit

Permalink
implement affirm example screen (#1816)
Browse files Browse the repository at this point in the history
Co-authored-by: Remon <[email protected]>
  • Loading branch information
remonh87 and Remon authored Jul 10, 2024
1 parent 8a03bf0 commit 749a4d2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
87 changes: 87 additions & 0 deletions example/lib/screens/regional_payment_methods/affirm_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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 AffirmScreen extends StatelessWidget {
const AffirmScreen({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': ['affirm'],
}),
);

print('blaat ${json.decode(response.body)}');

Check warning on line 27 in example/lib/screens/regional_payment_methods/affirm_screen.dart

View workflow job for this annotation

GitHub Actions / Typo CI

blaat

"blaat" is a typo. Did you mean "blast"?
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.affirm(paymentMethodData: PaymentMethodData()),
);

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Payment succesfully completed'),

Check warning on line 50 in example/lib/screens/regional_payment_methods/affirm_screen.dart

View workflow job for this annotation

GitHub Actions / Typo CI

succesfully

"succesfully" is a typo. Did you mean "successfully"?
),
);
} on Exception catch (e) {
if (e is StripeException) {
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: 'Affirm',
tags: ['Payment method'],
padding: EdgeInsets.all(16),
children: [
LoadingButton(
onPressed: () async {
await _pay(context);
},
text: 'Pay',
),
],
);
}
}
7 changes: 7 additions & 0 deletions example/lib/screens/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:stripe_example/screens/payment_sheet/payment_element/payment_ele
import 'package:stripe_example/screens/payment_sheet/payment_sheet_deffered_screen.dart';
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/affirm_screen.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';
Expand Down Expand Up @@ -237,6 +238,12 @@ class Example extends StatelessWidget {
builder: (context) => AliPayScreen(),
platformsSupported: [DevicePlatform.android, DevicePlatform.ios],
),
Example(
title: 'Affirm',
leading: SizedBox(),
builder: (context) => AffirmScreen(),
platformsSupported: [DevicePlatform.android, DevicePlatform.ios],
),
Example(
title: 'Cash app Pay',
builder: (context) => CashAppScreen(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PaymentMethod with _$PaymentMethod {
@JsonKey(name: 'Upi') required Upi upi,

/// Containing additional data in case paymentmethod type is UPI.
/// Containing additional data in case paymentmethod type is Us bank account.
@JsonKey(name: 'USBankAccount') required UsBankAccount usBankAccount,

/// Id related to the customer to which this paymentmethod has been saved.
Expand Down

0 comments on commit 749a4d2

Please sign in to comment.