Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callback for initiated PaymentStatus #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ class _CoffeeShopState extends State<CoffeeShop> {
label: 'Blue Coffee Beans',
manual: false));

void onPaymentResult(result) {
Future<void> onPaymentResult(result) async {
if (result is PaymentResponse) {
showToast(context, result.status.name);
switch (result.status) {
case PaymentStatus.initiated:
// handle initiated.
// register the payment id on the server in the user payments data
await Future.delayed(
Durations.medium2,
() {
print("initiated payment with Id: ${result.id}");
},
);
break;
case PaymentStatus.paid:
// handle success.
break;
Expand Down
2 changes: 1 addition & 1 deletion example/lib/widgets/payment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:moyasar/moyasar.dart';

class PaymentMethods extends StatelessWidget {
final PaymentConfig paymentConfig;
final Function onPaymentResult;
final Function(dynamic) onPaymentResult;

const PaymentMethods(
{super.key, required this.paymentConfig, required this.onPaymentResult});
Expand Down
10 changes: 9 additions & 1 deletion lib/src/widgets/credit_card.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:moyasar/moyasar.dart';
Expand All @@ -14,7 +16,7 @@ class CreditCard extends StatefulWidget {
required this.onPaymentResult,
this.locale = const Localization.en()});

final Function onPaymentResult;
final FutureOr<void> Function(dynamic) onPaymentResult;
final PaymentConfig config;
final Localization locale;

Expand Down Expand Up @@ -77,6 +79,12 @@ class _CreditCardState extends State<CreditCard> {
return;
}

/// return the initiated status with the payment id
/// wait the [onPaymentResult] to complete to make sure that
/// the app register this payment on the backend before continue.
/// if [onPaymentResult] has any exception this will stop open [ThreeDSWebView]
await widget.onPaymentResult(result);

final String transactionUrl =
(result.source as CardPaymentResponseSource).transactionUrl;

Expand Down
4 changes: 3 additions & 1 deletion test/widgets/credit_card_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moyasar/moyasar.dart';
Expand All @@ -12,7 +14,7 @@ Widget createTestableApp(
description: "Coffee",
creditCard: CreditCardConfig(saveCard: tokenizeCard, manual: manual));

void onPaymentResult() {}
FutureOr<void> onPaymentResult(dynamic) {}

return MaterialApp(
home: Scaffold(
Expand Down