<commit_before><?php
/*
 * HiPay fullservice SDK
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 *
 * @copyright      Copyright (c) 2016 - HiPay
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 *
 */
namespace HiPay\FullserviceMagento\Model;


use HiPay\Fullservice\Gateway\Model\Transaction;
use HiPay\Fullservice\Gateway\Mapper\TransactionMapper;
use HiPay\Fullservice\Enum\Transaction\TransactionStatus;
use HiPay\FullserviceMagento\Model\Email\Sender\FraudReviewSender;
use HiPay\FullserviceMagento\Model\Email\Sender\FraudDenySender;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;

class Notify {
	
	/**
	 *
	 * @var \Magento\Sales\Model\OrderFactory $_orderFactory
	 */
	protected $_orderFactory;
	
	/**
	 * @var FraudReviewSender
	 */
	protected $fraudReviewSender;
	
	/**
	 * @var FraudDenySender
	 */
	protected $fraudDenySender;
	
	/**
	 * 
	 * @var OrderSender $orderSender
	 */
	protected $orderSender;
	
	/**
	 * @var \Magento\Sales\Model\Order
	 */
	protected $_order;
	
	/**
	 * 
	 * @var Transaction $_transaction Response Model Transaction
	 */
	protected $_transaction;
	
	/**
	 *
	 * @var \HiPay\FullserviceMagento\Model\FullserviceMethod $_methodInstance
	 */
	protected $_methodInstance;
	
	
	public function __construct(
			\Magento\Sales\Model\OrderFactory $orderFactory,
			OrderSender $orderSender,
			FraudReviewSender $fraudReviewSender,
			FraudDenySender $fraudDenySender,
			\Magento\Payment\Helper\Data $paymentHelper,
			$params = []
			){

			$this->_orderFactory = $orderFactory;
			$this->orderSender = $orderSender;
			$this->fraudReviewSender = $fraudReviewSender;
			$this->fraudDenySender = $fraudDenySender;
	
			if (isset($params['response']) && is_array($params['response'])) {
				$this->_transaction = (new TransactionMapper($params['response']))->getModelObjectMapped();
				
				$this->_order = $this->_orderFactory->create()->loadByIncrementId($this->_transaction->getOrder()->getId());
				if (!$this->_order->getId()) {
					throw new \Exception(sprintf('Wrong order ID: "%s".', $this->_transaction->getOrder()->getId()));
				}
				
				//Retieve method model
				$this->_methodInstance = $paymentHelper->getMethodInstance($this->_order->getPayment()->getMethod());
				
				//Debug transaction notification if debug enabled
				$this->_methodInstance->debugData($this->_transaction->toArray());
				
			} else {
				throw new \Exception('Posted data response as array is required.');
			}
		
	}
	
	
	
	
	public function processTransaction(){
		
		switch ($this->_transaction->getStatus()){
			case TransactionStatus::BLOCKED: //110
				$this->_setFraudDetected();
			case TransactionStatus::DENIED: //111
				$this->_doTransactionDenied();
				break;
			case TransactionStatus::AUTHORIZED_AND_PENDING: //112
			case TransactionStatus::PENDING_PAYMENT: //200
				$this->_setFraudDetected();
				$this->_doTransactionAuthorizedAndPending();
				break;
			case TransactionStatus::AUTHORIZATION_REQUESTED: //142
				$this->_changeStatus(Config::STATUS_AUTHORIZATION_REQUESTED);
				break;
			case TransactionStatus::REFUSED: //113
			case TransactionStatus::CANCELLED: //115 Cancel order and transaction
			case TransactionStatus::AUTHORIZATION_REFUSED: //163
			case TransactionStatus::CAPTURE_REFUSED: //173
				$this->_doTransactionFailure();
				break;
			case TransactionStatus::EXPIRED: //114 Hold order, the merchant can unhold and try a new capture
				$this->_doTransactionVoid();
				break;
			case TransactionStatus::AUTHORIZED: //116
				$this->_doTransactionAuthorization();<commit_msg>Validate by status before transaction process<commit_after><?php
/*
 * HiPay fullservice SDK
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MIT License
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/mit-license.php
 *
 * @copyright      Copyright (c) 2016 - HiPay
 * @license        http://opensource.org/licenses/mit-license.php MIT License
 *
 */
namespace HiPay\FullserviceMagento\Model;


use HiPay\Fullservice\Gateway\Model\Transaction;
use HiPay\Fullservice\Gateway\Mapper\TransactionMapper;
use HiPay\Fullservice\Enum\Transaction\TransactionStatus;
use HiPay\FullserviceMagento\Model\Email\Sender\FraudReviewSender;
use HiPay\FullserviceMagento\Model\Email\Sender\FraudDenySender;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;

class Notify {
	
	/**
	 *
	 * @var \Magento\Sales\Model\OrderFactory $_orderFactory
	 */
	protected $_orderFactory;
	
	/**
	 * @var FraudReviewSender
	 */
	protected $fraudReviewSender;
	
	/**
	 * @var FraudDenySender
	 */
	protected $fraudDenySender;
	
	/**
	 * 
	 * @var OrderSender $orderSender
	 */
	protected $orderSender;
	
	/**
	 * @var \Magento\Sales\Model\Order
	 */
	protected $_order;
	
	/**
	 * 
	 * @var Transaction $_transaction Response Model Transaction
	 */
	protected $_transaction;
	
	/**
	 *
	 * @var \HiPay\FullserviceMagento\Model\FullserviceMethod $_methodInstance
	 */
	protected $_methodInstance;
	
	
	public function __construct(
			\Magento\Sales\Model\OrderFactory $orderFactory,
			OrderSender $orderSender,
			FraudReviewSender $fraudReviewSender,
			FraudDenySender $fraudDenySender,
			\Magento\Payment\Helper\Data $paymentHelper,
			$params = []
			){

			$this->_orderFactory = $orderFactory;
			$this->orderSender = $orderSender;
			$this->fraudReviewSender = $fraudReviewSender;
			$this->fraudDenySender = $fraudDenySender;
	
			if (isset($params['response']) && is_array($params['response'])) {
				$this->_transaction = (new TransactionMapper($params['response']))->getModelObjectMapped();
				
				$this->_order = $this->_orderFactory->create()->loadByIncrementId($this->_transaction->getOrder()->getId());
				if (!$this->_order->getId()) {
					throw new \Exception(sprintf('Wrong order ID: "%s".', $this->_transaction->getOrder()->getId()));
				}
				
				//Retieve method model
				$this->_methodInstance = $paymentHelper->getMethodInstance($this->_order->getPayment()->getMethod());
				
				//Debug transaction notification if debug enabled
				$this->_methodInstance->debugData($this->_transaction->toArray());
				
			} else {
				throw new \Exception('Posted data response as array is required.');
			}
		
	}
	
	
	protected function canProcessTransaction(){
		
		switch ($this->_transaction->getStatus()){
			case TransactionStatus::EXPIRED: //114
				
				if(in_array($this->_order->getStatus(),array(Config::STATUS_AUTHORIZED))){
					return true;
				}
				
				break;
			case  TransactionStatus::AUTHORIZED: //116
				if($this->_order->getState() == \Magento\Sales\Model\Order::STATE_NEW ||
					$this->_order->getState() == \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT ||
					$this->_order->getState() == \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW ||
					in_array($this->_order->getStatus(),array(Config::STATUS_AUTHORIZATION_REQUESTED))){
					return true;
				}
				break;
			case TransactionStatus::CAPTURE_REQUESTED: //117
				if(!$this->_order->hasInvoices() || $this->_order->getBaseTotalDue() == $this->_order->getBaseGrandTotal())
				{
					return true;
				}
				break;
			default:
				return true;
		}
		
		return false;
		
	}
	
	
	
	
	public function processTransaction(){
		
		if(!$this->canProcessTransaction()){
			return $this;
		}
		
		//Write about notification in order history
		$this->_doTransactionMessage("Status code: " . $this->_transaction->getStatus());
		
		switch ($this->_transaction->getStatus()){
			case TransactionStatus::BLOCKED: //110
				$this->_setFraudDetected();
			case TransactionStatus::DENIED: //111
				$this->_doTransactionDenied();
				break;
			case TransactionStatus::AUTHORIZED_AND_PENDING: //112
			case TransactionStatus::PENDING_PAYMENT: //200
				$this->_setFraudDetected();
				$this->_doTransactionAuthorizedAndPending();
				break;
			case TransactionStatus::AUTHORIZATION_REQUESTED: //142
				$this->_changeStatus(Config::STATUS_AUTHORIZATION_REQUESTED);
				break;
			case TransactionStatus::REFUSED: //113
			case TransactionStatus::CANCELLED: //115 Cancel order and transaction
			case TransactionStatus::AUTHORIZATION_REFUSED: //163
			case TransactionStatus::CAPTURE_REFUSED: //173
				$this->_doTransactionFailure();
				break;
			case TransactionStatus::EXPIRED: //114 Hold order, the merchant can unhold and try a new capture
				$this->_doTransactionVoid();
				break;
			case TransactionStatus::AUTHORIZED: //116
				$this->_doTransactionAuthorization();
{'fraction_non_alphanumeric': 0.13394998287084617, 'fraction_numerical': 0.009478131780290054, 'mean_word_length': 4.0449308755760365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1835771', 'n_tokens_mistral': 3107, 'n_tokens_neox': 2566, 'n_words': 503}