Skip to content

Commit

Permalink
refactor 3d/MD status checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nuryagdym committed Mar 23, 2024
1 parent 3ba798e commit 7bef059
Show file tree
Hide file tree
Showing 37 changed files with 814 additions and 308 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ parameters:
path: src/Factory/PosFactory.php

-
message: "#^Parameter \\#2 \\$rawPaymentResponseData of method Mews\\\\Pos\\\\DataMapper\\\\ResponseDataMapper\\\\GarantiPosResponseDataMapper\\:\\:map3DPaymentData\\(\\) expects array\\{Order\\: array\\<string, array\\<string, string\\|null\\>\\|string\\>, Response\\: array\\<string, string\\>, Transaction\\: array\\<string, array\\<string, string\\>\\|string\\>\\}\\|null, array\\<string, mixed\\>\\|null given\\.$#"
message: "#^Parameter \\#2 \\$rawPaymentResponseData of method Mews\\\\Pos\\\\DataMapper\\\\ResponseDataMapper\\\\GarantiPosResponseDataMapper\\:\\:map3DPaymentData\\(\\) expects array\\{Order\\: array\\<string, array\\<string, string\\|null\\>\\|string\\>, Response\\: array\\<string, string\\>, Transaction\\: array\\<string, array\\<string, string\\>\\|string\\>\\}\\|null, array\\<string, mixed\\> given\\.$#"
count: 1
path: src/Gateways/GarantiPos.php

Expand Down
42 changes: 30 additions & 12 deletions src/DataMapper/ResponseDataMapper/EstPosResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
$raw3DAuthResponseData = $this->emptyStringsToNull($raw3DAuthResponseData);
$paymentModel = $this->mapSecurityType($raw3DAuthResponseData['storetype']);
$paymentResponseData = $this->getDefaultPaymentResponse($txType, $paymentModel);
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
if (null !== $rawPaymentResponseData) {
$paymentResponseData = $this->mapPaymentResponse($rawPaymentResponseData, $txType, $order);
}

$threeDResponse = [
'transaction_security' => $this->mapResponseTransactionSecurity($raw3DAuthResponseData['mdStatus']),
'md_status' => $raw3DAuthResponseData['mdStatus'],
'transaction_security' => null === $mdStatus ? null : $this->mapResponseTransactionSecurity($mdStatus),
'md_status' => $mdStatus,
'order_id' => $raw3DAuthResponseData['oid'],
'masked_number' => $raw3DAuthResponseData['maskedCreditCard'],
'month' => $raw3DAuthResponseData['Ecom_Payment_Card_ExpDate_Month'],
Expand All @@ -134,11 +135,11 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
'eci' => null,
'tx_status' => null,
'cavv' => null,
'md_error_message' => '1' !== $raw3DAuthResponseData['mdStatus'] ? $raw3DAuthResponseData['mdErrorMsg'] : null,
'md_error_message' => !$this->is3dAuthSuccess($mdStatus) ? $raw3DAuthResponseData['mdErrorMsg'] : null,
'3d_all' => $raw3DAuthResponseData,
];

if ('1' === $raw3DAuthResponseData['mdStatus']) {
if ($this->is3dAuthSuccess($mdStatus)) {
$threeDResponse['eci'] = $raw3DAuthResponseData['eci'];
$threeDResponse['cavv'] = $raw3DAuthResponseData['cavv'];
}
Expand All @@ -158,7 +159,8 @@ public function map3DPayResponseData(array $raw3DAuthResponseData, string $txTyp

$raw3DAuthResponseData = $this->emptyStringsToNull($raw3DAuthResponseData);
$procReturnCode = $this->getProcReturnCode($raw3DAuthResponseData);
if (self::PROCEDURE_SUCCESS_CODE === $procReturnCode && \in_array($raw3DAuthResponseData['mdStatus'], ['1', '2', '3', '4'])) {
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
if (self::PROCEDURE_SUCCESS_CODE === $procReturnCode && $this->is3dAuthSuccess($mdStatus)) {
$status = self::TX_APPROVED;
}

Expand All @@ -167,8 +169,8 @@ public function map3DPayResponseData(array $raw3DAuthResponseData, string $txTyp

$response = [
'order_id' => $raw3DAuthResponseData['oid'],
'transaction_security' => $this->mapResponseTransactionSecurity($raw3DAuthResponseData['mdStatus']),
'md_status' => $raw3DAuthResponseData['mdStatus'],
'transaction_security' => null === $mdStatus ? null : $this->mapResponseTransactionSecurity($mdStatus),
'md_status' => $mdStatus,
'status' => $status,
'proc_return_code' => $procReturnCode,
'masked_number' => $raw3DAuthResponseData['maskedCreditCard'],
Expand Down Expand Up @@ -206,8 +208,8 @@ public function map3DHostResponseData(array $raw3DAuthResponseData, string $txTy
{
$raw3DAuthResponseData = $this->emptyStringsToNull($raw3DAuthResponseData);
$status = self::TX_DECLINED;

if (\in_array($raw3DAuthResponseData['mdStatus'], [1, 2, 3, 4])) {
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
if ($this->is3dAuthSuccess($mdStatus)) {
$status = self::TX_APPROVED;
}

Expand All @@ -216,8 +218,8 @@ public function map3DHostResponseData(array $raw3DAuthResponseData, string $txTy

$response = [
'order_id' => $raw3DAuthResponseData['oid'],
'transaction_security' => $this->mapResponseTransactionSecurity($raw3DAuthResponseData['mdStatus']),
'md_status' => $raw3DAuthResponseData['mdStatus'],
'transaction_security' => null === $mdStatus ? null : $this->mapResponseTransactionSecurity($mdStatus),
'md_status' => $mdStatus,
'status' => $status,
'amount' => $this->formatAmount($raw3DAuthResponseData['amount']),
'currency' => $this->mapCurrency($raw3DAuthResponseData['currency']),
Expand Down Expand Up @@ -443,6 +445,22 @@ public function mapHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return \in_array($mdStatus, ['1'], true);
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $raw3DAuthResponseData['mdStatus'] ?? null;
}

/**
* @param string $mdStatus
*
Expand All @@ -453,7 +471,7 @@ protected function mapResponseTransactionSecurity(string $mdStatus): string
$transactionSecurity = 'MPI fallback';
if ('1' === $mdStatus) {
$transactionSecurity = 'Full 3D Secure';
} elseif (in_array($mdStatus, ['2', '3', '4'])) {
} elseif (\in_array($mdStatus, ['2', '3', '4'])) {
$transactionSecurity = 'Half 3D Secure';
}

Expand Down
30 changes: 23 additions & 7 deletions src/DataMapper/ResponseDataMapper/GarantiPosResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
]);

$commonResult = $this->map3DCommonResponseData($raw3DAuthResponseData, PosInterface::MODEL_3D_SECURE);

$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
// todo refactor
if (\in_array($raw3DAuthResponseData['mdstatus'], ['1', '2', '3', '4'], true)) {
if ($this->is3dAuthSuccess($mdStatus)) {
//these data only available on success
$commonResult['auth_code'] = $raw3DAuthResponseData['authcode'];
$commonResult['transaction_id'] = $raw3DAuthResponseData['transid'];
Expand Down Expand Up @@ -335,6 +335,22 @@ public function mapHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return \in_array($mdStatus, ['1', '2', '3', '4'], true);
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $raw3DAuthResponseData['mdstatus'] ?? null;
}

/**
* 100001 => 1000.01
* @param string $amount
Expand All @@ -358,11 +374,11 @@ protected function formatAmount(string $amount): float
protected function map3DCommonResponseData(array $raw3DAuthResponseData, string $paymentModel): array
{
$procReturnCode = $raw3DAuthResponseData['procreturncode'];
$mdStatus = $raw3DAuthResponseData['mdstatus'];
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);

$status = self::TX_DECLINED;

if (\in_array($mdStatus, ['1', '2', '3', '4']) && 'Error' !== $raw3DAuthResponseData['response']) {
if ($this->is3dAuthSuccess($mdStatus) && 'Error' !== $raw3DAuthResponseData['response']) {
$status = self::TX_APPROVED;
}

Expand All @@ -371,10 +387,10 @@ protected function map3DCommonResponseData(array $raw3DAuthResponseData, string
'transaction_id' => null,
'auth_code' => null,
'ref_ret_num' => null,
'transaction_security' => $this->mapResponseTransactionSecurity($mdStatus),
'transaction_security' => null === $mdStatus ? null : $this->mapResponseTransactionSecurity($mdStatus),
'transaction_type' => $this->mapTxType($raw3DAuthResponseData['txntype']),
'proc_return_code' => $procReturnCode,
'md_status' => $raw3DAuthResponseData['mdstatus'],
'md_status' => $mdStatus,
'status' => $status,
'status_detail' => $this->getStatusDetail($procReturnCode),
'masked_number' => null,
Expand Down Expand Up @@ -403,7 +419,7 @@ protected function map3DCommonResponseData(array $raw3DAuthResponseData, string
*/
protected function mapResponseTransactionSecurity(string $mdStatus): string
{
if (!in_array($mdStatus, ['1', '2', '3', '4'])) {
if (!$this->is3dAuthSuccess($mdStatus)) {
return 'MPI fallback';
}

Expand Down
24 changes: 20 additions & 4 deletions src/DataMapper/ResponseDataMapper/InterPosResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ public function mapOrderHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return \in_array($mdStatus, ['1', '2', '3', '4'], true);
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $raw3DAuthResponseData['3DStatus'] ?? null;
}

/**
* @param string $mdStatus
*
Expand All @@ -218,7 +234,7 @@ protected function mapResponseTransactionSecurity(string $mdStatus): string
$transactionSecurity = 'MPI fallback';
if ('1' === $mdStatus) {
$transactionSecurity = 'Full 3D Secure';
} elseif (in_array($mdStatus, ['2', '3', '4'])) {
} elseif (\in_array($mdStatus, ['2', '3', '4'])) {
$transactionSecurity = 'Half 3D Secure';
}

Expand Down Expand Up @@ -323,7 +339,7 @@ private function map3DCommonResponseData(array $raw3DAuthResponseData, ?array $r
'3d_auth_response' => $raw3DAuthResponseData,
'provision_response' => $rawPaymentResponseData,
]);
$status = $raw3DAuthResponseData['mdStatus'];
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
$procReturnCode = $this->getProcReturnCode($raw3DAuthResponseData);
$paymentResponseData = $this->getDefaultPaymentResponse($txType, $paymentModel);
if (null !== $rawPaymentResponseData) {
Expand All @@ -334,9 +350,9 @@ private function map3DCommonResponseData(array $raw3DAuthResponseData, ?array $r
'order_id' => $paymentResponseData['order_id'] ?? $raw3DAuthResponseData['OrderId'],
'proc_return_code' => $paymentResponseData['proc_return_code'] ?? $procReturnCode,
'ref_ret_num' => $paymentResponseData['ref_ret_num'] ?? $raw3DAuthResponseData['HostRefNum'],
'transaction_security' => $this->mapResponseTransactionSecurity($raw3DAuthResponseData['mdStatus']),
'transaction_security' => null === $mdStatus ? null : $this->mapResponseTransactionSecurity($mdStatus),
'payment_model' => $paymentModel,
'md_status' => $status,
'md_status' => $mdStatus,
'masked_number' => $raw3DAuthResponseData['Pan'],
'month' => null,
'year' => null,
Expand Down
16 changes: 16 additions & 0 deletions src/DataMapper/ResponseDataMapper/KuveytPosResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ public function mapOrderHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return self::PROCEDURE_SUCCESS_CODE === $mdStatus;
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $this->getProcReturnCode($raw3DAuthResponseData);
}

/**
* "101" => 1.01
* @param string $amount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ public function mapOrderHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return true;
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return null;
}

/**
* @param string $mdStatus
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
'provision_response' => $rawPaymentResponseData,
]);
$raw3DAuthResponseData = $this->emptyStringsToNull($raw3DAuthResponseData);
$threeDAuthStatus = ('Y' === $raw3DAuthResponseData['Status']) ? self::TX_APPROVED : self::TX_DECLINED;
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
$threeDAuthStatus = $this->is3dAuthSuccess($mdStatus) ? self::TX_APPROVED : self::TX_DECLINED;

if (self::TX_APPROVED === $threeDAuthStatus && null !== $rawPaymentResponseData) {
$paymentResponseData = $this->mapPaymentResponse($rawPaymentResponseData, $txType, $order);
Expand All @@ -75,7 +76,7 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
'status_detail' => null,
'error_code' => self::TX_DECLINED === $threeDAuthStatus ? $raw3DAuthResponseData['ErrorCode'] : null,
'error_message' => self::TX_DECLINED === $threeDAuthStatus ? $raw3DAuthResponseData['ErrorMessage'] : null,
'md_status' => $raw3DAuthResponseData['Status'],
'md_status' => $mdStatus,
'md_error_message' => self::TX_DECLINED === $threeDAuthStatus ? $raw3DAuthResponseData['ErrorMessage'] : null,
'transaction_security' => null,
'all' => $rawPaymentResponseData,
Expand Down Expand Up @@ -234,6 +235,26 @@ public function mapOrderHistoryResponse(array $rawResponseData): array
throw new NotImplementedException();
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
/**
* Y => 3D secure
* A => Half 3D secure
*/
return 'Y' === $mdStatus;
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $raw3DAuthResponseData['Status'] ?? null;
}

/**
* @param string $mdStatus
*
Expand Down
26 changes: 22 additions & 4 deletions src/DataMapper/ResponseDataMapper/PayForPosResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function map3DPaymentData(array $raw3DAuthResponseData, ?array $rawPaymen
'provision_response' => $rawPaymentResponseData,
]);
$procReturnCode = $this->getProcReturnCode($raw3DAuthResponseData);
$threeDAuthStatus = ('1' === $raw3DAuthResponseData['3DStatus']) ? self::TX_APPROVED : self::TX_DECLINED;
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
$threeDAuthStatus = $this->is3dAuthSuccess($mdStatus) ? self::TX_APPROVED : self::TX_DECLINED;
$paymentResponseData = [];

$mapped3DResponseData = $this->map3DCommonResponseData($raw3DAuthResponseData);
Expand Down Expand Up @@ -378,6 +379,22 @@ public function mapHistoryResponse(array $rawResponseData): array
return $result;
}

/**
* @inheritDoc
*/
public function is3dAuthSuccess(?string $mdStatus): bool
{
return \in_array($mdStatus, ['1'], true);
}

/**
* @inheritDoc
*/
public function extractMdStatus(array $raw3DAuthResponseData): ?string
{
return $raw3DAuthResponseData['3DStatus'] ?? null;
}

/**
* @param string $mdStatus
*
Expand All @@ -388,7 +405,7 @@ protected function mapResponseTransactionSecurity(string $mdStatus): string
$transactionSecurity = 'MPI fallback';
if ('1' === $mdStatus) {
$transactionSecurity = 'Full 3D Secure';
} elseif (in_array($mdStatus, ['2', '3', '4'])) {
} elseif (\in_array($mdStatus, ['2', '3', '4'])) {
$transactionSecurity = 'Half 3D Secure';
}

Expand Down Expand Up @@ -473,7 +490,8 @@ private function map3DPaymentResponseCommon(array $rawPaymentResponseData, strin
private function map3DCommonResponseData(array $raw3DAuthResponseData): array
{
$procReturnCode = $this->getProcReturnCode($raw3DAuthResponseData);
$threeDAuthStatus = ('1' === $raw3DAuthResponseData['3DStatus']) ? self::TX_APPROVED : self::TX_DECLINED;
$mdStatus = $this->extractMdStatus($raw3DAuthResponseData);
$threeDAuthStatus = $this->is3dAuthSuccess($mdStatus) ? self::TX_APPROVED : self::TX_DECLINED;

$result = [
'transaction_security' => null,
Expand All @@ -483,7 +501,7 @@ private function map3DCommonResponseData(array $raw3DAuthResponseData): array
'amount' => $this->formatAmount($raw3DAuthResponseData['PurchAmount']),
'currency' => $this->mapCurrency($raw3DAuthResponseData['Currency']),
'tx_status' => $raw3DAuthResponseData['TxnResult'],
'md_status' => $raw3DAuthResponseData['3DStatus'],
'md_status' => $mdStatus,
'md_error_code' => (self::TX_DECLINED === $threeDAuthStatus) ? $procReturnCode : null,
'md_error_message' => (self::TX_DECLINED === $threeDAuthStatus) ? $raw3DAuthResponseData['ErrMsg'] : null,
'md_status_detail' => $this->getStatusDetail($procReturnCode),
Expand Down
Loading

0 comments on commit 7bef059

Please sign in to comment.