Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function __construct(
*/
protected function _toHtml()
{
if ($this->getRequest()->getParam('upa') === 'true') {
return '';
}

$order = $this->_checkoutSession->getLastRealOrder();
$paymentData = $order->getPayment()->getData();
$paymentAdditionalInfo = $paymentData['additional_information'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class PaynowAdditionalInformation extends AdditionalInformation
*/
protected function _toHtml()
{
if ($this->getRequest()->getParam('upa') === 'true') {
return '';
}

if ($this->getPaymentType() !== 'paynow') {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class PromptpayAdditionalInformation extends AdditionalInformation
*/
protected function _toHtml()
{
if ($this->getRequest()->getParam('upa') === 'true') {
return '';
}

if ($this->getPaymentType() !== 'promptpay') {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions Block/Checkout/Onepage/Success/TescoAdditionalInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class TescoAdditionalInformation extends AdditionalInformation
*/
protected function _toHtml()
{
if ($this->getRequest()->getParam('upa') === 'true') {
return '';
}

if ($this->getPaymentType() !== 'bill_payment_tesco_lotus') {
return;
}
Expand Down
141 changes: 79 additions & 62 deletions Controller/Callback/UPACallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,40 @@
*/
public function execute()
{
$finalPayment = [];
$order = $this->session->getLastRealOrder();
$chargeId = $this->getRequest()->getParam('chargeId');

if (!$this->isValid($order)) {
return $this->redirect(self::PATH_CART);
}

$orderState = $order->getState();
if ($orderState === Order::STATE_PROCESSING) {
return $this->redirect(self::PATH_SUCCESS);
return $this->redirect(self::PATH_SUCCESS, ['upa' => 'true']);
}

try {
$payment = $order->getPayment();
$charge = $this->charge->find($chargeId);
if (! $charge instanceof \Omise\Payment\Model\Api\BaseObject) {
$checkoutSession = $this->getCheckoutSession($payment);
$sessionPayments = $checkoutSession->payments;

if($checkoutSession && !is_array($sessionPayments) || empty($sessionPayments)) {

Check warning on line 143 in Controller/Callback/UPACallback.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected 1 space(s) after IF keyword; 0 found

Check warning on line 143 in Controller/Callback/UPACallback.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected "if (...) {\n"; found "if(...) {\n"
$errorMessage = __('The payment session is invalid or no payment information was found. Please contact our support if you have any questions.');

Check warning on line 144 in Controller/Callback/UPACallback.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Line exceeds 120 characters; contains 160 characters
return $this->redirectBackToCart($order, $errorMessage);
}

$sessionStatus = $checkoutSession->status;
$finalPayment = $this->getFinalPayment($sessionPayments, $sessionStatus);

if (!empty($finalPayment) && !empty($finalPayment['charge_id'])) {
$chargeId = $finalPayment['charge_id'];
$charge = $this->charge->find($chargeId);
} else {
$errorMessage = __('The payment session is invalid or no payment information was found. Please contact our support if you have any questions.');

Check warning on line 155 in Controller/Callback/UPACallback.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Line exceeds 120 characters; contains 160 characters
return $this->redirectBackToCart($order,$errorMessage);

Check warning on line 156 in Controller/Callback/UPACallback.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

No space found after comma in argument list
}

if (!$charge instanceof \Omise\Payment\Model\Api\BaseObject) {
throw new LocalizedException(
__('Couldn\'t retrieve charge transaction. Please contact administrator.')
);
Expand All @@ -162,17 +180,19 @@
->setAdditionalInformation([
Transaction::RAW_DETAILS => [
'omise_charge_id' => $charge->id,
'status' => $charge->status
'status' => $charge->status,
'charge_id' => $charge->id
]
])
->setFailSafe(true)
->build(Transaction::TYPE_PAYMENT);
$order->save();
return $this->redirect(self::PATH_SUCCESS);
return $this->redirect(self::PATH_SUCCESS, ['upa' => 'true']);
}

$payment->setTransactionId($charge->id);
$payment->setLastTransId($charge->id);
$payment->setAdditionalInformation('charge_id', $charge->id);

if ($charge->isSuccessful()) {
return $this->handleSuccess($order, $charge, $payment);
Expand Down Expand Up @@ -231,19 +251,9 @@
$order->getBaseCurrency()->formatTxt($invoice->getBaseGrandTotal())
)
);
} else {
$payment->addTransactionCommentsToOrder(
$payment->addTransaction(Transaction::TYPE_AUTH),
$payment->prependMessage(
__(
'Authorized amount of %1 via Omise Gateway.',
$order->getBaseCurrency()->formatTxt($order->getTotalDue())
)
)
);
}
$order->save();
return $this->redirect(self::PATH_SUCCESS);
return $this->redirect(self::PATH_SUCCESS, ['upa' => 'true']);
}

/**
Expand Down Expand Up @@ -272,7 +282,7 @@

// TODO: Should redirect users to a page that tell users that
// their payment is in review instead of success page.
return $this->redirect(self::PATH_SUCCESS);
return $this->redirect(self::PATH_SUCCESS, ['upa' => 'true']);
}

/**
Expand All @@ -283,7 +293,7 @@
*/
private function isValid($order)
{
if (! $order->getId()) {
if (!$order->getId()) {
$this->messageManager->addErrorMessage(__('The order session no longer exists, please make an order
again or contact our support if you have any questions.'));

Expand All @@ -299,38 +309,16 @@
return false;
}

$chargeId = $this->request->getParam('chargeId');
$isValidCharge = $this->validateChargeId($payment, $chargeId);

if (!$chargeId || !$isValidCharge) {
$this->invalid(
$order,
__('The URL is invalid. Please contact our support if you have any questions.')
);

return false;
}

$orderState = $order->getState();
$validOrderStates = [Order::STATE_PENDING_PAYMENT, Order::STATE_PAYMENT_REVIEW, Order::STATE_PROCESSING];

if (!in_array($orderState, $validOrderStates)) {
$this->invalid($order, __('Invalid order status, cannot validate the payment. Please contact our
support if you have any questions.'));

return false;
}

if (!$payment->getAdditionalInformation('session_id')) {
$this->cancel(
$order,
__('Cannot retrieve a session reference id. Please contact our support to confirm your payment.')
);
$this->session->restoreQuote();

return false;
}

return true;
}

Expand All @@ -346,12 +334,13 @@

/**
* @param string $path
* @param array $arguments
*
* @return \Magento\Framework\App\ResponseInterface
*/
protected function redirect($path)
protected function redirect($path, array $arguments = [])
{
return $this->_redirect($path, ['_secure' => true]);
return $this->_redirect($path, array_merge(['_secure' => true], $arguments));
}

/**
Expand Down Expand Up @@ -382,29 +371,57 @@
$this->messageManager->addErrorMessage($message);
}

private function redirectBackToCart($order, $errorMessage)
{
$this->invalid($order, $errorMessage);
$this->checkoutSession->restoreQuote();
return $this->redirect(self::PATH_CART);
}

/**
* @var Magento\Sales\Model\Order\Payment
* @var string
* @return bool
* @param Magento\Sales\Model\Order\Payment $payment
* @return \Omise\Payment\Model\Api\CheckoutSession|null
*/
private function validateChargeId($payment, $chargeId)
private function getCheckoutSession($payment)
{
try {
$sessionId = $payment->getAdditionalInformation('session_id');
$checkoutSessionInfo = $this->omiseCheckoutSession->getSessionInfo($sessionId);
$sessionId = $payment->getAdditionalInformation('session_id');
if (empty($sessionId)) {
$this->checkoutSession->restoreQuote();
throw new LocalizedException(
__('Cannot retrieve a session reference id. Please contact our support to confirm your payment.')
);
}

$checkoutSession = $this->omiseCheckoutSession->getSessionInfo($sessionId);
return $checkoutSession;
}

if (!is_array($checkoutSessionInfo->payments)) {
return false;
}

foreach ($checkoutSessionInfo->payments as $charge) {
if ($charge['charge_id'] === $chargeId) {
return true;
}
/**
* @param array $payments
* @param string $sessionStatus
* @return array|null
*/
private function getFinalPayment($payments, $sessionStatus)
{
if (!is_array($payments) || empty($payments)) {
return null;
}

$paymentsWithChargeId = array_filter($payments, function ($payment) {
return !empty($payment['charge_id']);
});

if (empty($paymentsWithChargeId)) {
return null;
}

foreach ($paymentsWithChargeId as $payment) {
$paymentStatus = strtolower(isset($payment['status']) ? (string) $payment['status'] : '');
if ($paymentStatus === strtolower((string) $sessionStatus)) {
return $payment;
}
} catch (\Exception $e) {
return false;
}
return false;

return end($paymentsWithChargeId);
}
}
33 changes: 3 additions & 30 deletions Helper/OmiseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@
use Omise\Payment\Model\Config\Conveniencestore;
use Omise\Payment\Model\Config\WeChatPay;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;

class OmiseHelper extends AbstractHelper
{
/**
* Path to check if UPA feature is enabled in deployment config.
* @var string
*/
protected const UPA_FEATURE_PATH = 'omise_payment/omise_feature_upa';

/**
* @var array
*/
Expand Down Expand Up @@ -226,26 +219,18 @@ class OmiseHelper extends AbstractHelper
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var DeploymentConfig $deploymentConfig
*/
protected $deploymentConfig;


/**
* @param Header $header
* @param Config $config
* @param ScopeConfigInterface $scopeConfig
* @param DeploymentConfig $deploymentConfig
*/
public function __construct(
Config $config,
ScopeConfigInterface $scopeConfig,
DeploymentConfig $deploymentConfig
ScopeConfigInterface $scopeConfig
) {
$this->config = $config;
$this->scopeConfig = $scopeConfig;
$this->deploymentConfig = $deploymentConfig;
$this->omisePaymentMethods = array_merge(
$this->offsitePaymentMethods,
$this->offlinePaymentMethods,
Expand All @@ -267,17 +252,6 @@ public function getConfig(string $fieldId, ?int $storeId = null)
$storeId
);
}

/**
* Check whether merchant-specific feature is enabled.
*/
public function isUpaFeatureEnabled(): bool
{
return (bool) $this->deploymentConfig->get(
self::UPA_FEATURE_PATH,
false
);
}

/**
* @param string $methodcode
Expand All @@ -286,8 +260,7 @@ public function isUpaFeatureEnabled(): bool
public function isAllowUpa($methodCode)
{
$isUpaFeatureFlagEnabled = $this->config->getIsUpaFeatureFlagEnabled();
$upaFeatureEnabled = $this->isUpaFeatureEnabled();
if ($isUpaFeatureFlagEnabled && $upaFeatureEnabled) {
if ($isUpaFeatureFlagEnabled) {
return $this->isOffsitePaymentMethod($methodCode) || $this->isOfflinePaymentMethod($methodCode);
}
return false;
Expand Down
Loading
Loading