-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hieu Bui
committed
Dec 3, 2024
1 parent
1073886
commit e3798c7
Showing
13 changed files
with
155 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
lib/utils/conditional_import/stripe/confirm_payment/confirm_payment.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
7 changes: 7 additions & 0 deletions
7
lib/utils/conditional_import/stripe/confirm_payment/confirm_payment_pseudo.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
lib/utils/conditional_import/stripe/confirm_payment/confirm_payment_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
lib/utils/conditional_import/stripe/stripe_payment_element/stripe_payment.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...utils/conditional_import/stripe/stripe_payment_element/stripe_payment_element_pseudo.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
lib/utils/conditional_import/stripe/stripe_payment_element/stripe_payment_element_web.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |