From 917d51a0971c46c583b3a10c13217cbc5f13d879 Mon Sep 17 00:00:00 2001 From: asus Date: Tue, 5 Mar 2024 17:22:11 +0100 Subject: [PATCH] - wip payment - already solved success and failure messages and redirection - solved increase date multiple times - users redirection works if error AND partner to posts instead of project --- .../fipfcard_plugin/html/paypal/paypal.html | 22 --- .../fipfcard_plugin/js/paypal/on_approve.js | 37 ++--- .../js/paypal/result_message.js | 2 + plugins/fipfcard_plugin/php/paypal/paypal.php | 44 +++--- .../php/paypal/route_api_orders.php | 16 +-- .../php/paypal/route_api_orders_capture.php | 12 +- .../php/paypal/route_api_utils.php | 4 +- .../php/paypal/update_user_payment.php | 133 +++++++++--------- plugins/fipfcard_plugin/php/redirections.php | 21 ++- plugins/fipfcard_plugin/php/utils/globals.php | 40 +++++- .../fipfcard_plugin/utils/plgntls_class.php | 2 +- private | 2 +- 12 files changed, 181 insertions(+), 154 deletions(-) diff --git a/plugins/fipfcard_plugin/html/paypal/paypal.html b/plugins/fipfcard_plugin/html/paypal/paypal.html index 64dbc40..95f2278 100644 --- a/plugins/fipfcard_plugin/html/paypal/paypal.html +++ b/plugins/fipfcard_plugin/html/paypal/paypal.html @@ -1,24 +1,2 @@ -

old button :

-
- - - - -
-

new button :

-

diff --git a/plugins/fipfcard_plugin/js/paypal/on_approve.js b/plugins/fipfcard_plugin/js/paypal/on_approve.js index 2f51882..17585f0 100644 --- a/plugins/fipfcard_plugin/js/paypal/on_approve.js +++ b/plugins/fipfcard_plugin/js/paypal/on_approve.js @@ -37,25 +37,30 @@ export async function onApprove(data, actions) { } else if (!orderData.purchase_units) { throw new Error(JSON.stringify(orderData)); } else { - // (3) Successful transaction -> Show confirmation or thank you message - // Or go to another URL: actions.redirect('thank_you.html'); - const transaction = - orderData?.purchase_units?.[0]?.payments?.captures?.[0] || - orderData?.purchase_units?.[0]?.payments?.authorizations?.[0]; - // to show a message on page - //resultMessage(`Transaction ${transaction.status}: ${transaction.id}

See console for all available details`,); - console.log( - "Capture result", - orderData, - JSON.stringify(orderData, null, 2), - ); - actions.redirect('https://local_fipfcard_plugin.com/'); + try { + // (3) Successful transaction -> Show confirmation or thank you message + // Or go to another URL: actions.redirect('thank_you.html'); + const transaction = + orderData?.purchase_units?.[0]?.payments?.captures?.[0] || + orderData?.purchase_units?.[0]?.payments?.authorizations?.[0]; + // to show a message on page + //resultMessage(`Transaction ${transaction.status}: ${transaction.id}

See console for all available details`); + resultMessage(eval(PLGNTLS_data.paypal_message_success)); + console.log( + "Capture result", + orderData, + JSON.stringify(orderData, null, 2), + ); + actions.redirect(PLGNTLS_data.paypal_redirection_success); + } catch (error) { + console.error("payment ok but error on traitment afterwards : ", error); + } } } catch (error) { console.error(error); - resultMessage( - `Sorry, your transaction could not be processed...

${error}`, - ); + //resultMessage(`Sorry, your transaction could not be processed...

${error}`); + resultMessage(eval(PLGNTLS_data.paypal_message_failure)); + actions.redirect(PLGNTLS_data.paypal_redirection_failure); } } diff --git a/plugins/fipfcard_plugin/js/paypal/result_message.js b/plugins/fipfcard_plugin/js/paypal/result_message.js index 95cc864..00f78ee 100644 --- a/plugins/fipfcard_plugin/js/paypal/result_message.js +++ b/plugins/fipfcard_plugin/js/paypal/result_message.js @@ -2,6 +2,8 @@ // Example function to show a result to the user. Your site's UI library can be used instead. //function resultMessage(message) { export function resultMessage(message) { + const buttons = document.querySelector("#paypal-button-container"); const container = document.querySelector("#result-message"); + buttons.style.display = 'none'; container.innerHTML = message; } diff --git a/plugins/fipfcard_plugin/php/paypal/paypal.php b/plugins/fipfcard_plugin/php/paypal/paypal.php index a527f09..af48377 100644 --- a/plugins/fipfcard_plugin/php/paypal/paypal.php +++ b/plugins/fipfcard_plugin/php/paypal/paypal.php @@ -42,36 +42,36 @@ add_action('template_redirect', 'check_paypal_request'); /** * call to paypal_shortcode_content() */ -function fipf_paypal_shortcode_content() +function paypal_shortcode_content_FIPF() { $fipfcard_paypal = new PLGNTLS_class(); $pp_sdk_currency = "EUR"; - $pp_sdk_debug = "true"; - $pp_sdk_base_url="https://sandbox.paypal.com"; - $pp_sdk_base_url="https://www.paypal.com"; -// $pp_sdk_client_token="abc123xyz=="; - $pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID . "¤cy=$pp_sdk_currency&debug=$pp_sdk_debug"; - $pp_sdk_src="$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID ; -// $pp_sdk_attributes="src='$pp_sdk_src' data-client-token='$pp_sdk_client_token'"; -// $pp_sdk_attributes="src='$pp_sdk_src'"; -// $pp_sdk_html_script=""; + $pp_sdk_base_url = "https://www.paypal.com"; + $pp_sdk_url = "$pp_sdk_base_url/sdk/js?client-id=" . PAYPAL_CLIENT_ID . "¤cy=$pp_sdk_currency"; + + $paypal_redirection_success = PAYPAL_REDIRECTION_SUCCESS; + $paypal_redirection_failure = PAYPAL_REDIRECTION_FAILURE; + $paypal_message_success = PAYPAL_MESSAGE_SUCCESS; + $paypal_message_failure = PAYPAL_MESSAGE_FAILURE; $added_to_front = $fipfcard_paypal->add_to_front( array( - $pp_sdk_src, - // 'js/paypal/result_message.js', - // 'js/paypal/create_order.js', - // 'js/paypal/on_approve.js', - //"js/paypal/paypal.js", + $pp_sdk_url, array("js/paypal/paypal.js", 'type'=>'module'), "html/paypal/paypal.html", ), + compact ( + 'paypal_redirection_success', + 'paypal_redirection_failure', + 'paypal_message_success', + 'paypal_message_failure', + ), ); return $added_to_front; } -add_shortcode('fipf_paypal_shortcode', 'fipf_paypal_shortcode_content'); +add_shortcode('fipf_paypal_shortcode', 'paypal_shortcode_content_FIPF'); @@ -79,32 +79,32 @@ add_shortcode('fipf_paypal_shortcode', 'fipf_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/ -function fipf_add_id_to_script( $tag, $handle, $src ) { +function add_id_to_script_FIPF( $tag, $handle, $src ) { if ( $handle === 'PLGNTLS_paypal_js' ) { $tag = ''; } return $tag; } -add_filter( 'script_loader_tag', 'fipf_add_id_to_script', 10, 3 ); +add_filter( 'script_loader_tag', 'add_id_to_script_FIPF', 10, 3 ); */ // handling routes and endpoints // diff routes and endpoints : https://stackoverflow.com/q/56075017/9497573 -function fipf_routes_endpoints() +function routes_endpoints_FIPF() { $base_rest_route = "fipf_plugin/api/v1"; register_rest_route($base_rest_route, '/orders', array( 'methods' => 'POST', - 'callback' => 'fipf_handle_orders_request', + 'callback' => 'handle_orders_request_FIPF', )); // 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' => 'fipf_handle_orders_capture_request', + 'callback' => 'handle_orders_capture_request_FIPF', )); }; -add_action('rest_api_init', 'fipf_routes_endpoints'); +add_action('rest_api_init', 'routes_endpoints_FIPF'); ?> diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php index e39b96f..2d764a4 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 @@ include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php'); /** * @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend */ -function fipf_handle_orders_request($request_data) { +function handle_orders_request_FIPF($request_data) { try { // Extract cart information from request body $cart = $request_data['cart']; // Process the order and get the response - $order_response = fipf_create_order($cart); + $order_response = create_order_FIPF($cart); $json_response = $order_response['json_response']; $http_status_code = $order_response['http_status_code']; - fipf_update_user_payment($json_response, 'start'); + update_user_payment_FIPF($json_response, 'start'); // Return response return new WP_REST_Response($json_response, $http_status_code); @@ -43,11 +43,11 @@ function fipf_handle_orders_request($request_data) { * Create an order to start the transaction. * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create */ -function fipf_create_order( $cart ) +function create_order_FIPF( $cart ) { // use the cart information passed from the front-end to calculate the purchase unit details - $access_token = fipf_generate_access_token(); + $access_token = generate_access_token_FIPF(); $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders'; $payload = array( @@ -55,8 +55,8 @@ function fipf_create_order( $cart ) 'purchase_units' => array( array( 'amount' => array( - 'currency_code' => "USD", - 'value' => "100.00", + 'currency_code' => "EUR", + 'value' => "1.00", ), ), ), @@ -89,7 +89,7 @@ function fipf_create_order( $cart ) curl_close($ch); // in utils - return fipf_handle_response($response); + return handle_response_FIPF($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 aca4c90..a7f97c9 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php @@ -11,18 +11,18 @@ if (!defined('ABSPATH')) { include_once(PLGNTLS_class::get_path() . '/php/paypal/route_api_utils.php'); include_once(PLGNTLS_class::get_path() . '/php/paypal/update_user_payment.php'); -function fipf_handle_orders_capture_request($request) { +function handle_orders_capture_request_FIPF($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 = fipf_capture_order($order_id); + $response_data = capture_order_FIPF($order_id); $http_status_code = $response_data['http_status_code']; $json_response = $response_data['json_response']; - fipf_update_user_payment($json_response, 'end'); + update_user_payment_FIPF($json_response, 'end'); return new WP_REST_Response($json_response, $http_status_code); } @@ -37,8 +37,8 @@ function fipf_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 fipf_capture_order($orderID) { - $access_token = fipf_generate_access_token(); +function capture_order_FIPF($orderID) { + $access_token = generate_access_token_FIPF(); $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture'; $headers = array( @@ -67,7 +67,7 @@ function fipf_capture_order($orderID) { curl_close($ch); // in utils - return fipf_handle_response($response); + return handle_response_FIPF($response); }; diff --git a/plugins/fipfcard_plugin/php/paypal/route_api_utils.php b/plugins/fipfcard_plugin/php/paypal/route_api_utils.php index 0e6326f..91e11a8 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 fipf_handle_response($response) { +function handle_response_FIPF($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 fipf_generate_access_token() +function generate_access_token_FIPF() { try { diff --git a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php index b6a57cf..28a6d32 100644 --- a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php +++ b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php @@ -1,35 +1,35 @@ id; @@ -64,16 +64,14 @@ function fipf_update_user_payment($message, $step) // 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 - fipf_schedule_delete_orderid($user_id, $order_id); - fipf_validate_payment_for_user($user_id, $order_id); + schedule_delete_orderid_FIPF($user_id, $order_id); // if transaction is COMPLETED, then delete order_id and update user - if ($status === 'COMPLETED') - { + if ($status === 'COMPLETED') { // find the user containing the order_id and delete this order_id - $user_id_to_update = fipf_delete_order_id_on_success($user_id, $order_id); + $user_id_to_update = delete_order_id_on_success_FIPF($user_id, $order_id); // proceed to validate payment for user - fipf_validate_payment_for_user($user_id_to_update, $order_id); + validate_payment_for_user_FIPF($user_id_to_update, $order_id); } @@ -88,56 +86,65 @@ function fipf_update_user_payment($message, $step) -/** - * - * change acf field [carte_est_valide](validite) to true - * change acf field [date_d_achat](achat) to new current date - * change acf field [date_fin_validite](echance) to previous date + 1 year - * - * create scheduled emails to inform of end of validity - * - * acf uses 'Y-m-d H:i:s' format : - * -> https://www.advancedcustomfields.com/resources/date-time-picker/ - */ -function fipf_validate_payment_for_user($user_id, $order_id) -{ +/* +* +* const CARD_IS_VALID : acf field 'true false' [carte_est_valide](validite) +* const CARD_DATE_PURCHASE : acf field 'date picker' [date_d_achat](achat) +* const CARD_DATE_VALIDITY : acf field 'date picker' [date_fin_validite](echance) +* +* - change CARD_IS_VALID to true +* - change CARD_DATE_PURCHASE to now +* - change CARD_DATE_VALIDITY to previous value + 1 year +* +* - create scheduled emails to inform of end of validity +* +* 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_date_format = 'Y-m-d H:i:s'; - /** - * update card validity to true - */ + /* + * update card validity to true + */ $post_id = 'user_'.$user_id; - update_field('carte_est_valide', true, $post_id); + update_field(CARD_IS_VALID, true, $post_id); - /** - * update purchase date to now - */ + /* + * update purchase date to now + */ $date_now = date($acf_date_format); - update_field('date_d_achat', $date_now, $post_id); + update_field(CARD_DATE_PURCHASE, $date_now, $post_id); - /** - * update date limit validity to add 1 year - */ - $current_date_limit = get_field('date_fin_validite', $post_id); + /* + * get current date limit + * if no date, use now + * if paste date, use now + */ + $current_date_limit = get_field(CARD_DATE_VALIDITY, $post_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); - // id current date limit is not in the futur, use now date - if ($current_date_limit === null) + + if ($current_date_limit === null) { $current_date_limit = $date_now; + } else { // 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 like) + // 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; if ($date_is_in_past) $current_date_limit = $date_now; } - // add one year to current date limit + + /* + * 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('date_fin_validite', $new_date_limit, $post_id); @@ -158,21 +165,21 @@ function fipf_validate_payment_for_user($user_id, $order_id) * -> 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) +function schedule_delete_orderid_FIPF($user_id, $order_id) { $delay = time() + MINUTE_IN_SECONDS; - wp_schedule_single_event($delay, 'fipf_orderid_deletion_event', array($user_id, $order_id)); + 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 */ -function fipf_delete_order_id_later($user_id, $order_id) +function delete_order_id_later_FIPF($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_later', 5, 2); +add_action('orderid_deletion_event_FIPF', 'delete_order_id_later_FIPF', 5, 2); @@ -183,7 +190,7 @@ add_action('fipf_orderid_deletion_event', 'fipf_delete_order_id_later', 5, 2); * @return mixed num - user_id * bool false - if no match found */ -function fipf_delete_order_id_on_success($current_user_id, $order_id) +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); if ($del_ret === true) diff --git a/plugins/fipfcard_plugin/php/redirections.php b/plugins/fipfcard_plugin/php/redirections.php index 59e0307..1cea86e 100644 --- a/plugins/fipfcard_plugin/php/redirections.php +++ b/plugins/fipfcard_plugin/php/redirections.php @@ -9,9 +9,13 @@ if (!defined('ABSPATH')) { - +/* +* redirect users to profil +* if prof -> author page +* if partenaire -> post page / project page / home page +*/ function redirection_page_CIPF(){ - if (!is_page('redirection_cipf')) + if (!is_page(PROF_PARTENAIRE_REDIRECTION_PAGE)) return; $base_url = home_url(); @@ -25,13 +29,18 @@ function redirection_page_CIPF(){ } else if (current_user_can('partenaire')) { $args = array( - 'post_type' => 'project', + 'post_type' => 'post,project', 'author' => $current_user_id, 'posts_per_page' => 1, ); - $query = reset(get_posts($args)); - $post_id = $query->ID; - $redirect_url = get_permalink($query->ID); + $posts = get_posts($args); + if (empty($posts)) + $redirect_url = $base_url; + else { + $query = reset($posts); + $post_id = $query->ID; + $redirect_url = get_permalink($query->ID); + } wp_redirect($redirect_url, 301); } exit; diff --git a/plugins/fipfcard_plugin/php/utils/globals.php b/plugins/fipfcard_plugin/php/utils/globals.php index f085920..b61718b 100644 --- a/plugins/fipfcard_plugin/php/utils/globals.php +++ b/plugins/fipfcard_plugin/php/utils/globals.php @@ -17,16 +17,12 @@ const CONSOLE_OFF = true; */ const FIPFCARD_CONSOLE_OFF = false; -/* -* a variable that will contain the name of the first script enqueued -*/ -$fipfcard_first_script = null; - /* -* path to ajax.js file, from root of js dir +* redirections users */ -$fipfcard_ajax_file = "utils/ajax.js"; +const PROF_PARTENAIRE_REDIRECTION_PAGE = 'redirection_cipf'; +define('PROF_PARTENAIRE_REDIRECTION_URL', home_url() . '/' . PROF_PARTENAIRE_REDIRECTION_PAGE); /* @@ -51,4 +47,34 @@ const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiR */ const PAYPAL_API_BASE_URL = "https://api-m.sandbox.paypal.com"; +/* +* paypal redirections +*/ +const PAYPAL_REDIRECTION_SUCCESS = PROF_PARTENAIRE_REDIRECTION_URL; +const PAYPAL_REDIRECTION_FAILURE = PROF_PARTENAIRE_REDIRECTION_URL; + +/* +* paypal messages +* put the message betweeen backticks `message` and then between single quotes '`message`' +* because it will be evaluated in front by js, so it need something to evaluate, in `` +* you can then use variables available in the context of execution : '`Transaction ${transaction.status}`' +* is it good strategy ? idk + +const PAYPAL_MESSAGE_SUCCESS = '`Transaction ${transaction.status}: ${transaction.id}

See console for all available details`'; +const PAYPAL_MESSAGE_FAILURE = '`Sorry, your transaction could not be processed...

${error}`'; + +*/ +const PAYPAL_MESSAGE_SUCCESS = '`paiement reussi`'; +const PAYPAL_MESSAGE_FAILURE = '`paiement raté`'; + +/* +* acf fields for card : +* - card_is_valid : has valid card +* - card_date_purchase : date of purchase +* - card_date_validity : date end of validity +*/ +const CARD_IS_VALID = 'carte_est_valide'; +const CARD_DATE_PURCHASE = 'date_d_achat'; +const CARD_DATE_VALIDITY = 'date_fin_validite'; + ?> diff --git a/plugins/fipfcard_plugin/utils/plgntls_class.php b/plugins/fipfcard_plugin/utils/plgntls_class.php index d5f7f11..cca8f63 100644 --- a/plugins/fipfcard_plugin/utils/plgntls_class.php +++ b/plugins/fipfcard_plugin/utils/plgntls_class.php @@ -263,10 +263,10 @@ class PLGNTLS_class /* * uncomment to print all enqueued files, can be usefull */ + /* global $wp_scripts; error_log("wp_scripts->queue:"); error_log(json_encode($wp_scripts->queue)); - /* global $wp_styles; error_log("wp_styles->queue:"); error_log(json_encode($wp_styles->queue)); diff --git a/private b/private index c57d99e..ff731c1 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit c57d99e92f456a9e64e89f85d98af3422bfcd202 +Subproject commit ff731c1ad4dcd4ec401f9052c14a3ce1af432a49