We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit a73e2010 authored by Alessio Zampatti's avatar Alessio Zampatti Committed by GitHub

Cloud API optimizations (#338)

* removed customerCollectionFactory

* do not expose storemanager, storeid mandatory for getAdyenMerchantAccount

* phpdoc fix
parent d443f4a8
......@@ -30,6 +30,10 @@ use Magento\Payment\Gateway\Http\ClientInterface;
class TransactionPosCloudSync implements ClientInterface
{
/**
* @var int
*/
protected $storeId;
public function __construct(
\Magento\Framework\Model\Context $context,
......@@ -37,6 +41,7 @@ class TransactionPosCloudSync implements ClientInterface
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Model\RecurringType $recurringType,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
) {
$this->_encryptor = $encryptor;
......@@ -44,22 +49,21 @@ class TransactionPosCloudSync implements ClientInterface
$this->_adyenLogger = $adyenLogger;
$this->_recurringType = $recurringType;
$this->_appState = $context->getAppState();
$this->storeId = $storeManager->getStore()->getId();
// initialize client
$apiKey = $this->_adyenHelper->getApiKey();
$client = new \Adyen\Client();
$client->setApplicationName("Magento 2 plugin");
$client = $this->_adyenHelper->initializeAdyenClient($this->storeId);
$apiKey = $this->_adyenHelper->getPosApiKey($this->storeId);
$client->setXApiKey($apiKey);
$client->setTimeout(5);
if ($this->_adyenHelper->isDemoMode()) {
$client->setEnvironment(\Adyen\Environment::TEST);
} else {
$client->setEnvironment(\Adyen\Environment::LIVE);
//Set configurable option in M2
$posTimeout = $this->_adyenHelper->getAdyenPosCloudConfigData('pos_timeout', $this->storeId);
if (!empty($posTimeout)) {
$client->setTimeout($posTimeout);
}
// assign magento log
$client->setLogger($adyenLogger);
$this->_client = $client;
}
/**
......@@ -80,13 +84,13 @@ class TransactionPosCloudSync implements ClientInterface
//always do status call and return the response of the status call
$service = new \Adyen\Service\PosPayment($this->_client);
$poiId = $this->_adyenHelper->getPoiId();
$poiId = $this->_adyenHelper->getPoiId($this->storeId);
$newServiceID = date("dHis");
$statusDate = date("U");
$timeDiff = (int)$statusDate - (int)$request['initiateDate'];
$totalTimeout = $this->_adyenHelper->getAdyenPosCloudConfigData('total_timeout');
$totalTimeout = $this->_adyenHelper->getAdyenPosCloudConfigData('total_timeout', $this->storeId);
if ($timeDiff > $totalTimeout) {
throw new \Magento\Framework\Exception\LocalizedException(__("Pos Timeout."));
}
......
......@@ -58,6 +58,7 @@ class PosCloudResponseValidator extends AbstractValidator
/**
* @param array $validationSubject
* @return \Magento\Payment\Gateway\Validator\ResultInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function validate(array $validationSubject)
{
......@@ -92,7 +93,7 @@ class PosCloudResponseValidator extends AbstractValidator
}
if (!empty($paymentResponse['PaymentReceipt'])) {
$formattedReceipt = $this->adyenHelper->formatTerminalAPIReceipt(json_encode($paymentResponse['PaymentReceipt']));
$formattedReceipt = $this->adyenHelper->formatTerminalAPIReceipt($paymentResponse['PaymentReceipt']);
$payment->setAdditionalInformation('receipt', $formattedReceipt);
}
return $this->createResult($isValid, $errorMessages);
......
......@@ -1188,58 +1188,79 @@ class Data extends AbstractHelper
* @param integer|null $storeId
* @return string the X API Key for the specified or current store
*/
public function getApiKey($storeId = null)
public function getPosApiKey($storeId = null)
{
if ($this->isDemoMode($storeId)) {
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test')));
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test', $storeId)));
} else {
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live')));
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live', $storeId)));
}
return $apiKey;
}
public function getPoiId()
/**
* Return the Terminal ID for the current store/mode
*
* @param int|null $storeId
* @return mixed
*/
public function getPoiId($storeId = null)
{
$poiId = $this->getAdyenPosCloudConfigData('pos_terminal_id');
$poiId = $this->getAdyenPosCloudConfigData('pos_terminal_id', $storeId);
return $poiId;
}
public function getAdyenMerchantAccount($method, $storeId)
/**
* Return the merchant account name configured for the proper payment method.
* If it is not configured for the specific payment method,
* return the merchant account name defined in required settings.
*
* @param $paymentMethod
* @param int $storeId
* @return string
*/
public function getAdyenMerchantAccount($paymentMethod, $storeId)
{
$merchantAccount = $this->getAdyenAbstractConfigData("merchant_account", $storeId);
$merchantAccountPos = $this->getAdyenPosCloudConfigData('pos_merchant_account', $storeId);
if ($method == 'adyen_pos_cloud' && !empty($merchantAccountPos)) {
if ($paymentMethod == 'adyen_pos_cloud' && !empty($merchantAccountPos)) {
return $merchantAccountPos;
}
return $merchantAccount;
}
/**
* Format the Receipt sent in the Terminal API response in HTML
* so that it can be easily shown to the shopper
*
* @param $paymentReceipt
* @return string
*/
public function formatTerminalAPIReceipt($paymentReceipt)
{
$paymentReceipt = json_decode($paymentReceipt);
$formatted = "<table class='terminal-api-receipt'>";
$formattedHtml = "<table class='terminal-api-receipt'>";
foreach ($paymentReceipt as $receipt) {
if ($receipt->DocumentQualifier == "CustomerReceipt") {
foreach ($receipt->OutputContent->OutputText as $item) {
parse_str($item->Text, $textParts);
$formatted .= "<tr class='terminal-api-receipt'>";
if ($receipt['DocumentQualifier'] == "CustomerReceipt") {
foreach ($receipt['OutputContent']['OutputText'] as $item) {
parse_str($item['Text'], $textParts);
$formattedHtml .= "<tr class='terminal-api-receipt'>";
if (!empty($textParts['name'])) {
$formatted .= "<td class='terminal-api-receipt-name'>" . $textParts['name'] . "</td>";
$formattedHtml .= "<td class='terminal-api-receipt-name'>" . $textParts['name'] . "</td>";
} else {
$formatted .= "<td class='terminal-api-receipt-name'>&nbsp;</td>";
$formattedHtml .= "<td class='terminal-api-receipt-name'>&nbsp;</td>";
}
if (!empty($textParts['value'])) {
$formatted .= "<td class='terminal-api-receipt-value' align='right'>" . $textParts['value'] . "</td>";
$formattedHtml .= "<td class='terminal-api-receipt-value' align='right'>" . $textParts['value'] . "</td>";
} else {
$formatted .= "<td class='terminal-api-receipt-value' align='right'>&nbsp;</td>";
$formattedHtml .= "<td class='terminal-api-receipt-value' align='right'>&nbsp;</td>";
}
$formatted .= "</tr>";
$formattedHtml .= "</tr>";
}
}
}
$formatted .= "</table>";
return $formatted;
$formattedHtml .= "</table>";
return $formattedHtml;
}
/**
......
......@@ -37,6 +37,14 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
private $_recurringType;
private $_appState;
/**
* @var int
*/
protected $storeId;
/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;
/**
......@@ -50,30 +58,26 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Magento\Checkout\Model\Session $_checkoutSession,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
)
{
$this->_adyenHelper = $adyenHelper;
$this->_adyenLogger = $adyenLogger;
$this->_checkoutSession = $_checkoutSession;
$this->storeId = $storeManager->getStore()->getId();
// initialize client
$apiKey = $this->_adyenHelper->getApiKey();
$client = new \Adyen\Client();
$client->setApplicationName("Magento 2 plugin");
$client = $this->_adyenHelper->initializeAdyenClient($this->storeId);
$apiKey = $this->_adyenHelper->getPosApiKey($this->storeId);
$client->setXApiKey($apiKey);
//Set configurable option in M2
$posTimeout = $this->_adyenHelper->getAdyenPosCloudConfigData('pos_timeout');
$posTimeout = $this->_adyenHelper->getAdyenPosCloudConfigData('pos_timeout', $this->storeId);
if (!empty($posTimeout)) {
$client->setTimeout($posTimeout);
}
if ($this->_adyenHelper->isDemoMode()) {
$client->setEnvironment(\Adyen\Environment::TEST);
} else {
$client->setEnvironment(\Adyen\Environment::LIVE);
}
// assign magento log
$client->setLogger($adyenLogger);
......@@ -94,7 +98,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
$service = new \Adyen\Service\PosPayment($this->_client);
$transactionType = \Adyen\TransactionType::NORMAL;
$poiId = $this->_adyenHelper->getPoiId();
$poiId = $this->_adyenHelper->getPoiId($this->storeId);
$serviceID = date("dHis");
$initiateDate = date("U");
$timeStamper = date("Y-m-d") . "T" . date("H:i:s+00:00");
......@@ -143,7 +147,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
// If customer exists add it into the request to store request
if (!empty($customerId)) {
$shopperEmail = $quote->getCustomerEmail();
$recurringContract = $this->_adyenHelper->getAdyenPosCloudConfigData('recurring_type');
$recurringContract = $this->_adyenHelper->getAdyenPosCloudConfigData('recurring_type', $this->storeId);
if (!empty($recurringContract) && !empty($shopperEmail) && !empty($customerId)) {
$recurringDetails = [
......
......@@ -1310,7 +1310,7 @@ class Cron
}
if ($_paymentCode == "adyen_pos_cloud") {
$captureModePos = $this->_adyenHelper->getAdyenPosCloudConfigData('capture_mode_pos');
$captureModePos = $this->_adyenHelper->getAdyenPosCloudConfigData('capture_mode_pos', $this->_order->getStoreId());
if (strcmp($captureModePos, 'auto') === 0) {
$this->_adyenLogger->addAdyenNotificationCronjob(
'This payment method is POS Cloud and configured to be working as auto capture '
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment