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
* Live
*/
const LIVE = 'live';
/*
* Live
*/
const DONE = 'done';
/*
* Additional data
*/
......
......@@ -44,24 +44,15 @@ class Json extends \Magento\Framework\App\Action\Action
*/
public function execute()
{
//TODO validate the notification with authentication!!
// check duplicates
// _isDuplicate
try {
$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'] : "";
if($notificationMode != "" && $this->_validateNotificationMode($notificationMode))
{
foreach($notificationItems['notificationItems'] as $notificationItem)
{
$status = $this->_processNotification($notificationItem['NotificationRequestItem']);
$status = $this->_processNotification($notificationItem['NotificationRequestItem'], $notificationMode);
if($status == "401"){
$this->_return401();
return;
......@@ -78,11 +69,8 @@ class Json extends \Magento\Framework\App\Action\Action
$this->_return401();
return;
}
throw new \Magento\Framework\Exception\LocalizedException(__('Mismatch between Live/Test modes of Magento store and the Adyen platform'));
}
} catch (Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
}
......@@ -95,7 +83,7 @@ class Json extends \Magento\Framework\App\Action\Action
protected function _validateNotificationMode($notificationMode)
{
$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 false;
......@@ -107,52 +95,62 @@ class Json extends \Magento\Framework\App\Action\Action
* @param $response
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _processNotification($response)
protected function _processNotification($response, $notificationMode)
{
// validate the notification
if($this->authorised($response))
{
try {
// check if notificaiton already exists
if(!$this->_isDuplicate($response)) {
$notification = $this->_objectManager->create('Adyen\Payment\Model\Notification');
try {
if(isset($response['pspReference'])) {
$notification->setPspreference($response['pspReference']);
}
if(isset($response['merchantReference'])) {
$notification->setMerchantReference($response['merchantReference']);
}
if(isset($response['eventCode'])) {
$notification->setEventCode($response['eventCode']);
}
if(isset($response['success'])) {
$notification->setSuccess($response['success']);
}
if(isset($response['paymentMethod'])) {
$notification->setPaymentMethod($response['paymentMethod']);
}
if(isset($response['amount'])) {
$notification->setAmountValue($response['amount']['value']);
$notification->setAmountCurrency($response['amount']['currency']);
}
if(isset($response['reason'])) {
$notification->setReason($response['reason']);
}
if(isset($response['additionalData'])) {
$notification->setAddtionalData(serialize($response['additionalData']));
}
if(isset($response['done'])) {
$notification->setDone($response['done']);
}
$notification = $this->_objectManager->create('Adyen\Payment\Model\Notification');
if (isset($response['pspReference'])) {
$notification->setPspreference($response['pspReference']);
}
if (isset($response['merchantReference'])) {
$notification->setMerchantReference($response['merchantReference']);
}
if (isset($response['eventCode'])) {
$notification->setEventCode($response['eventCode']);
}
if (isset($response['success'])) {
$notification->setSuccess($response['success']);
}
if (isset($response['paymentMethod'])) {
$notification->setPaymentMethod($response['paymentMethod']);
}
if (isset($response['amount'])) {
$notification->setAmountValue($response['amount']['value']);
$notification->setAmountCurrency($response['amount']['currency']);
}
if (isset($response['reason'])) {
$notification->setReason($response['reason']);
}
$notification->save();
$notification->setLive($notificationMode);
} catch(Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
if (isset($response['additionalData'])) {
$notification->setAddtionalData(serialize($response['additionalData']));
}
if (isset($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();
} catch (Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
}
}
}
}
......@@ -165,7 +163,7 @@ class Json extends \Magento\Framework\App\Action\Action
// Add CGI support
$this->_fixCgiHttpAuthentication();
$internalMerchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchantAccount');
$internalMerchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData('merchant_account');
$username = $this->_adyenHelper->getAdyenAbstractConfigData('notification_username');
$password = $this->_adyenHelper->getNotificationPassword();
......@@ -208,6 +206,22 @@ class Json extends \Magento\Framework\App\Action\Action
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
*/
......
......@@ -102,20 +102,17 @@ class Cron
//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->modify('-1 day');
// excecute notifications from 2 minute or earlier because order could not yet been created by mangento
$dateEnd = new \DateTime();
$dateEnd->modify('-2 minute');
// TODO: format to right timezones db is now having my local time
$dateRange = ['from' => $dateStart, 'to' => $dateEnd, 'datetime' => true];
$notifications = $this->_notificationFactory->create();
$notifications->addFieldToFilter('done', 0);
$notifications->addFieldToFilter('created_at', $dateRange);
......@@ -179,65 +176,19 @@ class Cron
$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();
foreach($this->_debugData as $debug) {
$this->_logger->info($debug);
}
print_R($this->_debugData);
echo $this->_order->getId();die();
$eventCode = $notification->getEventCode();
// TODO: set done to true!!
// set done to true
$dateEnd = new \DateTime();
$notification->setDone(true);
$notification->setUpdatedAt($dateEnd);
$notification->save();
}
echo 'end1';
// get currenttime
// $date = new date();
$this->_logger->info("END OF THE CRONJOB");
}
protected function _declareVariables($notification)
......
......@@ -54,6 +54,18 @@ class Notification extends \Magento\Framework\Model\AbstractModel
$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.
*
......@@ -223,6 +235,28 @@ class Notification extends \Magento\Framework\Model\AbstractModel
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.
*
......
......@@ -8,4 +8,23 @@ class Notification extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
$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