Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[MIT-1912] Implement whitelabel installment" #466

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions assets/javascripts/omise-installment-form.js

This file was deleted.

81 changes: 3 additions & 78 deletions assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,6 @@
}
}

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 @@ -275,21 +250,13 @@
$('.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() {
const omiseCardElement = document.getElementById('omise-card');
if (omiseCardElement && Boolean(omise_params.secure_form_enabled) && $('#payment_method_omise').is(':checked')) {
if (omiseCardElement && Boolean(omise_params.secure_form_enabled)) {
showOmiseEmbeddedCardForm({
element: omiseCardElement,
publicKey: omise_params.key,
Expand All @@ -304,55 +271,18 @@
$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 @@ -362,18 +292,13 @@
/* 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 () {
setupOmiseForm();
initializeSecureCardForm();
});

setupOmiseForm();
initializeSecureCardForm();
googlePay();
})
})(jQuery)
59 changes: 3 additions & 56 deletions includes/gateway/class-omise-payment-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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 @@ -106,14 +105,6 @@ public function getTotalAmount()
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 @@ -142,8 +133,9 @@ public function get_charge_request($order_id, $order)
$payload['zero_interest_installments'] = $provider['zero_interest_installments'];
}

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

return $requestData;
}
Expand All @@ -159,49 +151,4 @@ 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: 47 additions & 11 deletions templates/payment/form-installment.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
<?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 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 else: ?>
<p>
<?php
Expand All @@ -16,6 +54,4 @@
}
?>
</p>
<?php endif;
// @codeCoverageIgnoreEnd
?>
<?php endif; ?>
4 changes: 0 additions & 4 deletions tests/unit/includes/gateway/class-omise-offsite-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public 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 @@ -72,23 +72,18 @@ function WC() {

public function testGetChargeRequest()
{
if (!function_exists('wc_clean')) {
function wc_clean() {
return 'src_test_123';
}
}
$expectedAmount = 999999;
$expectedCurrency = 'thb';
$orderId = 'order_123';
$orderMock = $this->getOrderMock($expectedAmount, $expectedCurrency);

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

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

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

public function testCharge()
Expand All @@ -99,32 +94,4 @@ public function testCharge()
$obj = new Omise_Payment_Installment();
$this->getChargeTest($obj);
}

public function testGetParamsForJS()
{
$mock = Mockery::mock('overload:Omise_Payment_Offsite');
$instance = new Omise_Payment_Installment($mock);
$result = $instance->getParamsForJS();

$this->assertIsArray($result);
$this->assertArrayHasKey('key', $result);
$this->assertArrayHasKey('amount', $result);
$this->assertEquals('pkey_test_123', $result['key']);
$this->assertEquals(99999900, $result['amount']);
}

public function testConvertToCents()
{
$instance = new Omise_Payment_Installment();
$this->assertEquals(100, $instance->convertToCents(1.00));
$this->assertEquals(150, $instance->convertToCents(1.50));
$this->assertEquals(0, $instance->convertToCents(0.00));

$this->assertEquals(10000, $instance->convertToCents(100));
$this->assertEquals(0, $instance->convertToCents(0));

$this->assertEquals(100, $instance->convertToCents('1.00'));
$this->assertEquals(0, $instance->convertToCents('0.00'));
$this->assertEquals(10000, $instance->convertToCents('100'));
}
}
Loading