Skip to content

Commit

Permalink
Release v0.0.8: Fix build failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieu Bui committed Dec 3, 2024
1 parent 1073886 commit e3798c7
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/data/datasource/payment/payment_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import 'package:flutter_stripe/flutter_stripe.dart';
abstract class PaymentDataSource {
Future<Map<String, dynamic>?> createPaymentIntent(
CreatePaymentIntentRequestBody body);
Future<PaymentIntent> confirmPaymentIntentWeb();
Future<PaymentIntent?> confirmPaymentIntentWeb();
}
15 changes: 4 additions & 11 deletions lib/data/datasource_impl/payment/payment_datasource_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'package:ecoparking_flutter/data/datasource/payment/payment_datasource.da
import 'package:ecoparking_flutter/data/models/payment/create_payment_intent_request_body.dart';
import 'package:ecoparking_flutter/data/supabase_data/database_functions_name.dart';
import 'package:ecoparking_flutter/di/supabase_utils.dart';
import 'package:ecoparking_flutter/utils/mixins/oauth_mixin/mixin_utils.dart';
import 'package:flutter_stripe_web/flutter_stripe_web.dart';
import 'package:ecoparking_flutter/utils/conditional_import/stripe/confirm_payment/confirm_payment.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

class PaymentDataSourceImpl extends PaymentDataSource {
@override
Expand All @@ -28,14 +28,7 @@ class PaymentDataSourceImpl extends PaymentDataSource {
}

@override
Future<PaymentIntent> confirmPaymentIntentWeb() {
return WebStripe.instance.confirmPaymentElement(
ConfirmPaymentElementOptions(
confirmParams: ConfirmPaymentParams(
return_url: '${MixinUtils().getRedirectURL()}/payment-return',
),
redirect: PaymentConfirmationRedirect.ifRequired,
),
);
Future<PaymentIntent?> confirmPaymentIntentWeb() {
return StripeConfirmPayment.confirmPayment();
}
}
52 changes: 52 additions & 0 deletions lib/data/network/payment/stripe_payment_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:dio/dio.dart';
import 'package:ecoparking_flutter/config/env_loader.dart';
import 'package:ecoparking_flutter/data/models/payment/create_payment_intent_request_body.dart';
import 'package:ecoparking_flutter/data/models/payment/create_payment_intent_response.dart';
import 'package:ecoparking_flutter/data/network/dio_client.dart';
import 'package:ecoparking_flutter/di/global/get_it_initializer.dart';
import 'package:ecoparking_flutter/di/global/network_di.dart';

class StripePaymentApi {
final DioClient _dioClient = getIt.get<DioClient>(
instanceName: NetworkDi.serverDioClientName,
);

StripePaymentApi();

Future<CreatePaymentIntentResponse> createPaymentIntent(
CreatePaymentIntentRequestBody body) async {
final url = Uri.parse('https://api.stripe.com/v1/payment_intents');
final secretKey = EnvLoader.stripeSecretKey;
final requestBody = {
"amount": body.amount,
"currency": body.currency,
"description": body.description,
"metadata": {
"parking_name": body.metadata.parkingName,
"parking_address": body.metadata.parkingAddress,
"vehicle": body.metadata.vehicle,
"start_date_time": body.metadata.startDateTime,
"end_date_time": body.metadata.endDateTime,
"hours": body.metadata.hours,
"days": body.metadata.days,
}
};

final response = await _dioClient
.post(
url.toString(),
data: requestBody,
options: Options(
headers: {
"Authorization": "Bearer $secretKey",
'Content-Type': 'application/x-www-form-urlencoded'
},
),
)
.onError((error, stackTrace) => throw Exception(error));

return CreatePaymentIntentResponse.fromJson(
response.data as Map<String, dynamic>,
);
}
}
2 changes: 1 addition & 1 deletion lib/data/repository/payment/payment_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PaymentRepositoryImpl extends PaymentRepository {
}

@override
Future<PaymentIntent> confirmPaymentIntentWeb() {
Future<PaymentIntent?> confirmPaymentIntentWeb() {
return _paymentDataSource.confirmPaymentIntentWeb();
}
}
2 changes: 1 addition & 1 deletion lib/domain/repository/payment/payment_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ abstract class PaymentRepository {
Future<Map<String, dynamic>?> createPaymentIntent(
CreatePaymentIntentRequestBody body);

Future<PaymentIntent> confirmPaymentIntentWeb();
Future<PaymentIntent?> confirmPaymentIntentWeb();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ class ConfirmPaymentIntentWebInteractor with InteractorLoggy {
try {
yield const Right(ConfirmPaymentIntentWebLoading());

final PaymentIntent paymentIntent =
final PaymentIntent? paymentIntent =
await _paymentRepository.confirmPaymentIntentWeb();

yield Right(ConfirmPaymentIntentWebSuccess(paymentIntent: paymentIntent));
if (paymentIntent != null) {
yield Right(
ConfirmPaymentIntentWebSuccess(paymentIntent: paymentIntent));
} else {
yield const Left(ConfirmPaymentIntentWebEmpty());
}
} on StripeException catch (e) {
loggy.error(
'ConfirmPaymentIntentWebInteractor::execute(): stripe failure: $e');
Expand Down
5 changes: 3 additions & 2 deletions lib/pages/review_summary/widgets/checkout_web.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:ecoparking_flutter/pages/review_summary/widgets/loading_button.dart';
import 'package:ecoparking_flutter/utils/conditional_import/stripe/stripe_payment_element/stripe_payment.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe_web/flutter_stripe_web.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

class CheckoutWeb extends StatelessWidget {
final String clientSecret;
Expand All @@ -20,7 +21,7 @@ class CheckoutWeb extends StatelessWidget {
physics: const ClampingScrollPhysics(),
child: Column(
children: <Widget>[
PaymentElement(
StripePayment(
clientSecret: clientSecret,
onCardChanged: onCardChanged,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'confirm_payment_pseudo.dart'
if (dart.library.html) 'confirm_payment_web.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:flutter_stripe/flutter_stripe.dart';

class StripeConfirmPayment {
static Future<PaymentIntent?> confirmPayment() async {
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:ecoparking_flutter/utils/mixins/oauth_mixin/mixin_utils.dart';
import 'package:flutter_stripe_web/flutter_stripe_web.dart';

class StripeConfirmPayment {
static Future<PaymentIntent> confirmPayment() async {
return WebStripe.instance.confirmPaymentElement(
ConfirmPaymentElementOptions(
confirmParams: ConfirmPaymentParams(
return_url: '${MixinUtils().getRedirectURL()}/payment-return',
),
redirect: PaymentConfirmationRedirect.ifRequired,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:ecoparking_flutter/utils/conditional_import/stripe/stripe_payment_element/stripe_payment_element_pseudo.dart'
if (dart.library.html) 'package:ecoparking_flutter/utils/conditional_import/stripe/stripe_payment_element/stripe_payment_element_web.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

class StripePayment extends StatelessWidget {
final String clientSecret;
final void Function(CardFieldInputDetails?) onCardChanged;

const StripePayment({
super.key,
required this.clientSecret,
required this.onCardChanged,
});

@override
Widget build(BuildContext context) {
return StripePaymentElement(
clientSecret: clientSecret,
onCardChanged: onCardChanged,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

class StripePaymentElement extends StatelessWidget {
final String clientSecret;
final void Function(CardFieldInputDetails?) onCardChanged;

const StripePaymentElement({
super.key,
required this.clientSecret,
required this.onCardChanged,
});

@override
Widget build(BuildContext context) {
return const Placeholder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_stripe_web/flutter_stripe_web.dart';

class StripePaymentElement extends StatelessWidget {
final String clientSecret;
final void Function(CardFieldInputDetails?) onCardChanged;

const StripePaymentElement({
super.key,
required this.clientSecret,
required this.onCardChanged,
});

@override
Widget build(BuildContext context) {
return PaymentElement(
clientSecret: clientSecret,
onCardChanged: onCardChanged,
);
}
}

0 comments on commit e3798c7

Please sign in to comment.