Skip to content

Commit

Permalink
Merge pull request #19 from degica/fix/sessionError
Browse files Browse the repository at this point in the history
Fix null quote issue
  • Loading branch information
Dinwy authored Jul 26, 2024
2 parents 09e865a + bb5a785 commit 3fed78c
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/app/code/Komoju/Payments/Model/WebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,9 @@ public function paymentDeadline()
{
return $this->data()['payment_deadline'];
}

public function getPaymentMethod()
{
return $this->data()['payment_details']['type'];
}
}
50 changes: 50 additions & 0 deletions src/app/code/Komoju/Payments/Observer/RestoreAfterCancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Komoju\Payments\Observer;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Quote\Api\CartRepositoryInterface;
use Psr\Log\LoggerInterface;

class RestoreAfterCancel implements ObserverInterface
{
protected CheckoutSession $checkoutSession;
protected CartRepositoryInterface $quoteRepository;
private LoggerInterface $logger;

public function __construct(
CheckoutSession $checkoutSession,
CartRepositoryInterface $quoteRepository,
LoggerInterface $logger = null
) {
$this->checkoutSession = $checkoutSession;
$this->quoteRepository = $quoteRepository;
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

public function execute(Observer $observer)
{
$lastRealQuoteId = $this->checkoutSession->getLastRealQuoteId();

if (!$lastRealQuoteId) {
$quote = $observer->getEvent()->getQuote();
$quote->setIsActive(true);
return;
}

$order = $observer->getEvent()->getOrder();
$quote = $this->quoteRepository->get($lastRealQuoteId);

if ($order) {
try {
$this->checkoutSession->replaceQuote($quote);
$this->checkoutSession->restoreQuote();
} catch (Exception $e) {
$this->logger->info('RestoreAfterCancel:: Fail to restore');
}
}
}
}
24 changes: 14 additions & 10 deletions src/app/code/Komoju/Payments/Observer/RestoreQuoteFromSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Session\SessionManager;
use Magento\Sales\Model\Order;
use Magento\Quote\Api\CartRepositoryInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -24,34 +23,39 @@
class RestoreQuoteFromSession implements ObserverInterface
{
protected CheckoutSession $checkoutSession;
protected SessionManager $sessionManager;
protected CartRepositoryInterface $quoteRepository;
private LoggerInterface $logger;

public function __construct(
CheckoutSession $checkoutSession,
SessionManager $sessionManager,
CartRepositoryInterface $quoteRepository,
LoggerInterface $logger = null
) {
$this->checkoutSession = $checkoutSession;
$this->sessionManager = $sessionManager;
$this->quoteRepository = $quoteRepository;
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}

public function execute(Observer $observer)
{
$quote = $this->checkoutSession->getQuote();
$lastRealQuoteId = $this->checkoutSession->getLastRealQuoteId();

if (!$lastRealQuoteId) {
$this->logger->info('No last real quote ID found');
return;
}

$quote = $this->quoteRepository->get($lastRealQuoteId);

if ($quote) {
$order = $this->checkoutSession->getLastRealOrder();
if ($order) {
$status = $order->getStatus();

if ($order && $order->getStatus() == Order::STATE_PENDING_PAYMENT) {
$this->checkoutSession->restoreQuote();
} else {
$this->checkoutSession->clearQuote();
$this->checkoutSession->clearStorage();
if ($status == Order::STATE_PENDING_PAYMENT && $quote->getItemsCount() > 0) {
$this->checkoutSession->replaceQuote($quote);
$this->checkoutSession->restoreQuote();
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/app/code/Komoju/Payments/Observer/SaveQuoteBeforeOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Komoju\Payments\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Psr\Log\LoggerInterface;

class SaveQuoteBeforeOrder implements ObserverInterface
{
protected $checkoutSession;
protected $logger;

public function __construct(
CheckoutSession $checkoutSession,
LoggerInterface $logger
) {
$this->checkoutSession = $checkoutSession;
$this->logger = $logger;
}

public function execute(Observer $observer)
{
$quote = $observer->getEvent()->getQuote();
$this->checkoutSession->setLastRealQuoteId($quote->getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ public function processEvent()
*/
private function prependExternalOrderNum($str)
{
return __('KOMOJU External Order ID: %1 %2', $this->webhookEvent->externalOrderNum(), $str);
return __('KOMOJU - %1', $this->getTranslatedPaymentMethod($this->webhookEvent->getPaymentMethod())) .
' ' .
__('External Order ID: %1 %2', $this->webhookEvent->externalOrderNum(), $str);
}

private function getTranslatedPaymentMethod($paymentMethod)
{
return __($paymentMethod);
}
}
8 changes: 7 additions & 1 deletion src/app/code/Komoju/Payments/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
<event name="payment_method_is_active">
<observer name="disable_payment" instance="Komoju\Payments\Observer\PaymentMethodAvailable" />
</event>
<event name="controller_action_postdispatch_checkout_index_index">
<event name="sales_model_service_quote_submit_before">
<observer name="save_quote_before_order" instance="Komoju\Payments\Observer\SaveQuoteBeforeOrder" />
</event>
<event name="controller_action_predispatch_checkout_index_index">
<observer name="restore_quote_from_session" instance="Komoju\Payments\Observer\RestoreQuoteFromSession" />
</event>
<event name="order_cancel_after">
<observer name="restore_cart_after_cancel" instance="Komoju\Payments\Observer\RestoreAfterCancel" />
</event>
</config>
19 changes: 18 additions & 1 deletion src/app/code/Komoju/Payments/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Konbini,Konbini
"Order has been fully refunded.","Order has been fully refunded."
"Refund for order created. Amount: %1 %2","Refund for order created. Amount: %1 %2"
"Unknown event type: %1","Unknown event type: %1"
"KOMOJU External Order ID: %1 %2","KOMOJU External Order ID: %1 %2"
"KOMOJU - %1","KOOJU - %1",
"External Order ID: %1 %2","External Order ID: %1 %2"
"Payment Type","Payment Type"
"Place Order","Place Order"
KOMOJU,KOMOJU
Expand All @@ -28,3 +29,19 @@ Title,Title
"Encountered an issue communicating with KOMOJU. Please wait a moment and try again.","Encountered an issue communicating with KOMOJU. Please wait a moment and try again."
”Show Title at Checkout”,"Show Title at Checkout"
"Display the title above the payment methods on the checkout page","Display the title above the payment methods on the checkout page"

payment_method,translation
"credit_card","Credit Card"
"paypay","PayPay"
"linepay","Line Pay"
"merpay","MerPay"
"rakutenpay","Rakuten Pay"
"paidy","Paidy"
"konbini","Convenience Store Payment"
"bank_transfer","Bank Transfer"
"pay_easy","Pay easy"
"web_money","Web Money"
"bit_cash","Bit Cash"
"net_cash","Net Cash"
"alipay","Alipay"
"aupay","au Pay"
19 changes: 18 additions & 1 deletion src/app/code/Komoju/Payments/i18n/ja_JP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Konbini,コンビニ
"Order has been fully refunded.","全額返金されました"
"Refund for order created. Amount: %1 %2","払い戻しが作成されました。金額: %1 %2"
"Unknown event type: %1","不明なイベントタイプ: %1"
"KOMOJU External Order ID: %1 %2","KOMOJU 外部注文ID: %1 %2"
"KOMOJU - %1", "KOMOJU - %1"
"External Order ID: %1 %2","外部注文ID: %1 %2"
"Payment Type","支払いの種類"
"Place Order","注文"
KOMOJU,KOMOJU
Expand All @@ -28,3 +29,19 @@ Title,題名
"Encountered an issue communicating with KOMOJU. Please wait a moment and try again.","KOMOJUとの通信に問題が発生しました。しばらく待ってから再実行してください。"
"Show Title at Checkout","チェックアウトでタイトルを表示"
"Display the title above the payment methods on the checkout page","チェックアウトページの支払い方法の上にタイトルを表示"

payment_method,translation
"credit_card","クレジットカード決済"
"paypay","PayPay"
"linepay","Line Pay"
"merpay","メルペイ"
"rakutenpay","楽天ペイ"
"paidy","ペイディ(Paidy)"
"konbini","コンビニ決済"
"bank_transfer","銀行振込"
"pay_easy","Pay Easy"
"web_money","Web Money"
"bit_cash","Bit Cash"
"net_cash","Net Cash"
"alipay","Ali Pay"
"aupay","au Pay"

0 comments on commit 3fed78c

Please sign in to comment.