Skip to content

Commit

Permalink
Merge branch 'release/v5.7.0' into 'master'
Browse files Browse the repository at this point in the history
release/v5.7.0 into master

See merge request agence-dnd/marketplace/magento-2/external/module-checkout-magento2-plugin!153
  • Loading branch information
JuliosDnd committed Dec 18, 2023
2 parents f731dd6 + 8d2664e commit 73ff7a9
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 16 deletions.
11 changes: 10 additions & 1 deletion Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,16 @@ public function needsRiskRules($methodId): bool
return (!$this->getValue('risk_rules_enabled', $methodId) === true);
}

/**
* Check ABC refund after NAS migration
*
* @return bool
*/
public function isAbcRefundAfterNasMigrationActive($storeCode = null): bool
{
return (bool) $this->getValue('abc_refund_enable', null, $storeCode, ScopeInterface::SCOPE_STORE);
}

/**
* @param int $storeCode
* @param string $scope
Expand All @@ -571,5 +581,4 @@ public function getEnvironment(int $storeCode, string $scope): Environment

return Environment::production();
}

}
19 changes: 17 additions & 2 deletions Model/Methods/AlternativePaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -1069,7 +1076,15 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
Expand Down
20 changes: 18 additions & 2 deletions Model/Methods/ApplePayMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -504,7 +511,16 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The refund request could not be processed.')
Expand Down
28 changes: 25 additions & 3 deletions Model/Methods/CardPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,14 @@ public function capture(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canCapture()) {
Expand Down Expand Up @@ -558,7 +565,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -568,7 +582,15 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
Expand Down
20 changes: 18 additions & 2 deletions Model/Methods/GooglePayMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -500,7 +507,16 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The refund request could not be processed.')
Expand Down
20 changes: 18 additions & 2 deletions Model/Methods/MotoMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -341,7 +348,16 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The refund request could not be processed.')
Expand Down
20 changes: 18 additions & 2 deletions Model/Methods/VaultMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,14 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
$storeCode = $payment->getOrder()->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Check the status
if (!$this->canRefund()) {
Expand All @@ -561,7 +568,16 @@ public function refund(InfoInterface $payment, $amount): AbstractMethod
}

// Process the refund request
$response = $api->refundOrder($payment, $amount);
try {
$response = $api->refundOrder($payment, $amount);
} catch (CheckoutApiException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
$response = $api->refundOrder($payment, $amount);
}

if (!$api->isValidResponse($response)) {
throw new LocalizedException(
__('The refund request could not be processed.')
Expand Down
21 changes: 21 additions & 0 deletions Model/Service/ApiHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ public function init(
return $this;
}

public function initAbcForRefund(
$storeCode = null,
string $scope = ScopeInterface::SCOPE_WEBSITE
): ApiHandlerService
{
$secretKey = $this->config->getValue('abc_refund_secret_key', null, $storeCode, $scope);
$publicKey = $this->config->getValue('abc_refund_public_key', null, $storeCode, $scope);

$api = CheckoutSdk::builder();
$environment = $this->config->getEnvironment((int)$storeCode, $scope);

$this->checkoutApi = $api
->previous()->staticKeys()
->publicKey($publicKey)
->secretKey($secretKey)
->environment($environment)
->build();

return $this;
}

/**
* Checks if a response is valid
*
Expand Down
17 changes: 17 additions & 0 deletions Model/Service/OrderStatusHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public function setOrderStatus(OrderInterface $order, array $webhook): void
case 'payment_expired':
$this->paymentExpired();
break;
case 'payment_declined':
$this->declined();
break;
case 'payment_authentication_failed':
$this->paymentAuthenticationFailed();
break;
Expand Down Expand Up @@ -331,4 +334,18 @@ protected function paymentAuthenticationFailed(): void
$this->order->addStatusHistoryComment(__('3DS authentication failed/expired.'));
$this->handleFailedPayment($this->order);
}

/**
* Set the order status for a payment authorization failed webhook.
*
* @return void
* @throws NoSuchEntityException
*/
protected function declined(): void
{
$this->state = Order::STATE_CANCELED;
$this->status = $this->order->getConfig()->getStateDefaultStatus($this->state);
$this->order->addStatusHistoryComment(__('Failed payment authorization'));
$this->handleFailedPayment($this->order);
}
}
10 changes: 9 additions & 1 deletion Plugin/Api/RefundInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace CheckoutCom\Magento2\Plugin\Api;

use Checkout\CheckoutArgumentException;
use CheckoutCom\Magento2\Gateway\Config\Config;
use CheckoutCom\Magento2\Model\Service\ApiHandlerService;
use CheckoutCom\Magento2\Model\Service\MethodHandlerService;
Expand Down Expand Up @@ -113,7 +114,14 @@ public function beforeRefund(
$storeCode = $this->storeManager->getStore()->getCode();

// Initialize the API handler
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
try {
$api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);
} catch (CheckoutArgumentException $e) {
if (!$this->config->isAbcRefundAfterNasMigrationActive($storeCode)) {
throw new LocalizedException(__($e->getMessage()));
}
$api = $this->apiHandler->initAbcForRefund($storeCode, ScopeInterface::SCOPE_STORE);
}

// Get the method and method id
$methodId = $order->getPayment()->getMethodInstance()->getCode();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"magento/framework": ">=100.0.1"
},
"type": "magento2-module",
"version": "5.6.0",
"version": "5.7.0",
"autoload": {
"files": [
"registration.php"
Expand Down
23 changes: 23 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@
<config_path>settings/checkoutcom_configuration/gateway_responses</config_path>
</field>
</group>
<group id="abc_online_refund" type="text" showInDefault="1" showInWebsite="1" showInStore="0">
<label>ABC online refund after NAS migration</label>
<field id="enable" type="select" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Enable Feature</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>settings/checkoutcom_configuration/abc_refund_enable</config_path>
<requires>
<field id="secret_key"/>
<field id="public_key"/>
</requires>
</field>

<field id="secret_key" type="obscure" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>settings/checkoutcom_configuration/abc_refund_secret_key</config_path>
</field>

<field id="public_key" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Public Key</label>
<config_path>settings/checkoutcom_configuration/abc_refund_public_key</config_path>
</field>
</group>
</group>

<group id="checkoutcom_card_payment" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
<currencies_x1000>BHD,LYD,JOD,KWD,OMR,TND</currencies_x1000>
<default_method>checkoutcom_card_payment</default_method>
<github_api_url>https://api.github.com/repos/checkout/checkout-magento2-plugin/releases</github_api_url>
<abc_refund_enable>0</abc_refund_enable>
<abc_refund_secret_key backend_model="Magento\Config\Model\Config\Backend\Encrypted"/>
</checkoutcom_configuration>
</settings>
<payment>
Expand Down

0 comments on commit 73ff7a9

Please sign in to comment.