diff --git a/Helper/Data.php b/Helper/Data.php index 43741fe4d..bbdc17a32 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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; @@ -112,6 +113,11 @@ class Data extends AbstractHelper private $state; private $customerSession; + /** + * @var CheckPaymentType + */ + public $checkPaymentType; + /** * @param Context $context * @param Account $configProviderAccount @@ -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); @@ -148,6 +155,7 @@ public function __construct( $this->json = $json; $this->state = $state; $this->customerSession = $customerSession; + $this->checkPaymentType = $checkPaymentType; } /** @@ -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(); @@ -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)); diff --git a/Observer/HtmlTransactionIdObserver.php b/Observer/HtmlTransactionIdObserver.php index 2ee48710e..39abc9627 100644 --- a/Observer/HtmlTransactionIdObserver.php +++ b/Observer/HtmlTransactionIdObserver.php @@ -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 * @@ -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( @@ -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; - } } diff --git a/Plugin/Onepage/Success.php b/Plugin/Onepage/Success.php index 2edcfddd4..50bc0d531 100644 --- a/Plugin/Onepage/Success.php +++ b/Plugin/Onepage/Success.php @@ -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; @@ -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; } /** @@ -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 ) @@ -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; - } } \ No newline at end of file diff --git a/Service/CheckPaymentType.php b/Service/CheckPaymentType.php new file mode 100644 index 000000000..26253bb0e --- /dev/null +++ b/Service/CheckPaymentType.php @@ -0,0 +1,49 @@ +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; + } +}