Skip to content

Commit

Permalink
Fix card payment.
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishgurung committed Oct 7, 2024
1 parent 0ebc588 commit 9660eb2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 10 deletions.
48 changes: 47 additions & 1 deletion assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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 googlePay() {
window.addEventListener('loadpaymentdata', event => {
document.getElementById('place_order').style.display = 'inline-block';
Expand Down Expand Up @@ -206,7 +231,7 @@

function initializeSecureCardForm() {
const omiseCardElement = document.getElementById('omise-card');
if (omiseCardElement && Boolean(omise_params.secure_form_enabled) && $('#payment_method_omise').is(':checked')) {
if (omiseCardElement && $('#payment_method_omise').is(':checked')) {
showOmiseEmbeddedCardForm({
element: omiseCardElement,
publicKey: omise_params.key,
Expand All @@ -226,10 +251,31 @@
}
}

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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'd2aee42914cfc4b82c03f61eae373fe9');
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => '570d41a8416c53741769f934a3335f8d');
2 changes: 1 addition & 1 deletion includes/blocks/assets/js/build/omise_installment.js

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

4 changes: 2 additions & 2 deletions includes/blocks/assets/js/omise-installment.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ const InstallmentPaymentMethod = (props) => {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
"source": wlbInstallmentRef.current.source,
"token": wlbInstallmentRef.current.token,
"omise_source": wlbInstallmentRef.current.source,
"omise_token": wlbInstallmentRef.current.token,
}
}
};
Expand Down
20 changes: 17 additions & 3 deletions includes/gateway/class-omise-payment-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public function charge($order_id, $order)
$this->id . "_callback"
);
$requestData['description'] = 'staging';
$requestData['source'] = isset($_POST['source']) ? wc_clean($_POST['source']) : '';
$requestData['card'] = isset($_POST['token']) ? wc_clean($_POST['token']) : '';
$requestData['source'] = isset($_POST['omise_source']) ? wc_clean($_POST['omise_source']) : '';
$requestData['card'] = isset($_POST['omise_token']) ? wc_clean($_POST['omise_token']) : '';
return OmiseCharge::create($requestData);
}

Expand All @@ -151,7 +151,7 @@ public function is_capability_support($available_payment_methods)
* @codeCoverageIgnore
*/
public function omise_scripts() {
if ( is_checkout() && $this->is_available() ) {
if ( is_checkout()) {
wp_enqueue_script(
'omise-js',
Omise::OMISE_JS_LINK,
Expand All @@ -175,6 +175,20 @@ public function omise_scripts() {
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->convert_to_cents($this->get_total_amount()),
];
}
}
2 changes: 1 addition & 1 deletion templates/payment/form-installment.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
// @codeCoverageIgnoreStart
if ( ! empty( $viewData['installment_backends'] ) ) : ?>
if ( ! empty( $viewData['installments_enabled'] ) ) : ?>
<div id="omise-installment" style="width:100%; max-width: 400px;"></div>
<script>
window.LOCALE = `<?php echo get_locale(); ?>`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function set_additional_data()
$result = $property->getValue($this->obj);

$this->assertIsArray($result);
// $this->assertArrayHasKey('is_zero_interest', $result);
$this->assertArrayHasKey('installment_min_limit', $result);
$this->assertArrayHasKey('installments_enabled', $result);
$this->assertArrayHasKey('total_amount', $result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ public function getInstallmentBackends() {
$this->assertArrayHasKey('installment_min_limit', $result);
}

public function testGetParamsForJS()
{
$clazz = new stdClass();
$clazz->cart = new stdClass();
$clazz->cart->total = 999999;

Monkey\Functions\expect('WC')->andReturn($clazz);
Monkey\Functions\expect('add_action');
$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()
{
Monkey\Functions\expect('add_action');
Expand Down

0 comments on commit 9660eb2

Please sign in to comment.