From ec18dc60b47f5d9da1e30b69e5992b0e0c1cc7cd Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 25 Feb 2024 15:00:21 +0100 Subject: [PATCH] prefixed all paypal related fonctions with fipf --- plugins/fipfcard_plugin/fipfcard_plugin.php | 2 +- plugins/fipfcard_plugin/php/paypal/paypal.php | 10 +-- .../php/paypal/route_api_orders.php | 12 +-- .../php/paypal/route_api_orders_capture.php | 12 +-- .../php/paypal/route_api_utils.php | 4 +- .../php/paypal/update_user_payment.php | 79 +++++++++---------- 6 files changed, 59 insertions(+), 60 deletions(-) diff --git a/plugins/fipfcard_plugin/fipfcard_plugin.php b/plugins/fipfcard_plugin/fipfcard_plugin.php index 219f2ec..227ad7b 100644 --- a/plugins/fipfcard_plugin/fipfcard_plugin.php +++ b/plugins/fipfcard_plugin/fipfcard_plugin.php @@ -98,7 +98,7 @@ add_shortcode('custom_frontend_posting_form', 'custom_frontend_posting_form'); * call to paypal_shortcode_content() */ include_once(PLGNTLS_class::get_path() . '/php/paypal/paypal.php'); -add_shortcode('fipf_paypal_shortcode', 'paypal_shortcode_content'); +add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content'); diff --git a/plugins/fipfcard_plugin/php/paypal/paypal.php b/plugins/fipfcard_plugin/php/paypal/paypal.php index 403e4e3..7f2e8fe 100644 --- a/plugins/fipfcard_plugin/php/paypal/paypal.php +++ b/plugins/fipfcard_plugin/php/paypal/paypal.php @@ -38,7 +38,7 @@ add_action('template_redirect', 'check_paypal_request'); -function paypal_shortcode_content() +function fipf_paypal_shortcode_content() { $fipfcard_paypal = new PLGNTLS_class(); @@ -69,13 +69,13 @@ function paypal_shortcode_content() * the js file paypal.js needs to be imported as a module to use import * @see https://developer.wordpress.org/reference/hooks/script_loader_tag/ */ -add_filter( 'script_loader_tag', 'add_id_to_script', 10, 3 ); -function add_id_to_script( $tag, $handle, $src ) { +function fipf_add_id_to_script( $tag, $handle, $src ) { if ( $handle === 'PLGNTLS_paypal_js' ) { $tag = ''; } return $tag; } +add_filter( 'script_loader_tag', 'fipf_add_id_to_script', 10, 3 ); // handling routes and endpoints @@ -85,12 +85,12 @@ function fipf_routes_endpoints() $base_rest_route = "fipf_plugin/api/v1"; register_rest_route($base_rest_route, '/orders', array( 'methods' => 'POST', - 'callback' => 'handle_orders_request', + 'callback' => 'fipf_handle_orders_request', )); // https://local_fipfcard_plugin.com/wp-json/fipf_plugin/api/v1/orders/21T129305J264761D/capture register_rest_route($base_rest_route, '/orders/(?P[a-zA-Z0-9]+)/capture', array( 'methods' => 'POST', - 'callback' => 'handle_orders_capture_request', + 'callback' => 'fipf_handle_orders_capture_request', )); }; add_action('rest_api_init', 'fipf_routes_endpoints'); diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index aa4991f..bccf68c 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -15,17 +15,17 @@ if (!defined('ABSPATH')) { /** * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend */ -function handle_orders_request($request_data) { +function fipf_handle_orders_request($request_data) { try { // Extract cart information from request body $cart = $request_data['cart']; // Process the order and get the response - $order_response = create_order($cart); + $order_response = fipf_create_order($cart); $json_response = $order_response['json_response']; $http_status_code = $order_response['http_status_code']; - update_user_payment($json_response, 'start'); + fipf_update_user_payment($json_response, 'start'); // Return response return new WP_REST_Response($json_response, $http_status_code); @@ -43,11 +43,11 @@ function handle_orders_request($request_data) { * Create an order to start the transaction. * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create */ -function create_order( $cart ) +function fipf_create_order( $cart ) { // use the cart information passed from the front-end to calculate the purchase unit details - $access_token = generate_access_token(); + $access_token = fipf_generate_access_token(); $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders'; $payload = array( @@ -89,7 +89,7 @@ function create_order( $cart ) curl_close($ch); // in utils - return handle_response($response); + return fipf_handle_response($response); }; 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 d831be0..0a37799 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php @@ -10,18 +10,18 @@ if (!defined('ABSPATH')) { die('You can not access this file!'); } -function handle_orders_capture_request($request) { +function fipf_handle_orders_capture_request($request) { $order_id = $request['orderID']; try { // Implement captureOrder function logic here // Make sure you implement captureOrder function similar to the Node.js code - $response_data = capture_order($order_id); + $response_data = fipf_capture_order($order_id); $http_status_code = $response_data['http_status_code']; $json_response = $response_data['json_response']; - update_user_payment($json_response, 'end'); + fipf_update_user_payment($json_response, 'end'); return new WP_REST_Response($json_response, $http_status_code); } @@ -36,8 +36,8 @@ function handle_orders_capture_request($request) { * Capture payment for the created order to complete the transaction. * @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture */ -function capture_order($orderID) { - $access_token = generate_access_token(); +function fipf_capture_order($orderID) { + $access_token = fipf_generate_access_token(); $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture'; $headers = array( @@ -66,7 +66,7 @@ function capture_order($orderID) { curl_close($ch); // in utils - return handle_response($response); + return fipf_handle_response($response); }; diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_utils.php b/plugins/fipfcard_plugin/php/paypal/route_api_utils.php index eebd1f1..0e6326f 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_utils.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_utils.php @@ -12,7 +12,7 @@ if (!defined('ABSPATH')) { /** * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend */ -function handle_response($response) { +function fipf_handle_response($response) { try { // Decode JSON response @@ -54,7 +54,7 @@ async function handleResponse(response) { * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend * @see https://developer.paypal.com/api/rest/authentication/ */ -function generate_access_token() +function fipf_generate_access_token() { try { diff --git a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php index 967bd00..2c85d76 100644 --- a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php +++ b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php @@ -10,22 +10,6 @@ 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 @@ -45,7 +29,7 @@ if (!defined('ABSPATH')) { * ['aaaaaa'] - $del_ret === false * */ -function update_user_payment($message, $step) +function fipf_update_user_payment($message, $step) { $order_id = $message->id; @@ -80,34 +64,17 @@ error_log("... update user"); // 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 ? - // 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 - $delay = time() + MINUTE_IN_SECONDS; - wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id)); + fipf_schedule_delete_orderid($user_id, $order_id); // if transaction is COMPLETED, then delete order_id and update user if ($status === 'COMPLETED') { // find the user containing the order_id and delete this order_id - $user_id_to_update = delete_order_id($order_id, $user_id); + $user_id_to_update = fipf_delete_order_id_on_success($user_id, $order_id); // proceed to validate payment for user + fipf_validate_payment_for_user($user_id_to_update, $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 :"); @@ -116,16 +83,48 @@ error_log("--- out update_user_payment"); } + + + + +/** + * + */ +function fipf_validate_payment_for_user($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 + */ +function fipf_schedule_delete_orderid($user_id, $order_id) +{ + $delay = time() + MINUTE_IN_SECONDS; + wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id)); +} /** * action hook for the scheduled event * TODO: ne marche pas je ne sais pas pourquoi, pas urgent a resoudre */ -function fipf_delete_order_id($user_id, $order_id) +function fipf_delete_order_id_later($user_id, $order_id) { error_log("delete order_id[$order_id] from user_id[$user_id]"); delete_user_meta($user_id, 'fipf_order_id', $order_id); } -add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id', 5, 2); +add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id_later', 5, 2); + + + @@ -133,7 +132,7 @@ add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id', 5, 2); * @return mixed num - user_id * bool false - if no match found */ -function delete_order_id($order_id, $current_user_id) +function fipf_delete_order_id_on_success($current_user_id, $order_id) { $del_ret = delete_user_meta($current_user_id, 'fipf_order_id', $order_id); if ($del_ret === true)