25-7-26-1
This commit is contained in:
158
lib/controller/payment/paymob.dart
Normal file
158
lib/controller/payment/paymob.dart
Normal file
@@ -0,0 +1,158 @@
|
||||
import 'package:Intaleq/constant/box_name.dart';
|
||||
import 'package:dio/dio.dart' as dio;
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../constant/api_key.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../functions/encrypt_decrypt.dart';
|
||||
|
||||
class PaymobManager extends GetxController {
|
||||
String authanticationToken1 = "";
|
||||
String orderId1 = "";
|
||||
|
||||
Future<String> getPaymentKey(int amount, String currency) async {
|
||||
try {
|
||||
String authanticationToken = await _getAuthanticationToken();
|
||||
|
||||
int orderId = await _getOrderId(
|
||||
authanticationToken: authanticationToken,
|
||||
amount: (100 * amount).toString(),
|
||||
currency: currency,
|
||||
);
|
||||
|
||||
String paymentKey = await _getPaymentKey(
|
||||
authanticationToken: authanticationToken,
|
||||
amount: (100 * amount).toString(),
|
||||
currency: currency,
|
||||
orderId: orderId.toString(),
|
||||
);
|
||||
authanticationToken1 = authanticationToken.toString();
|
||||
orderId1 = orderId.toString();
|
||||
update();
|
||||
return paymentKey;
|
||||
} catch (e) {
|
||||
throw Exception();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> payWithPayMob(int amount, String currency) async {
|
||||
// 1. Fetch Payment Key (Assuming PaymobManager is a custom class)
|
||||
String paymentToken;
|
||||
try {
|
||||
paymentToken = await PaymobManager().getPaymentKey(amount, currency);
|
||||
} on Exception catch (e) {
|
||||
// Handle errors gracefully, e.g., display error message to user
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Prepare Payment Data Payload
|
||||
final Map<String, dynamic> data = {
|
||||
"source": {
|
||||
"identifier": box.read(BoxName.phone), //"01010101010"
|
||||
"subtype": "WALLET",
|
||||
},
|
||||
"payment_token": paymentToken,
|
||||
};
|
||||
|
||||
// 3. Make Payment Request using Dio
|
||||
final dio = Dio();
|
||||
try {
|
||||
final response = await dio.post(
|
||||
'https://accept.paymobsolutions.com/api/acceptance/payments/pay',
|
||||
data: data,
|
||||
);
|
||||
|
||||
// 4. Handle Payment Response
|
||||
if (response.statusCode == 200) {
|
||||
final paymentData = response.data; // Assuming JSON response
|
||||
|
||||
// Navigate to success screen or display success message
|
||||
launchUrl(Uri.parse(paymentData['iframe_redirection_url']));
|
||||
} else {
|
||||
// Payment failed: Handle errors (e.g., display error message)
|
||||
}
|
||||
} on DioError catch (e) {
|
||||
// Handle network or Dio-related errors
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getStatusAfterPaid() async {
|
||||
final dio.Response response = await Dio().post(
|
||||
"https://accept.paymob.com/api/ecommerce/orders/transaction_inquiry",
|
||||
data: {
|
||||
"auth_token": authanticationToken1,
|
||||
"merchant_order_id": "970960",
|
||||
"order_id": orderId1
|
||||
});
|
||||
return response.data["success"];
|
||||
}
|
||||
|
||||
Future<String> _getAuthanticationToken() async {
|
||||
final dio.Response response =
|
||||
await Dio().post("https://accept.paymob.com/api/auth/tokens", data: {
|
||||
"api_key": AK.payMobApikey,
|
||||
'username': AK.usernamePayMob,
|
||||
"password": AK.passwordPayMob,
|
||||
});
|
||||
Log.print('token: ${response}');
|
||||
return response.data["token"];
|
||||
}
|
||||
|
||||
Future<int> _getOrderId({
|
||||
required String authanticationToken,
|
||||
required String amount,
|
||||
required String currency,
|
||||
}) async {
|
||||
final dio.Response response = await Dio()
|
||||
.post("https://accept.paymob.com/api/ecommerce/orders", data: {
|
||||
"auth_token": authanticationToken,
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"delivery_needed": "false",
|
||||
"items": [],
|
||||
});
|
||||
Log.print('id: ${response}');
|
||||
return response.data["id"];
|
||||
}
|
||||
|
||||
Future<String> _getPaymentKey({
|
||||
required String authanticationToken,
|
||||
required String orderId,
|
||||
required String amount,
|
||||
required String currency,
|
||||
}) async {
|
||||
final dio.Response response = await Dio()
|
||||
.post("https://accept.paymob.com/api/acceptance/payment_keys", data: {
|
||||
"expiration": 200,
|
||||
"auth_token": authanticationToken.toString(),
|
||||
"order_id": orderId.toString(),
|
||||
"integration_id":
|
||||
4601103, ////todo wallet or online card int.parse(AK.integrationIdPayMob),
|
||||
"lock_order_when_paid": "false",
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"billing_data": {
|
||||
"first_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[0]).toString(),
|
||||
"last_name":
|
||||
(box.read(BoxName.name).toString().split(' ')[1]).toString(),
|
||||
"email": (box.read(BoxName.email)),
|
||||
"phone_number": (box.read(BoxName.phone)),
|
||||
"apartment": "NA",
|
||||
"floor": "NA",
|
||||
"street": "NA",
|
||||
"building": "NA",
|
||||
"shipping_method": "NA",
|
||||
"postal_code": "NA",
|
||||
"city": "NA",
|
||||
"country": box.read(BoxName.countryCode),
|
||||
"state": "NA"
|
||||
},
|
||||
});
|
||||
Log.print('token: ${response}');
|
||||
return response.data["token"];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user