Skip to content

Commit

Permalink
Added WeChat Pay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Mar 27, 2024
1 parent 79bd394 commit 0e31693
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 1 deletion.
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
12 changes: 12 additions & 0 deletions Gateway/Request/APMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -106,6 +107,11 @@ class APMBuilder implements BuilderInterface
*/
const SOURCE_SHIPPING = 'shipping';

/**
* @var string
*/
const SOURCE_IP = 'ip';

/**
* @var \Omise\Payment\Helper\ReturnUrlHelper
*/
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->helper->getClientIp()
];
break;
}

return $paymentInfo;
Expand Down
47 changes: 46 additions & 1 deletion Helper/OmiseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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
{
Expand Down Expand Up @@ -73,7 +74,8 @@ class OmiseHelper extends AbstractHelper
MaybankQR::CODE,
Shopeepay::CODE,
Atome::CODE,
PayPay::CODE
PayPay::CODE,
WeChatPay::CODE
];

/**
Expand Down Expand Up @@ -143,6 +145,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 +212,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 Down Expand Up @@ -513,4 +517,45 @@ public function hasShopeepayFailed($paymentMethod, $isChargeSuccess)
{
return $paymentMethod === 'omise_offsite_shopeepay' && !$isChargeSuccess;
}

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) {

Check warning on line 536 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected "foreach (...) {\n"; found "foreach(...) {\n"

Check warning on line 536 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected 1 space(s) after FOREACH keyword; 0 found
if (empty($_SERVER[$header])) {

Check warning on line 537 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Direct use of $_SERVER Superglobal detected.
continue;
}

if ($header === 'HTTP_X_FORWARDED_FOR') {
return $this->processForwardedForHeader($_SERVER[$header]);

Check warning on line 542 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Direct use of $_SERVER Superglobal detected.
}

return $_SERVER[$header];

Check warning on line 545 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Direct use of $_SERVER Superglobal detected.
}

// return default remote IP address
return $_SERVER['REMOTE_ADDR'];

Check warning on line 549 in Helper/OmiseHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Direct use of $_SERVER Superglobal detected.
}

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;
}
}
47 changes: 47 additions & 0 deletions Helper/RequestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Omise\Payment\Helper;

class RequestHelper
{
public function get_client_ip()

Check warning on line 7 in Helper/RequestHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Method name &quot;RequestHelper::get_client_ip&quot; is not in camel caps format
{
$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) {

Check warning on line 22 in Helper/RequestHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected &quot;foreach (...) {\n&quot;; found &quot;foreach(...) {\n&quot;

Check warning on line 22 in Helper/RequestHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Expected 1 space(s) after FOREACH keyword; 0 found
if (empty($_SERVER[$header])) {

Check warning on line 23 in Helper/RequestHelper.php

View workflow job for this annotation

GitHub Actions / M2 Coding Standard

Direct use of $_SERVER Superglobal detected.
continue;
}

if ($header === 'HTTP_X_FORWARDED_FOR') {
return self::process_forwarded_for_header($_SERVER[$header]);
}

return $_SERVER[$header];
}

// return default remote IP address
return $_SERVER['REMOTE_ADDR'];
}

private function process_forwarded_for_header($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;
}
}
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';
}
29 changes: 29 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,35 @@
</field>
</group>

<group id="omise_offsite_wechat_pay" translate="label" sortOrder="536" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable WeChat Pay Payment</label>
<comment>Enable customers to pay using WeChat Pay payment method</comment>
<field id="active" translate="label comment" type="select" sortOrder="537" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable/Disable</label>
<comment>Enable PayPay payment method.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>payment/omise_offsite_wechat_pay/active</config_path>
</field>
<field id="title" translate="label" type="text" sortOrder="538" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Title</label>
<comment>This controls the title which the user sees during checkout.</comment>
<config_path>payment/omise_offsite_wechat_pay/title</config_path>
</field>
<field id="allowspecific" translate="label comment" type="select" sortOrder="539" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Allowed Countries</label>
<config_path>payment/omise_offsite_wechat_pay/allowspecific</config_path>
<source_model>Magento\Payment\Model\Config\Source\Allspecificcountries</source_model>
<comment>If not set to all, guest customers will not have a billing country and may not be able to check out.</comment>
</field>
<field id="specificcountry" translate="label" type="multiselect" sortOrder="540" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Payment from specific countries</label>
<config_path>payment/omise_offsite_wechat_pay/specificcountry</config_path>
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
<depends>
<field id="allowspecific">1</field>
</depends>
</field>
</group>
</group>
</section>
</system>
Expand Down
14 changes: 14 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@
<can_refund>1</can_refund>
<payment_action>authorize_capture</payment_action>
</omise_offsite_paypay>

<omise_offsite_wechat_pay>
<active>0</active>
<title>WeChat Pay</title>
<model>OmiseWeChatPayAdapter</model>
<is_gateway>1</is_gateway>
<can_initialize>1</can_initialize>
<can_use_checkout>1</can_use_checkout>
<can_capture>1</can_capture>
<can_review_payment>1</can_review_payment>
<can_refund_partial_per_invoice>1</can_refund_partial_per_invoice>
<can_refund>1</can_refund>
<payment_action>authorize_capture</payment_action>
</omise_offsite_wechat_pay>
</payment>
</default>
</config>
48 changes: 48 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,54 @@
</virtualType>
<!-- /Google Pay :: Config -->

<!-- WeChatPay payment solution -->
<virtualType name="OmiseWeChatPayAdapter" type="Magento\Payment\Model\Method\Adapter">
<arguments>
<argument name="code" xsi:type="const">Omise\Payment\Model\Config\WeChatPay::CODE</argument>
<argument name="formBlockType" xsi:type="string">Magento\Payment\Block\Form</argument>
<argument name="infoBlockType" xsi:type="string">Magento\Payment\Block\Info</argument>
<argument name="valueHandlerPool" xsi:type="object">OmiseAPMWeChatPayValueHandlerPool</argument>
<argument name="validatorPool" xsi:type="object">OmiseAPMWeChatPayValidatorPool</argument>
<argument name="commandPool" xsi:type="object">OmiseAPMCommandPool</argument>
</arguments>
</virtualType>

<!-- WeChatPay :: Value Handler Pool -->
<virtualType name="OmiseAPMWeChatPayValueHandlerPool" type="Magento\Payment\Gateway\Config\ValueHandlerPool">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="default" xsi:type="string">OmiseAPMWeChatPayConfigValueHandler</item>
</argument>
</arguments>
</virtualType>

<virtualType name="OmiseAPMWeChatPayValidatorPool" type="Magento\Payment\Gateway\Validator\ValidatorPool">
<arguments>
<argument name="validators" xsi:type="array">
<item name="country" xsi:type="string">OmiseAPMWeChatPayCountryValidator</item>
</argument>
</arguments>
</virtualType>

<virtualType name="OmiseAPMWeChatPayCountryValidator" type="Magento\Payment\Gateway\Validator\CountryValidator">
<arguments>
<argument name="config" xsi:type="object">OmiseAPMWeChatPayConfig</argument>
</arguments>
</virtualType>

<virtualType name="OmiseAPMWeChatPayConfigValueHandler" type="Magento\Payment\Gateway\Config\ConfigValueHandler">
<arguments>
<argument name="configInterface" xsi:type="object">OmiseAPMWeChatPayConfig</argument>
</arguments>
</virtualType>

<virtualType name="OmiseAPMWeChatPayConfig" type="Magento\Payment\Gateway\Config\Config">
<arguments>
<argument name="methodCode" xsi:type="const">Omise\Payment\Model\Config\WeChatPay::CODE</argument>
</arguments>
</virtualType>
<!-- /WeChatPay payment :: Value Handler -->

<!-- Credit Card payment solution -->
<virtualType name="OmiseCcAdapter" type="Magento\Payment\Model\Method\Adapter">
<arguments>
Expand Down
3 changes: 3 additions & 0 deletions view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<item name="omise_offsite_paypay" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
</item>
<item name="omise_offsite_wechat_pay" xsi:type="array">
<item name="isBillingAddressRequired" xsi:type="boolean">true</item>
</item>
</item>
</item>
</item>
Expand Down
3 changes: 3 additions & 0 deletions view/frontend/web/images/wechat_pay.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
define(
[
'ko',
'Omise_Payment/js/view/payment/omise-offsite-method-renderer',
'Magento_Checkout/js/view/payment/default',
'Magento_Checkout/js/model/quote',
],
function (
ko,
Base,
Component,
quote
) {
'use strict';

return Component.extend(Base).extend({
defaults: {
template: 'Omise_Payment/payment/offsite-common-form'
},

isPlaceOrderActionAllowed: ko.observable(quote.billingAddress() != null),

code: 'omise_offsite_wechat_pay',
restrictedToCurrencies: ['thb'],
logo: {
file: "images/wechat_pay.svg",
width: "30",
height: "30",
name: "wechat_pay"
}
});
}
);

0 comments on commit 0e31693

Please sign in to comment.