Skip to content

Commit

Permalink
Merge pull request #1512 from ardriveapp/dev
Browse files Browse the repository at this point in the history
PE-5174: Release Flutter App v2.26.1
  • Loading branch information
matibat authored Dec 5, 2023
2 parents 8b5f191 + 7761cef commit 11a3e2c
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 50 deletions.
1 change: 1 addition & 0 deletions android/fastlane/metadata/android/en-US/changelogs/82.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Corrects the top-up modal able to display proper promo code discounts details.
18 changes: 18 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,15 @@
"@turboErrorMessageSessionExpired": {},
"turboErrorMessageUnknown": "The payment was not successful. Please check your card information and try again.",
"@turboErrorMessageUnknown": {},
"turboPercentageDiscountApplied": "({percentage}% discount applied)",
"@turboPercentageDiscountApplied": {
"description": "E.g. \"50% discount applied\"",
"placeholders": {
"percentage": {
"type": "String"
}
}
},
"turboPleaseEnterAmountBetween": "Please enter an amount between {min} - {max}",
"@turboPleaseEnterAmountBetween": {
"description": "Error message for when the given amount is not in range",
Expand All @@ -1836,6 +1845,15 @@
}
}
},
"turboUsdDiscountApplied": "(${amount} discount applied)",
"@turboUsdDiscountApplied": {
"description": "E.g. \"10$ discount applied\"",
"placeholders": {
"amount": {
"type": "String"
}
}
},
"unableToFetchEstimateAtThisTime": "Unable to fetch the estimate at this time.",
"@unableToFetchEstimateAtThisTime": {},
"unableToUpdateQuote": "Unable to update quote. Please try again.",
Expand Down
114 changes: 76 additions & 38 deletions lib/turbo/services/payment_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,29 @@ class PaymentService {
nonce: nonce,
wallet: wallet,
);
final result = await httpClient.get(
url: '$turboPaymentUri/v1/balance',
headers: {
'x-nonce': nonce,
'x-signature': signature,
'x-public-key': publicKey,
},
).onError((ArDriveHTTPException error, stackTrace) {
if (error.statusCode == 404) {
logger.w('user not found');
throw TurboUserNotFound();
}

logger.e('error getting balance', error, stackTrace);
try {
final result = await httpClient.get(
url: '$turboPaymentUri/v1/balance',
headers: {
'x-nonce': nonce,
'x-signature': signature,
'x-public-key': publicKey,
},
);

throw error;
});
final price = BigInt.parse((json.decode(result.data)['winc']));

final price = BigInt.parse((json.decode(result.data)['winc']));

return price;
return price;
} catch (error, stackTrace) {
if (error is ArDriveHTTPException) {
if (error.statusCode == 404) {
logger.w('user not found');
throw TurboUserNotFound();
}
}
logger.e('error getting balance', error, stackTrace);
rethrow;
}
}

Future<PaymentModel> getPaymentIntent({
Expand Down Expand Up @@ -159,30 +161,30 @@ Future<ArDriveHTTPResponse> _requestPriceForFiat(
final acceptedStatusCodes = [200, 202, 204];
final String urlParams = _urlParamsForGetPriceForFiat(promoCode: promoCode);

final result = await httpClient
.get(
url: '$turboPaymentUri/v1/price/$currency/$amount$urlParams',
headers: signatureHeaders,
)
.onError(
(ArDriveHTTPException error, stackTrace) {
try {
final result = await httpClient.get(
url: '$turboPaymentUri/v1/price/$currency/$amount$urlParams',
headers: signatureHeaders,
);

if (!acceptedStatusCodes.contains(result.statusCode)) {
throw PaymentServiceException(
'Turbo price fetch failed with status code ${result.statusCode}',
);
}

return result;
} catch (error) {
if (error is ArDriveHTTPException) {
if (error.statusCode == 400) {
logger.e('Invalid promo code: $promoCode');
throw PaymentServiceInvalidPromoCode(promoCode: promoCode);
}
throw PaymentServiceException(
'Turbo price fetch failed with exception: $error',
);
},
);

if (!acceptedStatusCodes.contains(result.statusCode)) {
}
throw PaymentServiceException(
'Turbo price fetch failed with status code ${result.statusCode}',
'Turbo price fetch failed with exception: $error',
);
}

return result;
}

Future<Map<String, dynamic>> _signatureHeadersForGetPriceForFiat({
Expand Down Expand Up @@ -262,7 +264,7 @@ class TurboUserNotFound implements Exception {
TurboUserNotFound();
}

class PaymentServiceException implements Exception {
class PaymentServiceException implements Exception, Equatable {
final String message;

PaymentServiceException([this.message = '']);
Expand All @@ -271,15 +273,32 @@ class PaymentServiceException implements Exception {
String toString() {
return 'PaymentServiceException{message: $message}';
}

@override
List<Object> get props => [message];

@override
bool? get stringify => true;
}

class PaymentServiceInvalidPromoCode implements PaymentServiceException {
final String? promoCode;

PaymentServiceInvalidPromoCode({required this.promoCode});
const PaymentServiceInvalidPromoCode({required this.promoCode});

@override
String get message => 'Invalid promo code: "$promoCode"';

@override
String toString() {
return 'PaymentServiceInvalidPromoCode{promoCode: $promoCode}';
}

@override
List<Object> get props => [promoCode ?? ''];

@override
bool? get stringify => true;
}

class PriceForFiat extends Equatable {
Expand All @@ -288,6 +307,25 @@ class PriceForFiat extends Equatable {
final int? quotedPaymentAmount;
final List<Adjustment> adjustments;

bool get hasReachedMaximumDiscount {
final maxDiscount = adjustments.first.maxDiscount;
if (maxDiscount == null) {
return false;
}

final adjustmentAmount = adjustments.first.adjustmentAmount;
return maxDiscount + adjustmentAmount == 0;
}

String? get adjustmentAmount {
if (adjustments.isEmpty) {
return null;
}

final adjustmentAmount = -adjustments.first.adjustmentAmount / 100;
return adjustmentAmount.toStringAsFixed(2);
}

String? get humanReadableDiscountPercentage {
if (adjustments.isEmpty) {
return null;
Expand Down
2 changes: 2 additions & 0 deletions lib/turbo/topup/blocs/payment_form/payment_form_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class PaymentFormState extends Equatable {
priceEstimate.humanReadableDiscountPercentage;
String get paymentAmount => priceEstimate.paymentAmount.toStringAsFixed(2);
BigInt get winstonCredits => priceEstimate.winstonCredits;
bool get hasReachedMaximumDiscount => priceEstimate.hasReachedMaximumDiscount;
String? get adjustmentAmount => priceEstimate.adjustmentAmount;

@override
List<Object> get props => [
Expand Down
5 changes: 4 additions & 1 deletion lib/turbo/topup/models/payment_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ class Adjustment extends Equatable {
final double operatorMagnitude;
final String operator;
final int adjustmentAmount;
final int? maxDiscount;

const Adjustment({
required this.name,
required this.description,
required this.operatorMagnitude,
required this.operator,
required this.adjustmentAmount,
required this.maxDiscount,
});

String get humanReadableDiscountPercentage {
Expand All @@ -105,7 +107,7 @@ class Adjustment extends Equatable {

@override
String toString() {
return 'Adjustment{name: $name, description: $description, operatorMagnitude: $operatorMagnitude, operator: $operator, adjustmentAmount: $adjustmentAmount}';
return 'Adjustment{name: $name, description: $description, operatorMagnitude: $operatorMagnitude, operator: $operator, adjustmentAmount: $adjustmentAmount, maxDiscount: $maxDiscount}';
}

@override
Expand All @@ -115,5 +117,6 @@ class Adjustment extends Equatable {
operatorMagnitude,
operator,
adjustmentAmount,
maxDiscount ?? '',
];
}
2 changes: 2 additions & 0 deletions lib/turbo/topup/models/price_estimate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class PriceEstimate extends Equatable {
? estimate.actualPaymentAmount! / 100
: priceInCurrency;
BigInt get winstonCredits => estimate.winstonCredits;
bool get hasReachedMaximumDiscount => estimate.hasReachedMaximumDiscount;
String? get adjustmentAmount => estimate.adjustmentAmount;

factory PriceEstimate.zero() => PriceEstimate(
estimate: PriceForFiat.zero(),
Expand Down
33 changes: 23 additions & 10 deletions lib/turbo/topup/views/topup_payment_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,7 @@ class TurboPaymentFormViewState extends State<TurboPaymentFormView> {
),
),
if (state.hasPromoCodeApplied)
TextSpan(
text:
' (${state.humanReadableDiscountPercentage}% discount applied)', // TODO: localize
style: ArDriveTypography.body.buttonNormalRegular(
color: ArDriveTheme.of(context)
.themeData
.colors
.themeFgDisabled,
),
),
_buildDiscountText(context, state),
],
),
);
Expand All @@ -281,6 +272,28 @@ class TurboPaymentFormViewState extends State<TurboPaymentFormView> {
);
}

TextSpan _buildDiscountText(BuildContext context, PaymentFormState state) {
final String text;
final hasReachedMaximumDiscount = state.hasReachedMaximumDiscount;

if (hasReachedMaximumDiscount) {
text = appLocalizationsOf(context).turboUsdDiscountApplied(
state.adjustmentAmount!,
);
} else {
text = appLocalizationsOf(context).turboPercentageDiscountApplied(
state.humanReadableDiscountPercentage!,
);
}

return TextSpan(
text: ' $text',
style: ArDriveTypography.body.buttonNormalRegular(
color: ArDriveTheme.of(context).themeData.colors.themeFgDisabled,
),
);
}

Widget _header(BuildContext context) {
return SizedBox(
width: double.maxFinite,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Secure, permanent storage

publish_to: 'none'

version: 2.26.0
version: 2.26.1

environment:
sdk: '>=3.0.2 <4.0.0'
Expand Down
Loading

0 comments on commit 11a3e2c

Please sign in to comment.