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