Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/var 591 #23

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Api/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ interface ConfigInterface
public const XML_PATH_GENERAL_CHECK_AVAILABLE_PAYMENT_METHODS = 'cm_payments/general/check_available_methods';
public const XML_PATH_GENERAL_CUSTOM_SUCCESS_URL = 'cm_payments/general/custom_success_url';
public const XML_PATH_GENERAL_CUSTOM_ERROR_URL = 'cm_payments/general/custom_error_url';
public const XML_PATH_GENERAL_SHIPPING_FEE_NAME = 'cm_payments/general/shipping_fee_name';
public const XML_PATH_GENERAL_ADJUSTMENT_FEE_NAME = 'cm_payments/general/adjustment_fee_name';
public const XML_PATH_PAYMENT_LOG_ALL_API_CALLS = 'cm_payments/general/log_all_calls';
public const XML_PATH_PAYMENT_PROFILE = 'payment/cm_payments_methods/profile';
public const XML_PATH_PAYMENT_CREDIT_CARD_PROFILE = 'payment/cm_payments_creditcard/profile';
public const XML_PATH_PAYMENT_CREDIT_CARD_MODE = 'payment/cm_payments_creditcard/mode';
Expand Down Expand Up @@ -229,4 +232,19 @@ public function getCustomerSuccessUrl(): ?string;
* @return string
*/
public function getCustomerErrorUrl(): ?string;

/**
* @return string
*/
public function getShippingFeeName(): ?string;

/**
* @return string
*/
public function getAdjustmentFeeName(): ?string;

/**
* @return bool
*/
public function isLogAllRestApiCalls(): bool;
}
23 changes: 22 additions & 1 deletion Client/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use CM\Payments\Api\Config\ConfigInterface;
use CM\Payments\Client\Api\ApiClientInterface;
use CM\Payments\Client\Api\RequestInterface;
use CM\Payments\Logger\CMPaymentsLogger;
use CM\Payments\Model\Adminhtml\Source\Mode;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\GuzzleException;
Expand All @@ -34,15 +35,23 @@ class ApiClient implements ApiClientInterface
*/
private $config;

/**
* @var CMPaymentsLogger
*/
private CMPaymentsLogger $logger;

/**
* ApiClient constructor.
*
* @param CMPaymentsLogger $logger
* @param ConfigInterface $config
*/
public function __construct(
ConfigInterface $config
ConfigInterface $config,
CMPaymentsLogger $logger
) {
$this->config = $config;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -74,6 +83,18 @@ public function execute(RequestInterface $request): array
$response = \GuzzleHttp\json_decode($response, true);
}

if ($this->config->isLogAllRestApiCalls()) {
$this->logger->debug(
'CM REST request:',
[
'method' => $request->getRequestMethod(),
'endpoint' => $request->getEndpoint(),
'options' => \json_encode($options, JSON_PRETTY_PRINT),
'response' => \json_encode($response, JSON_PRETTY_PRINT)
]
);
}

return $response;
}

Expand Down
37 changes: 37 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,43 @@ public function getCustomerErrorUrl(): ?string
);
}

/**
* @inheritDoc
*/
public function getShippingFeeName(): ?string
{
return $this->getConfig(
self::XML_PATH_GENERAL_SHIPPING_FEE_NAME,
ScopeInterface::SCOPE_STORES,
(string)$this->storeManager->getStore()->getId()
);
}

/**
* @inheritDoc
*/
public function getAdjustmentFeeName(): ?string
{
return $this->getConfig(
self::XML_PATH_GENERAL_ADJUSTMENT_FEE_NAME,
ScopeInterface::SCOPE_STORES,
(string)$this->storeManager->getStore()->getId()
);
}

/**
* @inheritDoc
*/
public function isLogAllRestApiCalls(): bool
{
return $this->getConfig(
self::XML_PATH_PAYMENT_LOG_ALL_API_CALLS,
ScopeInterface::SCOPE_STORES,
(string)$this->storeManager->getStore()->getId(),
true
);
}

/**
* @inheritDoc
*/
Expand Down
15 changes: 12 additions & 3 deletions Service/Order/Item/AddOrderItemsAdjustmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace CM\Payments\Service\Order\Item;

use CM\Payments\Api\Config\ConfigInterface;
use CM\Payments\Api\Service\Order\Item\AddOrderItemsAdjustmentServiceInterface;
use CM\Payments\Client\Model\Request\OrderItemCreate;
use CM\Payments\Client\Model\Request\OrderItemCreateFactory as ClientOrderItemCreateFactory;
Expand All @@ -29,15 +30,23 @@ class AddOrderItemsAdjustmentService implements AddOrderItemsAdjustmentServiceIn
*/
private $clientOrderItemCreateFactory;

/**
* @var ConfigInterface
*/
private ConfigInterface $config;

/**
* AddOrderItemsAdjustmentObserver constructor
*
* @param ClientOrderItemCreateFactory $clientOrderItemCreateFactory
* @param ConfigInterface $config
*/
public function __construct(
ClientOrderItemCreateFactory $clientOrderItemCreateFactory
ClientOrderItemCreateFactory $clientOrderItemCreateFactory,
ConfigInterface $config
) {
$this->clientOrderItemCreateFactory = $clientOrderItemCreateFactory;
$this->config = $config;
}

/**
Expand All @@ -63,8 +72,8 @@ public function execute(
$orderItemCreate->setItemId(count($orderItemsCreateRequest->getPayload()) + 1);
$orderItemCreate->setType(OrderItemsRequestBuilderInterface::TYPE_DISCOUNT);
$orderItemCreate->setSku(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_SKU);
$orderItemCreate->setName(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_NAME);
$orderItemCreate->setDescription(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_NAME);
$orderItemCreate->setName($this->config->getAdjustmentFeeName());
$orderItemCreate->setDescription($this->config->getAdjustmentFeeName());
$orderItemCreate->setQuantity(1);
$orderItemCreate->setUnitAmount((int)round($difference));
$orderItemCreate->setAmount((int)round($difference));
Expand Down
29 changes: 19 additions & 10 deletions Service/OrderItemsRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace CM\Payments\Service;

use CM\Payments\Api\Config\ConfigInterface;
use CM\Payments\Api\Service\Order\Item\Request\RequestPartByOrderItemInterface;
use CM\Payments\Api\Service\Order\Item\Request\RequestPartByQuoteItemInterface;
use CM\Payments\Api\Service\OrderItemsRequestBuilderInterface;
Expand Down Expand Up @@ -40,22 +41,30 @@ class OrderItemsRequestBuilder implements OrderItemsRequestBuilderInterface
*/
private $quoteItemRequestParts;

/**
* @var ConfigInterface
*/
private ConfigInterface $config;

/**
* OrderItemsRequestBuilder constructor
*
* @param ClientOrderItemCreateFactory $clientOrderItemCreateFactory
* @param OrderItemsCreateRequestFactory $orderItemsCreateRequestFactory
* @param ConfigInterface $config
* @param RequestPartByOrderItemInterface[] $orderItemRequestParts
* @param RequestPartByQuoteItemInterface[] $quoteItemRequestParts
*/
public function __construct(
ClientOrderItemCreateFactory $clientOrderItemCreateFactory,
OrderItemsCreateRequestFactory $orderItemsCreateRequestFactory,
ConfigInterface $config,
array $orderItemRequestParts,
array $quoteItemRequestParts
) {
$this->clientOrderItemCreateFactory = $clientOrderItemCreateFactory;
$this->orderItemsCreateRequestFactory = $orderItemsCreateRequestFactory;
$this->config = $config;
$this->orderItemRequestParts = $orderItemRequestParts;
$this->quoteItemRequestParts = $quoteItemRequestParts;
}
Expand All @@ -79,9 +88,9 @@ public function create(string $orderKey, array $orderItems): OrderItemsCreateReq
}

return $this->orderItemsCreateRequestFactory->create([
'orderKey' => $orderKey,
'orderItems' => $orderItemsCreate
]);
'orderKey' => $orderKey,
'orderItems' => $orderItemsCreate
]);
}

/**
Expand All @@ -103,8 +112,8 @@ private function addDynamicOrderItems(array $orderItems): array
$shippingItem->setIsVirtual(0);
$shippingItem->setItemId($lastItem[0]->getItemId() + 1);
$shippingItem->setSku(self::ITEM_SHIPPING_FEE_SKU);
$shippingItem->setName(self::ITEM_SHIPPING_FEE_NAME);
$shippingItem->setDescription(self::ITEM_SHIPPING_FEE_NAME);
$shippingItem->setName($this->config->getShippingFeeName());
$shippingItem->setDescription($this->config->getShippingFeeName());
$shippingItem->setQtyOrdered(1);

$orderItems[] = $shippingItem;
Expand Down Expand Up @@ -133,9 +142,9 @@ public function createByQuoteItems(string $orderKey, array $quoteItems): OrderIt
}

return $this->orderItemsCreateRequestFactory->create([
'orderKey' => $orderKey,
'orderItems' => $orderItemsCreate
]);
'orderKey' => $orderKey,
'orderItems' => $orderItemsCreate
]);
}

/**
Expand All @@ -157,8 +166,8 @@ private function addDynamicQuoteItems(array $quoteItems): array
$shippingItem->setIsVirtual(0);
$shippingItem->setItemId($lastItem[0]->getItemId() + 1);
$shippingItem->setSku(self::ITEM_SHIPPING_FEE_SKU);
$shippingItem->setName(self::ITEM_SHIPPING_FEE_NAME);
$shippingItem->setDescription(self::ITEM_SHIPPING_FEE_NAME);
$shippingItem->setName($this->config->getShippingFeeName());
$shippingItem->setDescription($this->config->getShippingFeeName());
$shippingItem->setQty(1);

$quoteItems[] = $shippingItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ private function getOrderItemCreate(int $unitAmount, int $amount, string $curren
$orderItemCreate->setItemId(2);
$orderItemCreate->setType(OrderItemsRequestBuilderInterface::TYPE_DISCOUNT);
$orderItemCreate->setSku(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_SKU);
$orderItemCreate->setName(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_NAME);
$orderItemCreate->setDescription(OrderItemsRequestBuilderInterface::ITEM_ADJUSTMENT_FEE_NAME);
$orderItemCreate->setName('CM Adjustment Fee');
$orderItemCreate->setDescription('CM Adjustment Fee');
$orderItemCreate->setQuantity(1);
$orderItemCreate->setUnitAmount($unitAmount);
$orderItemCreate->setAmount($amount);
Expand Down
20 changes: 20 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>cm_payments/general/check_available_methods</config_path>
</field>
<field id="log_all_calls" translate="label" type="select" sortOrder="40" showInDefault="1"
showInWebsite="1" showInStore="1" canRestore="1">
<label>Log all outgoing rest api calls</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>cm_payments/general/log_all_calls</config_path>
</field>
</group>

<group id="api_details" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"
Expand Down Expand Up @@ -141,7 +147,21 @@
<label>Error Redirect Path</label>
<config_path>cm_payments/general/custom_error_url</config_path>
</field>
</group>

<group id="fee_labels" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1"
showInStore="1">
<label><![CDATA[Fee Labels]]></label>
<field id="shipping_fee_name" translate="label" type="text" sortOrder="1" showInDefault="1"
showInWebsite="1" showInStore="1" canRestore="1">
<label>Shipping Fee Name</label>
<config_path>cm_payments/general/shipping_fee_name</config_path>
</field>
<field id="adjustment_fee_name" translate="label" type="text" sortOrder="2" showInDefault="1"
showInWebsite="1" showInStore="1" canRestore="1">
<label>Adjustment Fee Name</label>
<config_path>cm_payments/general/adjustment_fee_name</config_path>
</field>
</group>
</section>

Expand Down
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<live_merchant_password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<update_on_result_page>1</update_on_result_page>
<check_available_methods>1</check_available_methods>
<shipping_fee_name>CM Shipping Fee</shipping_fee_name>
<adjustment_fee_name>CM Adjustment Fee</adjustment_fee_name>
<log_all_calls>0</log_all_calls>
</general>
</cm_payments>
<payment>
Expand Down
Loading