You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
4.7 KiB
Dart
141 lines
4.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:amazon_payfort/amazon_payfort.dart';
|
|
import 'package:diplomaticquarterapp/config/config.dart';
|
|
import 'package:diplomaticquarterapp/core/service/base_service.dart';
|
|
import 'package:diplomaticquarterapp/services/payfort_services/payfort_project_details_resp_model.dart';
|
|
import 'package:diplomaticquarterapp/services/payfort_services/sdk_token_response_model.dart';
|
|
import 'package:diplomaticquarterapp/uitl/utils.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:network_info_plus/network_info_plus.dart';
|
|
|
|
class PayfortService extends BaseService {
|
|
final AmazonPayfort _payfort = AmazonPayfort.instance;
|
|
|
|
final NetworkInfo _info = NetworkInfo();
|
|
|
|
Future<void> initPayfortSDK() async {
|
|
await AmazonPayfort.initialize(
|
|
PayFortOptions(environment: payFortEnvironment),
|
|
);
|
|
}
|
|
|
|
Future<PayfortProjectDetailsRespModel> getPayfortConfigurations({int serviceId, int projectId, int integrationId = 2}) async {
|
|
Map<String, dynamic> body = {"Integration_Id": integrationId, "ServID": serviceId, "ProjectID": projectId};
|
|
|
|
PayfortProjectDetailsRespModel payfortProjectDetailsRespModel = PayfortProjectDetailsRespModel();
|
|
await baseAppClient.post(
|
|
getPayFortProjectDetails,
|
|
onSuccess: (response, statusCode) async {
|
|
payfortProjectDetailsRespModel = PayfortProjectDetailsRespModel.fromJson(response.isNotEmpty ? response.first : response);
|
|
},
|
|
onFailure: (String error, int statusCode) {
|
|
Utils.showErrorToast(error);
|
|
return null;
|
|
},
|
|
body: body,
|
|
isAllowAny: true,
|
|
);
|
|
return payfortProjectDetailsRespModel;
|
|
}
|
|
|
|
Future<SdkTokenResponse> generateSdkSignatureFromAPI(SdkTokenRequest request) async {
|
|
var response = await post(
|
|
Uri.parse(payFortEnvironment.paymentApi),
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: jsonEncode(request.asRequest()),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
var decodedResponse = jsonDecode(response.body);
|
|
return SdkTokenResponse.fromMap(decodedResponse);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<SdkTokenResponse> _generateSdkResponse({
|
|
String applePayAccessCode,
|
|
String merchantIdentifier,
|
|
String applePayShaType,
|
|
String applePayShaRequestPhrase,
|
|
}) async {
|
|
try {
|
|
String deviceId = await _payfort.getDeviceId();
|
|
|
|
/// Step 2: Generate the Signature
|
|
SdkTokenRequest tokenRequest = SdkTokenRequest(
|
|
accessCode: applePayAccessCode,
|
|
deviceId: deviceId ?? '',
|
|
merchantIdentifier: merchantIdentifier,
|
|
);
|
|
|
|
String signature = await _payfort.generateSignature(
|
|
shaType: applePayShaType,
|
|
concatenatedString: tokenRequest.toConcatenatedString(applePayShaRequestPhrase),
|
|
);
|
|
|
|
tokenRequest = tokenRequest.copyWith(signature: signature);
|
|
|
|
/// Step 3: Generate the SDK Token
|
|
return await generateSdkSignatureFromAPI(tokenRequest);
|
|
} catch (e) {
|
|
print("Error here: ${e.toString()}");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> paymentWithApplePay({
|
|
SucceededCallback onSucceeded,
|
|
FailedCallback onFailed,
|
|
String customerName,
|
|
String customerEmail,
|
|
String orderDescription,
|
|
double orderAmount,
|
|
String merchantIdentifier,
|
|
String applePayAccessCode,
|
|
String applePayShaRequestPhrase,
|
|
String merchantReference,
|
|
String currency = "SAR",
|
|
String applePayShaType = "SHA-256",
|
|
String countryIsoCode = "SA",
|
|
}) async {
|
|
try {
|
|
SdkTokenResponse sdkTokenResponse = await _generateSdkResponse(
|
|
applePayAccessCode: applePayAccessCode,
|
|
merchantIdentifier: merchantIdentifier,
|
|
applePayShaType: applePayShaType,
|
|
applePayShaRequestPhrase: applePayShaRequestPhrase,
|
|
);
|
|
|
|
if (sdkTokenResponse != null && sdkTokenResponse.sdkToken == null) {
|
|
onFailed(sdkTokenResponse.responseMessage ?? '');
|
|
return;
|
|
}
|
|
|
|
/// Step 4: Processing Payment [Don't multiply with 100]
|
|
/// Amount value send always round ex. [100] not [100.00, 100.21]
|
|
FortRequest request = FortRequest(
|
|
amount: orderAmount,
|
|
customerName: customerName,
|
|
customerEmail: customerEmail,
|
|
orderDescription: orderDescription,
|
|
sdkToken: sdkTokenResponse?.sdkToken ?? '',
|
|
merchantReference: merchantReference,
|
|
currency: currency,
|
|
customerIp: (await _info.getWifiIP() ?? ''),
|
|
);
|
|
|
|
_payfort.callPayFortForApplePay(
|
|
request: request,
|
|
countryIsoCode: countryIsoCode,
|
|
applePayMerchantId: applePayMerchantId,
|
|
callback: ApplePayResultCallback(
|
|
onSucceeded: onSucceeded,
|
|
onFailed: onFailed,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
onFailed(e.toString());
|
|
}
|
|
}
|
|
}
|