Skip to content

Commit

Permalink
Feature/v 2 payments (#69)
Browse files Browse the repository at this point in the history
*Added V2 payment handling
  • Loading branch information
andrii-onufriichuk authored Jan 24, 2024
1 parent 467266a commit 1a64d2c
Show file tree
Hide file tree
Showing 45 changed files with 1,406 additions and 933 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: composer install --no-ansi --no-interaction --no-progress --no-scripts

- name: Run PHPStan
run: ./vendor/bin/phpstan
run: ./vendor/bin/phpstan -v

php-cs:
name: PHP CS
Expand Down
8 changes: 8 additions & 0 deletions Api/Data/ProcessOrderResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public function getCustomerMessage(): ?string;
* @return void
*/
public function setCustomerMessage(string $customerMessage): void;

/**
* @param string|null $messageGroup
* @return void
*/
public function setSessionMessage(
?string $messageGroup = null
): void;
}
66 changes: 66 additions & 0 deletions Block/Order/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Block\Order;

use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template\Context as TemplateContext;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address\Renderer as AddressRenderer;

class Info extends \Magento\Sales\Block\Order\Info
{
/** @var Order */
private $order;

/** @var Session */
private $session;

/**
* @param TemplateContext $context
* @param Registry $registry
* @param PaymentHelper $paymentHelper
* @param AddressRenderer $addressRenderer
* @param Session $session
* @param Order $order
* @param array $data
*/
public function __construct(
TemplateContext $context,
Registry $registry,
PaymentHelper $paymentHelper,
AddressRenderer $addressRenderer,
Session $session,
Order $order,
array $data = []
) {
$this->session = $session;
$this->order = $order;
parent::__construct($context, $registry, $paymentHelper, $addressRenderer, $data);
}

/**
* @inheritDoc
* @return void
* @throws LocalizedException
*/
protected function _prepareLayout(): void
{
$orderId = $this->getOrder()->getRealOrderId();
$payment = $this->getOrder()->getPayment();
if (!$orderId) {
$orderId = $this->session->getLastRealOrderId();
}
if (!$payment) {
$order = $this->order->loadByIncrementId($orderId);
$payment = $order->getPayment();
}
$this->pageConfig->getTitle()->set(__('Order # %1', $orderId));
$infoBlock = $this->paymentHelper->getInfoBlock($payment, $this->getLayout());
$this->setChild('payment_info', $infoBlock);
}
}
66 changes: 66 additions & 0 deletions Block/Order/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Block\Order;

use Klarna\Core\Api\OrderRepositoryInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Http\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Payment\Helper\Data;
use Magento\Sales\Model\Order;

class View extends \Magento\Sales\Block\Order\View
{
/** @var Session */
private $session;

/** @var Order */
private $order;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param Registry $registry
* @param Context $httpContext
* @param Data $paymentHelper
* @param Session $session
* @param Order $order
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Registry $registry,
Context $httpContext,
Data $paymentHelper,
Session $session,
Order $order,
array $data = []
) {
$this->session = $session;
$this->order = $order;
parent::__construct($context, $registry, $httpContext, $paymentHelper, $data);
}

/**
* @inheritDoc
* @return void
* @throws LocalizedException
*/
protected function _prepareLayout(): void
{
$orderId = $this->getOrder()->getRealOrderId();
$payment = $this->getOrder()->getPayment();
if (!$orderId) {
$orderId = $this->session->getLastRealOrderId();
}
if (!$payment) {
$order = $this->order->loadByIncrementId($orderId);
$payment = $order->getPayment();
}
$this->pageConfig->getTitle()->set(__('Order # %1', $orderId));
$infoBlock = $this->_paymentHelper->getInfoBlock($payment, $this->getLayout());
$this->setChild('payment_info', $infoBlock);
}
}
61 changes: 30 additions & 31 deletions Controller/CardPayments/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Rvvup\Payments\Gateway\Method;
use Rvvup\Payments\Model\SdkProxy;
use Rvvup\Sdk\Exceptions\ApiError;

Expand All @@ -32,67 +34,64 @@ class Confirm implements HttpPostActionInterface, CsrfAwareActionInterface
*/
private $orderRepository;

/** @var Validator */
/** @var Validator */
private $formKeyValidator;

/**
* @var SessionManagerInterface
*/
private $checkoutSession;

/**
* @param ResultFactory $resultFactory
* @param SdkProxy $sdkProxy
* @param RequestInterface $request
* @param OrderRepositoryInterface $orderRepository
* @param Validator $formKeyValidator
* @param SessionManagerInterface $checkoutSession $
*/
public function __construct(
ResultFactory $resultFactory,
SdkProxy $sdkProxy,
RequestInterface $request,
OrderRepositoryInterface $orderRepository,
Validator $formKeyValidator
Validator $formKeyValidator,
SessionManagerInterface $checkoutSession
) {
$this->resultFactory = $resultFactory;
$this->sdkProxy = $sdkProxy;
$this->request = $request;
$this->orderRepository = $orderRepository;
$this->formKeyValidator = $formKeyValidator;
$this->checkoutSession = $checkoutSession;
}

public function execute()
{
$response = $this->resultFactory->create($this->resultFactory::TYPE_JSON);

try {
$orderId = $this->request->getParam('order_id', false);
$order = $this->orderRepository->get((int)$orderId);

if ($order) {
$rvvupOrderId = (string) $order->getPayment()->getAdditionalInformation('rvvup_order_id');
$rvvupOrder = $this->sdkProxy->getOrder($rvvupOrderId);
$rvvupPaymentId = $rvvupOrder['payments'][0]['id'];

$authorizationResponse = $this->request->getParam('auth', false);
$threeDSecureResponse = $this->request->getParam('three_d', null);

$this->sdkProxy->confirmCardAuthorization(
$rvvupPaymentId,
$rvvupOrderId,
$authorizationResponse,
$threeDSecureResponse
);

$response->setData([
'success' => true,
]);
} else {
$response->setData([
'success' => false,
'error_message' => 'Order not found during card authorization',
'retryable' => false,
]);
}
$quote = $this->checkoutSession->getQuote();
$rvvupOrderId = (string)$quote->getPayment()->getAdditionalInformation('transaction_id');
$rvvupPaymentId = $quote->getPayment()->getAdditionalInformation(Method::PAYMENT_ID);

$authorizationResponse = $this->request->getParam('auth', false);
$threeDSecureResponse = $this->request->getParam('three_d');

$this->sdkProxy->confirmCardAuthorization(
$rvvupPaymentId,
$rvvupOrderId,
$authorizationResponse,
$threeDSecureResponse
);

$response->setData([
'success' => true,
]);

$response->setHttpResponseCode(200);
return $response;
} catch (\Exception $exception) {

$data = [
'success' => false,
'error_message' => $exception->getMessage()
Expand Down
19 changes: 9 additions & 10 deletions Controller/Express/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Rvvup\Payments\Gateway\Method;
use Rvvup\Payments\Model\SdkProxy;

class Cancel implements HttpGetActionInterface
Expand Down Expand Up @@ -39,16 +40,14 @@ public function __construct(
public function execute()
{
$payment = $this->checkoutSession->getQuote()->getPayment();
if ($payment->getAdditionalInformation('is_rvvup_express_payment')) {
$rvvupOrderId = $payment->getAdditionalInformation('rvvup_order_id');
$order = $this->sdkProxy->getOrder($rvvupOrderId);
if ($order && isset($order['payments'])) {
$paymentId = $order['payments'][0]['id'];
$this->sdkProxy->cancelPayment($paymentId, $rvvupOrderId);
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData(['success' => true]);
return $result;
}
if ($payment->getAdditionalInformation(Method::EXPRESS_PAYMENT_KEY)) {
$rvvupOrderId = $payment->getAdditionalInformation(Method::ORDER_ID);
$paymentId = $payment->getAdditionalInformation(Method::PAYMENT_ID);

$this->sdkProxy->cancelPayment($paymentId, $rvvupOrderId);
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$result->setData(['success' => true]);
return $result;
}

$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
Expand Down
Loading

0 comments on commit 1a64d2c

Please sign in to comment.