Skip to content

Commit

Permalink
Integrate TrueMoney payment with WC blocks (#458)
Browse files Browse the repository at this point in the history
* Integrated installment with WC blocks.

* Set terms to null if bank is changed.

* removed commented if statement

* Integrated FPX with WC blocks.

* Integrated Atome payment with WC blocks.

* Integrate Truemoney payment with WC block.

* Fix test

---------

Co-authored-by: Aashish <aashish@omise.co>
  • Loading branch information
aashishgurung and Aashish authored May 28, 2024
1 parent 52db0cf commit ac676b1
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 8 deletions.
1 change: 1 addition & 0 deletions includes/blocks/assets/js/build/omise_truemoney.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'afcc99074584615987473fd96623c09d');
1 change: 1 addition & 0 deletions includes/blocks/assets/js/build/omise_truemoney.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions includes/blocks/assets/js/omise-truemoney.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import {useEffect, useState} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
import { getSetting } from '@woocommerce/settings';

const settings = getSetting( 'omise_truemoney_data', {} )
const label = decodeEntities( settings.title ) || 'No title set'
const Label = ( props ) => {
const { PaymentMethodLabel } = props.components
return <PaymentMethodLabel text={ label } />
}

const TruemoneyPaymentMethod = ({content, ...props}) => {
const Content = content;
const {eventRegistration, emitResponse} = props;
const {onPaymentSetup} = eventRegistration;
const description = decodeEntities( settings.description.trim() || '' )
const { is_wallet} = settings.data;
const [showPhoneField, setShowPhoneField] = useState(false)
const [useDefaultPhoneNumber, setUseDefaultPhoneNumber] = useState(true)
const [phoneNumber, setPhoneNumber] = useState('')

const onChangeDefaultPhoneNumber = (e) => {
setUseDefaultPhoneNumber(!useDefaultPhoneNumber)
setShowPhoneField(!showPhoneField)

if (useDefaultPhoneNumber) {
setPhoneNumber('');
}
}

const onChangePhoneNumber = (e) => {
setPhoneNumber(e.target.value);
}

useEffect(() => {
const unsubscribe = onPaymentSetup(async () => {
if (!useDefaultPhoneNumber && phoneNumber.length === 0) {
return {type: emitResponse.responseTypes.ERROR, message: 'Enter a phone number'}
}
try {
return {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
"omise_phone_number_default": (useDefaultPhoneNumber ? 1 : 0).toString(),
'omise_phone_number': phoneNumber,
}
}
};
} catch (error) {
return {type: emitResponse.responseTypes.ERROR, message: error.message}
}
});
return () => unsubscribe();
}, [
onPaymentSetup,
emitResponse.responseTypes.ERROR,
emitResponse.responseTypes.SUCCESS,
useDefaultPhoneNumber,
phoneNumber
]);

return (<>
{description && <p>{description}</p>}
{
is_wallet && (
<fieldset id="omise-form-truemoney">
{ __( 'TrueMoney phone number', 'omise' ) }<br/>

<p id="omise_phone_number_default_field" className="form-row form-row-wide omise-label-inline">
<input
id="omise_phone_number_default"
type="checkbox"
name="omise_phone_number_default"
value={useDefaultPhoneNumber}
defaultChecked={true}
onChange={onChangeDefaultPhoneNumber}
/>
<label htmlFor="omise_phone_number_default">{ __( 'Same as Billing Detail', 'omise' ) }</label>
</p>

<p id="omise_phone_number_field" className="form-row form-row-wide" style={{display: showPhoneField ? "block" : "none"}}>
<span className="woocommerce-input-wrapper">
<input
id="omise_phone_number"
className="input-text"
name="omise_phone_number"
type="tel"
autoComplete="off"
onChange={onChangePhoneNumber}
/>
</span>
</p>

<p className="omise-secondary-text">
{ __( 'One-Time Password (OTP) will be sent to the phone number above', 'omise' ) }
</p>
</fieldset>
)
}
</>)
}

registerPaymentMethod( {
name: settings.name,
label: <Label />,
content: <TruemoneyPaymentMethod />,
edit: <TruemoneyPaymentMethod />,
canMakePayment: () => true,
ariaLabel: label,
supports: {
features: settings.supports,
}
} )
2 changes: 1 addition & 1 deletion includes/blocks/gateways/abstract-omise-block-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function is_active() {
return $this->gateway->is_available();
}

/**
/**
* Returns an array of scripts/handles to be registered for this payment method.
*
* @return array
Expand Down
14 changes: 14 additions & 0 deletions includes/blocks/gateways/omise-block-truemoney.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class Omise_Block_Truemoney extends Omise_Block_Payment {
/**
* Payment method name/id/slug.
*
* @var string
*/
protected $name = 'omise_truemoney';

public function set_additional_data() {
$this->additional_data = [ 'is_wallet' => $this->gateway->is_wallet() ];
}
}
1 change: 1 addition & 0 deletions includes/blocks/omise-block-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Omise_Block_Payments {
Omise_Block_Installment::class,
Omise_Block_Fpx::class,
Omise_Block_Atome::class,
Omise_Block_Truemoney::class,
];

function __construct($container) {
Expand Down
1 change: 0 additions & 1 deletion includes/gateway/class-omise-payment-promptpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public function display_qrcode( $order, $context = 'view' ) {
}

$charge = OmiseCharge::retrieve( $this->get_charge_id_from_order() );
error_log(print_r($charge['return_uri'], true));
if ( $this->get_pending_status() !== $charge['status'] ) {
return;
}
Expand Down
23 changes: 18 additions & 5 deletions includes/gateway/class-omise-payment-truemoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct()
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->restricted_countries = array( 'TH' );
$this->source_type = $this->get_source();
$this->source_type = null;

add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_api_' . $this->id . '_callback', 'Omise_Callback::execute' );
Expand Down Expand Up @@ -75,30 +75,43 @@ public function init_form_fields()
public function payment_fields()
{
parent::payment_fields();
if (self::WALLET === $this->source_type) {
if ($this->is_wallet()) {
Omise_Util::render_view( 'templates/payment/form-truemoney.php', [] );
}
}

public function is_wallet()
{
// Moved the logic to set source type from constructor because
// the Capability API is not being called from WC blocks.
if (!$this->source_type) {
$this->source_type = $this->get_source();
}

return self::WALLET === $this->source_type;
}

/**
* @inheritdoc
*/
public function charge($order_id, $order)
{
$requestData = $this->get_charge_request($order_id, $order);
return OmiseCharge::create($requestData);
$request_data = $this->get_charge_request($order_id, $order);
return OmiseCharge::create($request_data);
}

public function get_charge_request($order_id, $order)
{
$is_wallet = $this->is_wallet();

$request_data = $this->build_charge_request(
$order_id,
$order,
$this->source_type,
$this->id . '_callback'
);

if (self::WALLET === $this->source_type) {
if ($is_wallet) {
$phone_option = $_POST['omise_phone_number_default'];
$is_phone_option_checked = isset($phone_option) && 1 == $phone_option;
$phone_number = $is_phone_option_checked ?
Expand Down
1 change: 1 addition & 0 deletions omise-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function block_init()
require_once __DIR__ . '/includes/blocks/gateways/omise-block-installment.php';
require_once __DIR__ . '/includes/blocks/gateways/omise-block-fpx.php';
require_once __DIR__ . '/includes/blocks/gateways/omise-block-atome.php';
require_once __DIR__ . '/includes/blocks/gateways/omise-block-truemoney.php';
Omise_Block::init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function setUp(): void

public function test_get_charge_request()
{
$this->omise_capability_mock->shouldReceive('retrieve')->once();
// set source type to truemoney wallet
$obj = new Omise_Payment_Truemoney();
$obj->source_type = 'truemoney';
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
'omise_installment': '/includes/blocks/assets/js/omise-installment.js',
'omise_fpx': '/includes/blocks/assets/js/omise-fpx.js',
'omise_atome': '/includes/blocks/assets/js/omise-atome.js',
'omise_truemoney': '/includes/blocks/assets/js/omise-truemoney.js',
},
output: {
path: path.resolve( __dirname, 'includes/blocks/assets/js/build' ),
Expand Down

0 comments on commit ac676b1

Please sign in to comment.