Skip to content
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
131 changes: 61 additions & 70 deletions lib/src/components/card/adyen_card_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@ import 'dart:convert';
import 'package:adyen_checkout/adyen_checkout.dart';
import 'package:adyen_checkout/src/components/card/card_advanced_component.dart';
import 'package:adyen_checkout/src/components/card/card_session_component.dart';
import 'package:adyen_checkout/src/generated/platform_api.g.dart';
import 'package:adyen_checkout/src/util/constants.dart';
import 'package:adyen_checkout/src/util/dto_mapper.dart';
import 'package:adyen_checkout/src/util/sdk_version_number_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

class AdyenCardComponent extends StatelessWidget {
class AdyenCardComponent extends StatefulWidget {
final CardComponentConfiguration configuration;
final Map<String, dynamic> paymentMethod;
final Checkout checkout;
final Future<void> Function(PaymentResult) onPaymentResult;
final Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers;
final _isStoredPaymentMethodIndicator =
Constants.isStoredPaymentMethodIndicator;
final SdkVersionNumberProvider _sdkVersionNumberProvider =
SdkVersionNumberProvider.instance;

AdyenCardComponent({
const AdyenCardComponent({
super.key,
required this.configuration,
required this.paymentMethod,
Expand All @@ -30,79 +27,73 @@ class AdyenCardComponent extends StatelessWidget {
this.gestureRecognizers,
});

@override
State<AdyenCardComponent> createState() => _AdyenCardComponentState();
}

class _AdyenCardComponentState extends State<AdyenCardComponent> {
late final Future<String> _sdkVersionNumberFuture;

@override
void initState() {
super.initState();
_sdkVersionNumberFuture =
SdkVersionNumberProvider.instance.getSdkVersionNumber();
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _sdkVersionNumberProvider.getSdkVersionNumber(),
final double initialViewHeight =
_determineInitialHeight(widget.configuration.cardConfiguration);
final bool isStoredPaymentMethod = widget.paymentMethod
.containsKey(Constants.isStoredPaymentMethodIndicator);
final String encodedPaymentMethod = json.encode(widget.paymentMethod);

return FutureBuilder<String>(
future: _sdkVersionNumberFuture,
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.data != null) {
final sdkVersionNumber = snapshot.data ?? "";
return switch (checkout) {
SessionCheckout() => _buildCardSessionFlowWidget(sdkVersionNumber),
AdvancedCheckout it =>
_buildCardAdvancedFlowWidget(sdkVersionNumber, it),
};
} else {
return Container(
height: _determineInitialHeight(configuration.cardConfiguration),
);
final String? sdkVersion = snapshot.data;
if (sdkVersion == null) {
return Container(height: initialViewHeight);
}
},
);
}

CardSessionComponent _buildCardSessionFlowWidget(String sdkVersionNumber) {
final SessionCheckout sessionCheckout = checkout as SessionCheckout;
final String encodedPaymentMethod = json.encode(paymentMethod);
final double initialHeight =
_determineInitialHeight(configuration.cardConfiguration);
final bool isStoredPaymentMethod =
paymentMethod.containsKey(_isStoredPaymentMethodIndicator);

return CardSessionComponent(
cardComponentConfiguration: configuration.toDTO(sdkVersionNumber),
paymentMethod: encodedPaymentMethod,
session: sessionCheckout.toDTO(),
onPaymentResult: onPaymentResult,
initialViewHeight: initialHeight,
isStoredPaymentMethod: isStoredPaymentMethod,
onBinLookup: configuration.cardConfiguration.onBinLookup,
onBinValue: configuration.cardConfiguration.onBinValue,
);
}

CardAdvancedComponent _buildCardAdvancedFlowWidget(
String sdkVersionNumber,
Checkout advancedCheckout,
) {
final initialHeight =
_determineInitialHeight(configuration.cardConfiguration);
final String encodedPaymentMethod = json.encode(paymentMethod);
final bool isStoredPaymentMethod =
paymentMethod.containsKey(_isStoredPaymentMethodIndicator);

return CardAdvancedComponent(
cardComponentConfiguration: configuration.toDTO(sdkVersionNumber),
paymentMethod: encodedPaymentMethod,
advancedCheckout: advancedCheckout,
onPaymentResult: onPaymentResult,
initialViewHeight: initialHeight,
isStoredPaymentMethod: isStoredPaymentMethod,
gestureRecognizers: gestureRecognizers,
onBinLookup: configuration.cardConfiguration.onBinLookup,
onBinValue: configuration.cardConfiguration.onBinValue,
final CardComponentConfigurationDTO cardComponentConfiguration =
widget.configuration.toDTO(sdkVersion);
return switch (widget.checkout) {
SessionCheckout it => CardSessionComponent(
cardComponentConfiguration: cardComponentConfiguration,
session: it.toDTO(),
paymentMethod: encodedPaymentMethod,
onPaymentResult: widget.onPaymentResult,
initialViewHeight: initialViewHeight,
isStoredPaymentMethod: isStoredPaymentMethod,
gestureRecognizers: widget.gestureRecognizers,
onBinLookup: widget.configuration.cardConfiguration.onBinLookup,
onBinValue: widget.configuration.cardConfiguration.onBinValue,
),
AdvancedCheckout it => CardAdvancedComponent(
cardComponentConfiguration: cardComponentConfiguration,
paymentMethod: encodedPaymentMethod,
onPaymentResult: widget.onPaymentResult,
advancedCheckout: it,
initialViewHeight: initialViewHeight,
isStoredPaymentMethod: isStoredPaymentMethod,
gestureRecognizers: widget.gestureRecognizers,
onBinLookup: widget.configuration.cardConfiguration.onBinLookup,
onBinValue: widget.configuration.cardConfiguration.onBinValue,
)
};
},
);
}

double _determineInitialHeight(CardConfiguration cardConfiguration) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return _determineInitialAndroidViewHeight(cardConfiguration);
case TargetPlatform.iOS:
return _determineInitialIosViewHeight(cardConfiguration);
default:
throw UnsupportedError('Unsupported platform view');
}
return switch (defaultTargetPlatform) {
TargetPlatform.android =>
_determineInitialAndroidViewHeight(cardConfiguration),
TargetPlatform.iOS => _determineInitialIosViewHeight(cardConfiguration),
_ => throw UnsupportedError('Unsupported platform view'),
};
}

double _determineInitialAndroidViewHeight(
Expand Down
3 changes: 1 addition & 2 deletions lib/src/components/card/base_card_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,13 @@ class _BaseCardComponentState extends State<BaseCardComponent> {

@override
void initState() {
super.initState();
_cardWidget = _buildCardWidget();
_componentCommunicationStream = _componentFlutterApi
.componentCommunicationStream.stream
.where((communicationModel) =>
communicationModel.componentId == widget.componentId)
.listen(_onComponentCommunication);

super.initState();
}

@override
Expand Down
15 changes: 5 additions & 10 deletions lib/src/components/card/card_component_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@ class CardComponentContainer extends StatelessWidget {
if (viewportHeight == null)
SizedBox(
height: initialViewPortHeight,
child: _buildLoadingWidget(),
child: switch (defaultTargetPlatform) {
TargetPlatform.android =>
const Center(child: CircularProgressIndicator()),
_ => const Center(child: SizedBox.shrink()),
},
)
],
);
}

Widget _buildLoadingWidget() {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return const Center(child: CircularProgressIndicator());
default:
return const Center(child: SizedBox.shrink());
}
}

double _determineHeight(int? height) {
return height == null ? initialViewPortHeight : height + bottomSpacing;
}
Expand Down
Loading