4/16/1
This commit is contained in:
129
lib/controller/payment/paymob.dart
Normal file
129
lib/controller/payment/paymob.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
import 'package:SEFER/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';
|
||||
|
||||
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) {
|
||||
print("Exc==========================================");
|
||||
print(e.toString());
|
||||
throw Exception();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> payWithPayMob(int amount, String currency) async {
|
||||
String key = await PaymobManager().getPaymentKey(amount, currency);
|
||||
await launchUrl(
|
||||
Uri.parse(
|
||||
'https://accept.paymob.com/api/acceptance/iframes/837992?payment_token=$key'),
|
||||
);
|
||||
|
||||
// String paymentStatus = await _getStatusAfterPaid();
|
||||
}
|
||||
|
||||
Future<String> _getStatusAfterPaid() async {
|
||||
print(authanticationToken1);
|
||||
print(orderId1);
|
||||
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
|
||||
});
|
||||
print(response.data);
|
||||
print(response.data['success']);
|
||||
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,
|
||||
});
|
||||
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": [],
|
||||
});
|
||||
print('id is');
|
||||
print(response.data["id"]);
|
||||
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": int.parse(AK.integrationIdPayMob),
|
||||
"lock_order_when_paid": "false",
|
||||
"amount_cents": amount,
|
||||
"currency": currency,
|
||||
"billing_data": {
|
||||
"first_name": box.read(BoxName.nameDriver) ?? box.read(BoxName.name),
|
||||
"last_name": box.read(BoxName.lastNameDriver) ?? box.read(BoxName.name),
|
||||
"email": box.read(BoxName.emailDriver) ?? box.read(BoxName.email),
|
||||
"phone_number":
|
||||
box.read(BoxName.phoneDriver) ?? 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"
|
||||
},
|
||||
});
|
||||
|
||||
return response.data["token"];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user