Skip to content

Commit

Permalink
Merge pull request #20 from appinlet/release/2.4.1
Browse files Browse the repository at this point in the history
Release/2.4.1
  • Loading branch information
appinlet authored May 14, 2021
2 parents 2dcf2fd + 96c382d commit 07003ad
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 188 deletions.
116 changes: 76 additions & 40 deletions Paygate/PayWeb/Controller/Cron/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*
* Released under the GNU General Public License
*/

namespace PayGate\PayWeb\Controller\Cron;

use PayGate\PayWeb\Model\Config as PayGateConfig;
use PayGate\PayWeb\Model\PayGate;

/**
* Responsible for loading page content.
*
Expand All @@ -16,50 +20,77 @@
*/
class Index extends \PayGate\PayWeb\Controller\AbstractPaygate
{
/**
* @var \Magento\Framework\App\Area
*/
private $state;
/**
* @var \Magento\Framework\DB\Transaction
*/
private $transactionModel;
/**
* @var PayGateConfig
*/
private $paygateConfig;
/**
* @var \Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory
*/
protected $transactionSearchResultInterfaceFactory;

public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Session\Generic $paygateSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl, \Psr\Log\LoggerInterface $logger, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, PayGate $paymentMethod, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Order\Email\Sender\OrderSender $OrderSender, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\Order\Payment\Transaction\Builder $_transactionBuilder, \Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory $transactionSearchResultInterfaceFactory, PayGateConfig $paygateConfig, \Magento\Framework\DB\Transaction $transactionModel, \Magento\Framework\App\State $state)
{
$this->state = $state;
$this->transactionModel = $transactionModel;
$this->paygateConfig = $paygateConfig;
$this->transactionSearchResultInterfaceFactory = $transactionSearchResultInterfaceFactory;
parent::__construct($context, $pageFactory, $customerSession, $checkoutSession, $orderFactory, $paygateSession, $urlHelper, $customerUrl, $logger, $transactionFactory, $invoiceService, $invoiceSender, $paymentMethod, $urlBuilder, $orderRepository, $storeManager, $OrderSender, $date, $orderCollectionFactory, $_transactionBuilder);
}

public function execute()
{
$cutoffTime = ( new \DateTime() )->sub( new \DateInterval( 'PT10M' ) )->format( 'Y-m-d H:i:s' );
$this->_logger->info( 'Cutoff: ' . $cutoffTime );
$ocf = $this->_orderCollectionFactory->create();
$ocf->addAttributeToSelect( 'entity_id' );
$ocf->addAttributeToFilter( 'status', ['eq' => 'pending_payment'] );
$ocf->addAttributeToFilter( 'created_at', ['lt' => $cutoffTime] );
$ocf->addAttributeToFilter( 'updated_at', ['lt' => $cutoffTime] );
$orderIds = $ocf->getData();

$this->_logger->info( 'Orders for cron: ' . json_encode( $orderIds ) );

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

foreach ( $orderIds as $orderId ) {
$order_id = $orderId['entity_id'];
$transactionSearchResult = $objectManager->get( '\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory' );
$transaction = $transactionSearchResult->create()->addOrderIdFilter( $order_id )->getFirstItem();

$transactionId = $transaction->getData( 'txn_id' );
$order = $this->orderRepository->get( $orderId['entity_id'] );
$PaymentTitle = $order->getPayment()->getMethodInstance()->getTitle();

if ( !empty( $transactionId ) & $PaymentTitle == "PayGate" ) {
$orderquery['orderId'] = $order->getRealOrderId();
$orderquery['country'] = $order->getBillingAddress()->getCountryId();
$orderquery['currency'] = $order->getOrderCurrencyCode();
$orderquery['amount'] = $order->getGrandTotal();
$orderquery['reference'] = $order->getRealOrderId();
$orderquery['transaction_id'] = $transactionId;

$result = explode( "&", $this->getQueryResult( $orderquery ) );
$this->updatePaymentStatus( $order, $result );
}
$this->state->emulateAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND, function () {

$cutoffTime = ( new \DateTime() )->sub( new \DateInterval( 'PT10M' ) )->format( 'Y-m-d H:i:s' );
$this->_logger->info( 'Cutoff: ' . $cutoffTime );
$ocf = $this->_orderCollectionFactory->create();
$ocf->addAttributeToSelect( 'entity_id' );
$ocf->addAttributeToFilter( 'status', ['eq' => 'pending_payment'] );
$ocf->addAttributeToFilter( 'created_at', ['lt' => $cutoffTime] );
$ocf->addAttributeToFilter( 'updated_at', ['lt' => $cutoffTime] );
$orderIds = $ocf->getData();

$this->_logger->info( 'Orders for cron: ' . json_encode( $orderIds ) );

foreach ( $orderIds as $orderId ) {
$order_id = $orderId['entity_id'];
$transactionSearchResult = $this->transactionSearchResultInterfaceFactory;
$transaction = $transactionSearchResult->create()->addOrderIdFilter( $order_id )->getFirstItem();

$transactionId = $transaction->getData( 'txn_id' );
$order = $this->orderRepository->get( $orderId['entity_id'] );
$PaymentTitle = $order->getPayment()->getMethodInstance()->getTitle();

if ( !empty( $transactionId ) & $PaymentTitle == "PayGate PayWeb" ) {
$orderquery['orderId'] = $order->getRealOrderId();
$orderquery['country'] = $order->getBillingAddress()->getCountryId();
$orderquery['currency'] = $order->getOrderCurrencyCode();
$orderquery['amount'] = $order->getGrandTotal();
$orderquery['reference'] = $order->getRealOrderId();
$orderquery['transaction_id'] = $transactionId;

$result = explode( "&", $this->getQueryResult( $orderquery ) );

$this->updatePaymentStatus( $order, $result );

}

}
}
});
}

public function getQueryResult( $orderquery )
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$config = $objectManager->get( "PayGate\PayWeb\Model\Config" )->getApiCredentials();
$config = $this->paygateConfig->getApiCredentials();
$encryption_key = $config['encryption_key'];
$paygate_id = $config['paygate_id'];

Expand All @@ -86,7 +117,6 @@ public function getQueryResult( $orderquery )
curl_setopt( $ch, CURLOPT_URL, 'https://secure.paygate.co.za/payweb3/query.trans' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_NOBODY, false );
curl_setopt( $ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST'] );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fieldsString );

Expand All @@ -101,14 +131,21 @@ public function getQueryResult( $orderquery )

public function updatePaymentStatus( $order, $resp )
{

if ( is_array( $resp ) && count( $resp ) > 0 ) {

$paymentData = array();
foreach ( $resp as $param ) {
$pr = explode( "=", $param );
$paymentData[$pr[0]] = $pr[1];
}
if(isset($paymentData['ERROR'])){
$status = \Magento\Sales\Model\Order::STATE_CANCELED;
$order->setStatus( $status );
$order->setState( $status );
$order->save();
return false;
}

if ( $paymentData['TRANSACTION_STATUS'] == 1 ) {
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
$order->setStatus( $status );
Expand All @@ -117,7 +154,6 @@ public function updatePaymentStatus( $order, $resp )
try {
$this->generateInvoice( $order );
} catch ( \Exception $ex ) {

$this->_logger->error( $ex->getMessage() );
}
} else {
Expand Down Expand Up @@ -145,7 +181,7 @@ public function generateInvoice( $order )
$invoice->register();

// Save the invoice to the order
$transaction = $this->_objectManager->create( 'Magento\Framework\DB\Transaction' )
$transaction = $this->transactionModel
->addObject( $invoice )
->addObject( $invoice->getOrder() );

Expand Down
83 changes: 49 additions & 34 deletions Paygate/PayWeb/Controller/Notify/Indexm220.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@
namespace PayGate\PayWeb\Controller\Notify;

use PayGate\PayWeb\Controller\AbstractPaygate;
use PayGate\PayWeb\Model\PayGate;

class Indexm220 extends AbstractPaygate
{
/**
* @var \Magento\Framework\DB\Transaction
*/
private $transactionModel;

/**
* indexAction
*
*/

public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Session\Generic $paygateSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl, \Psr\Log\LoggerInterface $logger, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, PayGate $paymentMethod, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Order\Email\Sender\OrderSender $OrderSender, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\Order\Payment\Transaction\Builder $_transactionBuilder,\Magento\Framework\DB\Transaction $transactionModel)
{
$this->transactionModel = $transactionModel;
parent::__construct($context, $pageFactory, $customerSession, $checkoutSession, $orderFactory, $paygateSession, $urlHelper, $customerUrl, $logger, $transactionFactory, $invoiceService, $invoiceSender, $paymentMethod, $urlBuilder, $orderRepository, $storeManager, $OrderSender, $date, $orderCollectionFactory, $_transactionBuilder);
}

public function execute()
{
echo "OK";
Expand Down Expand Up @@ -91,42 +103,45 @@ public function execute()
$order = $this->orderRepository->get( $reference );
switch ( $status ) {
case 1:
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
if ( $this->getConfigData( 'Successful_Order_status' ) != "" ) {
$status = $this->getConfigData( 'Successful_Order_status' );
}

$model = $this->_paymentMethod;
$order_successful_email = $model->getConfigData( 'order_email' );

if ( $order_successful_email != '0' ) {
$this->OrderSender->send( $order );
$order->addStatusHistoryComment( __( 'Notified customer about order #%1.', $order->getId() ) )->setIsCustomerNotified( true )->save();
$orderState = $order->getState();
if ($orderState != \Magento\Sales\Model\Order::STATE_COMPLETE && $orderState != \Magento\Sales\Model\Order::STATE_PROCESSING) {
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
if ( $this->getConfigData( 'Successful_Order_status' ) != "" ) {
$status = $this->getConfigData( 'Successful_Order_status' );
}

$model = $this->_paymentMethod;
$order_successful_email = $model->getConfigData( 'order_email' );

if ( $order_successful_email != '0' ) {
$this->OrderSender->send( $order );
$order->addStatusHistoryComment( __( 'Notified customer about order #%1.', $order->getId() ) )->setIsCustomerNotified( true )->save();
}

// Capture invoice when payment is successfull
$invoice = $this->_invoiceService->prepareInvoice( $order );
$invoice->setRequestedCaptureCase( \Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE );
$invoice->register();

// Save the invoice to the order
$transaction = $this->transactionModel
->addObject( $invoice )
->addObject( $invoice->getOrder() );

$transaction->save();

// Magento\Sales\Model\Order\Email\Sender\InvoiceSender
$send_invoice_email = $model->getConfigData( 'invoice_email' );
if ( $send_invoice_email != '0' ) {
$this->invoiceSender->send( $invoice );
$order->addStatusHistoryComment( __( 'Notified customer about invoice #%1.', $invoice->getId() ) )->setIsCustomerNotified( true )->save();
}

// Save Transaction Response
$this->createTransaction( $order, $paygate_data );
$order->setState( $status )->setStatus( $status )->save();
}

// Capture invoice when payment is successfull
$invoice = $this->_invoiceService->prepareInvoice( $order );
$invoice->setRequestedCaptureCase( \Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE );
$invoice->register();

// Save the invoice to the order
$transaction = $this->_objectManager->create( 'Magento\Framework\DB\Transaction' )
->addObject( $invoice )
->addObject( $invoice->getOrder() );

$transaction->save();

// Magento\Sales\Model\Order\Email\Sender\InvoiceSender
$send_invoice_email = $model->getConfigData( 'invoice_email' );
if ( $send_invoice_email != '0' ) {
$this->invoiceSender->send( $invoice );
$order->addStatusHistoryComment( __( 'Notified customer about invoice #%1.', $invoice->getId() ) )->setIsCustomerNotified( true )->save();
}

// Save Transaction Response
$this->createTransaction( $order, $paygate_data );
$order->setState( $status )->setStatus( $status )->save();

exit;
break;
case 2:
Expand Down
Loading

0 comments on commit 07003ad

Please sign in to comment.