Skip to content

Commit

Permalink
applePay browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jun 21, 2024
1 parent c6416d0 commit 0ce1715
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Model/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(Session $session)
$this->checkoutSession = $session;
}

public function validate(array $constraints): bool
public function validate(array $constraints, string $browser): bool
{
foreach ($constraints as $constraint) {
switch ($constraint['type']) {
Expand All @@ -31,6 +31,12 @@ public function validate(array $constraints): bool
return false;
}

break;
case 'supported':
if (!$this->validateBrowser($constraint['field'], $browser)) {
return false;
}

break;
default:
break;
Expand All @@ -54,4 +60,13 @@ private function validateMaximalTotal(float $maximal): bool
{
return $this->checkoutSession->getQuote()->getBaseGrandTotal() <= $maximal;
}

private function validateBrowser(string $browserSupport, string $browser): bool
{
if ($browserSupport == 'ApplePaySession' && $browser != 'Safari') {
return false;
}

return true;
}
}
25 changes: 23 additions & 2 deletions Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tpay\Magento2\Model;

use Laminas\Http\PhpEnvironment\Request;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Helper\Data;
Expand Down Expand Up @@ -44,6 +45,9 @@ class MethodListPlugin
/** @var ConstraintValidator */
private $constraintValidator;

/** @var Request */
protected $request;

public function __construct(
Data $data,
ScopeConfigInterface $scopeConfig,
Expand All @@ -53,7 +57,8 @@ public function __construct(
Session $checkoutSession,
TransactionApiFacade $transactions,
ConstraintValidator $constraintValidator,
TpayInterface $paymentMethod
TpayInterface $paymentMethod,
Request $request
) {
$this->data = $data;
$this->scopeConfig = $scopeConfig;
Expand All @@ -64,6 +69,7 @@ public function __construct(
$this->transactions = $transactions;
$this->constraintValidator = $constraintValidator;
$this->paymentMethod = $paymentMethod;
$this->request = $request;
}

public function afterGetAvailableMethods(MethodList $compiled, $result)
Expand All @@ -90,10 +96,12 @@ public function afterGetAvailableMethods(MethodList $compiled, $result)
return $result;
}

$browser = $this->getBrowser();

foreach ($channelList as $onsiteChannel) {
$channel = $channels[$onsiteChannel];

if (!empty($channel->constraints) && !$this->constraintValidator->validate($channel->constraints)) {
if (!empty($channel->constraints) && !$this->constraintValidator->validate($channel->constraints, $browser)) {
continue;
}

Expand Down Expand Up @@ -158,4 +166,17 @@ private function getChannels(): array

return [$channelList, $channels];
}

private function getBrowser(): string
{
$userAgent = $this->request->getHeader('User-Agent')->getFieldValue();

if (strpos($userAgent, 'Chrome')) {
return 'Chrome';
} elseif (strpos($userAgent, 'Safari')) {
return 'Safari';
}

return 'Other';
}
}
12 changes: 12 additions & 0 deletions view/base/web/js/open_render_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ require(['jquery', 'mage/translate'], function ($, $t) {
return true;
}

function isBrowserSupport(channelId) {
if (channelId === 170) { //ApplePay
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}

return true;
}

function ShowChannelsCombo() {
var str = '',
i,
Expand All @@ -71,6 +79,10 @@ require(['jquery', 'mage/translate'], function ($, $t) {
continue;
}

if (!isBrowserSupport(parseInt(id))) {
continue;
}

tile = getBankTile(id, groupName, logoSrc);

if (inArray(id, others) === false) {
Expand Down

0 comments on commit 0ce1715

Please sign in to comment.