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 ...@@ -247,7 +247,7 @@ class Redirect extends \Magento\Payment\Block\Form
if ($customerId > 0) { if ($customerId > 0) {
$formFields['recurringContract'] = $recurringType; $formFields['recurringContract'] = $recurringType;
$formFields['shopperReference'] = $customerId; $formFields['shopperReference'] = $this->_adyenHelper->getCustomerReference($customerId);
} else { } else {
// required for openinvoice payment methods use unique id // required for openinvoice payment methods use unique id
$uniqueReference = "guest_" . $realOrderId . "_" . $this->_order->getStoreId(); $uniqueReference = "guest_" . $realOrderId . "_" . $this->_order->getStoreId();
......
...@@ -196,7 +196,7 @@ class PayByMailCommand implements CommandInterface ...@@ -196,7 +196,7 @@ class PayByMailCommand implements CommandInterface
if ($customerId > 0) { if ($customerId > 0) {
$formFields['recurringContract'] = $recurringType; $formFields['recurringContract'] = $recurringType;
$formFields['shopperReference'] = $customerId; $formFields['shopperReference'] = $this->_adyenHelper->getCustomerReference($customerId);
} }
// Sign request using secret key // Sign request using secret key
......
...@@ -30,6 +30,20 @@ use Magento\Payment\Gateway\Request\BuilderInterface; ...@@ -30,6 +30,20 @@ use Magento\Payment\Gateway\Request\BuilderInterface;
class CustomerDataBuilder implements 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 * Add shopper data into request
* *
...@@ -49,7 +63,7 @@ class CustomerDataBuilder implements BuilderInterface ...@@ -49,7 +63,7 @@ class CustomerDataBuilder implements BuilderInterface
$customerId = $order->getCustomerId(); $customerId = $order->getCustomerId();
if ($customerId > 0) { if ($customerId > 0) {
$result['shopperReference'] = $customerId; $result['shopperReference'] = $this->adyenHelper->getCustomerReference($customerId);
} }
$result ['shopperEmail'] = $customerEmail; $result ['shopperEmail'] = $customerEmail;
......
...@@ -75,14 +75,12 @@ class PosCloudResponseValidator extends AbstractValidator ...@@ -75,14 +75,12 @@ class PosCloudResponseValidator extends AbstractValidator
// Do the status call(try to place an order) // Do the status call(try to place an order)
return $this->createResult($isValid, $errorMessages); return $this->createResult($isValid, $errorMessages);
} else { } else {
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__($response['error'])); throw new \Magento\Framework\Exception\LocalizedException(__($response['error']));
} }
} }
// We receive a SaleToPOIRequest when the terminal is not reachable // We receive a SaleToPOIRequest when the terminal is not reachable
if (!empty($response['SaleToPOIRequest'])){ if (!empty($response['SaleToPOIRequest'])){
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__("The terminal could not be reached.")); throw new \Magento\Framework\Exception\LocalizedException(__("The terminal could not be reached."));
} }
...@@ -102,7 +100,6 @@ class PosCloudResponseValidator extends AbstractValidator ...@@ -102,7 +100,6 @@ class PosCloudResponseValidator extends AbstractValidator
if (!empty($paymentResponse) && $paymentResponse['Response']['Result'] != 'Success') { if (!empty($paymentResponse) && $paymentResponse['Response']['Result'] != 'Success') {
$errorMsg = __($paymentResponse['Response']['ErrorCondition']); $errorMsg = __($paymentResponse['Response']['ErrorCondition']);
$this->adyenLogger->error($errorMsg); $this->adyenLogger->error($errorMsg);
$this->adyenLogger->error(json_encode($response));
throw new \Magento\Framework\Exception\LocalizedException(__("The transaction could not be completed.")); throw new \Magento\Framework\Exception\LocalizedException(__("The transaction could not be completed."));
} }
......
...@@ -825,7 +825,6 @@ class Data extends AbstractHelper ...@@ -825,7 +825,6 @@ class Data extends AbstractHelper
$baCollection = $this->_billingAgreementCollectionFactory->create(); $baCollection = $this->_billingAgreementCollectionFactory->create();
$baCollection->addFieldToFilter('customer_id', $customerId); $baCollection->addFieldToFilter('customer_id', $customerId);
$baCollection->addFieldToFilter('store_id', $storeId);
$baCollection->addFieldToFilter('method_code', 'adyen_oneclick'); $baCollection->addFieldToFilter('method_code', 'adyen_oneclick');
$baCollection->addActiveFilter(); $baCollection->addActiveFilter();
...@@ -1184,15 +1183,16 @@ class Data extends AbstractHelper ...@@ -1184,15 +1183,16 @@ class Data extends AbstractHelper
return $formFields; 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()) { if ($this->isDemoMode($storeId)) {
case true:
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test'))); $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test')));
break; } else {
default:
$apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live'))); $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live')));
break;
} }
return $apiKey; return $apiKey;
} }
...@@ -1241,6 +1241,18 @@ class Data extends AbstractHelper ...@@ -1241,6 +1241,18 @@ class Data extends AbstractHelper
return $formatted; 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 * Initializes and returns Adyen Client and sets the required parameters of it
......
...@@ -38,6 +38,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -38,6 +38,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
private $_appState; private $_appState;
protected $_checkoutSession; protected $_checkoutSession;
private $_customerCollectionFactory;
/** /**
* AdyenInitiateTerminalApi constructor. * AdyenInitiateTerminalApi constructor.
...@@ -45,7 +46,9 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -45,7 +46,9 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger * @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 * @param array $data
*/ */
public function __construct( public function __construct(
...@@ -55,6 +58,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -55,6 +58,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
\Adyen\Payment\Logger\AdyenLogger $adyenLogger, \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Model\RecurringType $recurringType, \Adyen\Payment\Model\RecurringType $recurringType,
\Magento\Checkout\Model\Session $_checkoutSession, \Magento\Checkout\Model\Session $_checkoutSession,
\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,
array $data = [] array $data = []
) )
{ {
...@@ -64,6 +68,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -64,6 +68,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
$this->_recurringType = $recurringType; $this->_recurringType = $recurringType;
$this->_appState = $context->getAppState(); $this->_appState = $context->getAppState();
$this->_checkoutSession = $_checkoutSession; $this->_checkoutSession = $_checkoutSession;
$this->_customerCollectionFactory = $customerCollectionFactory;
// initialize client // initialize client
$apiKey = $this->_adyenHelper->getApiKey(); $apiKey = $this->_adyenHelper->getApiKey();
...@@ -149,6 +154,19 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -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 customer exists add it into the request to store request
if (!empty($customerId)) { if (!empty($customerId)) {
$shopperEmail = $quote->getCustomerEmail(); $shopperEmail = $quote->getCustomerEmail();
...@@ -157,7 +175,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface ...@@ -157,7 +175,7 @@ class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
if (!empty($recurringContract) && !empty($shopperEmail) && !empty($customerId)) { if (!empty($recurringContract) && !empty($shopperEmail) && !empty($customerId)) {
$recurringDetails = [ $recurringDetails = [
'shopperEmail' => $shopperEmail, 'shopperEmail' => $shopperEmail,
'shopperReference' => strval($customerId), 'shopperReference' => $this->_adyenHelper->getCustomerReference($customerId),
'recurringContract' => $recurringContract 'recurringContract' => $recurringContract
]; ];
$request['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'] = http_build_query($recurringDetails); $request['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'] = http_build_query($recurringDetails);
......
...@@ -70,11 +70,11 @@ class Agreement extends \Magento\Paypal\Model\Billing\Agreement ...@@ -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 * Not yet possible to set different reference on customer level like magento 1.x version
* *
* @return int * @return string
*/ */
public function getCustomerReference() public function getCustomerReference()
{ {
return $this->getCustomerId(); return $this->_adyenHelper->getCustomerReference($this->getCustomerId());
} }
/** /**
......
...@@ -300,7 +300,7 @@ class Cron ...@@ -300,7 +300,7 @@ class Cron
$dateStart = new \DateTime(); $dateStart = new \DateTime();
$dateStart->modify('-5 day'); $dateStart->modify('-5 day');
$dateEnd = new \DateTime(); $dateEnd = new \DateTime();
$dateEnd->modify('-1 minute'); // $dateEnd->modify('-1 minute');
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true]; $dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
// create collection // create collection
...@@ -332,22 +332,6 @@ class Cron ...@@ -332,22 +332,6 @@ class Cron
continue; 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 // log the executed notification
$this->_adyenLogger->addAdyenNotificationCronjob(print_r($notification->debug(), 1)); $this->_adyenLogger->addAdyenNotificationCronjob(print_r($notification->debug(), 1));
...@@ -945,7 +929,8 @@ class Cron ...@@ -945,7 +929,8 @@ class Cron
$recurringDetailReference = $this->_pspReference; $recurringDetailReference = $this->_pspReference;
$storeId = $this->_order->getStoreId(); $storeId = $this->_order->getStoreId();
$customerReference = $this->_order->getCustomerId(); $customerId = $this->_order->getCustomerId();
$customerReference = $this->_adyenHelper->getCustomerReference($customerId);
$listRecurringContracts = null; $listRecurringContracts = null;
$this->_adyenLogger->addAdyenNotificationCronjob( $this->_adyenLogger->addAdyenNotificationCronjob(
__('CustomerReference is: %1 and storeId is %2 and RecurringDetailsReference is %3', __('CustomerReference is: %1 and storeId is %2 and RecurringDetailsReference is %3',
...@@ -982,7 +967,7 @@ class Cron ...@@ -982,7 +967,7 @@ class Cron
} }
$billingAgreements = $this->_billingAgreementCollectionFactory->create(); $billingAgreements = $this->_billingAgreementCollectionFactory->create();
$billingAgreements->addFieldToFilter('customer_id', $customerReference); $billingAgreements->addFieldToFilter('customer_id', $customerId);
// Get collection and update existing agreements // Get collection and update existing agreements
......
...@@ -49,5 +49,10 @@ ...@@ -49,5 +49,10 @@
<source_model>Adyen\Payment\Model\Config\Source\RecurringPaymentType</source_model> <source_model>Adyen\Payment\Model\Config\Source\RecurringPaymentType</source_model>
<config_path>payment/adyen_oneclick/recurring_payment_type</config_path> <config_path>payment/adyen_oneclick/recurring_payment_type</config_path>
</field> </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> </group>
</include> </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