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 2dfcff7e authored by stkams's avatar stkams Committed by Aleffio

customer reference prefix (#325)

* customer reference prefix

* added java doc; made customer_reference_prefix only available in default

* workaround for adyen notification processing delay

* updated the module version to that of master

* Allow to retrieve api key for a specified store
parent 3ecb944b
......@@ -247,7 +247,7 @@ class Redirect extends \Magento\Payment\Block\Form
if ($customerId > 0) {
$formFields['recurringContract'] = $recurringType;
$formFields['shopperReference'] = $customerId;
$formFields['shopperReference'] = $this->_adyenHelper->getCustomerReference($customerId);
} else {
// required for openinvoice payment methods use unique id
$uniqueReference = "guest_" . $realOrderId . "_" . $this->_order->getStoreId();
......
......@@ -196,7 +196,7 @@ class PayByMailCommand implements CommandInterface
if ($customerId > 0) {
$formFields['recurringContract'] = $recurringType;
$formFields['shopperReference'] = $customerId;
$formFields['shopperReference'] = $this->_adyenHelper->getCustomerReference($customerId);
}
// Sign request using secret key
......
......@@ -30,6 +30,20 @@ use Magento\Payment\Gateway\Request\BuilderInterface;
class CustomerDataBuilder implements BuilderInterface
{
/**
* @var \Adyen\Payment\Helper\Data
*/
private $adyenHelper;
/**
* CustomerDataBuilder constructor.
* @param \Adyen\Payment\Helper\Data $adyenHelper
*/
public function __construct(\Adyen\Payment\Helper\Data $adyenHelper)
{
$this->adyenHelper = $adyenHelper;
}
/**
* Add shopper data into request
*
......@@ -49,7 +63,7 @@ class CustomerDataBuilder implements BuilderInterface
$customerId = $order->getCustomerId();
if ($customerId > 0) {
$result['shopperReference'] = $customerId;
$result['shopperReference'] = $this->adyenHelper->getCustomerReference($customerId);
}
$result ['shopperEmail'] = $customerEmail;
......
......@@ -75,14 +75,12 @@ class PosCloudResponseValidator extends AbstractValidator
// Do the status call(try to place an order)
return $this->createResult($isValid, $errorMessages);
} else {
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__($response['error']));
}
}
// We receive a SaleToPOIRequest when the terminal is not reachable
if (!empty($response['SaleToPOIRequest'])){
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__("The terminal could not be reached."));
}
......@@ -102,7 +100,6 @@ class PosCloudResponseValidator extends AbstractValidator
if (!empty($paymentResponse) && $paymentResponse['Response']['Result'] != 'Success') {
$errorMsg = __($paymentResponse['Response']['ErrorCondition']);
$this->adyenLogger->error($errorMsg);
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__("The transaction could not be completed."));
}
......
......@@ -825,7 +825,6 @@ class Data extends AbstractHelper
$baCollection = $this->_billingAgreementCollectionFactory->create();
$baCollection->addFieldToFilter('customer_id', $customerId);
$baCollection->addFieldToFilter('store_id', $storeId);
$baCollection->addFieldToFilter('method_code', 'adyen_oneclick');
$baCollection->addActiveFilter();
......@@ -1184,15 +1183,16 @@ class Data extends AbstractHelper
return $formFields;
}
public function getApiKey()
/**
* @param integer|null $storeId
* @return string the X API Key for the specified or current store
*/
public function getApiKey($storeId = null)
{
switch ($this->isDemoMode()) {
case true:
if ($this->isDemoMode($storeId)) {
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test')));
break;
default:
} else {
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live')));
break;
}
return $apiKey;
}
......@@ -1241,6 +1241,18 @@ class Data extends AbstractHelper
return $formatted;
}
/**
* @param $customerId int|string the numerice ID of the customer
* @return string customer ID, prefixed by customer_reference_prefix if that is set
*/
public function getCustomerReference($customerId)
{
$prefix = $this->getAdyenOneclickConfigData('customer_reference_prefix');
if (empty($prefix)) {
return strval($customerId);
}
return $prefix . strval($customerId);
}
/**
* Initializes and returns Adyen Client and sets the required parameters of it
......
......@@ -38,6 +38,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
private $_appState;
protected $_checkoutSession;
private $_customerCollectionFactory;
/**
* AdyenInitiateTerminalApi constructor.
......@@ -45,7 +46,9 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param RecurringType $recurringType
* @param \Adyen\Payment\Model\RecurringType $recurringType
* @param \Magento\Checkout\Model\Session $_checkoutSession
* @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory
* @param array $data
*/
public function __construct(
......@@ -55,6 +58,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Model\RecurringType $recurringType,
\Magento\Checkout\Model\Session $_checkoutSession,
\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,
array $data = []
)
{
......@@ -64,6 +68,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
$this->_recurringType = $recurringType;
$this->_appState = $context->getAppState();
$this->_checkoutSession = $_checkoutSession;
$this->_customerCollectionFactory = $customerCollectionFactory;
// initialize client
$apiKey = $this->_adyenHelper->getApiKey();
......@@ -149,6 +154,19 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
],
];
if (empty($customerId)) {
// No customer ID in quote but the customer might still exist; so find him/her by email address
$shopperEmail = $quote->getCustomerEmail();
$collection = $this->_customerCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addFieldToFilter('email', ['eq' => $shopperEmail]);
$customer = $collection->getFirstItem();
if ($customer && !empty($customer->getId())) {
$customerId = $customer->getId();
}
}
// If customer exists add it into the request to store request
if (!empty($customerId)) {
$shopperEmail = $quote->getCustomerEmail();
......@@ -157,7 +175,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
if (!empty($recurringContract) && !empty($shopperEmail) && !empty($customerId)) {
$recurringDetails = [
'shopperEmail' => $shopperEmail,
'shopperReference' => strval($customerId),
'shopperReference' => $this->_adyenHelper->getCustomerReference($customerId),
'recurringContract' => $recurringContract
];
$request['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'] = http_build_query($recurringDetails);
......
......@@ -70,11 +70,11 @@ class Agreement extends \Magento\Paypal\Model\Billing\Agreement
/**
* Not yet possible to set different reference on customer level like magento 1.x version
*
* @return int
* @return string
*/
public function getCustomerReference()
{
return $this->getCustomerId();
return $this->_adyenHelper->getCustomerReference($this->getCustomerId());
}
/**
......
......@@ -300,7 +300,7 @@ class Cron
$dateStart = new \DateTime();
$dateStart->modify('-5 day');
$dateEnd = new \DateTime();
$dateEnd->modify('-1 minute');
// $dateEnd->modify('-1 minute');
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
// create collection
......@@ -332,22 +332,6 @@ class Cron
continue;
}
/**
* If the event is a RECURRING_CONTRACT wait an extra 5 minutes
* before processing so we are sure the RECURRING_CONTRACT
*/
if (trim($notification->getEventCode()) == Notification::RECURRING_CONTRACT &&
strtotime($notification->getCreatedAt()) >= strtotime('-5 minutes', time())
) {
$this->_adyenLogger->addAdyenNotificationCronjob(
"This is a recurring_contract notification wait an extra 5 minutes
before processing this to make sure the contract exists"
);
// set processing back to false
$this->_updateNotification($notification, false, false);
continue;
}
// log the executed notification
$this->_adyenLogger->addAdyenNotificationCronjob(print_r($notification->debug(), 1));
......@@ -945,7 +929,8 @@ class Cron
$recurringDetailReference = $this->_pspReference;
$storeId = $this->_order->getStoreId();
$customerReference = $this->_order->getCustomerId();
$customerId = $this->_order->getCustomerId();
$customerReference = $this->_adyenHelper->getCustomerReference($customerId);
$listRecurringContracts = null;
$this->_adyenLogger->addAdyenNotificationCronjob(
__('CustomerReference is: %1 and storeId is %2 and RecurringDetailsReference is %3',
......@@ -982,7 +967,7 @@ class Cron
}
$billingAgreements = $this->_billingAgreementCollectionFactory->create();
$billingAgreements->addFieldToFilter('customer_id', $customerReference);
$billingAgreements->addFieldToFilter('customer_id', $customerId);
// Get collection and update existing agreements
......
......@@ -49,5 +49,10 @@
<source_model>Adyen\Payment\Model\Config\Source\RecurringPaymentType</source_model>
<config_path>payment/adyen_oneclick/recurring_payment_type</config_path>
</field>
<field id="customer_reference_prefix" translate="label" type="text" sortOrder="50" showInDefault="1">
<label>Customer Reference Prefix</label>
<tooltip>This optional prefix is used for the customer reference showing on payments. If you change this later then card details of earlier payments can't be retrieved.</tooltip>
<config_path>payment/adyen_oneclick/customer_reference_prefix</config_path>
</field>
</group>
</include>
\ No newline at end of file
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