This commit is contained in:
Hamza-Ayed
2023-10-25 22:41:28 +03:00
parent 10c92520b8
commit 4d4ee53e00
3 changed files with 38 additions and 78 deletions

View File

@@ -79,19 +79,23 @@ class PaymentController extends GetxController {
}); });
} }
Future<void> makePayment(int amount, String currency) async { late String clientSecret;
Future<void> makePayment(int amount, String currency, Function method) async {
var newAmount = amount * 100;
try { try {
String clientSecret = clientSecret = await getClientSecret(newAmount.toString(), currency);
await _getClientSecret((amount).toString(), currency);
await initializePaymentSheet(clientSecret); await initializePaymentSheet(clientSecret);
await Stripe.instance.presentPaymentSheet(); await Stripe.instance.presentPaymentSheet();
method();
} catch (e) { } catch (e) {
throw Exception(e.toString()); rethrow;
} }
} }
Future<void> initializePaymentSheet(String clientSecret) async { Future<void> initializePaymentSheet(String clientSecret) async {
Stripe.instance.initPaymentSheet( await Stripe.instance.initPaymentSheet(
paymentSheetParameters: SetupPaymentSheetParameters( paymentSheetParameters: SetupPaymentSheetParameters(
paymentIntentClientSecret: clientSecret, paymentIntentClientSecret: clientSecret,
merchantDisplayName: 'Sefer', merchantDisplayName: 'Sefer',
@@ -99,13 +103,23 @@ class PaymentController extends GetxController {
); );
} }
Future<String> _getClientSecret(String amount, currency) async { Future<String> getClientSecret(String amount, currency) async {
var res = await CRUD().postStripe( var res = await CRUD().postStripe(
link: 'https://api.stripe.com/v1/payment_intents', link: 'https://api.stripe.com/v1/payment_intents',
payload: {'amount': amount, 'currency': currency}, payload: {'amount': amount, 'currency': currency},
); );
// Convert the res object to a JSON object
final jsonResponse = jsonDecode(res); final jsonResponse = jsonDecode(res);
return jsonResponse['client_secret']; print(jsonResponse);
// Check if the client_secret property exists and is not null
if (jsonResponse.containsKey('client_secret') &&
jsonResponse['client_secret'] != null) {
// Return the client_secret property
return jsonResponse['client_secret'] as String;
} else {
throw Exception('Failed to fetch client secret');
}
} }
@override @override

View File

@@ -195,7 +195,11 @@ class HomeCaptain extends StatelessWidget {
), ),
TextButton( TextButton(
onPressed: () { onPressed: () {
PaymentController().makePayment(18, 'JOD'); PaymentController().makePayment(
14,
'USD',
() => Get.snackbar('Yes you pay success', '',
backgroundColor: Colors.greenAccent));
}, },
child: const Text( child: const Text(
"Pay 18 dollar", "Pay 18 dollar",

View File

@@ -84,7 +84,7 @@ class PassengerWallet extends StatelessWidget {
height: 10, height: 10,
), ),
GetBuilder<CreditCardController>( GetBuilder<CreditCardController>(
builder: (reditCardControllerc) => Row( builder: (creditCardController) => Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
MyElevatedButton( MyElevatedButton(
@@ -94,19 +94,19 @@ class PassengerWallet extends StatelessWidget {
.validate()) { .validate()) {
SecureStorage().saveData( SecureStorage().saveData(
BoxName.cardNumber, BoxName.cardNumber,
reditCardControllerc creditCardController
.cardNumberController.text); .cardNumberController.text);
SecureStorage().saveData( SecureStorage().saveData(
BoxName.cardHolderName, BoxName.cardHolderName,
reditCardControllerc creditCardController
.cardHolderNameController.text); .cardHolderNameController.text);
SecureStorage().saveData( SecureStorage().saveData(
BoxName.cvvCode, BoxName.cvvCode,
reditCardControllerc creditCardController
.cvvCodeController.text); .cvvCodeController.text);
SecureStorage().saveData( SecureStorage().saveData(
BoxName.expiryDate, BoxName.expiryDate,
reditCardControllerc creditCardController
.expiryDateController.text); .expiryDateController.text);
} }
}, },
@@ -423,73 +423,15 @@ class PassengerWallet extends StatelessWidget {
'You will choose one of above !'.tr, 'You will choose one of above !'.tr,
AppColor.redColor); AppColor.redColor);
} }
// controller.changePromoSheetDialogue();
// Get.to(() {
// UsePaypal(
// sandboxMode: true,
// clientId:
// "AXE7bR3WzFEfAInUA2PBDOsW4zolx11Qr3jVOOjjJ7KGDLY3tnMIhZgyFT5Qkvj1NKWPaE8VZbG4UrZs",
// secretKey:
// "EJPW01FKqOm-SKKP16LQ_2cr3b124aZgmE0vi3JfbkWgLffv2xIjT4iCIfQv8RYn3W8h4DzxgZu_UxZC",
// returnURL:
// "https://samplesite.com/return",
// cancelURL:
// "https://samplesite.com/cancel",
// transactions: const [
// {
// "amount": {
// "total": '10.12',
// "currency": "USD",
// "details": {
// "subtotal": '10.12',
// "shipping": '0',
// "shipping_discount": 0
// }
// },
// "description":
// "The payment transaction description.",
// // "payment_options": {
// // "allowed_payment_method":
// // "INSTANT_FUNDING_SOURCE"
// // },
// "item_list": {
// "items": [
// {
// "name": "A demo product",
// "quantity": 1,
// "price": '10.12',
// "currency": "USD"
// }
// ],
// // shipping address is not required though
// "shipping_address": {
// "recipient_name": "Jane Foster",
// "line1": "Travis County",
// "line2": "",
// "city": "Austin",
// "country_code": "US",
// "postal_code": "73301",
// "phone": "+00000000",
// "state": "Texas"
// },
// }
// }
// ],
// note:
// "Contact us for any questions on your order.",
// onSuccess: (Map params) async {
// print("onSuccess: $params");
// },
// onError: (error) {
// print("onError: $error");
// },
// onCancel: (params) {
// print('cancelled: $params');
// });
// });
}, },
), ),
MyElevatedButton(
title: 'Pay with Credit Card'.tr,
onPressed: () => controller.makePayment(
controller.selectedAmount as int,
'JOD',
() => Get.snackbar('Hi', '')),
),
MyElevatedButton( MyElevatedButton(
title: 'Cancel'.tr, title: 'Cancel'.tr,
kolor: AppColor.redColor, kolor: AppColor.redColor,