Skip to content

Commit 0473ebc

Browse files
authored
Update API-v2.php
1 parent a344866 commit 0473ebc

File tree

1 file changed

+38
-76
lines changed

1 file changed

+38
-76
lines changed

templates/method-v2/API-v2.php

+38-76
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,6 @@ public function init_form_fields() {
171171
),
172172
'default' => 'no'
173173
),
174-
'delete_chargily_customer_ids' => array(
175-
'title' => __('Update the database', 'chargilytextdomain'),
176-
'type' => 'button',
177-
'description' => __('Use this button to Update the database. <br>
178-
Click this button if you have encountered problems in previous versions', 'chargilytextdomain'),
179-
),
180-
181174
// END
182175
);
183176
}
@@ -397,14 +390,10 @@ public function process_payment( $order_id ) {
397390
$order_type ='Test';
398391
$order->update_meta_data( 'chargily_order_type', $order_type );
399392
$order->save();
400-
$user_id = get_current_user_id();
401-
$chargily_customers_id = get_user_meta($user_id, 'chargily_customers_id_test', true);
402393
} else {
403394
$order_type ='Live';
404395
$order->update_meta_data( 'chargily_order_type', $order_type );
405396
$order->save();
406-
$user_id = get_current_user_id();
407-
$chargily_customers_id = get_user_meta($user_id, 'chargily_customers_id_live', true);
408397
}
409398

410399
$languages_type = $this->get_option('languages_type');
@@ -434,8 +423,6 @@ public function process_payment( $order_id ) {
434423
}
435424

436425
$encryption_key = $this->get_encryption_key();
437-
$user_id = get_current_user_id();
438-
//$chargily_customers_id = get_user_meta($user_id, 'chargily_customers_id', true);
439426

440427
function filter_empty_values($value) {
441428
if (is_array($value)) {
@@ -445,34 +432,12 @@ function filter_empty_values($value) {
445432
}
446433

447434
if ( is_user_logged_in() ) {
448-
if ( isset( $_COOKIE['chargily_customers_id_test'] ) ) {
449-
unset( $_COOKIE['chargily_customers_id_test'] );
450-
}
451-
if ( isset( $_COOKIE['chargily_customers_id_live'] ) ) {
452-
unset( $_COOKIE['chargily_customers_id_live'] );
453-
}
454-
}
455-
456-
if ($user_id) {
457435

458-
if ( is_user_logged_in() ) {
459-
if ( isset( $_COOKIE['chargily_customers_id_test'] ) ) {unset( $_COOKIE['chargily_customers_id_test'] );}
460-
if ( isset( $_COOKIE['chargily_customers_id_live'] ) ) {unset( $_COOKIE['chargily_customers_id_live'] );}
461-
}
462-
436+
$user_id = get_current_user_id();
463437
$is_test_mode = $this->get_option('test_mode') === 'yes';
464438
$meta_key = $is_test_mode ? 'chargily_customers_id_test' : 'chargily_customers_id_live';
465439
$chargily_customers_id = get_user_meta($user_id, $meta_key, true);
466-
if (isset($chargily_customers_id)) {
467-
$is_test_mode = $this->get_option('test_mode') === 'yes';
468-
$meta_key = $is_test_mode ? 'chargily_customers_id_test' : 'chargily_customers_id_live';
469-
$chargily_customers_id = get_user_meta($user_id, $meta_key, true);
470-
} else {
471-
$user_id = get_current_user_id();
472-
$is_test_mode = $this->get_option('test_mode') === 'yes';
473-
$meta_key = $is_test_mode ? 'chargily_customers_id_test' : 'chargily_customers_id_live';
474-
$chargily_customers_id = get_user_meta($user_id, $meta_key, true);
475-
if (!$this->customer_exists($chargily_customers_id, $user_id)) {
440+
if (!$this->customer_exists($chargily_customers_id, $user_id)) {
476441
// إنشاء بيانات العميل لإرسالها إلى API
477442
$address = array_filter(array(
478443
"country" => $order->get_billing_country(),
@@ -505,13 +470,14 @@ function filter_empty_values($value) {
505470
// No data to send
506471
return;
507472
}
473+
508474
$user_id = get_current_user_id();
509475
$chargily_customers_id = $this->create_chargily_customer($user_data, $user_id);
510476
if (is_wp_error($chargily_customers_id)) {
511477
wc_add_notice($chargily_customers_id->get_error_message(), 'error');
512478
return;
513479
}
514-
}
480+
// end
515481
}
516482
} else {
517483
// العميل هو زائر
@@ -644,11 +610,6 @@ function filter_empty_values($value) {
644610
);
645611
}
646612

647-
648-
if (!empty($chargily_customers_id)) {
649-
$payload['customer_id'] = $chargily_customers_id;
650-
}
651-
652613
$response = $this->create_chargilyv2_checkout($payload);
653614

654615
if (is_wp_error($response)) {
@@ -675,12 +636,15 @@ function filter_empty_values($value) {
675636
}
676637

677638

678-
private function customer_exists($customer_id, $user_id) {
639+
private function customer_exists($chargily_customers_id, $user_id) {
640+
if (empty($chargily_customers_id)) {
641+
$chargily_customers_id = "0000000099999";
642+
}
679643
$credentials = $this->get_api_credentials();
680644
$is_test_mode = $this->get_option('test_mode') === 'yes';
681645
$api_url = $is_test_mode
682-
? 'https://pay.chargily.net/test/api/v2/customers/' . $customer_id
683-
: 'https://pay.chargily.net/api/v2/customers/' . $customer_id;
646+
? 'https://pay.chargily.net/test/api/v2/customers/' . $chargily_customers_id
647+
: 'https://pay.chargily.net/api/v2/customers/' . $chargily_customers_id;
684648

685649
$headers = array(
686650
'Authorization' => 'Bearer ' . $credentials['api_secret'],
@@ -1175,7 +1139,6 @@ function update_chargily_pay_settingss() {
11751139
}
11761140
}
11771141

1178-
//-------------
11791142
function custom_override_checkout_fields( $fields ) {
11801143
$fields['billing']['billing_phone']['validate'] = array( 'phone' );
11811144
$fields['billing']['billing_phone']['custom_attributes'] = array(
@@ -1190,39 +1153,38 @@ function custom_override_checkout_fields( $fields ) {
11901153

11911154
function custom_checkout_phone_validation_script() {
11921155
if ( is_checkout() ) {
1193-
?>
1194-
<script>
1195-
jQuery(document).ready(function($) {
1196-
$('#billing_phone').on('change', function(){
1197-
var phone = $(this).val();
1198-
if ( phone.length < 8 || phone.length > 20 || !$.isNumeric(phone) ) {
1199-
$(this).get(0).setCustomValidity('رقم الهاتف يجب أن يكون بين 8 إلى 20 رقمًا.');
1200-
} else {
1201-
$(this).get(0).setCustomValidity('');
1202-
}
1203-
});
1204-
});
1205-
</script>
1206-
<?php
1156+
if ( is_user_logged_in() ) {
1157+
if ( isset( $_COOKIE['chargily_customers_id'] ) ) {unset( $_COOKIE['chargily_customers_id'] );}
1158+
if ( isset( $_COOKIE['chargily_customers_id_test'] ) ) {unset( $_COOKIE['chargily_customers_id_test'] );}
1159+
if ( isset( $_COOKIE['chargily_customers_id_live'] ) ) {unset( $_COOKIE['chargily_customers_id_live'] );}
1160+
}
1161+
?>
1162+
<script>
1163+
jQuery(document).ready(function($) {
1164+
$('#billing_phone').on('change', function(){
1165+
var phone = $(this).val();
1166+
if ( phone.length < 8 || phone.length > 20 || !$.isNumeric(phone) ) {
1167+
$(this).get(0).setCustomValidity('رقم الهاتف يجب أن يكون بين 8 إلى 20 رقمًا.');
1168+
} else {
1169+
$(this).get(0).setCustomValidity('');
1170+
}
1171+
});
1172+
});
1173+
function deleteCookie(name) {
1174+
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
1175+
}
1176+
1177+
window.onload = function() {
1178+
deleteCookie('chargily_customers_id');
1179+
deleteCookie('chargily_customers_id_test');
1180+
deleteCookie('chargily_customers_id_live');
1181+
};
1182+
</script>
1183+
<?php
12071184
}
12081185
}
12091186
add_action( 'wp_footer', 'custom_checkout_phone_validation_script' );
12101187

1211-
1212-
add_action('wp_ajax_delete_chargily_customer_ids', 'delete_chargily_customer_ids_callback');
1213-
function delete_chargily_customer_ids_callback() {
1214-
if ( is_admin() ) {
1215-
if (current_user_can('administrator') || current_user_can('shop_manager')) {
1216-
$users = get_users();
1217-
foreach ($users as $user) {
1218-
delete_user_meta($user->ID, 'chargily_customers_id');
1219-
}
1220-
wp_send_json_success('The database has been updated successfully.');
1221-
wp_die();
1222-
}
1223-
}
1224-
}
1225-
12261188
add_filter( 'manage_edit-shop_order_columns', 'chargily_order_items_column' );
12271189
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'chargily_order_items_column' );
12281190
function chargily_order_items_column( $columns ) {

0 commit comments

Comments
 (0)