Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
[RELEASE] First release of new PayEx Checkin module for Magento 2
Browse files Browse the repository at this point in the history
  • Loading branch information
samtroll committed May 14, 2019
1 parent 4057576 commit 76a4f47
Show file tree
Hide file tree
Showing 29 changed files with 1,619 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor
197 changes: 197 additions & 0 deletions Block/Widget/CheckinWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

namespace PayEx\Checkin\Block\Widget;

use Magento\Customer\Model\ResourceModel\AddressRepository;
use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Event\Manager as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\ScopeInterface;
use Magento\Widget\Block\BlockInterface;
use PayEx\Api\Service\Consumer\Resource\ConsumerNationalIdentifier;
use PayEx\Api\Service\Consumer\Resource\Request\InitiateConsumerSession as ConsumerSessionResource;
use PayEx\Api\Service\Data\RequestInterface;
use PayEx\Api\Service\Data\ResponseInterface;
use PayEx\Api\Service\Resource\Data\ResponseInterface as ResponseResourceInterface;
use PayEx\Checkin\Helper\Config;
use PayEx\Checkin\Model\ConsumerSession as PayExConsumerSession;
use PayEx\Client\Exception\ServiceException;
use PayEx\Client\Model\Service;
use PayEx\Core\Logger\Logger;

class CheckinWidget extends Template implements BlockInterface
{
protected $_template = 'PayEx_Checkin::widget/checkin-widget.phtml';

/**
* @var PayExConsumerSession $consumerSession
*/
protected $consumerSession;

/**
* @var CustomerSession|object $customerSession
*/
protected $customerSession;

/**
* @var CustomerRepository $customerRepository
*/
protected $customerRepository;

/**
* @var AddressRepository $addressRepository
*/
protected $addressRepository;

/**
* @var EventManager $eventManager
*/
protected $eventManager;

/**
* @var Logger $logger
*/
protected $logger;

/**
* @var Service $service
*/
protected $service;

/**
* @var Config $config
*/
protected $config;

public function __construct(
Template\Context $context,
PayExConsumerSession $consumerSession,
CustomerSession $customerSession,
CustomerRepository $customerRepository,
AddressRepository $addressRepository,
EventManager $eventManager,
Service $service,
Logger $logger,
Config $config,
array $data = []
) {
$this->consumerSession = $consumerSession;
$this->customerSession = $customerSession;
$this->customerRepository = $customerRepository;
$this->addressRepository = $addressRepository;
$this->eventManager = $eventManager;
$this->service = $service;
$this->logger = $logger;
$this->config = $config;
parent::__construct($context, $data);
}

public function isActive()
{
return $this->config->isActive();
}

public function isConsumerIdentified()
{
return $this->consumerSession->isIdentified();
}

/**
* Get default country
*
* @return string
*/
public function getDefaultCountry()
{
$configPath = 'general/country/default';
$scope = ScopeInterface::SCOPE_STORE;
return $this->_scopeConfig->getValue($configPath, $scope);
}

/**
* @param ConsumerSessionResource $consumerSessionData
* @return ResponseInterface|false
* @throws \PayEx\Client\Exception\ServiceException
*/
public function initiateConsumerSession(ConsumerSessionResource $consumerSessionData)
{
/** @var RequestInterface $consumerSession */
$consumerSession = $this->service->init('Consumer', 'InitiateConsumerSession', $consumerSessionData);

/** @var ResponseInterface $response */
$response = $consumerSession->send();

if (!($response instanceof ResponseInterface) || !($response->getResponseResource() instanceof ResponseResourceInterface)) {
$this->logger->error(sprintf('Invalid InitiateConsumerSession response: %s', print_r($response, true)));
return false;
}

$this->consumerSession->isInitiated(true);

return $response;
}

/**
* @return string|false
*/
public function getCheckinScript()
{
if ($this->consumerSession->getViewOperation()) {
$viewOperation = $this->consumerSession->getViewOperation();
return $viewOperation['href'];
}

$consumerSessionData = new ConsumerSessionResource();
$consumerSessionData->setConsumerCountryCode($this->getDefaultCountry());

if ($this->customerSession->isLoggedIn()) {
$customerId = $this->customerSession->getCustomerId();
$customer = $this->customerRepository->getById($customerId);
$email = $customer->getEmail();
try {
$billingAddress = $this->addressRepository->getById($customer->getDefaultBilling());
} catch (LocalizedException $e) {
$this->logger->error($e->getMessage());
return false;
}
$msisdn = $billingAddress->getTelephone();
$countryCode = $billingAddress->getCountryId();
$this->eventManager->dispatch('payex_checkin_before_initiate_consumer_session');

$nationalIdentifierData = new ConsumerNationalIdentifier();
$nationalIdentifierData->setCountryCode($countryCode);

$consumerSessionData->setMsisdn($msisdn)
->setEmail($email)
->setConsumerCountryCode($countryCode)
->setNationalIdentifier($nationalIdentifierData);
}

/** @var ResponseInterface|false $response */
try {
$response = $this->initiateConsumerSession($consumerSessionData);
} catch (ServiceException $e) {
$this->logger->error($e->getMessage());
return false;
}

if ($response instanceof ResponseInterface) {
$viewOperation = $response->getOperationByRel('view-consumer-identification');
$this->consumerSession->setViewOperation($viewOperation);
return $viewOperation['href'];
}

return false;
}

protected function _toHtml()
{
if (!$this->isActive()) {
return '';
}

return parent::_toHtml();
}
}
145 changes: 145 additions & 0 deletions Controller/Index/OnBillingDetailsAvailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace PayEx\Checkin\Controller\Index;

use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Event\Manager as EventManager;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\CookieManagerInterface;
use PayEx\Checkin\Helper\Config;
use PayEx\Checkin\Model\ConsumerSession;
use PayEx\Client\Model\Service;
use PayEx\Core\Logger\Logger;

class OnBillingDetailsAvailable extends Action
{
const COOKIE_DURATION = 86400; // One day (86400 seconds)

/** @var PageFactory $resultPageFactory */
protected $resultPageFactory;

/** @var JsonFactory $resultJsonFactory */
protected $resultJsonFactory;

/** @var Session|object $customerSession */
protected $customerSession;

/** @var ConsumerSession $consumerSession */
protected $consumerSession;

/** @var EventManager $eventManager */
protected $eventManager;

/** @var Logger $logger */
protected $logger;

/** @var Service $service */
protected $service;

/** @var Config $config */
protected $config;

/** @var CookieManagerInterface $cookieManager */
protected $cookieManager;

/** @var CookieMetadataFactory $cookieMetadataFactory */
protected $cookieMetadataFactory;

/**
* OnConsumerIdentifiedController constructor.
* @param Context $context
* @param PageFactory $resultPageFactory
* @param JsonFactory $resultJsonFactory
* @param Session $customerSession
* @param ConsumerSession $consumerSession
* @param EventManager $eventManager
* @param Config $config
* @param Logger $logger
* @param Service $service
* @param CookieManagerInterface $cookieManager
* @param CookieMetadataFactory $cookieMetadataFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
JsonFactory $resultJsonFactory,
Session $customerSession,
ConsumerSession $consumerSession,
EventManager $eventManager,
Config $config,
Logger $logger,
Service $service,
CookieManagerInterface $cookieManager,
CookieMetadataFactory $cookieMetadataFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->customerSession = $customerSession;
$this->consumerSession = $consumerSession;
$this->eventManager = $eventManager;
$this->logger = $logger;
$this->service = $service;
$this->config = $config;
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
}

/**
* @return ResponseInterface|Json|ResultInterface
* @throws \Exception
*/
public function execute()
{
if (!$this->config->isActive()) {
$result = $this->resultJsonFactory->create();
$result->setData(['data' => 'Forbidden! Module is not enabled']);
$result->setHttpResponseCode(403);

return $result;
}

/** @var HttpRequest $request */
$request = $this->getRequest();
$requestBody = json_decode($request->getContent());

$this->eventManager->dispatch('payex_checkin_before_billing_details_available', (array) $requestBody);

try {
$url = $requestBody->url;

/** @var \PayEx\Api\Service\Data\RequestInterface $session */
$session = $this->service->init('Consumer', 'GetBillingDetails');
$session->setRequestMethod('GET');
$session->setRequestEndpoint($url);
$response = $session->send()->getResponseData();

$metadata = $this->cookieMetadataFactory
->createPublicCookieMetadata()
->setDuration(self::COOKIE_DURATION)
->setPath($this->consumerSession->getCookiePath())
->setDomain($this->consumerSession->getCookieDomain());

$this->cookieManager
->setPublicCookie('billingDetails', json_encode($response), $metadata);

$result = $this->resultJsonFactory->create();
$result->setData(['data' => $response]);
$result->setHttpResponseCode(200);
} catch (\Exception $exception) {
$this->logger->Error($exception->getMessage());
$result = $this->resultJsonFactory->create();
$result->setHttpResponseCode(400);
}

return $result;
}
}
Loading

0 comments on commit 76a4f47

Please sign in to comment.