From 2fb1ec35aa356f7a0c949ff7bb89b6a504e31119 Mon Sep 17 00:00:00 2001 From: asus Date: Wed, 6 Mar 2024 23:44:46 +0100 Subject: [PATCH] - improved payement paypal for date and other things --- .../fipfcard_plugin/js/paypal/on_approve.js | 4 +- .../php/paypal/route_api_orders.php | 2 +- .../php/paypal/update_user_payment.php | 117 ++++++++---------- .../php/paypal/user_can_pay.php | 2 + plugins/fipfcard_plugin/php/user_infos.php | 1 - .../fipfcard_plugin/utils/plgntls_class.php | 3 +- private | 2 +- 7 files changed, 62 insertions(+), 69 deletions(-) diff --git a/plugins/fipfcard_plugin/js/paypal/on_approve.js b/plugins/fipfcard_plugin/js/paypal/on_approve.js index 17585f0..f01ff91 100644 --- a/plugins/fipfcard_plugin/js/paypal/on_approve.js +++ b/plugins/fipfcard_plugin/js/paypal/on_approve.js @@ -51,7 +51,7 @@ export async function onApprove(data, actions) { orderData, JSON.stringify(orderData, null, 2), ); - actions.redirect(PLGNTLS_data.paypal_redirection_success); + //actions.redirect(PLGNTLS_data.paypal_redirection_success); } catch (error) { console.error("payment ok but error on traitment afterwards : ", error); } @@ -60,7 +60,7 @@ export async function onApprove(data, actions) { console.error(error); //resultMessage(`Sorry, your transaction could not be processed...

${error}`); resultMessage(eval(PLGNTLS_data.paypal_message_failure)); - actions.redirect(PLGNTLS_data.paypal_redirection_failure); + //actions.redirect(PLGNTLS_data.paypal_redirection_failure); } } diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index e35d5d6..536d844 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -21,7 +21,7 @@ function handle_orders_request_FIPF($request_data) { if ($can_pay['success'] === false) throw new Exception($can_pay['message']); error_log("can_pay:"); - error_log($can_pay); + error_log(json_encode($can_pay)); // Process the order and get the response //$order_response = create_order_FIPF($cart); diff --git a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php index 7312ade..2bb713b 100644 --- a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php +++ b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php @@ -28,38 +28,20 @@ if (!defined('ABSPATH')) { * $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb'); * ['aaaaaa'] - $del_ret === false * +* order status : https://developer.paypal.com/docs/api/orders/v2/#orders_capture!c=201&path=status&t=response +* CREATED +* SAVED +* APPROVED ? +* VOIDED +* - COMPLETED +* PAYER_ACTION_REQUIRED +* */ -function update_user_payment_FIPF($message, $step) -{ - +function update_user_payment_FIPF($message, $step) { $order_id = $message->id; $user_id = get_current_user_id(); - /* - order status : https://developer.paypal.com/docs/api/orders/v2/#orders_capture!c=201&path=status&t=response - CREATED - SAVED - APPROVED ? - VOIDED - - COMPLETED - PAYER_ACTION_REQUIRED - */ $status = $message->status; - -//error_log("--- in update_user_payment, step :"); -//error_log($step); -////error_log("message :"); -////error_log(json_encode($message)); -//error_log("order_id :"); -//error_log($order_id); -//error_log("status :"); -//error_log($status); -//$user_meta_order_id = get_user_meta($user_id, 'fipf_order_id'); -//error_log("user_meta->fipf_order_id :"); -//error_log(json_encode($user_meta_order_id)); -//error_log("... update user"); - - // addind order_id to fipf_order_id meta field // it can duplicate, it's not a problem : delete_user_meta will delete all add_user_meta($user_id, 'fipf_order_id', $order_id); @@ -73,12 +55,6 @@ function update_user_payment_FIPF($message, $step) // proceed to validate payment for user validate_payment_for_user_FIPF($user_id_to_update, $order_id); } - - -//$user_meta_order_id = get_user_meta($user_id, 'fipf_order_id'); -//error_log("user_meta->fipf_order_id :"); -//error_log(json_encode($user_meta_order_id)); -//error_log("--- out update_user_payment"); } @@ -99,23 +75,27 @@ function update_user_payment_FIPF($message, $step) * * acf uses 'Y-m-d H:i:s' format : * -> https://www.advancedcustomfields.com/resources/date-time-picker/ +* */ function validate_payment_for_user_FIPF($user_id, $order_id) { $acf_card_state = PLGNTLS_class::ACF_CARD_STATE; $acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION; + $card_duration = PLGNTLS_class::CARD_VALIDITY_TIME; +error_log("---"); $acf_date_format = 'Y-m-d H:i:s'; $acf_id = 'user_'.$user_id; /* * update card validity to true - update_field(CARD_IS_VALID, true, $acf_id); + * */ update_field($acf_card_state, 'Renouvellement', $acf_id); - $date_now = date($acf_date_format); + $date_now = date_create('today'); /* * update purchase date to now + * update_field(CARD_DATE_PURCHASE, $date_now, $acf_id); */ @@ -123,34 +103,42 @@ function validate_payment_for_user_FIPF($user_id, $order_id) { * get current date limit * if no date, use now * if paste date, use now + * */ - $current_date_limit = get_field($acf_card_expiration, $acf_id); - // output is in format 'dd/mm/yyyy' which is not understood by php dates functions - // so i clean it - // -> not a reliable solution, someone can change the ouput format in dashboard - $current_date_limit = str_replace('/', '-', $current_date_limit); + $current_date_limit_object = get_field_object($acf_card_expiration, $acf_id); - if ($current_date_limit === null) { + if ($current_date_limit_object === false) { + error_log("current_date_limit_object === false"); + $current_date_limit = $date_now; + } + else if (empty($current_date_limit_object['value'])) { + error_log("empty current_date_limit_object['value']"); $current_date_limit = $date_now; } else { + error_log("else"); + $current_date_limit_string = $current_date_limit_object['value']; + $current_format_field = $current_date_limit_object['return_format']; // compare 2 dates : https://stackoverflow.com/q/8722806/9497573 // also I dont use strtotime to compare 2 ints, // because i don't know if it will fail one day (2000 bug alike) - $comp_current_date_limit = date_create($current_date_limit); - $comp_date_now = date_create($date_now); - $date_is_in_past = date_diff($comp_date_now, $comp_current_date_limit)->format("%R%a") < 0; + $current_date_limit = date_create_from_format($current_format_field, $current_date_limit_string); + $date_diff = date_diff($date_now, $current_date_limit); + $date_is_in_past = $date_diff->format('%R%a') < 0; + if ($date_is_in_past) $current_date_limit = $date_now; } /* * update date limit validity to add 1 year + * */ - $time_plus_one_year = strtotime('+1 year', strtotime($current_date_limit)); - $new_date_limit = date('Y-m-d H:i:s', $time_plus_one_year); - update_field($acf_card_expiration, $new_date_limit, $acf_id); + error_log("current_date_limit"); + error_log($current_date_limit); + $date_plus_one_year = $current_date_limit->add(date_interval_create_from_date_string('+'.$card_duration));; + update_field($acf_card_expiration, $date_plus_one_year->format($acf_date_format), $acf_id); } @@ -160,23 +148,25 @@ function validate_payment_for_user_FIPF($user_id, $order_id) { -/** - * add a schedule event to delete this order_id - * after 3 days ? - * time() + 60 = one minute from now - * time() + MINUTE_IN_SECONDS = one minute from now - * -> https://codex.wordpress.org/Easier_Expression_of_Time_Constants - * -> also strtotime : https://www.php.net/manual/en/function.strtotime.php - */ +/* +* add a schedule event to delete this order_id +* after 3 days ? +* time() + 60 = one minute from now +* time() + MINUTE_IN_SECONDS = one minute from now +* -> https://codex.wordpress.org/Easier_Expression_of_Time_Constants +* -> also strtotime : https://www.php.net/manual/en/function.strtotime.php +* +*/ function schedule_delete_orderid_FIPF($user_id, $order_id) { $delay = time() + MINUTE_IN_SECONDS; wp_schedule_single_event($delay, 'orderid_deletion_event_FIPF', array($user_id, $order_id)); } -/** - * action hook for the scheduled event - * TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre - */ +/* +* action hook for the scheduled event +* TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre +* +*/ function delete_order_id_later_FIPF($user_id, $order_id) { error_log("delete order_id[$order_id] from user_id[$user_id]"); @@ -189,10 +179,11 @@ add_action('orderid_deletion_event_FIPF', 'delete_order_id_later_FIPF', 5, 2); -/** - * @return mixed num - user_id - * bool false - if no match found - */ +/* +* @return mixed num - user_id +* bool false - if no match found +* +*/ function delete_order_id_on_success_FIPF($current_user_id, $order_id) { $del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id); diff --git a/plugins/fipfcard_plugin/php/paypal/user_can_pay.php b/plugins/fipfcard_plugin/php/paypal/user_can_pay.php index 99b95d4..66d0974 100644 --- a/plugins/fipfcard_plugin/php/paypal/user_can_pay.php +++ b/plugins/fipfcard_plugin/php/paypal/user_can_pay.php @@ -20,6 +20,8 @@ function can_pay_now_CIPF() { $user_id = get_current_user_id(); $acf_id = 'user_' . $user_id; + error_log("etat_carte:"); + error_log(json_encode(get_field_object($acf_card_state, $acf_id))); /* * check if payment is virement or immediat diff --git a/plugins/fipfcard_plugin/php/user_infos.php b/plugins/fipfcard_plugin/php/user_infos.php index 86b49ec..0f6d411 100644 --- a/plugins/fipfcard_plugin/php/user_infos.php +++ b/plugins/fipfcard_plugin/php/user_infos.php @@ -68,7 +68,6 @@ function output_list_front_CIPF($array) { function current_user_infos_CIPF($atts) { if (!is_user_logged_in()) return ; - error_log("----"); $current_user = wp_get_current_user(); $current_user_infos = $current_user->data; diff --git a/plugins/fipfcard_plugin/utils/plgntls_class.php b/plugins/fipfcard_plugin/utils/plgntls_class.php index c914727..a9db3c4 100644 --- a/plugins/fipfcard_plugin/utils/plgntls_class.php +++ b/plugins/fipfcard_plugin/utils/plgntls_class.php @@ -62,7 +62,8 @@ class PLGNTLS_class const ACF_CARD_PRICE_DELIVERY = 'livraison'; const ACF_CARD_PRICE_TOTAL = 'somme_a_regler'; const ACF_CARD_EXPIRATION = 'fin_de_validite'; - const CARD_RENEW_PERIOD = 31; // number of days before expiration when renew card start to be possible + const CARD_RENEW_PERIOD = 31; // int : number of days before expiration when renew card start to be possible + const CARD_VALIDITY_TIME = '1 year'; // string : time of validity of the card (ex: '1 month' or '1 year' or '60 days') private static $_root_path; private static $_root_url; diff --git a/private b/private index c04d227..c4950c3 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit c04d227649c57fa15a8f353b03d91dcfdda04fab +Subproject commit c4950c3dcda13354dac8757bd81bf76d71c4343c