Skip to content

changes for graphql #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 26 additions & 3 deletions app/code/LiqpayMagento/LiqPay/Block/SubmitForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace LiqpayMagento\LiqPay\Block;

use Exception;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Model\Order;
use LiqpayMagento\LiqPay\Sdk\LiqPay;
Expand All @@ -20,6 +21,8 @@ class SubmitForm extends Template
{
protected $_order = null;

protected ?string $language;

/* @var $_liqPay LiqPay */
protected $_liqPay;

Expand All @@ -28,9 +31,9 @@ class SubmitForm extends Template

public function __construct(
Template\Context $context,
LiqPay $liqPay,
Helper $helper,
array $data = []
LiqPay $liqPay,
Helper $helper,
array $data = []
)
{
parent::__construct($context, $data);
Expand Down Expand Up @@ -62,13 +65,33 @@ public function setOrder(Order $order)
protected function _toHtml()
{
$order = $this->getOrder();
$language = $this->getLanguage();

$html = $this->_liqPay->cnb_form(array(
'action' => 'pay',
'amount' => $order->getGrandTotal(),
'currency' => $order->getOrderCurrencyCode(),
'description' => $this->_helper->getLiqPayDescription($order),
'order_id' => $order->getIncrementId(),
'language' => $language,
'result_url'=>$this->_helper->getServerWebsiteUrl() . '/liqpay/checkout/thnks?language='.$language.'&orderNumber=' . $order->getIncrementId(),
'server_url'=>$this->_helper->getServerWebsiteUrl() . '/V1/liqpay/callback'
));
return $html;
}

public function getLanguage(): string
{
return $this->language ?? 'en';
}

public function setLanguage(string $language)
{
$this->language = $language;
}

public function getCacheLifetime()
{
return null;
}
}
39 changes: 33 additions & 6 deletions app/code/LiqpayMagento/LiqPay/Controller/Checkout/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@

namespace LiqpayMagento\LiqPay\Controller\Checkout;

use Exception;
use LiqpayMagento\LiqPay\Block\SubmitForm;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\View\LayoutFactory;
use Magento\Checkout\Model\Session as CheckoutSession;
use LiqpayMagento\LiqPay\Helper\Data as Helper;
Expand All @@ -36,17 +41,21 @@ class Form extends Action
*/
protected $_layoutFactory;

private $_localeResolver;

public function __construct(
Context $context,
CheckoutSession $checkoutSession,
Helper $helper,
LayoutFactory $layoutFactory
Context $context,
CheckoutSession $checkoutSession,
Helper $helper,
LayoutFactory $layoutFactory,
ResolverInterface $resolverLocale
)
{
parent::__construct($context);
$this->_checkoutSession = $checkoutSession;
$this->_helper = $helper;
$this->_layoutFactory = $layoutFactory;
$this->_localeResolver = $resolverLocale;
}

/**
Expand All @@ -67,11 +76,14 @@ public function execute()
}
if ($this->_helper->checkOrderIsLiqPayPayment($order)) {
/* @var $formBlock \LiqpayMagento\LiqPay\Block\SubmitForm */
$formBlock = $this->_layoutFactory->create()->createBlock('LiqpayMagento\LiqPay\Block\SubmitForm');
$formBlock = $this->_layoutFactory->create()->createBlock(SubmitForm::class);
$formBlock->setOrder($order);
$formBlock->setLanguage($this->getLanguageCode());
$content = $formBlock->toHtml();

$data = [
'status' => 'success',
'content' => $formBlock->toHtml(),
'content' => $content,
];
} else {
throw new \Exception(__('Order payment method is not a LiqPay payment method'));
Expand Down Expand Up @@ -101,4 +113,19 @@ protected function getCheckoutSession()
{
return $this->_checkoutSession;
}

/**
* @return string
*/
private function getLanguageCode(): string
{
$locale = $this->_localeResolver->getLocale();
$code = 'en';
if ($locale === 'ru_RU') {
$code = 'ru';
} elseif ($locale === 'uk_UA') {
$code = 'ua';
}
return $code;
}
}
108 changes: 108 additions & 0 deletions app/code/LiqpayMagento/LiqPay/Controller/Checkout/Thnks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* LiqPay Extension for Magento 2
*
* @author Volodymyr Konstanchuk http://konstanchuk.com
* @copyright Copyright (c) 2017 The authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

namespace LiqpayMagento\LiqPay\Controller\Checkout;

use Exception;
use LiqpayMagento\LiqPay\Helper\Data as Helper;
use LiqpayMagento\LiqPay\Model\LiqPayCallback;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\View\LayoutFactory;

class Thnks extends Action
{
/**
* @var CheckoutSession
*/
protected $_checkoutSession;

/**
* @var Helper
*/
protected $_helper;

/**
* @var LayoutFactory
*/
protected $_layoutFactory;

/**
* @var LiqPayCallback
*/
private LiqPayCallback $callback;

public function __construct(
Context $context,
CheckoutSession $checkoutSession,
Helper $helper,
LayoutFactory $layoutFactory,
LiqPayCallback $callback
)
{
parent::__construct($context);
$this->_checkoutSession = $checkoutSession;
$this->_helper = $helper;
$this->_layoutFactory = $layoutFactory;
$this->callback = $callback;
}

/**
* Dispatch request
*
* @return ResultInterface|ResponseInterface
* @throws NotFoundException
*/
public function execute()
{
$frontendUrl = $this->_helper->getFrontendWebsiteUrl();
// if ($this->getRequest()->getParam('language', false)) {
// $frontendUrl = $frontendUrl . '/' . $this->getRequest()->getParam('language') . '/';
// }

try {
if (!$this->getRequest()->isPost()) {
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($frontendUrl);
return $resultRedirect;
}

$this->callback->callback();
} catch (Exception $e) {

}

if ($this->getRequest()->getParam('orderNumber', false)) {
$number = $this->getRequest()->getParam('orderNumber');
$this->_checkoutSession->setLiqPayLastOrder($number);
$frontendUrl = $frontendUrl . \Dragonfly\BootstrapCheckout\Api\BoostrapCheckoutInterface::URL_KEY_ORDER_SUCCESS;
}

$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($frontendUrl);
return $resultRedirect;
}


/**
* Return checkout session object
*
* @return CheckoutSession
*/
protected function getCheckoutSession()
{
return $this->_checkoutSession;
}
}
46 changes: 39 additions & 7 deletions app/code/LiqpayMagento/LiqPay/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Data extends AbstractHelper
const XML_PATH_TEST_ORDER_SURFIX = 'payment/liqpaymagento_liqpay/sandbox_order_surfix';
const XML_PATH_DESCRIPTION = 'payment/liqpaymagento_liqpay/description';
const XML_PATH_CALLBACK_SECURITY_CHECK = 'payment/liqpaymagento_liqpay/security_check';
const XML_PATH_FRONTEND_WEBSITE_URL = 'payment/liqpaymagento_liqpay/frontend_website_url';
const XML_PATH_SERVER_WEBSITE_URL = 'payment/liqpaymagento_liqpay/server_website_url';

/**
* @var PaymentHelper
*/
Expand Down Expand Up @@ -73,34 +76,43 @@ public function isSecurityCheck()

public function getPublicKey()
{
return trim($this->scopeConfig->getValue(
$value = trim($this->scopeConfig->getValue(
self::XML_PATH_PUBLIC_KEY,
ScopeInterface::SCOPE_STORE
));

return !empty($value) ? trim($value) : '';
}

public function getPrivateKey()
{
return trim($this->scopeConfig->getValue(
$value = $this->scopeConfig->getValue(
self::XML_PATH_PRIVATE_KEY,
ScopeInterface::SCOPE_STORE
));
);

return !empty($value) ? trim($value) : '';
}

public function getTestOrderSurfix()
{
return trim($this->scopeConfig->getValue(
$value = $this->scopeConfig->getValue(
self::XML_PATH_TEST_ORDER_SURFIX,
ScopeInterface::SCOPE_STORE
));
);

return !empty($value) ? trim($value) : '';
}

public function getLiqPayDescription(\Magento\Sales\Api\Data\OrderInterface $order = null)
{
$description = trim($this->scopeConfig->getValue(
$value = $this->scopeConfig->getValue(
self::XML_PATH_DESCRIPTION,
ScopeInterface::SCOPE_STORE
));
);

$description = !empty($value) ? trim($value) : '';

$params = [
'{order_id}' => $order->getIncrementId(),
];
Expand Down Expand Up @@ -135,4 +147,24 @@ public function getLogger()
{
return $this->_logger;
}

public function getFrontendWebsiteUrl()
{
$value = $this->scopeConfig->getValue(
self::XML_PATH_FRONTEND_WEBSITE_URL,
ScopeInterface::SCOPE_STORE
);

return !empty($value) ? trim($value) : '';
}

public function getServerWebsiteUrl()
{
$value = $this->scopeConfig->getValue(
self::XML_PATH_SERVER_WEBSITE_URL,
ScopeInterface::SCOPE_STORE
);

return !empty($value) ? trim($value) : '';
}
}
29 changes: 18 additions & 11 deletions app/code/LiqpayMagento/LiqPay/Model/LiqPayCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function callback()
$data = $post['data'];
$receivedSignature = $post['signature'];

//@todo - can add logic wit liqpay data
$decodedData = $this->_liqPay->getDecodedData($data);
$orderId = $decodedData['order_id'] ?? null;
$receivedPublicKey = $decodedData['public_key'] ?? null;
Expand Down Expand Up @@ -172,6 +173,9 @@ public function callback()
if ($transactionId) {
$historyMessage[] = __('LiqPay transaction id %1.', $transactionId);
}

$historyMessage[] = '<br>'.json_encode($decodedData);

if (count($historyMessage)) {
$order->addStatusHistoryComment(implode(' ', $historyMessage))
->setIsCustomerNotified(true);
Expand All @@ -190,17 +194,20 @@ public function callback()

protected function getRealOrder($status, $orderId)
{
// if ($status == LiqPay::STATUS_SANDBOX) {
// $testOrderSurfix = $this->_helper->getTestOrderSurfix();
// if (!empty($testOrderSurfix)) {
// $testOrderSurfix = LiqPay::TEST_MODE_SURFIX_DELIM . $testOrderSurfix;
// if (strlen($testOrderSurfix) < strlen($orderId)
// && substr($orderId, -strlen($testOrderSurfix)) == $testOrderSurfix
// ) {
// $orderId = substr($orderId, 0, strlen($orderId) - strlen($testOrderSurfix));
// }
// }
// }
if ($status == LiqPay::STATUS_SANDBOX) {
$testOrderSurfix = $this->_helper->getTestOrderSurfix();
if (!empty($testOrderSurfix)) {
$testOrderSurfix = LiqPay::TEST_MODE_SURFIX_DELIM . $testOrderSurfix;
if (strlen($testOrderSurfix) < strlen($orderId)
&& substr($orderId, -strlen($testOrderSurfix)) == $testOrderSurfix
) {
$orderId = substr($orderId, 0, strlen($orderId) - strlen($testOrderSurfix));
}
}
}

$orderId = str_replace('-test', '', $orderId);

return $this->_order->loadByIncrementId($orderId);
}
}
7 changes: 6 additions & 1 deletion app/code/LiqpayMagento/LiqPay/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
}
return parent::isAvailable($quote);
}
}

public function getOrderRedirectUrl($order): string
{
return '/liqpay_redirect?id=' . $order->getId();
}
}
Loading