Skip to content

Commit c1c925b

Browse files
Update payment render (#290)
* finish v1 * finish update rms * update from back * 🧼 clean up * clean up 🧽 v2 * fix Indentation * fix sonar recommend * update comments according to comment * a bit typo * Update includes/gateway/class-omise-payment.php Co-authored-by: aashishgurung <[email protected]> * handle capabilities excption * remove to do after problems got fixed * Update includes/gateway/class-omise-payment.php Co-authored-by: aashishgurung <[email protected]> * Update class-omise-payment.php * Revert "Update class-omise-payment.php" This reverts commit fd0ce38. * try fix indentation Co-authored-by: aashishgurung <[email protected]>
1 parent 665d76e commit c1c925b

28 files changed

+258
-164
lines changed

assets/javascripts/omise-payment-form-handler.js

+79-80
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
1-
(function ( $, undefined ) {
2-
var $form = $( 'form.checkout, form#order_review' );
1+
(function ($, undefined) {
2+
var $form = $('form.checkout, form#order_review');
33

4-
function hideError(){
4+
function hideError() {
55
$(".woocommerce-error").remove();
66
}
77

8-
function showError(message){
9-
if(!message){
8+
function showError(message) {
9+
if (!message) {
1010
return;
1111
}
1212
$(".woocommerce-error, input.omise_token").remove();
13-
13+
1414
$ulError = $("<ul>").addClass("woocommerce-error");
15-
16-
if($.isArray(message)){
17-
$.each(message, function(i,v){
15+
16+
if ($.isArray(message)) {
17+
$.each(message, function (i, v) {
1818
$ulError.append($("<li>" + v + "</li>"));
1919
})
20-
}else{
20+
} else {
2121
$ulError.html("<li>" + message + "</li>");
2222
}
23-
24-
$form.prepend( $ulError );
25-
$("html, body").animate({ scrollTop:0 },"slow");
23+
24+
$form.prepend($ulError);
25+
$("html, body").animate({ scrollTop: 0 }, "slow");
2626
}
27-
28-
function omiseFormHandler(){
29-
function validSelection(){
27+
28+
function omiseFormHandler() {
29+
function validSelection() {
3030
$card_list = $("input[name='card_id']");
3131
$selected_card_id = $("input[name='card_id']:checked");
3232
// there is some existing cards but nothing selected then warning
33-
if($card_list.length > 0 && $selected_card_id.length === 0){
33+
if ($card_list.length > 0 && $selected_card_id.length === 0) {
3434
return false;
3535
}
36-
36+
3737
return true;
3838
}
39-
40-
function getSelectedCardId(){
39+
40+
function getSelectedCardId() {
4141
$selected_card_id = $("input[name='card_id']:checked");
42-
if($selected_card_id.length > 0){
42+
if ($selected_card_id.length > 0) {
4343
return $selected_card_id.val();
4444
}
45-
45+
4646
return "";
4747
}
48-
49-
if ( $( '#payment_method_omise' ).is( ':checked' ) ) {
50-
if( !validSelection() ){
51-
showError( omise_params.no_card_selected );
48+
49+
if ($('#payment_method_omise').is(':checked')) {
50+
if (!validSelection()) {
51+
showError(omise_params.no_card_selected);
5252
return false;
5353
}
54-
55-
if( getSelectedCardId() !== "" )
56-
{
54+
55+
if (getSelectedCardId() !== "") {
5756
//submit the form right away if the card_id is not blank
5857
return true;
5958
}
60-
61-
if ( 0 === $( 'input.omise_token' ).length ) {
59+
60+
if (0 === $('input.omise_token').length) {
6261
$form.block({
6362
message: null,
6463
overlayCSS: {
@@ -73,62 +72,62 @@
7372
omise_card_number_field = 'number',
7473
omise_card_state_field = 'state',
7574
omise_card_fields = {
76-
'name' : $( '#omise_card_name' ),
77-
'number' : $( '#omise_card_number' ),
78-
'expiration_month' : $( '#omise_card_expiration_month' ),
79-
'expiration_year' : $( '#omise_card_expiration_year' ),
80-
'security_code' : $( '#omise_card_security_code' ),
81-
'city' : $( '#billing_city' ),
82-
'state' : $( '#billing_state' ),
83-
'country' : $( '#billing_country' ),
84-
'postal_code' : $( '#billing_postcode' ),
85-
'street1' : $( '#billing_address_1' )
75+
'name': $('#omise_card_name'),
76+
'number': $('#omise_card_number'),
77+
'expiration_month': $('#omise_card_expiration_month'),
78+
'expiration_year': $('#omise_card_expiration_year'),
79+
'security_code': $('#omise_card_security_code'),
80+
'city': $('#billing_city'),
81+
'state': $('#billing_state'),
82+
'country': $('#billing_country'),
83+
'postal_code': $('#billing_postcode'),
84+
'street1': $('#billing_address_1')
8685
};
8786

88-
$.each( omise_card_fields, function( index, field ) {
87+
$.each(omise_card_fields, function (index, field) {
8988
if (index === omise_card_state_field) {
90-
omise_card[ index ] = field.find(":selected").text();
89+
omise_card[index] = field.find(":selected").text();
9190
} else {
92-
omise_card[ index ] = (index === omise_card_number_field) ? field.val().replace(/\s/g, '') : field.val();
91+
omise_card[index] = (index === omise_card_number_field) ? field.val().replace(/\s/g, '') : field.val();
9392
}
9493

95-
if ( "" === omise_card[ index ] ) {
96-
errors.push( omise_params[ 'required_card_' + index ] );
94+
if ("" === omise_card[index]) {
95+
errors.push(omise_params['required_card_' + index]);
9796
}
98-
} );
97+
});
9998

100-
if ( errors.length > 0 ) {
99+
if (errors.length > 0) {
101100
showError(errors);
102101
$form.unblock();
103102
return false;
104103
}
105104

106105
hideError();
107106

108-
if(Omise){
107+
if (Omise) {
109108
Omise.setPublicKey(omise_params.key);
110109
Omise.createToken("card", omise_card, function (statusCode, response) {
111110
if (statusCode == 200) {
112-
$.each( omise_card_fields, function( index, field ) {
111+
$.each(omise_card_fields, function (index, field) {
113112
const sensitiveDataIndex = ['name', 'number', 'expiration_year', 'expiration_month', 'security_code'];
114113
if (sensitiveDataIndex.includes(index)) {
115-
field.val( '' );
114+
field.val('');
116115
}
117-
} );
118-
$form.append( '<input type="hidden" class="omise_token" name="omise_token" value="' + response.id + '"/>' );
116+
});
117+
$form.append('<input type="hidden" class="omise_token" name="omise_token" value="' + response.id + '"/>');
119118
$form.submit();
120119
} else {
121120
handleTokensApiError(response);
122121
};
123122
});
124123
} else {
125-
showError( omise_params.cannot_load_omisejs + '<br/>' + omise_params.check_internet_connection );
124+
showError(omise_params.cannot_load_omisejs + '<br/>' + omise_params.check_internet_connection);
126125
$form.unblock();
127126
}
128-
127+
129128
return false;
130129
}
131-
130+
132131
}
133132
}
134133

@@ -163,12 +162,12 @@
163162
document.getElementById('googlepay-text').classList.add('googlepay-selected');
164163

165164
const form = document.querySelector('form.checkout');
166-
const input = document.createElement('input');
165+
const input = document.createElement('input');
167166
input.setAttribute('type', 'hidden');
168167
input.setAttribute('class', 'omise_token');
169168
input.setAttribute('name', 'omise_token');
170169
input.setAttribute('value', response.id);
171-
form.appendChild(input) ;
170+
form.appendChild(input);
172171
}
173172
else {
174173
handleTokensApiError(response)
@@ -178,38 +177,38 @@
178177
}
179178

180179
function handleTokensApiError(response) {
181-
if ( response.object && 'error' === response.object && 'invalid_card' === response.code ) {
182-
showError( omise_params.invalid_card + "<br/>" + response.message );
183-
} else if(response.message){
184-
showError( omise_params.cannot_create_token + "<br/>" + response.message );
185-
}else if(response.responseJSON && response.responseJSON.message){
186-
showError( omise_params.cannot_create_token + "<br/>" + response.responseJSON.message );
187-
}else if(response.status==0){
188-
showError( omise_params.cannot_create_token + "<br/>" + omise_params.cannot_connect_api + omise_params.retry_checkout );
189-
}else {
190-
showError( omise_params.cannot_create_token + "<br/>" + omise_params.retry_checkout );
180+
if (response.object && 'error' === response.object && 'invalid_card' === response.code) {
181+
showError(omise_params.invalid_card + "<br/>" + response.message);
182+
} else if (response.message) {
183+
showError(omise_params.cannot_create_token + "<br/>" + response.message);
184+
} else if (response.responseJSON && response.responseJSON.message) {
185+
showError(omise_params.cannot_create_token + "<br/>" + response.responseJSON.message);
186+
} else if (response.status == 0) {
187+
showError(omise_params.cannot_create_token + "<br/>" + omise_params.cannot_connect_api + omise_params.retry_checkout);
188+
} else {
189+
showError(omise_params.cannot_create_token + "<br/>" + omise_params.retry_checkout);
191190
}
192191
$form.unblock();
193192
}
194-
195-
$(function(){
196-
$( 'body' ).on( 'checkout_error', function () {
197-
$( '.omise_token' ).remove();
193+
194+
$(function () {
195+
$('body').on('checkout_error', function () {
196+
$('.omise_token').remove();
198197
});
199-
200-
$( 'form.checkout' ).unbind('checkout_place_order_omise');
201-
$( 'form.checkout' ).on( 'checkout_place_order_omise', function () {
198+
199+
$('form.checkout').unbind('checkout_place_order_omise');
200+
$('form.checkout').on('checkout_place_order_omise', function () {
202201
return omiseFormHandler();
203202
});
204-
203+
205204
/* Pay Page Form */
206-
$( 'form#order_review' ).on( 'submit', function () {
205+
$('form#order_review').on('submit', function () {
207206
return omiseFormHandler();
208207
});
209-
208+
210209
/* Both Forms */
211-
$( 'form.checkout, form#order_review' ).on( 'change', '#omise_cc_form input', function() {
212-
$( '.omise_token' ).remove();
210+
$('form.checkout, form#order_review').on('change', '#omise_cc_form input', function () {
211+
$('.omise_token').remove();
213212
});
214213

215214
googlePay();

includes/admin/class-omise-page-settings.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ protected function save( $data ) {
2727
$data['account_email'] = $account['email'];
2828
$data['account_country'] = $account['country'];
2929

30+
$capabilities = Omise_Capabilities::retrieve( $public_key, $secret_key );
31+
$backends = $capabilities->getBackends();
32+
$data['backends'] = $backends;
33+
3034
$this->update_settings( $data );
3135
$this->add_message(
3236
'message',
@@ -51,7 +55,13 @@ public static function render() {
5155
}
5256

5357
$settings = $page->get_settings();
54-
58+
59+
$available_payment_methods = array();
60+
$capabilities = Omise_Capabilities::retrieve();
61+
62+
if ( $capabilities ){
63+
$available_payment_methods = $capabilities->get_available_payment_methods();
64+
}
5565
/**
5666
* Added later at Omise-WooCommerce v3.11.
5767
* To migrate all the users that haven been using Omise-WooCommerce

includes/admin/views/omise-page-settings.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@
136136

137137
<h3><?php _e( 'Payment Methods', 'omise' ); ?></h3>
138138
<?php if ($settings['account_country']) : ?>
139-
<p><?php _e( 'The table below is a list of available payment methods that you can enable in your WooCommerce store.', 'omise' ); ?></p>
139+
<!-- account_country exists when API key is valid -->
140+
<p><?php _e( 'The table below is a list of available payment methods for this omise account that you can enable in your WooCommerce store.', 'omise' ); ?></p>
140141
<table class="form-table">
141142
<tbody>
142143
<tr>
@@ -172,8 +173,7 @@
172173
<?php
173174
foreach ( Omise()->payment_methods() as $gateway ) :
174175
$gateway = new $gateway;
175-
if ( $gateway->is_country_support( $settings['account_country'] ) ) :
176-
176+
if ( $gateway->is_country_support( $settings['account_country'] ) && $gateway->is_capability_support( $available_payment_methods) ) :
177177
echo '<tr>';
178178

179179
foreach ( $columns as $key => $column ) :

includes/backends/class-omise-backend-touch-n-go.php

-41
This file was deleted.

includes/class-omise-capabilities.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class Omise_Capabilities {
1818
/**
1919
* @return self The instance of Omise_Capabilities
2020
*/
21-
public static function retrieve() {
21+
public static function retrieve( $publickey = null, $secretkey = null ) {
2222
if ( ! self::$the_instance ) {
2323
try {
24-
$capabilities = OmiseCapabilities::retrieve();
24+
$capabilities = OmiseCapabilities::retrieve( $publickey , $secretkey );
2525
} catch(\Exception $e) {
2626
// suppressing error on the admin dashboard
2727
return null;
@@ -92,10 +92,29 @@ public function getFPXBanks() {
9292
return $this->capabilities->getBackends( $params );
9393
}
9494

95+
/**
96+
* Retrieves list of tokenization methods
97+
*
98+
* @return array
99+
*/
100+
public function getTokenizationMethods()
101+
{
102+
return $this->capabilities ? $this->capabilities['tokenization_methods'] : null;
103+
}
104+
95105
/**
96106
* @return bool True if merchant absorbs the interest or else, false.
97107
*/
98108
public function is_zero_interest() {
99109
return $this->capabilities['zero_interest_installments'];
100110
}
111+
112+
/**
113+
* @return array list of omise backends sourc_type.
114+
*/
115+
public function get_available_payment_methods() {
116+
$backends = $this->getBackends();
117+
$token_methods = $this->getTokenizationMethods();
118+
return array_merge(array_column($backends, '_id'),$token_methods);
119+
}
101120
}

0 commit comments

Comments
 (0)