Skip to content

Commit

Permalink
Merge pull request #41 from airwallex/leon-awx
Browse files Browse the repository at this point in the history
Leon awx
  • Loading branch information
leon-zhang-awx authored May 8, 2024
2 parents 27069d6 + 9d18aca commit d76b4a6
Show file tree
Hide file tree
Showing 46 changed files with 2,526 additions and 407 deletions.
38 changes: 38 additions & 0 deletions Api/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Airwallex\Payments\Api;

use Airwallex\Payments\Api\Data\PlaceOrderResponseInterface;
use Exception;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;

Expand Down Expand Up @@ -55,4 +56,41 @@ public function airwallexPlaceOrder(
* @return string
*/
public function redirectUrl(): string;

/**
* Get express data when initialize and quote data updated
*
* @return string
*/
public function expressData();

/**
* Add to cart
*
* @return string
*/
public function addToCart();

/**
* Post Address to get method and quote data
*
* @return string
*/
public function postAddress();

/**
* Apple pay validate merchant
*
* @return string
* @throws Exception
*/
public function validateMerchant();

/**
* Validate addresses before placing order
*
* @return string
* @throws Exception
*/
public function validateAddresses();
}
22 changes: 19 additions & 3 deletions Helper/AvailablePaymentMethodsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Checkout\Helper\Data as CheckoutData;

class AvailablePaymentMethodsHelper
{
Expand Down Expand Up @@ -51,6 +52,17 @@ class AvailablePaymentMethodsHelper
*/
private Configuration $configuration;

/**
* @var array
*/

private CheckoutData $checkoutHelper;

protected $methodsInExpress = [
'googlepay',
'applepay',
];

/**
* AvailablePaymentMethodsHelper constructor.
*
Expand All @@ -65,13 +77,15 @@ public function __construct(
CacheInterface $cache,
SerializerInterface $serializer,
StoreManagerInterface $storeManager,
Configuration $configuration
Configuration $configuration,
CheckoutData $checkoutHelper
) {
$this->availablePaymentMethod = $availablePaymentMethod;
$this->cache = $cache;
$this->storeManager = $storeManager;
$this->serializer = $serializer;
$this->configuration = $configuration;
$this->checkoutHelper = $checkoutHelper;
}

/**
Expand All @@ -98,6 +112,9 @@ public function isMobileDetectInstalled(): bool
*/
public function isAvailable(string $code): bool
{
if ($code === 'express') {
return $this->canInitialize() && !!array_intersect($this->methodsInExpress, $this->getAllMethods());
}
return $this->canInitialize() && in_array($code, $this->getAllMethods(), true);
}

Expand All @@ -124,7 +141,6 @@ private function getAllMethods(): array
AbstractMethod::CACHE_TAGS,
self::CACHE_TIME
);

return $methods;
}

Expand All @@ -142,7 +158,7 @@ private function getCacheName(): string
private function getCurrencyCode(): string
{
try {
return $this->storeManager->getStore()->getBaseCurrency()->getCode();
return $this->checkoutHelper->getQuote()->getQuoteCurrencyCode() ?: '';
} catch (Exception $exception) {
return '';
}
Expand Down
135 changes: 126 additions & 9 deletions Helper/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Airwallex\Payments\Helper;

use Airwallex\Payments\Model\Config\Source\Mode;
Expand All @@ -25,7 +26,11 @@ class Configuration extends AbstractHelper
private const DEMO_BASE_URL = 'https://pci-api-demo.airwallex.com/api/v1/';
private const PRODUCTION_BASE_URL = 'https://pci-api.airwallex.com/api/v1/';

private const EXPRESS_PREFIX = 'payment/airwallex_payments_express/';

/**
* Client id
*
* @return string|null
*/
public function getClientId(): ?string
Expand All @@ -34,6 +39,8 @@ public function getClientId(): ?string
}

/**
* Api key
*
* @return string|null
*/
public function getApiKey(): ?string
Expand All @@ -42,14 +49,18 @@ public function getApiKey(): ?string
}

/**
* Is request logger enabled
*
* @return bool
*/
public function isRequestLoggerEnable(): bool
{
return (bool) $this->scopeConfig->getValue('airwallex/general/request_logger');
return (bool)$this->scopeConfig->getValue('airwallex/general/request_logger');
}

/**
* Webhook secret key
*
* @return string
*/
public function getWebhookSecretKey(): string
Expand All @@ -60,6 +71,8 @@ public function getWebhookSecretKey(): string
}

/**
* Mode
*
* @return string
*/
public function getMode(): string
Expand All @@ -68,34 +81,138 @@ public function getMode(): string
}

/**
* Api url
*
* @return string
*/
public function getApiUrl(): string
{
return $this->isDemoMode() ? self::DEMO_BASE_URL : self::PRODUCTION_BASE_URL;
}

/**
* Is demo mode
*
* @return bool
*/
private function isDemoMode(): bool
{
return $this->getMode() === Mode::DEMO;
}

/**
* Card capture enabled
*
* @return bool
*/
public function isCardCaptureEnabled(): bool
{
return $this->scopeConfig->getValue('payment/airwallex_payments_card/airwallex_payment_action')
=== MethodInterface::ACTION_AUTHORIZE_CAPTURE;
}

/**
* Card enabled
*
* @return bool
*/
public function isCardActive(): bool
{
return !!$this->scopeConfig->getValue('payment/airwallex_payments_card/active');
}

/**
* Express capture enabled
*
* @return bool
*/
public function isExpressCaptureEnabled(): bool
{
return $this->scopeConfig->getValue('payment/airwallex_payments_express/airwallex_payment_action')
=== MethodInterface::ACTION_AUTHORIZE_CAPTURE;
}

/**
* Express display area
*
* @return string
*/
public function getCardPaymentAction(): string
public function expressDisplayArea(): string
{
return $this->scopeConfig->getValue('payment/airwallex_payments_card/airwallex_payment_action');
return $this->scopeConfig->getValue('payment/airwallex_payments_express/display_area');
}

/**
* Express seller name
*
* @return string
*/
public function getApiUrl(): string
public function getExpressSellerName(): string
{
return $this->isDemoMode() ? self::DEMO_BASE_URL : self::PRODUCTION_BASE_URL;
return $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'seller_name') ?: '';
}

/**
* Express style
*
* @return array
*/
public function getExpressStyle(): array
{
return [
"button_height" => $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'button_height'),
"apple_pay_button_theme" => $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'apple_pay_button_theme'),
"google_pay_button_theme" => $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'google_pay_button_theme'),
"apple_pay_button_type" => $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'apple_pay_button_type'),
"google_pay_button_type" => $this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'google_pay_button_type'),
];
}

/**
* Is express active
*
* @return bool
*/
private function isDemoMode(): bool
public function isExpressActive(): bool
{
return $this->getMode() === Mode::DEMO;
if (!$this->isCardActive()) {
return false;
}
return !!$this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'active');
}

/**
* Is express phone required
*
* @return bool
*/
public function isCaptureEnabled()
public function isExpressPhoneRequired(): bool
{
return $this->scopeConfig->getValue('customer/address/telephone_show') === "req";
}

/**
* Country code
*
* @return string
*/
public function getCountryCode(): string
{
return $this->scopeConfig->getValue('paypal/general/merchant_country')
?: $this->scopeConfig->getValue('general/country/default');
}

/**
* Express button sort
*
* @return array
*/
public function getExpressButtonSort(): array
{
return $this->scopeConfig->getValue('payment/airwallex_payments_card/airwallex_payment_action') === MethodInterface::ACTION_AUTHORIZE_CAPTURE;
$sorts = [];
$sorts['google'] = (int)$this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'google_pay_sort_order');
$sorts['apple'] = (int)$this->scopeConfig->getValue(self::EXPRESS_PREFIX . 'apple_pay_sort_order');
asort($sorts);
return array_keys($sorts);
}
}
72 changes: 72 additions & 0 deletions Model/Adminhtml/Notifications/ExpressDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Airwallex\Payments\Model\Adminhtml\Notifications;

use Airwallex\Payments\Helper\Configuration;
use Magento\Framework\Notification\MessageInterface;

class ExpressDisabled implements MessageInterface
{
/**
* @var string|null
*/
private ?string $displayedText = null;

/**
* @var Configuration
*/
private Configuration $configuration;

/**
* Dependencies constructor.
*
* @param Configuration $configuration
*/
public function __construct(
Configuration $configuration
){
$this->configuration = $configuration;
$this->expressDisabled();
}

/**
* @return string
*/
public function getIdentity(): string
{
return 'airwallex_payments_notification_express_disabled';
}

/**
* @return bool
*/
public function isDisplayed(): bool
{
return $this->displayedText !== null;
}

/**
* @return string
*/
public function getText(): string
{
return $this->displayedText;
}

/**
* @return int
*/
public function getSeverity(): int
{
return self::SEVERITY_NOTICE;
}

/**
* @return void
*/
private function expressDisabled(): void {
if (!$this->configuration->isCardActive()) {
$this->displayedText = __('Airwallex Express Checkout is also disabled. To use Express Checkout, you must first enable Credit Card payments.');
}
}
}
Loading

0 comments on commit d76b4a6

Please sign in to comment.