Skip to content

Commit

Permalink
[MIT-1912] Implement whitelabel installment
Browse files Browse the repository at this point in the history
move omise token and source inside get_charge_request

fix test

update test

ignore coverage for view form
  • Loading branch information
mmkittisak authored and aashishgurung committed Oct 4, 2024
1 parent aa34827 commit c20f13f
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 91 deletions.
28 changes: 28 additions & 0 deletions assets/javascripts/omise-installment-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function showOmiseInstallmentForm({
element,
publicKey,
onSuccess,
onError,
locale,
amount,
}) {
const noop = () => { }

element.style.height = 500 + 'px'

OmiseCard.configure({
publicKey: publicKey,
amount,
element,
iframeAppId: 'omise-checkout-installment-form',
customCardForm: false,
customInstallmentForm: true,
locale: locale,
defaultPaymentMethod: 'installment'
});

OmiseCard.open({
onCreateSuccess: onSuccess ?? noop,
onError: onError ?? noop
});
}
114 changes: 93 additions & 21 deletions assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@
}
}

function omiseInstallmentFormHandler() {
function getSelectedCardId() {
const $selected_card_id = $("input[name='card_id']:checked");
if ($selected_card_id.length > 0) {
return $selected_card_id.val();
}

return "";
}

if ($('#payment_method_omise_installment').is(':checked')) {
if (getSelectedCardId() !== "") {
//submit the form right away if the card_id is not blank
return true;
}

if (0 === $('input.omise_token').length && 0 === $('input.omise_source').length) {
requestCardToken();
return false;
}
return true;
}
return true;
}

function traditionalForm() {
$form.block({
message: null,
Expand Down Expand Up @@ -250,42 +275,84 @@
$('.omise_save_customer_card').val(payload.remember)
}
$form.append('<input type="hidden" class="omise_token" name="omise_token" value="' + payload.token + '"/>');
if (payload.source) {
$form.append('<input type="hidden" class="omise_source" name="omise_source" value="' + payload.source + '"/>');
}
$form.submit();
} else {
if (payload.source) {
$form.append('<input type="hidden" class="omise_source" name="omise_source" value="' + payload.source + '"/>');
}
$form.submit();
}
}

function initializeSecureCardForm() {
// This will be undefined when WC block is enabled in Checkout page
if (typeof HIDE_REMEMBER_CARD !== 'undefined') {
const omiseCardElement = document.getElementById('omise-card');
if (omiseCardElement && Boolean(omise_params.secure_form_enabled)) {
showOmiseEmbeddedCardForm({
element: omiseCardElement,
publicKey: omise_params.key,
hideRememberCard: HIDE_REMEMBER_CARD,
locale: LOCALE,
theme: CARD_FORM_THEME ?? 'light',
design: FORM_DESIGN,
brandIcons: CARD_BRAND_ICONS,
onSuccess: handleCreateOrder,
onError: (error) => {
showError(error)
$form.unblock()
}
})
}
const omiseCardElement = document.getElementById('omise-card');
if (omiseCardElement && Boolean(omise_params.secure_form_enabled) && $('#payment_method_omise').is(':checked')) {
showOmiseEmbeddedCardForm({
element: omiseCardElement,
publicKey: omise_params.key,
hideRememberCard: HIDE_REMEMBER_CARD,
locale: LOCALE,
theme: CARD_FORM_THEME ?? 'light',
design: FORM_DESIGN,
brandIcons: CARD_BRAND_ICONS,
onSuccess: handleCreateOrder,
onError: (error) => {
showError(error)
$form.unblock()
}
})
} else {
OmiseCard.destroy();
}
}

function initializeInstallmentForm() {
const omiseInstallmentElement = document.getElementById('omise-installment');
if (omiseInstallmentElement && $('#payment_method_omise_installment').is(':checked')){
showOmiseInstallmentForm({
element: omiseInstallmentElement,
publicKey: omise_installment_params.key,
amount: omise_installment_params.amount,
locale: LOCALE,
onSuccess: handleCreateOrder,
onError: (error) => {
showError(error)
$form.unblock()
}
})
} else {
OmiseCard.destroy();
}
}

function setupOmiseForm() {
var selectedPaymentMethod = $('input[name="payment_method"]:checked').val();
if (selectedPaymentMethod === 'omise') {
initializeSecureCardForm();
} else if (selectedPaymentMethod === 'omise_installment') {
initializeInstallmentForm();
} else {
OmiseCard.destroy();
}
}

$(function () {
$('body').on('checkout_error', function () {
$('.omise_token').remove();
$('.omise_source').remove();
});

$('form.checkout').unbind('checkout_place_order_omise');
$('form.checkout').on('checkout_place_order_omise', function () {
return omiseFormHandler();
});
$('form.checkout').unbind('checkout_place_order_omise_installment');
$('form.checkout').on('checkout_place_order_omise_installment', function () {
return omiseInstallmentFormHandler();
});

/* Pay Page Form */
$('form#order_review').on('submit', function () {
Expand All @@ -295,13 +362,18 @@
/* Both Forms */
$('form.checkout, form#order_review').on('change', '#omise_cc_form input', function() {
$('.omise_token').remove();
$('.omise_source').remove();
});

$('form.checkout').on('change', 'input[name="payment_method"]', function() {
setupOmiseForm();
});

$(document).on('updated_checkout', function () {
initializeSecureCardForm();
setupOmiseForm();
});

initializeSecureCardForm();
setupOmiseForm();
googlePay();
})
})(jQuery)
59 changes: 56 additions & 3 deletions includes/gateway/class-omise-payment-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct()
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_order_action_' . $this->id . '_sync_payment', array($this, 'sync_payment'));
add_action('woocommerce_api_' . $this->id . '_callback', 'Omise_Callback::execute');
add_action('wp_enqueue_scripts', array( $this, 'omise_scripts' ));
}

/**
Expand Down Expand Up @@ -107,6 +108,14 @@ public function get_total_amount()
return WC()->cart->total;
}

/**
* Get the total amount of an order in cents
*/
public function convertToCents($amount)
{
return intval(floatval($amount) * 100);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -140,9 +149,8 @@ public function get_charge_request($order_id, $order)
$payload['zero_interest_installments'] = $provider['zero_interest_installments'];
}

$requestData['source'] = array_merge($requestData['source'], [
'installment_terms' => sanitize_text_field($installment_terms)
]);
$requestData['source'] = isset( $_POST['omise_source'] ) ? wc_clean( $_POST['omise_source'] ) : '';
$requestData['card'] = isset( $_POST['omise_token'] ) ? wc_clean( $_POST['omise_token'] ) : '';

return $requestData;
}
Expand All @@ -158,4 +166,49 @@ public function is_capability_support($available_payment_methods)
{
return preg_grep('/^installment_/', $available_payment_methods);
}

/**
* @codeCoverageIgnore
*/
public function omise_scripts() {
if ( is_checkout() && $this->is_available() ) {
wp_enqueue_script(
'omise-js',
Omise::OMISE_JS_LINK,
[ 'jquery' ],
OMISE_WOOCOMMERCE_PLUGIN_VERSION,
true
);

wp_enqueue_script(
'omise-installment-form',
plugins_url( '../../assets/javascripts/omise-installment-form.js', __FILE__ ),
[ 'omise-js' ],
OMISE_WOOCOMMERCE_PLUGIN_VERSION,
true
);

wp_enqueue_script(
'omise-payment-form-handler',
plugins_url( '../../assets/javascripts/omise-payment-form-handler.js', __FILE__ ),
[ 'omise-js' ],
OMISE_WOOCOMMERCE_PLUGIN_VERSION,
true
);

wp_localize_script(
'omise-payment-form-handler',
'omise_installment_params',
$this->getParamsForJS()
);
}
}

public function getParamsForJS()
{
return [
'key' => $this->public_key(),
'amount' => $this->convertToCents($this->getTotalAmount()),
];
}
}
58 changes: 11 additions & 47 deletions templates/payment/form-installment.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,11 @@
<?php if ( ! empty( $viewData['installment_backends'] ) ) : ?>
<fieldset id="omise-form-installment">
<ul class="omise-banks-list">
<?php foreach ( $viewData['installment_backends'] as $backend ) : ?>
<li class="item">
<input id="<?php echo $backend->_id; ?>" type="radio" name="source[type]" value="<?php echo $backend->_id; ?>" />
<label for="<?php echo $backend->_id; ?>">
<div class="bank-logo <?php echo $backend->provider_code; ?>"></div>
<div class="bank-label">
<span class="title"><?php echo $backend->provider_name; ?></span><br/>
<select id="<?php echo $backend->_id; ?>_installment_terms" name="<?php echo $backend->_id; ?>_installment_terms" class="installment-term-select-box">
<option>Select term</option>
<?php foreach ( $backend->available_plans as $installment_plan ) : ?>
<option value="<?php echo $installment_plan['term_length']; ?>">
<?php
echo sprintf(
__( '%d months', 'omise' ),
$installment_plan['term_length']
);
?>

<?php
echo sprintf(
__( '( %s / months )', 'omise' ),
wc_price( $installment_plan['monthly_amount'] )
);
?>
</option>
<?php endforeach; ?>
</select>
<?php if ( ! $viewData['is_zero_interest'] ): ?>
<br/><span class="omise-installment-interest-rate">
<?php echo sprintf( __( '( interest %g%% )', 'omise' ), $backend->interest_rate ); ?>
</span>
<?php endif; ?>
</div>
</label>
</li>
<?php endforeach; ?>
</ul>
<div class="omise-buttom-note">
<p>
<?php echo $viewData['is_zero_interest'] ? __( 'All installment payments are interest free', 'omise' ) : __( 'Monthly payment rates shown may be inaccurate as interest rates are subject to change by its bank issuer.', 'omise' ); ?>
</p>
</div>
</fieldset>
<?php
// @codeCoverageIgnoreStart
if ( ! empty( $viewData['installment_backends'] ) ) : ?>
<div id="omise-installment" style="width:100%; max-width: 400px;"></div>
<script>
window.LOCALE = `<?php echo get_locale(); ?>`;
window.OMISE_CUSTOM_FONT_OTHER = 'Other';
</script>
<?php else: ?>
<p>
<?php
Expand All @@ -54,4 +16,6 @@
}
?>
</p>
<?php endif; ?>
<?php endif;
// @codeCoverageIgnoreEnd
?>
4 changes: 4 additions & 0 deletions tests/unit/includes/gateway/class-omise-offsite-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ protected function setUp(): void
$offsite->shouldReceive('init_settings');
$offsite->shouldReceive('get_option');
$offsite->shouldReceive('get_provider');
$offsite->shouldReceive('public_key')
->andReturn('pkey_test_123');
$offsite->shouldReceive('is_available')
->andReturn(true);
$offsite->shouldReceive('build_charge_request')
->andReturn([
'source' => [ 'type' => $this->sourceType ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,6 @@ public function get_total_amount_from_cart()
$this->assertEquals($total, 999999);
}

public function test_get_charge_request()
{
$this->backend_installment_mock->shouldReceive('get_provider');

Monkey\Functions\expect('add_action');

$expectedAmount = 999999;
$expectedCurrency = 'thb';
$orderId = 'order_123';
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

$_POST['source'] = ['type' => $this->sourceType];
$_POST[$this->sourceType . '_installment_terms'] = 3;

$installment = new Omise_Payment_Installment();
$result = $installment->get_charge_request($orderId, $orderMock);

$this->assertEquals($this->sourceType, $result['source']['type']);
}

public function test_charge()
{
$this->backend_installment_mock->shouldReceive('get_provider');
Expand Down

0 comments on commit c20f13f

Please sign in to comment.