Skip to content

Commit

Permalink
Merge pull request #700 from buckaroo-it/BP-2434-create-a-service-for…
Browse files Browse the repository at this point in the history
…-checking-type-of-payment-method

BP-2434 create a service for checking type of payment method
  • Loading branch information
LucianTuriacArnia authored Apr 12, 2023
2 parents 2e6549c + b4555b0 commit 7d09134
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 57 deletions.
17 changes: 10 additions & 7 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
namespace Buckaroo\Magento2\Helper;

use Buckaroo\Magento2\Model\Config\Source\Business;
use Buckaroo\Magento2\Service\CheckPaymentType;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
Expand Down Expand Up @@ -112,6 +113,11 @@ class Data extends AbstractHelper
private $state;
private $customerSession;

/**
* @var CheckPaymentType
*/
public $checkPaymentType;

/**
* @param Context $context
* @param Account $configProviderAccount
Expand All @@ -132,7 +138,8 @@ public function __construct(
\Magento\Config\Model\Config\ScopeDefiner $scopeDefiner,
\Magento\Framework\Serialize\Serializer\Json $json,
State $state,
Session $customerSession
Session $customerSession,
CheckPaymentType $checkPaymentType
) {
parent::__construct($context);

Expand All @@ -148,6 +155,7 @@ public function __construct(
$this->json = $json;
$this->state = $state;
$this->customerSession = $customerSession;
$this->checkPaymentType = $checkPaymentType;
}

/**
Expand Down Expand Up @@ -476,7 +484,7 @@ public function getPaymentMethodsList()
*/
public function checkCustomerGroup(string $paymentMethod, bool $forceB2C = false): bool
{
if ($this->isBuckarooMethod($paymentMethod)) {
if ($this->checkPaymentType->isBuckarooMethod($paymentMethod)) {
$paymentMethodCode = $this->getBuckarooMethod($paymentMethod);
$configProvider = $this->configProviderMethodFactory->get($paymentMethodCode);
$configCustomerGroup = $configProvider->getSpecificCustomerGroup();
Expand Down Expand Up @@ -547,11 +555,6 @@ private function checkCustomerGroupFrontArea(array $configCustomerGroupArr): boo
return true;
}

public function isBuckarooMethod(string $paymentMethod): bool
{
return strpos($paymentMethod, 'buckaroo_magento2_') !== false;
}

public function getBuckarooMethod(string $paymentMethod): string
{
return strtolower(str_replace('buckaroo_magento2_', '', $paymentMethod));
Expand Down
29 changes: 14 additions & 15 deletions Observer/HtmlTransactionIdObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@

namespace Buckaroo\Magento2\Observer;

use Buckaroo\Magento2\Service\CheckPaymentType;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;

class HtmlTransactionIdObserver implements ObserverInterface
{
/**
* @var CheckPaymentType
*/
private $checkPaymentType;

/**
* @param CheckPaymentType $checkPaymentType
*/
public function __construct(CheckPaymentType $checkPaymentType)
{
$this->checkPaymentType = $checkPaymentType;
}
/**
* Update txn_id to a link for the plaza transaction
*
Expand All @@ -40,7 +53,7 @@ public function execute(Observer $observer)
$order = $transaction->getOrder();
$txnIdArray = explode("-", $transaction->getTxnId());
$txnId = reset($txnIdArray);
if ($this->isBuckarooPayment($order->getPayment()) && $txnId !== false) {
if ($this->checkPaymentType->isBuckarooPayment($order->getPayment()) && $txnId !== false) {
$transaction->setData(
'html_txn_id',
sprintf(
Expand All @@ -52,18 +65,4 @@ public function execute(Observer $observer)
);
}
}

/**
* Is one of our payment methods
*
* @param OrderPaymentInterface|null $payment
* @return boolean
*/
public function isBuckarooPayment($payment)
{
if (!$payment instanceof OrderPaymentInterface) {
return false;
}
return strpos($payment->getMethod(), 'buckaroo_magento2') !== false;
}
}
48 changes: 13 additions & 35 deletions Plugin/Onepage/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Buckaroo\Magento2\Plugin\Onepage;

use Buckaroo\Magento2\Service\CheckPaymentType;
use Magento\Sales\Model\Order;
use Buckaroo\Magento2\Logging\Log;
use Magento\Framework\App\Action\Context;
Expand All @@ -42,16 +43,24 @@ class Success
*/
protected $logger;

/**
* @var CheckPaymentType
*/
protected $checkPaymentType;

/**
* @param Context $context
* @param Log $logger
* @param CheckPaymentType $checkPaymentType
*/
public function __construct(
Context $context,
Log $logger
Log $logger,
CheckPaymentType $checkPaymentType
) {
$this->resultRedirectFactory = $context->getResultRedirectFactory();
$this->logger = $logger;
$this->checkPaymentType = $checkPaymentType;
}

/**
Expand All @@ -66,15 +75,15 @@ public function aroundExecute(\Magento\Checkout\Controller\Onepage\Success $chec
$this->logger->addDebug(
var_export([
$order->getStatus() === BuckarooDataHelper::M2_ORDER_STATE_PENDING,
$this->paymentInTransit($payment),
$this->checkPaymentType->isPaymentInTransit($payment),
$order->getStatus() === Order::STATE_CANCELED
], true)
);

if ($this->isBuckarooPayment($payment) &&
if ($this->checkPaymentType->isBuckarooPayment($payment) &&
(
($order->getStatus() === BuckarooDataHelper::M2_ORDER_STATE_PENDING
&& $this->paymentInTransit($payment)
&& $this->checkPaymentType->isPaymentInTransit($payment)
)
|| $order->getStatus() === Order::STATE_CANCELED
)
Expand All @@ -83,35 +92,4 @@ public function aroundExecute(\Magento\Checkout\Controller\Onepage\Success $chec
}
return $proceed();
}

/**
* Is one of our payment methods
*
* @param OrderPaymentInterface|null $payment
*
* @return boolean
*/
public function isBuckarooPayment($payment)
{
if (!$payment instanceof OrderPaymentInterface) {
return false;
}
return strpos($payment->getMethod(), 'buckaroo_magento2') !== false;
}

/**
* Check if user is on the payment provider page
*
* @param OrderPaymentInterface|null $payment
*
* @return boolean
*/
protected function paymentInTransit(OrderPaymentInterface $payment = null)
{
if ($payment === null) {
return false;
}

return $payment->getAdditionalInformation(BuckarooAdapter::BUCKAROO_PAYMENT_IN_TRANSIT) === true;
}
}
49 changes: 49 additions & 0 deletions Service/CheckPaymentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Buckaroo\Magento2\Service;

use Buckaroo\Magento2\Model\Method\BuckarooAdapter;
use Magento\Sales\Api\Data\OrderPaymentInterface;

class CheckPaymentType
{
/**
* Is one of Buckaroo payment methods by string
*
* @param string $paymentMethod
* @return boolean
*/
public function isBuckarooMethod(string $paymentMethod): bool
{
return strpos($paymentMethod, 'buckaroo_magento2_') !== false;
}

/**
* Is one of Buckaroo payment methods by PaymentMethod
*
* @param OrderPaymentInterface|null $payment
* @return boolean
*/
public function isBuckarooPayment(?OrderPaymentInterface $payment): bool
{
if (!$payment instanceof OrderPaymentInterface) {
return false;
}
return strpos($payment->getMethod(), 'buckaroo_magento2') !== false;
}

/**
* Check if user is on the payment provider page
*
* @param OrderPaymentInterface|null $payment
* @return boolean
*/
public function isPaymentInTransit(?OrderPaymentInterface $payment): bool
{
if (!$payment instanceof OrderPaymentInterface) {
return false;
}

return $payment->getAdditionalInformation(BuckarooAdapter::BUCKAROO_PAYMENT_IN_TRANSIT) == true;
}
}

0 comments on commit 7d09134

Please sign in to comment.