Skip to content

Commit

Permalink
Added WeChat Pay. (#475)
Browse files Browse the repository at this point in the history
* Added WeChat Pay.

* Added tests and updated a few.

* Fixed a few M2 standard alert.

* Added and updated few tests

* Added one more test.

* Added @Covers annotation

* Fixed the dummy user agent in test.

---------

Co-authored-by: Aashish <[email protected]>
  • Loading branch information
aashishgurung and Aashish authored Mar 29, 2024
1 parent df2acbc commit 76bfd3e
Show file tree
Hide file tree
Showing 24 changed files with 587 additions and 138 deletions.
1 change: 1 addition & 0 deletions Cron/OrderSyncStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class OrderSyncStatus
"omise_offsite_shopeepay",
"omise_offsite_atome",
"omise_offsite_paypay",
"omise_offiste_wechat_pay"
];

/**
Expand Down
50 changes: 31 additions & 19 deletions Gateway/Request/APMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
use Omise\Payment\Model\Config\Mobilebanking;
use Omise\Payment\Model\Config\Rabbitlinepay;
use Omise\Payment\Model\Config\PayPay;
use Omise\Payment\Model\Config\WeChatPay;

use Omise\Payment\Helper\OmiseMoney;
use Omise\Payment\Helper\OmiseHelper as Helper;
use Omise\Payment\Model\Config\Internetbanking;
use Omise\Payment\Model\Config\Conveniencestore;
use Magento\Payment\Gateway\Helper\SubjectReader;
Expand All @@ -43,6 +43,7 @@
use Omise\Payment\Observer\MobilebankingDataAssignObserver;
use Omise\Payment\Observer\InternetbankingDataAssignObserver;
use Omise\Payment\Observer\ConveniencestoreDataAssignObserver;
use Omise\Payment\Helper\RequestHelper;

class APMBuilder implements BuilderInterface
{
Expand Down Expand Up @@ -107,14 +108,14 @@ class APMBuilder implements BuilderInterface
const SOURCE_SHIPPING = 'shipping';

/**
* @var \Omise\Payment\Helper\ReturnUrlHelper
* @var string
*/
protected $returnUrl;
const SOURCE_IP = 'ip';

/**
* @var Helper
* @var \Omise\Payment\Helper\ReturnUrlHelper
*/
protected $helper;
protected $returnUrl;

/**
* @var OmiseMoney
Expand All @@ -131,22 +132,27 @@ class APMBuilder implements BuilderInterface
*/
protected $config;

/**
* @var \Omise\Payment\Helper\RequestHelper
*/
private $requestHelper;

/**
* @param $helper \Omise\Payment\Helper\OmiseHelper
* @param $returnUrl \Omise\Payment\Helper\ReturnUrl
*/
public function __construct(
Helper $helper,
ReturnUrlHelper $returnUrl,
Config $config,
Capabilities $capabilities,
OmiseMoney $money
OmiseMoney $money,
RequestHelper $requestHelper
) {
$this->helper = $helper;
$this->returnUrl = $returnUrl;
$this->config = $config;
$this->capabilities = $capabilities;
$this->money = $money;
$this->requestHelper = $requestHelper;
}

/**
Expand Down Expand Up @@ -234,43 +240,43 @@ public function build(array $buildSubject)
case Alipayplus::ALIPAY_CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'alipay_cn',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Alipayplus::ALIPAYHK_CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'alipay_hk',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Alipayplus::DANA_CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'dana',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Alipayplus::GCASH_CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'gcash',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Alipayplus::KAKAOPAY_CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'kakaopay',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Touchngo::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'touch_n_go',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Mobilebanking::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => $method->getAdditionalInformation(MobilebankingDataAssignObserver::OFFSITE),
self::PLATFORM_TYPE => $this->helper->getPlatformType()
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType()
];
break;
case Rabbitlinepay::CODE:
Expand All @@ -281,19 +287,19 @@ public function build(array $buildSubject)
case Ocbcpao::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'mobile_banking_ocbc_pao',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case OcbcDigital::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => OcbcDigital::ID,
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Grabpay::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'grabpay',
self::PLATFORM_TYPE => $this->helper->getPlatformType(),
self::PLATFORM_TYPE => $this->requestHelper->getPlatformType(),
];
break;
case Boost::CODE:
Expand Down Expand Up @@ -339,6 +345,12 @@ public function build(array $buildSubject)
self::SOURCE_TYPE => PayPay::ID,
];
break;
case WeChatPay::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => WeChatPay::ID,
self::SOURCE_IP => $this->requestHelper->getClientIp()
];
break;
}

return $paymentInfo;
Expand All @@ -350,7 +362,7 @@ private function getShopeepaySource()
$isShopeepayEnabled = $this->capabilities->isBackendEnabled(Shopeepay::ID);

// If user is in mobile and jump app is enabled then return shopeepay_jumpapp as source
if ($this->helper->isMobilePlatform() && $isShopeepayJumpAppEnabled) {
if ($this->requestHelper->isMobilePlatform() && $isShopeepayJumpAppEnabled) {
return Shopeepay::JUMPAPP_ID;
}

Expand Down
49 changes: 7 additions & 42 deletions Helper/OmiseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Omise\Payment\Model\Config\Config;
use Omise\Payment\Model\Config\Paynow;
use Omise\Payment\Model\Config\PayPay;
use Magento\Framework\App\Request\Http;
use Magento\Store\Model\ScopeInterface;
use Omise\Payment\Model\Config\Grabpay;
use Omise\Payment\Model\Config\Ocbcpao;
Expand All @@ -38,14 +37,10 @@
use Omise\Payment\Model\Config\Internetbanking;
use Magento\Framework\App\Helper\AbstractHelper;
use Omise\Payment\Model\Config\Conveniencestore;
use Omise\Payment\Model\Config\WeChatPay;

class OmiseHelper extends AbstractHelper
{
/**
* @var \Magento\Framework\HTTP\Header
*/
protected $header;

/**
* @var array
*/
Expand Down Expand Up @@ -73,7 +68,8 @@ class OmiseHelper extends AbstractHelper
MaybankQR::CODE,
Shopeepay::CODE,
Atome::CODE,
PayPay::CODE
PayPay::CODE,
WeChatPay::CODE
];

/**
Expand Down Expand Up @@ -143,6 +139,7 @@ class OmiseHelper extends AbstractHelper
Shopeepay::JUMPAPP_ID => Shopeepay::CODE,
Atome::ID => Atome::CODE,
PayPay::ID => PayPay::CODE,
WeChatPay::ID => WeChatPay::CODE,

// offsite internet banking payment
Internetbanking::BBL_ID => Internetbanking::CODE,
Expand Down Expand Up @@ -209,6 +206,7 @@ class OmiseHelper extends AbstractHelper
Shopeepay::CODE => "ShopeePay Payment",
Atome::CODE => "Atome Payment",
PayPay::CODE => "PayPay Payment",
WeChatPay::CODE => "WeChat Pay Payment",

// offline payment
Paynow::CODE => "PayNow QR Payment",
Expand All @@ -225,16 +223,10 @@ class OmiseHelper extends AbstractHelper
/**
* @param Header $header
* @param Config $config
* @param Http $httpRequest
*/
public function __construct(
Header $header,
Config $config,
Http $httpRequest
) {
$this->header = $header;
public function __construct(Config $config)
{
$this->config = $config;
$this->httpRequest = $httpRequest;

$this->omisePaymentMethods = array_merge(
$this->offsitePaymentMethods,
Expand Down Expand Up @@ -406,33 +398,6 @@ public function is3DSecureEnabled($charge)
return false;
}

/**
* Get platform Type of WEB, IOS or ANDROID to add to source API parameter.
* @return string
*/
public function getPlatformType()
{
$userAgent = $this->header->getHttpUserAgent();

if (preg_match("/(Android)/i", $userAgent)) {
return "ANDROID";
}

if (preg_match("/(iPad|iPhone|iPod)/i", $userAgent)) {
return "IOS";
}

return "WEB";
}

/**
* Check if current platform is mobile or not
*/
public function isMobilePlatform()
{
return 'WEB' !== $this->getPlatformType();
}

/**
* Depending on the setting of state to generate invoice, we will either create an invoice or return a created one.
* Invoice will be marked as successfully paid and returned.
Expand Down
99 changes: 99 additions & 0 deletions Helper/RequestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Omise\Payment\Helper;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\HTTP\Header;

class RequestHelper
{
/**
* @var Magento\Framework\App\RequestInterface;
*/
private $request;

/**
* @var \Magento\Framework\HTTP\Header
*/
protected $header;

public function __construct(
RequestInterface $request,
Header $header
) {
$this->request = $request;
$this->header = $header;
}

public function getClientIp()
{
$headersToCheck = [
// Check for a client using a shared internet connection
'HTTP_CLIENT_IP',

// Check if the proxy is used for IP/IPs
'HTTP_X_FORWARDED_FOR',

// check for other possible forwarded IP headers
'HTTP_X_FORWARDED',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
];

foreach ($headersToCheck as $header) {
$headerValue = $this->request->getServer($header, '');

if (empty($headerValue)) {
continue;
}

if ($header === 'HTTP_X_FORWARDED_FOR' && !empty($headerValue)) {
return $this->processForwardedForHeader($headerValue);
}

return $headerValue;
}

// return default remote IP address
return $this->request->getServer('REMOTE_ADDR', '');
}

private function processForwardedForHeader($forwardedForHeader)
{
// Split if multiple IP addresses exist and get the last IP address
if (strpos($forwardedForHeader, ',') !== false) {
$multiple_ips = explode(",", $forwardedForHeader);
return trim(current($multiple_ips));
}

return $forwardedForHeader;
}

/**
* Get platform Type of WEB, IOS or ANDROID to add to source API parameter.
* @return string
*/
public function getPlatformType()
{
$userAgent = $this->header->getHttpUserAgent();

if (preg_match("/(Android)/i", $userAgent)) {
return "ANDROID";
}

if (preg_match("/(iPad|iPhone|iPod)/i", $userAgent)) {
return "IOS";
}

return "WEB";
}

/**
* Check if current platform is mobile or not
*/
public function isMobilePlatform()
{
return 'WEB' !== $this->getPlatformType();
}
}
18 changes: 18 additions & 0 deletions Model/Config/WeChatPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Omise\Payment\Model\Config;

use Omise\Payment\Model\Config\Config;

class WeChatPay extends Config
{
/**
* @var string
*/
const CODE = 'omise_offsite_wechat_pay';

/**
* Backends identifier
* @var string
*/
const ID = 'wechat_pay';
}
Loading

0 comments on commit 76bfd3e

Please sign in to comment.