Skip to content

Commit

Permalink
apple pay auto set
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-zhang-awx committed Oct 15, 2024
1 parent 707a795 commit 0f0e886
Show file tree
Hide file tree
Showing 16 changed files with 415 additions and 278 deletions.
7 changes: 0 additions & 7 deletions Api/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,4 @@ public function validateMerchant(): string;
* @throws Exception
*/
public function validateAddresses(): string;

/**
* Set apple pay domain
*
* @return string
*/
public function applePayDomain(): string;
}
157 changes: 157 additions & 0 deletions Controller/Adminhtml/Configuration/PayEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Airwallex\Payments\Controller\Adminhtml\Configuration;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use JsonException;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Math\Random;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Store\Model\StoreManager;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\RequestInterface;
use Airwallex\Payments\Helper\AvailablePaymentMethodsHelper;
use Airwallex\Payments\Model\Client\Request\ApplePayDomain\GetList;
use Airwallex\Payments\Model\Client\Request\ApplePayDomain\Add;

class PayEnable extends Action
{
protected JsonFactory $resultJsonFactory;
protected Context $context;
protected StoreManager $storeManager;
protected Random $random;
protected CacheInterface $cache;
protected RequestInterface $request;
protected DirectoryList $directoryList;
protected AvailablePaymentMethodsHelper $availablePaymentMethodsHelper;
protected GetList $appleDomainList;
protected Add $appleDomainAdd;

public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
StoreManager $storeManager,
Random $random,
CacheInterface $cache,
RequestInterface $request,
DirectoryList $directoryList,
AvailablePaymentMethodsHelper $availablePaymentMethodsHelper,
GetList $appleDomainList,
Add $appleDomainAdd
)
{
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->context = $context;
$this->storeManager = $storeManager;
$this->random = $random;
$this->cache = $cache;
$this->request = $request;
$this->directoryList = $directoryList;
$this->availablePaymentMethodsHelper = $availablePaymentMethodsHelper;
$this->appleDomainList = $appleDomainList;
$this->appleDomainAdd = $appleDomainAdd;
}

protected function getHost()
{
$host = trim($this->storeManager->getStore()->getBaseUrl(), '/');
$host = str_replace('http://', '', $host);
return str_replace('https://', '', $host);
}

private function methodInactiveTip($type): string
{
$link = "<a href='https://demo.airwallex.com/app/acquiring/payment-methods/other-pms'
style='color: red; font-weight: 600; text-decoration: underline;' target='_blank'>Airwallex</a>";
return 'You have not activated ' . $type . ' as a payment method.
Please go to ' . $link . ' to activate ' . $type . ' before try again.';
}

private function fileUploadFailedTip(): string
{
$link = "<a href='https://demo.airwallex.com/app/acquiring/settings/apple-pay/add-domain'
style='color: red; font-weight: 600; text-decoration: underline;' target='_blank'>download the file</a>";
return 'We could not add the domain file to your server. Please ' . $link . ' and host it on your
site at the following path: &lt;&lt;DOMAIN_NAME&gt;&gt;/.well-known/apple-developer-merchantid-domain-association';
}

/**
* Set apple pay domain
*
* @return Json
* @throws GuzzleException
* @throws JsonException
* @throws FileSystemException
* @throws Exception
*/
public function execute(): Json
{
$resultJson = $this->resultJsonFactory->create();
$methods = $this->request->getParam('methods');
$host = $this->getHost();
$types = $this->availablePaymentMethodsHelper->getLatestItems();
$isApplePayActive = false;
$isGooglePayActive = false;
if (empty($methods)) throw new CouldNotSaveException(__('post parameter methods is required'));
foreach ($types as $type) {
if (strstr($methods, 'apple_pay') && $type['name'] === 'applepay' && $type['active'] === true) {
$isApplePayActive = true;
}
if (strstr($methods, 'google_pay') && $type['name'] === 'googlepay' && $type['active'] === true) {
$isGooglePayActive = true;
}
}
if (strstr($methods, 'apple_pay') && !$isApplePayActive) throw new CouldNotSaveException(__($this->methodInactiveTip('Apple Pay')));
if (strstr($methods, 'google_pay') && !$isGooglePayActive) throw new CouldNotSaveException(__($this->methodInactiveTip('Google Pay')));

if (empty(strstr($methods, 'apple_pay'))) return $resultJson;
$list = $this->appleDomainList->send();
if (in_array($host, $list, true)) {
return $resultJson;
}
$this->uploadAppleDomainFile();

$list = $this->appleDomainAdd->setDomain($host)->send();
if (in_array($host, $list, true)) {
return $resultJson;
}

$link = "<a href='https://demo.airwallex.com/app/acquiring/settings/apple-pay/add-domain'
style='color: red; font-weight: 600; text-decoration: underline;' target='_blank'>Airwallex</a>";
$tip = "We could not register your domain. Please go to $link to specify the domain names that you’ll register with Apple before trying again.";
throw new CouldNotSaveException(__($tip));
}

/**
* @return void
* @throws FileSystemException
*/
public function uploadAppleDomainFile(): void
{
$filename = 'apple-developer-merchantid-domain-association';
$destinationDir = $this->directoryList->getPath(DirectoryList::PUB) . '/.well-known/';

if (file_exists($destinationDir . $filename)) return;
if (!is_dir($destinationDir)) {
try {
mkdir($destinationDir, 0755, true);
} catch (Exception $e) {
throw new FileSystemException(__($this->fileUploadFailedTip()));
}
}
$sourceFile = $this->directoryList->getPath(DirectoryList::APP) . '/../vendor/airwallex/payments-plugin-magento/' . $filename;
$destinationFile = $destinationDir . $filename;
try {
copy($sourceFile, $destinationFile);
} catch (Exception $e) {
throw new FileSystemException(__($this->fileUploadFailedTip()));
}
}
}
54 changes: 54 additions & 0 deletions Controller/Adminhtml/Configuration/UpdateSettingsToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Airwallex\Payments\Controller\Adminhtml\Configuration;

use Magento\Framework\Math\Random;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManager;
use Magento\Framework\App\CacheInterface;

class UpdateSettingsToken extends Action
{
public const CACHE_NAME = 'airwallex_update_settings_token';

protected JsonFactory $resultJsonFactory;
protected Context $context;
protected StoreManager $storeManager;
protected Random $random;
protected CacheInterface $cache;

public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
StoreManager $storeManager,
Random $random,
CacheInterface $cache
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->context = $context;
$this->storeManager = $storeManager;
$this->random = $random;
$this->cache = $cache;
}

/**
* @return Json
* @throws NoSuchEntityException
*/
public function execute(): Json
{
$resultJson = $this->resultJsonFactory->create();

$token = $this->random->getRandomString(32);
$this->cache->save($token, self::CACHE_NAME, [], 60 * 60 * 24);

$resultJson->setData(compact('token'));

return $resultJson;
}
}
17 changes: 17 additions & 0 deletions Model/Config/Adminhtml/ApplePayEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Airwallex\Payments\Model\Config\Adminhtml;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class ApplePayEnable extends Field
{
public string $type = 'apple';
protected $_template = 'Airwallex_Payments::config/pay_enable.phtml';

protected function _getElementHtml(AbstractElement $element): string
{
return parent::_getElementHtml($element) . $this->_toHtml();
}
}
17 changes: 17 additions & 0 deletions Model/Config/Adminhtml/GooglePayEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Airwallex\Payments\Model\Config\Adminhtml;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class GooglePayEnable extends Field
{
public string $type = 'google';
protected $_template = 'Airwallex_Payments::config/pay_enable.phtml';

protected function _getElementHtml(AbstractElement $element): string
{
return parent::_getElementHtml($element) . $this->_toHtml();
}
}
49 changes: 49 additions & 0 deletions Model/Config/Adminhtml/UpdateSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Airwallex\Payments\Model\Config\Adminhtml;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Form\Element\AbstractElement;

class UpdateSettings extends Field
{
protected $_template = 'Airwallex_Payments::config/update_settings.phtml';

/**
* @var ScopeConfigInterface
*/
private ScopeConfigInterface $scopeConfig;

/**
* WebhookUrl constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param Context $context
* @param array $data
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Context $context,
array $data = []
) {
parent::__construct($context, $data);
$this->scopeConfig = $scopeConfig;
}

protected function _getElementHtml(AbstractElement $element)
{
return $this->_toHtml();
}

public function getButtonHtml()
{
$data = [
'id' => 'airwallex_update_settings',
'label' => __('Connecting'),
];

return $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData($data)->toHtml();
}
}
19 changes: 0 additions & 19 deletions Model/Config/Source/Express/Checkout.php

This file was deleted.

Loading

0 comments on commit 0f0e886

Please sign in to comment.