Skip to content

Commit

Permalink
fix for market
Browse files Browse the repository at this point in the history
  • Loading branch information
has-well committed Jun 26, 2019
1 parent 930c306 commit 2c11e0e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 54 deletions.
25 changes: 13 additions & 12 deletions Block/Widget/Redirect.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Fondy\Fondy\Block\Widget;

/**
* Abstract class
*/

use \Magento\Framework\View\Element\Template;


Expand Down Expand Up @@ -64,7 +66,8 @@ public function __construct(
\Magento\Framework\App\Http\Context $httpContext,
\Fondy\Fondy\Model\Fondy $paymentConfig,
array $data = []
) {
)
{
parent::__construct($context, $data);
$this->_checkoutSession = $checkoutSession;
$this->_customerSession = $customerSession;
Expand Down Expand Up @@ -94,35 +97,33 @@ public function getGateUrl()
*/
public function getAmount()
{
$orderId = $this->_checkoutSession->getLastOrderId();
if ($orderId)
{
$orderId = $this->_checkoutSession->getLastOrderId();
if ($orderId) {
$incrementId = $this->_checkoutSession->getLastRealOrderId();
return $this->Config->getAmount($incrementId);
}
return $this->Config->getAmount($incrementId);
}
return null;
}


/**
* Получить данные формы
* Get forma data
*
* @return array|null
*/
public function getPostData()
{
$orderId = $this->_checkoutSession->getLastOrderId();
if ($orderId)
{
if ($orderId) {
$incrementId = $this->_checkoutSession->getLastRealOrderId();
return $this->Config->getPostData($incrementId);
}
return $this->Config->getPostData($incrementId);
}
return null;
}


/**
* Получить callback URL
* Get callback URL
*
* @return array
*/
Expand Down
30 changes: 27 additions & 3 deletions Controller/Url/FondySuccess.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Fondy\Fondy\Controller\Url;

use Magento\Framework\App\Action\Action;
use Magento\Sales\Model\Order;
use Symfony\Component\Config\Definition\Exception\Exception;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;

class FondySuccess extends Action
class FondySuccess extends Action implements CsrfAwareActionInterface
{
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
Expand All @@ -25,12 +29,32 @@ public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory
) {
)
{
$this->resultPageFactory = $resultPageFactory;
$this->jsonResultFactory = $jsonResultFactory;
parent::__construct($context);
}

/**
* @param RequestInterface $request
* @return InvalidRequestException|null
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException
{
return null;
}

/**
* @param RequestInterface $request
* @return bool|null
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}

/**
* Load the page defined
Expand All @@ -57,7 +81,7 @@ public function execute()
$paymentMethod = $this->_objectManager->create($model);
$response = $paymentMethod->processResponse($data);
$result = $this->jsonResultFactory->create();
$result->setData(['Result' => $response]);
$result->setData(['result' => $response]);
return $result;
}

Expand Down
54 changes: 52 additions & 2 deletions Controller/Url/Submit.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

namespace Fondy\Fondy\Controller\Url;

use Magento\Framework\App\Action\Action;
use Magento\Sales\Model\Order;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;

class Submit extends Action
class Submit extends Action implements CsrfAwareActionInterface
{
/** @var \Magento\Framework\View\Result\PageFactory */
public $resultPageFactory;
Expand All @@ -14,12 +18,20 @@ class Submit extends Action
*/
public $fondy;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Fondy\Fondy\Block\Widget\Redirect $fondy_form
) {
)
{
$this->resultPageFactory = $resultPageFactory;
$this->_checkoutSession = $checkoutSession;
$this->fondy = $fondy_form;
parent::__construct($context);
}
Expand All @@ -33,11 +45,33 @@ public function execute()

if (isset($url['response']['checkout_url'])) {
return $this->_redirect->redirect($this->_response, $url['response']['checkout_url']);
} else {
$this->restoreCart();
}

return $page;
}

/**
* @param RequestInterface $request
* @return InvalidRequestException|null
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException
{
return null;
}

/**
* @param RequestInterface $request
* @return bool|null
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}

/**
* @param $data
* @return mixed
Expand Down Expand Up @@ -79,4 +113,20 @@ private function doRequest($data)
}

}

/**
* Restore cart data
*/
public function restoreCart()
{
$lastQuoteId = $this->_checkoutSession->getLastQuoteId();
if ($quote = $this->_objectManager->get('Magento\Quote\Model\Quote')->loadByIdWithoutStore($lastQuoteId)) {
$quote->setIsActive(true)
->setReservedOrderId(null)
->save();
$this->_checkoutSession->setQuoteId($lastQuoteId);
}
$message = __('Payment failed. Please try again.');
$this->messageManager->addError($message);
}
}
39 changes: 10 additions & 29 deletions Model/Fondy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Fondy extends \Magento\Payment\Model\Method\AbstractMethod
/**
* @var bool
*/
protected $_isGateway = true;
protected $_isGateway = false;
/**
* Payment code
*
Expand Down Expand Up @@ -52,6 +52,8 @@ class Fondy extends \Magento\Payment\Model\Method\AbstractMethod

protected $_logger;

protected $_canUseCheckout = true;

public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
Expand Down Expand Up @@ -156,22 +158,6 @@ public function getCurrencyCode($orderId)
}


/**
* Set order state and status
* (Этот метод вызывается при нажатии на кнопку "Place Order")
*
* @param string $paymentAction
* @param \Magento\Framework\DataObject $stateObject
* @return void
*/
public function initialize($paymentAction, $stateObject)
{
$stateObject->setState(Order::STATE_PENDING_PAYMENT);
$stateObject->setStatus(Order::STATE_PENDING_PAYMENT);
$stateObject->setIsNotified(false);
}


/**
* Check whether payment method can be used with selected shipping method
* (Проверка возможности доставки)
Expand Down Expand Up @@ -217,8 +203,6 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)


/**
* Получить адрес платежного шлюза
*
* @return string
*/
public function getGateUrl()
Expand All @@ -228,9 +212,8 @@ public function getGateUrl()


/**
* Получить код проверки целостности данных из конфигурации
*
* @return mixed
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getDataIntegrityCode()
{
Expand All @@ -249,7 +232,9 @@ public function getPostData($orderId)
$postData = array(
'order_id' => $orderId . "#" . time(),
'merchant_id' => $this->getConfigData("FONDY_MERCHANT_ID"),
'amount' => round(number_format($this->getAmount($orderId), 2, '.', '') * 100),
'amount' => round(
number_format($this->getAmount($orderId), 2, '.', '') * 100
),
'order_desc' => __("Pay order №") . $orderId,
'product_id' => 'Fondy',
'server_callback_url' => $this->urlBuilder->getUrl('fondy/url/fondysuccess'),
Expand All @@ -275,9 +260,7 @@ public function getPostData($orderId)
private function checkFondyResponse($response)
{
$this->_logger->debug("checking parameters");
foreach (["order_id",
"order_status",
"signature"] as $param) {
foreach (["order_id", "order_status", "signature"] as $param) {
if (!isset($response[$param])) {
$this->_logger->debug("Pay URL: required field \"{$param}\" is missing");
return false;
Expand All @@ -304,8 +287,6 @@ private function checkFondyResponse($response)
*/
public function processResponse($responseData)
{
if (empty($responseData))
return 'No Data';
if ($responseData['product_id'] == 'FondyDirect') {
$this->_code = 'fondy_direct';
} elseif ($responseData['product_id'] == 'Fondy') {
Expand Down Expand Up @@ -344,7 +325,7 @@ protected function _processOrder(Order $order, $response)
"\$response" => $response
]);
try {
if (round($order->getGrandTotal() * 100) != $response["actual_amount"]) {
if (round($order->getGrandTotal() * 100) != $response["amount"]) {
$this->_logger->debug("_processOrder: amount mismatch, order FAILED");
return false;
}
Expand Down
40 changes: 33 additions & 7 deletions Model/FondyDirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,50 @@
class FondyDirect extends \Magento\Payment\Model\Method\Cc
{
const CODE = 'fondy_direct';

/**
* @var string
*/
protected $_code = self::CODE;

/**
* @var bool
*/
protected $_isGateway = true;
protected $_canCapture = true;
protected $_canAuthorize = true;
protected $_canCapturePartial = true;
protected $_canRefund = true;
protected $_canRefundInvoicePartial = true;

protected $_canRefund = false;
protected $_canRefundInvoicePartial = false;
/**
* @var mixed|null
*/
protected $_minAmount = null;
protected $_maxAmount = null;

/**
* @var array
*/
protected $_debugReplacePrivateDataKeys = ['number', 'exp_month', 'exp_year', 'cvc'];
protected $_logger;

/**
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;

/**
* FondyDirect constructor.
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cloudipsp/magento2",
"description": "Fondy Magento 2 payment module",
"type": "magento2-module",
"version": "1.2.1",
"version": "1.2.3",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down

0 comments on commit 2c11e0e

Please sign in to comment.