Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions app/code/Payfast/Payfast/Controller/Notify/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

/**
* Copyright (c) 2008 PayFast (Pty) Ltd
* You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website
* in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason,
* you may not use this plugin / code or part thereof.
* Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or
* part thereof in any way.
* You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason, you may not use this plugin / code or part thereof.
* Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or part thereof in any way.
*/


Expand All @@ -33,7 +30,6 @@ class Index extends AbstractPayfast implements CsrfAwareActionInterface, HttpPos
*/
public function execute(): ResultInterface
{
$this->_logger->debug('Notify: ' . json_encode($_POST));
$pre = __METHOD__ . " : ";
$this->_logger->debug($pre . 'bof');

Expand All @@ -56,11 +52,6 @@ public function execute(): ResultInterface
flush();
}

$passPhrase = $this->_config->getValue('passphrase');
if (empty($passPhrase)) {
$passPhrase = null;
}

//// Get data sent by PayFast
if (!$pfError) {
// Posted variables from ITN
Expand All @@ -80,7 +71,8 @@ public function execute(): ResultInterface
if (!pfValidSignature(
$pfData,
$pfParamString,
$passPhrase
$this->getConfigData('passphrase'),
$this->getConfigData('server')
)) {
$pfError = true;
$pfErrMsg = PF_ERR_INVALID_SIGNATURE;
Expand Down Expand Up @@ -110,7 +102,7 @@ public function execute(): ResultInterface

// Check order is in "pending payment" state
if ($this->_order->getState() !== Order::STATE_PENDING_PAYMENT) {
// $pfError = true;
$pfError = true;
$pfErrMsg = PF_ERR_ORDER_PROCESSED;
}
}
Expand All @@ -130,7 +122,7 @@ public function execute(): ResultInterface
pflog('Check status and update order');

// Successful
if ($pfData[Info::PAYMENT_STATUS] === "COMPLETE") {
if ($pfData[Info::PAYMENT_STATUS] == "COMPLETE") {
$this->setPaymentAdditionalInformation($pfData);
// Save invoice
$this->saveInvoice();
Expand Down Expand Up @@ -169,18 +161,7 @@ protected function saveInvoice()

/** @var \Magento\Sales\Model\Order $order */
$order = $invoice->getOrder();
$status = $this->getConfigData('successful_order_status');
$state = $this->getConfigData('successful_order_state');
if (!$status || $status === '') {
$status = Order::STATE_PROCESSING;
}
if (!$state || $state === '') {
$state = Order::STATE_PROCESSING;
}
$order->setIsInProcess(true);
$order->setState($state);
$order->setStatus($status);
$order->save();
$transaction = $this->transactionFactory->create();
$transaction->addObject($order)->save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ public function build(array $buildSubject)
$pfOutput = '';
// Create output string
foreach ($data as $key => $val) {
if (!empty($val) && $key !== 'currency') {
if (!empty($val)) {
$pfOutput .= $key . '=' . urlencode($val) . '&';
}
}

$passPhrase = $this->config->getValue('passphrase', $order->getStoreId()) ?? '';
if (!empty($passPhrase)) {
$pfOutput .= 'passphrase=' . urlencode($passPhrase);
} else {
$pfOutput = rtrim($pfOutput, '&');
$passPhrase = $this->config->getValue('passphrase', $order->getStoreId());
$pfOutput = substr($pfOutput, 0, -1);

if (!empty($passPhrase) && $this->config->getValue('server', $order->getStoreId()) !== 'test') {
$pfOutput = $pfOutput . "&passphrase=" . urlencode($passPhrase);
}

$this->logger->debug($pre . 'pfOutput for signature is : ' . $pfOutput);
Expand Down
109 changes: 50 additions & 59 deletions app/code/Payfast/Payfast/Model/Payfast.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason, you may not use this plugin / code or part thereof.
* Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or part thereof in any way.
*/

namespace Payfast\Payfast\Model;

require_once dirname(__FILE__) . '/../Model/payfast_common.inc';
Expand Down Expand Up @@ -33,13 +32,14 @@
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* PayFast Module.
*
* @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
/**
* PayFast Module.
*
* @method \Magento\Quote\Api\Data\PaymentMethodExtensionInterface getExtensionAttributes()
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/

class Payfast
{
/**
Expand Down Expand Up @@ -139,18 +139,18 @@ public function __construct(
TransactionRepositoryInterface $transactionRepository,
BuilderInterface $transactionBuilder
) {
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
$this->_checkoutSession = $checkoutSession;
$this->_exception = $exception;
$this->_storeManager = $storeManager;
$this->_urlBuilder = $urlBuilder;
$this->_checkoutSession = $checkoutSession;
$this->_exception = $exception;
$this->transactionRepository = $transactionRepository;
$this->transactionBuilder = $transactionBuilder;
$this->transactionBuilder = $transactionBuilder;

$parameters = ['params' => [$this->_code]];
$parameters = [ 'params' => [ $this->_code ] ];

$this->_config = $configFactory->create($parameters);

if (!defined('PF_DEBUG')) {
if (! defined('PF_DEBUG')) {
define('PF_DEBUG', $this->_config->getValue('debug'));
}
}
Expand Down Expand Up @@ -191,8 +191,8 @@ public function canUseForCurrency($currencyCode)
/**
* Payment action getter compatible with payment model
*
* @return string
* @see \Magento\Sales\Model\Payment::place()
* @return string
*/
public function getConfigPaymentAction()
{
Expand Down Expand Up @@ -233,8 +233,9 @@ protected function getStoreName()
* this where we compile data posted by the form to payfast
*
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getStandardCheckoutFormFields(): array
public function getStandardCheckoutFormFields()
{
$pre = __METHOD__ . ' : ';
// Variable initialization
Expand All @@ -247,16 +248,13 @@ public function getStandardCheckoutFormFields(): array

// If NOT test mode, use normal credentials
if ($this->_config->getValue('server') == 'live') {
$merchantId = $this->_config->getValue('merchant_id');
$merchantId = $this->_config->getValue('merchant_id');
$merchantKey = $this->_config->getValue('merchant_key');
} // If test mode, use generic / specific sandbox credentials
}
// If test mode, use generic sandbox credentials
else {
$merchantId = !empty($this->_config->getValue('merchant_id')) ?
$this->_config->getValue('merchant_id') :
'10000100';
$merchantKey = !empty($this->_config->getValue('merchant_key')) ?
$this->_config->getValue('merchant_key') :
'46f0cd694581a';
$merchantId = '10000100';
$merchantKey = '46f0cd694581a';
}

// Create description
Expand All @@ -269,22 +267,22 @@ public function getStandardCheckoutFormFields(): array
// Construct data for the form
$data = [
// Merchant details
'merchant_id' => $merchantId,
'merchant_key' => $merchantKey,
'return_url' => $this->getPaidSuccessUrl(),
'cancel_url' => $this->getPaidCancelUrl(),
'notify_url' => $this->getPaidNotifyUrl(),
'merchant_id' => $merchantId,
'merchant_key' => $merchantKey,
'return_url' => $this->getPaidSuccessUrl(),
'cancel_url' => $this->getPaidCancelUrl(),
'notify_url' => $this->getPaidNotifyUrl(),

// Buyer details
'name_first' => $order->getData('customer_firstname'),
'name_last' => $order->getData('customer_lastname'),
'name_first' => $order->getData('customer_firstname'),
'name_last' => $order->getData('customer_lastname'),
'email_address' => $order->getData('customer_email'),

// Item details
'm_payment_id' => $order->getRealOrderId(),
'amount' => $this->getTotalAmount($order),
'item_name' => 'Order #' . $order->getRealOrderId(),
//this html special characters breaks signature.
'm_payment_id' => $order->getRealOrderId(),
'amount' => $this->getTotalAmount($order),
'item_name' => $this->_storeManager->getStore()->getName() . ', Order #' . $order->getRealOrderId(),
//this html special characters breaks signature.
//'item_description' => $pfDescription,
];

Expand All @@ -297,21 +295,21 @@ public function getStandardCheckoutFormFields(): array
}

$passPhrase = $this->_config->getValue('passphrase');
if (!empty($passPhrase)) {
$pfOutput .= 'passphrase=' . urlencode($passPhrase);
} else {
$pfOutput = rtrim($pfOutput, '&');
$pfOutput = substr($pfOutput, 0, -1);

if (!empty($passPhrase) && $this->_config->getValue('server') !== 'test') {
$pfOutput = $pfOutput . "&passphrase=" . urlencode($passPhrase);
}

pflog($pre . 'pfOutput for signature is : ' . $pfOutput);

$pfSignature = md5($pfOutput);

$data['signature'] = $pfSignature;
$data['signature'] = $pfSignature;
$data['user_agent'] = 'Magento ' . $this->getAppVersion();
pflog($pre . 'data is :' . print_r($data, true));

return ($data);
return($data);
}

/**
Expand All @@ -322,11 +320,10 @@ public function getStandardCheckoutFormFields(): array
private function getAppVersion(): string
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$version = $objectManager->get('Magento\Framework\App\ProductMetadataInterface')->getVersion();
$version = $objectManager->get('Magento\Framework\App\ProductMetadataInterface')->getVersion();

return (preg_match('([0-9])', $version)) ? $version : '2.0.0';
return (preg_match('([0-9])', $version)) ? $version : '2.0.0';
}

/**
* getTotalAmount
*/
Expand Down Expand Up @@ -354,7 +351,7 @@ public function getNumberFormat($number)
*/
public function getPaidSuccessUrl()
{
return $this->_urlBuilder->getUrl('payfast/redirect/success', ['_secure' => true]);
return $this->_urlBuilder->getUrl('payfast/redirect/success', [ '_secure' => true ]);
}

/**
Expand All @@ -366,11 +363,7 @@ public function getPaidSuccessUrl()
*/
protected function getOrderTransaction($payment)
{
return $this->transactionRepository->getByTransactionType(
Transaction::TYPE_ORDER,
$payment->getId(),
$payment->getOrder()->getId()
);
return $this->transactionRepository->getByTransactionType(Transaction::TYPE_ORDER, $payment->getId(), $payment->getOrder()->getId());
}

/*
Expand All @@ -383,13 +376,12 @@ public function getOrderPlaceRedirectUrl()

return $this->_urlBuilder->getUrl('payfast/redirect');
}

/**
* Checkout redirect URL getter for onepage checkout (hardcode)
*
* @return string
* @see Quote\Payment::getCheckoutRedirectUrl()
* @see \Magento\Checkout\Controller\Onepage::savePaymentAction()
* @see Quote\Payment::getCheckoutRedirectUrl()
* @return string
*/
public function getCheckoutRedirectUrl()
{
Expand All @@ -405,15 +397,14 @@ public function getCheckoutRedirectUrl()
*/
public function getPaidCancelUrl()
{
return $this->_urlBuilder->getUrl('payfast/redirect/cancel', ['_secure' => true]);
return $this->_urlBuilder->getUrl('payfast/redirect/cancel', [ '_secure' => true ]);
}

/**
* getPaidNotifyUrl
*/
public function getPaidNotifyUrl()
{
return $this->_urlBuilder->getUrl('payfast/notify', ['_secure' => true]);
return $this->_urlBuilder->getUrl('payfast/notify', [ '_secure' => true ]);
}

/**
Expand All @@ -423,7 +414,7 @@ public function getPaidNotifyUrl()
*/
public function getPayFastUrl()
{
return ('https://' . $this->getPayfastHost($this->_config->getValue('server')) . '/eng/process');
return('https://' . $this->getPayfastHost($this->_config->getValue('server')) . '/eng/process');
}

/**
Expand All @@ -433,7 +424,7 @@ public function getPayFastUrl()
*/
public function getPayfastHost($serverMode)
{
if (!in_array($serverMode, ['live', 'test'])) {
if (!in_array($serverMode, [ 'live', 'test' ])) {
$pfHost = "payfast.{$serverMode}";
} else {
$pfHost = (($serverMode == 'live') ? 'www' : 'sandbox') . '.payfast.co.za';
Expand Down
Loading