Added InAppApple Pay with PayFort
parent
b769959823
commit
ccb9e93d62
@ -0,0 +1,44 @@
|
||||
enum ServiceTypeEnum {
|
||||
advancePayment, //3
|
||||
ancillaryOrder, //3
|
||||
appointmentPayment, //2
|
||||
covidPayment, //2
|
||||
erOnlineCheckIn, //3
|
||||
liveCareAppointment //4
|
||||
}
|
||||
|
||||
extension ServiceTypeEnumExt on ServiceTypeEnum {
|
||||
String value() {
|
||||
switch (this) {
|
||||
case ServiceTypeEnum.advancePayment:
|
||||
return "Advance Payment";
|
||||
case ServiceTypeEnum.ancillaryOrder:
|
||||
return "Ancillary Order";
|
||||
case ServiceTypeEnum.appointmentPayment:
|
||||
return "Appointment Payment";
|
||||
case ServiceTypeEnum.covidPayment:
|
||||
return "Covid Payment";
|
||||
case ServiceTypeEnum.erOnlineCheckIn:
|
||||
return "ER Online Check In";
|
||||
case ServiceTypeEnum.liveCareAppointment:
|
||||
return "LiveCare Appointment";
|
||||
}
|
||||
}
|
||||
|
||||
int getIdFromServiceEnum() {
|
||||
switch (this) {
|
||||
case ServiceTypeEnum.advancePayment:
|
||||
return 3;
|
||||
case ServiceTypeEnum.ancillaryOrder:
|
||||
return 3;
|
||||
case ServiceTypeEnum.appointmentPayment:
|
||||
return 2;
|
||||
case ServiceTypeEnum.covidPayment:
|
||||
return 2;
|
||||
case ServiceTypeEnum.erOnlineCheckIn:
|
||||
return 3;
|
||||
case ServiceTypeEnum.liveCareAppointment:
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
class PayfortProjectDetailsRespModel {
|
||||
String accessCode;
|
||||
int integrationId;
|
||||
String merchantIdentifier;
|
||||
int projectID;
|
||||
String projectName;
|
||||
int servID;
|
||||
String shaRequest;
|
||||
String shaResponse;
|
||||
|
||||
PayfortProjectDetailsRespModel({this.accessCode, this.integrationId, this.merchantIdentifier, this.projectID, this.projectName, this.servID, this.shaRequest, this.shaResponse});
|
||||
|
||||
PayfortProjectDetailsRespModel.fromJson(Map<String, dynamic> json) {
|
||||
accessCode = json['AccessCode'];
|
||||
integrationId = json['Integration_Id'];
|
||||
merchantIdentifier = json['MerchantIdentifier'];
|
||||
projectID = json['ProjectID'];
|
||||
projectName = json['ProjectName'];
|
||||
servID = json['ServID'];
|
||||
shaRequest = json['Sha_Request'];
|
||||
shaResponse = json['Sha_Response'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['AccessCode'] = this.accessCode;
|
||||
data['Integration_Id'] = this.integrationId;
|
||||
data['MerchantIdentifier'] = this.merchantIdentifier;
|
||||
data['ProjectID'] = this.projectID;
|
||||
data['ProjectName'] = this.projectName;
|
||||
data['ServID'] = this.servID;
|
||||
data['Sha_Request'] = this.shaRequest;
|
||||
data['Sha_Response'] = this.shaResponse;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,140 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
import 'package:amazon_payfort/amazon_payfort.dart';
|
||||
import 'package:diplomaticquarterapp/config/config.dart';
|
||||
import 'package:diplomaticquarterapp/core/enum/PayfortEnums.dart';
|
||||
import 'package:diplomaticquarterapp/locator.dart';
|
||||
import 'package:diplomaticquarterapp/services/payfort_services/payfort_project_details_resp_model.dart';
|
||||
import 'package:diplomaticquarterapp/services/payfort_services/payfort_service.dart';
|
||||
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
|
||||
import 'package:diplomaticquarterapp/uitl/utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class PayfortViewModel extends ChangeNotifier {
|
||||
PayfortService _payfortService = locator<PayfortService>();
|
||||
|
||||
Future<void> initPayfort() async {
|
||||
await _payfortService.initPayfortSDK();
|
||||
}
|
||||
|
||||
Future<PayfortProjectDetailsRespModel> getProjectDetailsForPayfort({ int serviceId, int projectId}) async {
|
||||
PayfortProjectDetailsRespModel payfortProjectDetailsRespModel = PayfortProjectDetailsRespModel();
|
||||
try {
|
||||
payfortProjectDetailsRespModel = await _payfortService.getPayfortConfigurations(serviceId: serviceId, projectId: projectId);
|
||||
return payfortProjectDetailsRespModel;
|
||||
} on Exception catch (e, s) {
|
||||
print(s);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> initiateApplePayWithPayfort({
|
||||
String customerName,
|
||||
String customerEmail,
|
||||
String orderDescription,
|
||||
String merchantReference,
|
||||
double orderAmount,
|
||||
SucceededCallback onSuccess,
|
||||
FailedCallback onFailed,
|
||||
ServiceTypeEnum serviceTypeEnum,
|
||||
int projectId,
|
||||
}) async {
|
||||
GifLoaderDialogUtils.showMyDialog(AppGlobal.context);
|
||||
PayfortProjectDetailsRespModel payfortProjectDetailsRespModel = await getProjectDetailsForPayfort(projectId: projectId, serviceId: serviceTypeEnum.getIdFromServiceEnum());
|
||||
|
||||
if (payfortProjectDetailsRespModel == null) {
|
||||
GifLoaderDialogUtils.hideDialog(AppGlobal.context);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await _payfortService.paymentWithApplePay(
|
||||
onSucceeded: onSuccess,
|
||||
onFailed: onFailed,
|
||||
customerName: customerName,
|
||||
customerEmail: customerEmail,
|
||||
orderDescription: orderDescription,
|
||||
orderAmount: orderAmount,
|
||||
merchantReference: merchantReference,
|
||||
merchantIdentifier: payfortProjectDetailsRespModel.merchantIdentifier ?? "",
|
||||
applePayAccessCode: payfortProjectDetailsRespModel.accessCode ?? "",
|
||||
applePayShaRequestPhrase: payfortProjectDetailsRespModel.shaRequest ?? "",
|
||||
);
|
||||
GifLoaderDialogUtils.hideDialog(AppGlobal.context);
|
||||
} catch (e) {
|
||||
Utils.showErrorToast(e.toString());
|
||||
GifLoaderDialogUtils.hideDialog(AppGlobal.context);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
class SdkTokenResponse {
|
||||
SdkTokenResponse({
|
||||
this.responseCode,
|
||||
this.deviceId,
|
||||
this.responseMessage,
|
||||
this.serviceCommand,
|
||||
this.sdkToken,
|
||||
this.signature,
|
||||
this.merchantIdentifier,
|
||||
this.accessCode,
|
||||
this.language,
|
||||
this.status,
|
||||
});
|
||||
|
||||
String responseCode;
|
||||
String deviceId;
|
||||
String responseMessage;
|
||||
String serviceCommand;
|
||||
String sdkToken;
|
||||
String signature;
|
||||
String merchantIdentifier;
|
||||
String accessCode;
|
||||
String language;
|
||||
String status;
|
||||
|
||||
factory SdkTokenResponse.fromMap(Map<String, dynamic> data) {
|
||||
return SdkTokenResponse(
|
||||
responseCode: data['response_code'],
|
||||
deviceId: data['device_id'],
|
||||
responseMessage: data['response_message'],
|
||||
serviceCommand: data['service_command'],
|
||||
sdkToken: data['sdk_token'],
|
||||
signature: data['signature'],
|
||||
merchantIdentifier: data['merchant_identifier'],
|
||||
accessCode: data['access_code'],
|
||||
language: data['language'],
|
||||
status: data['status'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'response_code': responseCode,
|
||||
'device_id': deviceId,
|
||||
'response_message': responseMessage,
|
||||
'service_command': serviceCommand,
|
||||
'sdk_token': sdkToken,
|
||||
'signature': signature,
|
||||
'merchant_identifier': merchantIdentifier,
|
||||
'access_code': accessCode,
|
||||
'language': language,
|
||||
'status': status,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue