Skip to content

Commit

Permalink
Used AdyenCheckout singleton instead injecting it through constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-SD committed Dec 13, 2023
1 parent 11e5be0 commit 081affd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
17 changes: 4 additions & 13 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,9 @@ class _MyAppState extends State<MyApp> {
}

void _initRepositories() {
_adyenDropInRepository = AdyenDropInRepository(
adyenCheckout: _adyenCheckout,
service: _service,
);

_adyenCardComponentRepository = AdyenCardComponentRepository(
adyenCheckout: _adyenCheckout,
service: _service,
);
_adyenDropInRepository = AdyenDropInRepository(service: _service);
_adyenCardComponentRepository =
AdyenCardComponentRepository(service: _service);
}

@override
Expand Down Expand Up @@ -86,10 +80,7 @@ class _MyAppState extends State<MyApp> {

MaterialPageRoute<dynamic> _buildDropInRoute() {
return MaterialPageRoute(
builder: (context) => DropInScreen(
repository: _adyenDropInRepository,
adyenCheckout: _adyenCheckout,
),
builder: (context) => DropInScreen(repository: _adyenDropInRepository),
);
}

Expand Down
4 changes: 1 addition & 3 deletions example/lib/repositories/adyen_base_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import 'package:adyen_checkout_example/utils/payment_flow_outcome_handler.dart';

class AdyenBaseRepository {
AdyenBaseRepository({
required this.adyenCheckout,
required this.service,
});

final AdyenCheckout adyenCheckout;
final Service service;
final PaymentFlowOutcomeHandler paymentFlowOutcomeHandler =
PaymentFlowOutcomeHandler();

Future<String> determineBaseReturnUrl() async {
if (Platform.isAndroid) {
return await adyenCheckout.getReturnUrl();
return await AdyenCheckout.instance.getReturnUrl();
} else if (Platform.isIOS) {
return Config.iOSReturnUrl;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:adyen_checkout_example/repositories/adyen_base_repository.dart';

class AdyenCardComponentRepository extends AdyenBaseRepository {
AdyenCardComponentRepository({
required super.adyenCheckout,
required super.service,
});

Expand Down
9 changes: 3 additions & 6 deletions example/lib/repositories/adyen_drop_in_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import 'package:adyen_checkout_example/network/models/session_response_network_m
import 'package:adyen_checkout_example/repositories/adyen_base_repository.dart';

class AdyenDropInRepository extends AdyenBaseRepository {
AdyenDropInRepository({
required super.adyenCheckout,
required super.service,
});
AdyenDropInRepository({required super.service});

//A session should not being created from the mobile application.
//Please provide a CheckoutSession object from your own backend.
Expand All @@ -35,8 +32,8 @@ class AdyenDropInRepository extends AdyenBaseRepository {
countryCode: Config.countryCode,
shopperLocale: Config.shopperLocale,
shopperReference: Config.shopperReference,
storePaymentMethodMode:
StorePaymentMethodMode.askForConsent.storePaymentMethodModeString,
storePaymentMethodMode: StorePaymentMethodMode
.askForConsent.storePaymentMethodModeString,
recurringProcessingModel:
RecurringProcessingModel.cardOnFile.recurringModelString,
shopperInteraction:
Expand Down
45 changes: 24 additions & 21 deletions example/lib/screens/drop_in/drop_in_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import 'package:flutter/material.dart';
class DropInScreen extends StatefulWidget {
const DropInScreen({
required this.repository,
required this.adyenCheckout,
super.key,
});

final AdyenCheckout adyenCheckout;
final AdyenDropInRepository repository;

@override
Expand Down Expand Up @@ -47,33 +45,38 @@ class _DropInScreenState extends State<DropInScreen> {
}

Future<void> startDropInSessions() async {
final SessionResponseNetworkModel sessionResponse =
await widget.repository.fetchSession();
final DropInConfiguration dropInConfiguration =
await _createDropInConfiguration();

final Session session = await widget.adyenCheckout.createSession(
sessionId: sessionResponse.id,
sessionData: sessionResponse.sessionData,
configuration: dropInConfiguration,
);

final PaymentResult paymentResult = await widget.adyenCheckout.startPayment(
paymentFlow: DropInSessionFlow(
dropInConfiguration: dropInConfiguration,
session: session,
),
);
try {
final SessionResponseNetworkModel sessionResponse =
await widget.repository.fetchSession();
final DropInConfiguration dropInConfiguration =
await _createDropInConfiguration();

final Session session = await AdyenCheckout.instance.createSession(
sessionId: sessionResponse.id,
sessionData: sessionResponse.sessionData,
configuration: dropInConfiguration,
);

final PaymentResult paymentResult =
await AdyenCheckout.instance.startPayment(
paymentFlow: DropInSessionFlow(
dropInConfiguration: dropInConfiguration,
session: session,
),
);

_showPaymentResultDialog(paymentResult);
_showPaymentResultDialog(paymentResult);
} catch (error) {
debugPrint(error.toString());
}
}

Future<void> startDropInAdvancedFlow() async {
final paymentMethodsResponse =
await widget.repository.fetchPaymentMethods();
final dropInConfiguration = await _createDropInConfiguration();

final paymentResult = await widget.adyenCheckout.startPayment(
final paymentResult = await AdyenCheckout.instance.startPayment(
paymentFlow: DropInAdvancedFlow(
dropInConfiguration: dropInConfiguration,
paymentMethodsResponse: paymentMethodsResponse,
Expand Down

0 comments on commit 081affd

Please sign in to comment.