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 0a67b506 authored by rikterbeek's avatar rikterbeek

implemented authentication for notification, let the notification handler save...

implemented authentication for notification, let the notification handler save the date so that timezone is correct, fix that live property is set in the notificaiton table and let the cronjob set notification to done when it is finished
parent ec38adaa
...@@ -48,6 +48,10 @@ interface NotificationInterface ...@@ -48,6 +48,10 @@ interface NotificationInterface
* Live * Live
*/ */
const LIVE = 'live'; const LIVE = 'live';
/*
* Live
*/
const DONE = 'done';
/* /*
* Additional data * Additional data
*/ */
......
...@@ -44,24 +44,15 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -44,24 +44,15 @@ class Json extends \Magento\Framework\App\Action\Action
*/ */
public function execute() public function execute()
{ {
//TODO validate the notification with authentication!!
// check duplicates
// _isDuplicate
try { try {
$notificationItems = json_decode(file_get_contents('php://input'), true); $notificationItems = json_decode(file_get_contents('php://input'), true);
// $notificationItems = json_decode('{"live":"false","notificationItems":[{"NotificationRequestItem":{"additionalData":{"expiryDate":"12\/2012"," NAME1 ":"VALUE1","authCode":"1234","cardSummary":"7777","totalFraudScore":"10","hmacSignature":"yGnVWLP+UcpqjHTJbO5IUkG4ZdIk3uHCu62QAJvbbyg=","NAME2":" VALUE2 ","fraudCheck-6-ShopperIpUsage":"10"},"amount":{"currency":"EUR","value":10500},"eventCode":"AUTHORISATION","eventDate":"2015-09-11T13:53:21+02:00","merchantAccountCode":"MagentoMerchantByteShop1","merchantReference":"000000023","operations":["CANCEL","CAPTURE","REFUND"],"paymentMethod":"visa","pspReference":"test_AUTHORISATION_1","reason":"1234:7777:12\/2012","success":"true"}}]}', true);
$notificationMode = isset($notificationItems['live']) ? $notificationItems['live'] : ""; $notificationMode = isset($notificationItems['live']) ? $notificationItems['live'] : "";
if($notificationMode != "" && $this->_validateNotificationMode($notificationMode)) if($notificationMode != "" && $this->_validateNotificationMode($notificationMode))
{ {
foreach($notificationItems['notificationItems'] as $notificationItem) foreach($notificationItems['notificationItems'] as $notificationItem)
{ {
$status = $this->_processNotification($notificationItem['NotificationRequestItem']); $status = $this->_processNotification($notificationItem['NotificationRequestItem'], $notificationMode);
if($status == "401"){ if($status == "401"){
$this->_return401(); $this->_return401();
return; return;
...@@ -78,11 +69,8 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -78,11 +69,8 @@ class Json extends \Magento\Framework\App\Action\Action
$this->_return401(); $this->_return401();
return; return;
} }
throw new \Magento\Framework\Exception\LocalizedException(__('Mismatch between Live/Test modes of Magento store and the Adyen platform')); throw new \Magento\Framework\Exception\LocalizedException(__('Mismatch between Live/Test modes of Magento store and the Adyen platform'));
} }
} catch (Exception $e) { } catch (Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
} }
...@@ -95,7 +83,7 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -95,7 +83,7 @@ class Json extends \Magento\Framework\App\Action\Action
protected function _validateNotificationMode($notificationMode) protected function _validateNotificationMode($notificationMode)
{ {
$mode = $this->_adyenHelper->getAdyenAbstractConfigData('demo_mode'); $mode = $this->_adyenHelper->getAdyenAbstractConfigData('demo_mode');
if ($mode=='Y' && $notificationMode == "false" || $mode=='N' && $notificationMode == 'true') { if ($mode=='1' && $notificationMode == "false" || $mode=='0' && $notificationMode == 'true') {
return true; return true;
} }
return false; return false;
...@@ -107,52 +95,62 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -107,52 +95,62 @@ class Json extends \Magento\Framework\App\Action\Action
* @param $response * @param $response
* @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\LocalizedException
*/ */
protected function _processNotification($response) protected function _processNotification($response, $notificationMode)
{ {
// validate the notification // validate the notification
if($this->authorised($response)) if($this->authorised($response))
{ {
// check if notificaiton already exists
if(!$this->_isDuplicate($response)) {
try { try {
$notification = $this->_objectManager->create('Adyen\Payment\Model\Notification'); $notification = $this->_objectManager->create('Adyen\Payment\Model\Notification');
if(isset($response['pspReference'])) { if (isset($response['pspReference'])) {
$notification->setPspreference($response['pspReference']); $notification->setPspreference($response['pspReference']);
} }
if(isset($response['merchantReference'])) { if (isset($response['merchantReference'])) {
$notification->setMerchantReference($response['merchantReference']); $notification->setMerchantReference($response['merchantReference']);
} }
if(isset($response['eventCode'])) { if (isset($response['eventCode'])) {
$notification->setEventCode($response['eventCode']); $notification->setEventCode($response['eventCode']);
} }
if(isset($response['success'])) { if (isset($response['success'])) {
$notification->setSuccess($response['success']); $notification->setSuccess($response['success']);
} }
if(isset($response['paymentMethod'])) { if (isset($response['paymentMethod'])) {
$notification->setPaymentMethod($response['paymentMethod']); $notification->setPaymentMethod($response['paymentMethod']);
} }
if(isset($response['amount'])) { if (isset($response['amount'])) {
$notification->setAmountValue($response['amount']['value']); $notification->setAmountValue($response['amount']['value']);
$notification->setAmountCurrency($response['amount']['currency']); $notification->setAmountCurrency($response['amount']['currency']);
} }
if(isset($response['reason'])) { if (isset($response['reason'])) {
$notification->setReason($response['reason']); $notification->setReason($response['reason']);
} }
if(isset($response['additionalData'])) {
$notification->setLive($notificationMode);
if (isset($response['additionalData'])) {
$notification->setAddtionalData(serialize($response['additionalData'])); $notification->setAddtionalData(serialize($response['additionalData']));
} }
if(isset($response['done'])) { if (isset($response['done'])) {
$notification->setDone($response['done']); $notification->setDone($response['done']);
} }
// do this to set both fields in the correct timezone
$date = new \DateTime();
$notification->setCreatedAt($date);
$notification->setUpdatedAt($date);
$notification->save(); $notification->save();
} catch(Exception $e) { } catch (Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
} }
} }
}
} }
...@@ -165,7 +163,7 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -165,7 +163,7 @@ class Json extends \Magento\Framework\App\Action\Action
// Add CGI support // Add CGI support
$this->_fixCgiHttpAuthentication(); $this->_fixCgiHttpAuthentication();
$internalMerchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchantAccount'); $internalMerchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchant_account');
$username = $this->_adyenHelper->getAdyenAbstractConfigData('notification_username'); $username = $this->_adyenHelper->getAdyenAbstractConfigData('notification_username');
$password = $this->_adyenHelper->getNotificationPassword(); $password = $this->_adyenHelper->getNotificationPassword();
...@@ -208,6 +206,22 @@ class Json extends \Magento\Framework\App\Action\Action ...@@ -208,6 +206,22 @@ class Json extends \Magento\Framework\App\Action\Action
return false; return false;
} }
/**
* $desc if notification is already saved ignore it
* @param $response
* @return bool
*/
protected function _isDuplicate($response)
{
$pspReference = trim($response['pspReference']);
$eventCode = trim($response['eventCode']);
$success = trim($response['success']);
$notification = $this->_objectManager->create('Adyen\Payment\Model\Notification');
return $notification->isDuplicate($pspReference, $eventCode, $success);
}
/** /**
* Fix these global variables for the CGI * Fix these global variables for the CGI
*/ */
......
...@@ -102,20 +102,17 @@ class Cron ...@@ -102,20 +102,17 @@ class Cron
//fixme somehow the created_at is saved in my timzone //fixme somehow the created_at is saved in my timzone
// loop over notifications that are not processed and from 1 minute ago $dateStart = new \DateTime();
// loop over notifications that are not processed and from 1 minute ago
$dateStart = new \DateTime(); $dateStart = new \DateTime();
$dateStart->modify('-1 day'); $dateStart->modify('-1 day');
// excecute notifications from 2 minute or earlier because order could not yet been created by mangento // excecute notifications from 2 minute or earlier because order could not yet been created by mangento
$dateEnd = new \DateTime(); $dateEnd = new \DateTime();
$dateEnd->modify('-2 minute'); $dateEnd->modify('-2 minute');
// TODO: format to right timezones db is now having my local time
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true]; $dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
$notifications = $this->_notificationFactory->create(); $notifications = $this->_notificationFactory->create();
$notifications->addFieldToFilter('done', 0); $notifications->addFieldToFilter('done', 0);
$notifications->addFieldToFilter('created_at', $dateRange); $notifications->addFieldToFilter('created_at', $dateRange);
...@@ -179,65 +176,19 @@ class Cron ...@@ -179,65 +176,19 @@ class Cron
$this->_processNotification(); $this->_processNotification();
} }
$id = $notification->getId();
// echo $id;
// $comment = "THIS IS A TEST";
// $status = \Magento\Sales\Model\Order::STATE_PROCESSING;
// $this->_order->setState($status);
// $this->_order->addStatusHistoryComment($comment, $status);
//
$this->_order->save(); $this->_order->save();
foreach($this->_debugData as $debug) { foreach($this->_debugData as $debug) {
$this->_logger->info($debug); $this->_logger->info($debug);
} }
// set done to true
$dateEnd = new \DateTime();
print_R($this->_debugData); $notification->setDone(true);
$notification->setUpdatedAt($dateEnd);
$notification->save();
echo $this->_order->getId();die();
$eventCode = $notification->getEventCode();
// TODO: set done to true!!
} }
echo 'end1';
// get currenttime
// $date = new date();
$this->_logger->info("END OF THE CRONJOB"); $this->_logger->info("END OF THE CRONJOB");
} }
protected function _declareVariables($notification) protected function _declareVariables($notification)
......
...@@ -54,6 +54,18 @@ class Notification extends \Magento\Framework\Model\AbstractModel ...@@ -54,6 +54,18 @@ class Notification extends \Magento\Framework\Model\AbstractModel
$this->_init('Adyen\Payment\Model\Resource\Notification'); $this->_init('Adyen\Payment\Model\Resource\Notification');
} }
/**
* Check if the Adyen Notification is already stored in the system
* @param $pspReference
* @param $event
* @param $success
* @return bool true if the notification is a duplicate
*/
public function isDuplicate($pspReference, $eventCode, $success) {
$result = $this->getResource()->getNotification($pspReference, $eventCode, $success);
return (empty($result)) ? false : true;
}
/** /**
* Gets the Pspreference for the notification. * Gets the Pspreference for the notification.
* *
...@@ -223,6 +235,28 @@ class Notification extends \Magento\Framework\Model\AbstractModel ...@@ -223,6 +235,28 @@ class Notification extends \Magento\Framework\Model\AbstractModel
return $this->setData(self::REASON, $reason); return $this->setData(self::REASON, $reason);
} }
/**
* Gets the Reason for the notification.
*
* @return int|null Reason.
*/
public function getLive()
{
return $this->getData(self::LIVE);
}
/**
* Sets Reason.
*
* @param string $reason
* @return $this
*/
public function setLive($live)
{
return $this->setData(self::LIVE, $live);
}
/** /**
* Gets the AdditionalData for the notification. * Gets the AdditionalData for the notification.
* *
......
...@@ -8,4 +8,23 @@ class Notification extends \Magento\Framework\Model\Resource\Db\AbstractDb ...@@ -8,4 +8,23 @@ class Notification extends \Magento\Framework\Model\Resource\Db\AbstractDb
{ {
$this->_init('adyen_notification', 'entity_id'); $this->_init('adyen_notification', 'entity_id');
} }
/**
* @desc get Notification for duplicate check
* @param $pspReference
* @param $eventCode
* @param $success
* @return array
*/
public function getNotification($pspReference, $eventCode, $success)
{
$adapter = $this->getReadConnection();
$select = $adapter->select()
->from(['notification' => $this->getTable('adyen_notification')])
->where('notification.pspreference=?', $pspReference)
->where('notification.event_code=?', $eventCode)
->where('notification.success=?', $success);
return $adapter->fetchPairs($select);
}
} }
\ 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