From 3d9b9bac9ebdbacea12f39fee09cbddca93c3d45 Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 25 Feb 2024 11:02:47 +0100 Subject: [PATCH] wip track payment to user --- plugins/fipfcard_plugin/fipfcard_plugin.php | 138 +++++++------- .../php/paypal/route_api_orders.php | 2 - .../php/paypal/route_api_orders_capture.php | 2 - .../php/paypal/update_user_payment.php | 174 ++++++++++-------- private | 2 +- 5 files changed, 171 insertions(+), 147 deletions(-) diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 355614f..219f2ec 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -238,22 +238,22 @@ $_POST "form_id":"modif_profile", "form_type_confirm":"" } -add_action('parse_request', 'my_custom_url_handler'); -function my_custom_url_handler($query) -{ - if ($_SERVER['REQUEST_METHOD'] !== 'POST' ) - return ; - if ($_SERVER['QUERY_STRING'] !== 'update_profile_acf' ) - return ; - // ! add verification that user can do this edit ! - - foreach($_POST['field_name'] as $field) - { - error_log("_POST[$field]"); - error_log($_POST[$field]); - } -} */ +// add_action('parse_request', 'my_custom_url_handler'); +// function my_custom_url_handler($query) +// { +// if ($_SERVER['REQUEST_METHOD'] !== 'POST' ) +// return ; +// if ($_SERVER['QUERY_STRING'] !== 'update_profile_acf' ) +// return ; +// // ! add verification that user can do this edit ! +// +// foreach($_POST['field_name'] as $field) +// { +// error_log("_POST[$field]"); +// error_log($_POST[$field]); +// } +// } /** @@ -272,63 +272,63 @@ function my_custom_url_handler($query) * - https://stackoverflow.com/questions/19802492/check-if-current-user-is-administrator-in-wordpress * - https://developer.wordpress.org/reference/functions/current_user_can/ */ -function PLGNTLS_my_custom_df_form_handler($form_id, $post_array, $form_type) -{ - error_log("in my_custom_df_form_handler"); - error_log("form_id"); - error_log($form_id); - error_log("post_array"); - error_log(json_encode($post_array)); - error_log("form_type"); - error_log($form_type); -} -add_action ('df_before_process', 'PLGNTLS_my_custom_df_form_handler', 10, 3); +// function PLGNTLS_my_custom_df_form_handler($form_id, $post_array, $form_type) +// { +// error_log("in my_custom_df_form_handler"); +// error_log("form_id"); +// error_log($form_id); +// error_log("post_array"); +// error_log(json_encode($post_array)); +// error_log("form_type"); +// error_log($form_type); +// } +// add_action ('df_before_process', 'PLGNTLS_my_custom_df_form_handler', 10, 3); -// Hook into the 'acf/save_post' action -/* -add_action('acf/save_post', 'handle_acf_form_submission', 5); // 20 is the priority, you can adjust it as needed -function handle_acf_form_submission($post_id) -{ - error_log("in acf/save_post"); - if ($_SERVER['REQUEST_METHOD'] === 'POST' ) - { - error_log("_SERVER['REQUEST_URI']"); - error_log(json_encode($_SERVER['REQUEST_URI'])); - error_log("_SERVER['QUERY_STRING']"); - error_log(json_encode($_SERVER['QUERY_STRING'])); - error_log("_POST['de_fb_ville']"); - error_log(json_encode($_POST['de_fb_ville'])); - } - // Check if the request is coming from the specific URL - if ($_SERVER['QUERY_STRING'] !== '/') { - return; // Exit if the request is not from the specific URL - } - - // Check if this is an autosave - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { - return; - } - - // Check if this is a revision - if (wp_is_post_revision($post_id)) { - return; - } - - // Check if the current user has permission to edit the post - if (!current_user_can('edit_post', $post_id)) { - return; - } - - // Your ACF form submission handling logic goes here - // For example, you can retrieve form data using $_POST and update ACF fields accordingly - if (isset($_POST['your_acf_field_name'])) { - $field_value = sanitize_text_field($_POST['your_acf_field_name']); - update_field('your_acf_field_name', $field_value, $post_id); - } -} -*/ +/** + * Hook into the 'acf/save_post' action + */ +// add_action('acf/save_post', 'handle_acf_form_submission', 5); // 20 is the priority, you can adjust it as needed +// function handle_acf_form_submission($post_id) +// { +// error_log("in acf/save_post"); +// if ($_SERVER['REQUEST_METHOD'] === 'POST' ) +// { +// error_log("_SERVER['REQUEST_URI']"); +// error_log(json_encode($_SERVER['REQUEST_URI'])); +// error_log("_SERVER['QUERY_STRING']"); +// error_log(json_encode($_SERVER['QUERY_STRING'])); +// error_log("_POST['de_fb_ville']"); +// error_log(json_encode($_POST['de_fb_ville'])); +// } +// // Check if the request is coming from the specific URL +// if ($_SERVER['QUERY_STRING'] !== '/') { +// return; // Exit if the request is not from the specific URL +// } +// +// // Check if this is an autosave +// if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { +// return; +// } +// +// // Check if this is a revision +// if (wp_is_post_revision($post_id)) { +// return; +// } +// +// // Check if the current user has permission to edit the post +// if (!current_user_can('edit_post', $post_id)) { +// return; +// } +// +// // Your ACF form submission handling logic goes here +// // For example, you can retrieve form data using $_POST and update ACF fields accordingly +// if (isset($_POST['your_acf_field_name'])) { +// $field_value = sanitize_text_field($_POST['your_acf_field_name']); +// update_field('your_acf_field_name', $field_value, $post_id); +// } +// } diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index bdb763b..aa4991f 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -25,9 +25,7 @@ function handle_orders_request($request_data) { $json_response = $order_response['json_response']; $http_status_code = $order_response['http_status_code']; -error_log("--- in handle_orders_request"); update_user_payment($json_response, 'start'); -error_log("--- out handle_orders_request"); // Return response return new WP_REST_Response($json_response, $http_status_code); diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php index 313aef5..d831be0 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php @@ -21,9 +21,7 @@ function handle_orders_capture_request($request) { $http_status_code = $response_data['http_status_code']; $json_response = $response_data['json_response']; -error_log("--- in handle_orders_capture_request"); update_user_payment($json_response, 'end'); -error_log("--- out handle_orders_capture_request"); return new WP_REST_Response($json_response, $http_status_code); } diff --git a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php index 8384de2..8315e80 100644 --- a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php +++ b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php @@ -9,104 +9,132 @@ if (!defined('ABSPATH')) { } + + +// function check_for_paypal_order_id($user_login, $user) +// { +// $user_id = $user->ID; +// //error_log("--- in check_for_paypal_order_id"); +// //error_log("user_id:"); +// //error_log($user_id); +// +// // // Save a value in the database +// // update_option('my_custom_option', 'my_value'); +// // // Retrieve the value from the database +// // $my_value = get_option('my_custom_option'); +// +// } +// add_action('wp_login', 'check_for_paypal_order_id', 10, 2); + +/** + * see documentation in private 'paypal.md' + * basically it check if the user who initiate the transaction + * is the same that finish it + * + * add_user_meta('user_id', 'fipf_order_id', 'aaaaaa'); + * ['aaaaaa'] + * add_user_meta('user_id', 'fipf_order_id', 'bbbbbb'); + * ['aaaaaa', 'bbbbbb'] + * add_user_meta('user_id', 'fipf_order_id', 'bbbbbb'); + * ['aaaaaa', 'bbbbbb', 'bbbbbb'] + * get_user_meta('user_id', 'fipf_order_id'); + * ['aaaaaa', 'bbbbbb', 'bbbbbb'] + * $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb'); + * ['aaaaaa'] - $del_ret === true + * $del_ret = delete_user_meta('user_id', 'fipf_order_id', 'bbbbbb'); + * ['aaaaaa'] - $del_ret === false + * + */ function update_user_payment($message, $step) { $order_id = $message->id; /* - "status":"CREATED" - "status":"COMPLETED" + 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; $user_id = get_current_user_id(); -//error_log("status :"); -//error_log($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); - - $user_meta_order_id = get_user_meta($user_id, 'fipf_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)); -// $user_meta_ordering = get_user_meta($user_id, 'fipf_ordering', true); -//error_log("user_meta->fipf_ordering :"); -//error_log($user_meta_ordering); - error_log("... update user"); - // update_user_meta : update or create a user meta field - // https://developer.wordpress.org/reference/functions/update_user_meta/ - /* - each time add_meta_user is called with key 'fipf_order_id' - a new value will be added in the array : - - ['aaaaaa'] - - add_meta_user('user_id', 'fipf_order_id', 'bbbbbb'); - - ['aaaaaa', 'bbbbbb'] - - add_meta_user('user_id', 'fipf_order_id', 'bbbbbb'); - - ['aaaaaa', 'bbbbbb', 'bbbbbb'] - calling get_user_meta with key 'fipf_order_id' will return - the array of values - - get_meta_user('user_id', 'fipf_order_id'); - - ['aaaaaa', 'bbbbbb', 'bbbbbb'] - calling delete_meta_user with key-value will delete only - the values matching this value - - ['aaaaaa', 'bbbbbb', 'bbbbbb'] - - delete_meta_user('user_id', 'fipf_order_id', 'bbbbbb'); - - ['aaaaaa'] - this allow to track multiple purchase : - if the fields fipf_order_id still have values - it means that purchases have started without finish, - maybe some will finish ? - in this case, what to do ? don't start a second one ? - or at least, don't erase the previous values, - so that the 2 purchase additions ? - -> paypal payment seems to be available for up to 3 days, or 29 days ? - https://developer.paypal.com/sdk/js/reference/#link-actions - */ - if ($step === 'start') + + // 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); + // add a schedule event to delete this order_id + // after 3 days ? + + // if transaction is COMPLETED, then delete order_id and update user + if ($status === 'COMPLETED') { - //update_user_meta($user_id, 'fipf_ordering', true); - //update_user_meta($user_id, 'fipf_order_id', $order_id); - add_user_meta($user_id, 'fipf_order_id', $order_id); - //delete_user_meta($user_id, 'fipf_order_id', '1FA58326FE555261J'); - } - else if ($step === 'end') - { - // check if purchase is from the right user - // TODO : what to do if not ? - // propose to contact diego ? -// $user_meta_order_id = get_user_meta($user_id, 'fipf_order_id', true); -// if ($user_meta_order_id !== $order_id) -// return ; - //$user_meta_ordering = get_user_meta($user_id, 'fipf_ordering', true); - //if ($user_meta_ordering !== true) - // return ; - - // when purchase is finished, success or not, reinit fipf_order* values - //update_user_meta($user_id, 'fipf_ordering', false); -// update_user_meta($user_id, 'fipf_order_id', ''); - - // if purchase is success, proceed to validate on user : - + // find the user containing the order_id and delete this order_id + $user_id_to_update = delete_order_id($order_id, $user_id); + // proceed to validate payment for user } - $user_meta_order_id = get_user_meta($user_id, 'fipf_order_id'); +// if ($step === 'start') +// { +// add_user_meta($user_id, 'fipf_order_id', $order_id); +// // add a schedule event to delete this order_id after 3 days ? +// } +// else if ($step === 'end') +// { +// // find the user containing the order_id and delete this order_id +// $user_id_to_update = delete_order_id($order_id, $user_id); +// // if purchase is success, proceed to validate on user : +// } + + +$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)); -// $user_meta_ordering = get_user_meta($user_id, 'fipf_ordering', true); -//error_log("user_meta->fipf_ordering :"); -//error_log($user_meta_ordering); - $user_meta = get_user_meta($user_id); -error_log("user_meta :"); -error_log(json_encode($user_meta)); - +error_log("--- out update_user_payment"); } +/** + * @return mixed num - user_id + * bool false - if no match found + */ +function delete_order_id($order_id, $current_user_id) +{ + $del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id); + if ($del_ret === true) + return $current_user_id; + + // it means the current user didn't have this order_id + // so we look for another user + $users = get_users(); + foreach ($users as $user) + { + $user_id = $user->ID; + $del_ret = delete_user_meta($user_id, 'fipf_order_id', $order_id); + if ($del_ret === true) + return $user_id; + } + + return false; +} + + ?> diff --git a/private b/private index 62a6fc0..2936107 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit 62a6fc0c86c187835f66bd68eba847c1c6717dde +Subproject commit 29361071fcff4543a72f30cb693d82e4fb1d5724