diff --git a/plugins/cipf_plugin/js/paypal/on_approve.js b/plugins/cipf_plugin/js/paypal/on_approve.js index 05e1955..dee59f8 100644 --- a/plugins/cipf_plugin/js/paypal/on_approve.js +++ b/plugins/cipf_plugin/js/paypal/on_approve.js @@ -62,7 +62,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/cipf_plugin/php/paypal/route_api_orders.php b/plugins/cipf_plugin/php/paypal/route_api_orders.php index 4adbef3..fadb6a1 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders.php @@ -51,6 +51,8 @@ function handle_orders_request_CIPF($request_data) { //function create_order_CIPF($cart) function create_order_CIPF() { + $paypal_api_base_url = PLGNTLS_class::PAYPAL_API_BASE_URL; + // use the cart information passed from the front-end to calculate the purchase unit details $access_token = generate_access_token_CIPF(); @@ -59,7 +61,7 @@ function create_order_CIPF() $acf_id = 'user_' . $user_id; $price = get_field('somme_a_regler', $acf_id); - $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders'; + $url = $paypal_api_base_url . '/v2/checkout/orders'; $payload = array( 'intent' => "CAPTURE", 'note' => 'ERRPYO005', diff --git a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php index a190d8c..0eb37ed 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php @@ -17,7 +17,7 @@ function handle_orders_capture_request_CIPF($request) { // Implement captureOrder function logic here // Make sure you implement captureOrder function similar to the Node.js code - $response_data = capture_order_CIPF($order_id); + $response_data = capture_order_cipf($order_id); $http_status_code = $response_data['http_status_code']; $json_response = $response_data['json_response']; @@ -35,9 +35,10 @@ function handle_orders_capture_request_CIPF($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_CIPF($orderID) { +function capture_order_CIPF($order_id) { + $paypal_api_base_url = PLGNTLS_class::PAYPAL_API_BASE_URL; $access_token = generate_access_token_CIPF(); - $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders/' . $orderID . '/capture'; + $url = $paypal_api_base_url . '/v2/checkout/orders/' . $order_id . '/capture'; $headers = array( "Content-Type: application/json", diff --git a/plugins/cipf_plugin/php/paypal/update_user_payment.php b/plugins/cipf_plugin/php/paypal/update_user_payment.php index ab6e26e..8482f9f 100644 --- a/plugins/cipf_plugin/php/paypal/update_user_payment.php +++ b/plugins/cipf_plugin/php/paypal/update_user_payment.php @@ -38,6 +38,8 @@ if (!defined('ABSPATH')) { * */ function update_user_payment_CIPF($message, $step) { + $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + $order_id = $message->id; $user_id = get_current_user_id(); $status = $message->status; @@ -55,15 +57,15 @@ function update_user_payment_CIPF($message, $step) { * - '' -> no message to output | on author page (after set to empty on author page) * */ - delete_user_meta($user_id, 'cipf_payement_status'); - add_user_meta($user_id, 'cipf_payement_status', 'started'); + delete_user_meta($user_id, $meta_payement_status); + add_user_meta($user_id, $meta_payement_status, 'started'); // 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_on_success_CIPF($user_id, $order_id); // change payement status to success - update_user_meta($user_id_to_update, 'cipf_payement_status', 'success'); + update_user_meta($user_id_to_update, $meta_payement_status, 'success'); // proceed to validate payment for user validate_payment_for_user_CIPF($user_id_to_update, $order_id); } @@ -92,18 +94,13 @@ function update_user_payment_CIPF($message, $step) { function validate_payment_for_user_CIPF($user_id, $order_id) { $acf_card_state = PLGNTLS_class::ACF_CARD_STATE; $acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION; + $acf_prof_can_renew = PLGNTLS_class::ACF_PROF_CAN_RENEW; $card_duration = PLGNTLS_class::CARD_VALIDITY_TIME; $prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV; $acf_date_format = 'Y-m-d H:i:s'; $acf_id = 'user_'.$user_id; - /* - * update card validity to true - * - */ - update_field($acf_card_state, 'Renouvellement', $acf_id); - $date_now = date_create('today'); /* * update purchase date to now @@ -151,10 +148,12 @@ function validate_payment_for_user_CIPF($user_id, $order_id) { * change user profil : * - to active * - card state is renewal + * - cannot renew * */ - update_field($prof_is_activ, 'Actif', $acf_id); - update_field($acf_card_state, 'Renouvellement', $acf_id); + update_field($prof_is_activ['_name'], $prof_is_activ['activ'], $acf_id); + update_field($acf_card_state['_name'], $acf_card_state['renew'], $acf_id); + update_field($acf_prof_can_renew['_name'], $acf_prof_can_renew['cannot'] , $acf_id); } diff --git a/plugins/cipf_plugin/php/paypal/user_can_pay.php b/plugins/cipf_plugin/php/paypal/user_can_pay.php index a243f6c..b9e9fac 100644 --- a/plugins/cipf_plugin/php/paypal/user_can_pay.php +++ b/plugins/cipf_plugin/php/paypal/user_can_pay.php @@ -8,7 +8,6 @@ * default true */ function can_pay_now_CIPF() { - $acf_card_state = PLGNTLS_class::ACF_CARD_STATE; $acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD; $acf_card_price_choice = PLGNTLS_class::ACF_CARD_PRICE_CHOICE; $acf_card_price_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY; @@ -23,7 +22,7 @@ function can_pay_now_CIPF() { /* * check if payment is virement or immediat * - $payement = get_field($acf_card_payment_method, $acf_id); + $payement = get_field($acf_card_payment_method['_name'], $acf_id); if (strtolower($payement) === 'virement') { return false; } @@ -35,8 +34,8 @@ function can_pay_now_CIPF() { * in case it was changed from admin pannel * */ - $tarif = (int)get_field($acf_card_price_choice, $acf_id); - $livraison = (int)get_field($acf_card_price_delivery, $acf_id); + $tarif = (int)get_field($acf_card_price_choice['_name'], $acf_id); + $livraison = (int)get_field($acf_card_price_delivery['_name'], $acf_id); $price = $tarif + $livraison; update_field($acf_card_price_total, $price, $acf_id); diff --git a/plugins/cipf_plugin/php/prof_check_page.php b/plugins/cipf_plugin/php/prof_check_page.php index e727f35..8f5b67e 100644 --- a/plugins/cipf_plugin/php/prof_check_page.php +++ b/plugins/cipf_plugin/php/prof_check_page.php @@ -21,7 +21,7 @@ function handle_prof_is_activ_CIPF($author_id) { * if prof is activ, do nothing more * */ - $is_activ = get_field($acf_prof_is_activ, $acf_id); + $is_activ = get_field($acf_prof_is_activ['_name'], $acf_id); if ($is_activ === 'Actif') return; @@ -51,7 +51,7 @@ function handle_prof_is_activ_CIPF($author_id) { /* -* check meta field 'cipf_payement_status' +* check meta field META_PAYEMENT_STATUS * if field value is 'success' * - hide block 'failure' * - and update field to '', so it will not show next time @@ -68,13 +68,15 @@ function handle_prof_is_activ_CIPF($author_id) { * */ function show_prof_paiement_messages_CIPF($user_id) { + $acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV; + $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + /* * if prof is inactive, do nothing more * */ - $acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV; $acf_id = 'user_' . $user_id; - $is_activ = get_field($acf_prof_is_activ, $acf_id); + $is_activ = get_field($acf_prof_is_activ['_name'], $acf_id); if (is_null($is_activ) || empty($is_activ)) return; if ($is_activ === 'Inactif') @@ -82,8 +84,7 @@ function show_prof_paiement_messages_CIPF($user_id) { $cipf_prof_payement = new PLGNTLS_class(); - $payement_status2 = get_user_meta($user_id, 'cipf_payement_status'); - $payement_status = get_user_meta($user_id, 'cipf_payement_status', true); + $payement_status = get_user_meta($user_id, $meta_payement_status, true); if ($payement_status === 'success') { $cipf_prof_payement->add_to_front(array( array( 'css' => '#cipf_prof_paiement_reussi {display: block;}' ) @@ -95,7 +96,7 @@ function show_prof_paiement_messages_CIPF($user_id) { )); } - update_user_meta($user_id, 'cipf_payement_status', ''); + update_user_meta($user_id, $meta_payement_status, ''); } diff --git a/plugins/cipf_plugin/php/register_partenaires.php b/plugins/cipf_plugin/php/register_partenaires.php index a32593a..44703f5 100644 --- a/plugins/cipf_plugin/php/register_partenaires.php +++ b/plugins/cipf_plugin/php/register_partenaires.php @@ -11,6 +11,7 @@ if (!defined('ABSPATH')) { /* * at registration, add role 'partenaire' when page url has path 'creation-du-compte-partenaire' +* */ function add_partenaires_PLGNTLS($customer_data){ $current_url = $_SERVER['HTTP_REFERER']; // not reliable to use referer, TODO: find another solution diff --git a/plugins/cipf_plugin/php/renew_card.php b/plugins/cipf_plugin/php/renew_card.php index 2a1d0d4..86c6165 100644 --- a/plugins/cipf_plugin/php/renew_card.php +++ b/plugins/cipf_plugin/php/renew_card.php @@ -26,10 +26,10 @@ function reset_some_fields_CIPF() { $acf_price = PLGNTLS_class::ACF_CARD_PRICE_CHOICE; $user_id = get_current_user_id(); - update_field($acf_cgv , array(""), 'user_'.$user_id); - update_field($acf_payement, array(""), 'user_'.$user_id); - update_field($acf_delivery, array(""), 'user_'.$user_id); - update_field($acf_price , array(""), 'user_'.$user_id); + update_field($acf_cgv['_name'] , array(""), 'user_'.$user_id); + update_field($acf_payement['_name'], array(""), 'user_'.$user_id); + update_field($acf_delivery['_name'], array(""), 'user_'.$user_id); + update_field($acf_price['_name'] , array(""), 'user_'.$user_id); } @@ -37,14 +37,21 @@ function reset_some_fields_CIPF() { /* -* prevent users to fill the renew form if -* they are not prof and logged in, -* and if their card is not in renewable state -* except admins and editor +* on renew page : +* - check restrictions +* - change some acf fields (if access granted) +* +* prevent users to fill the renew form if : +* - they are not prof and logged in, +* - and if their card is not in renewable state +* - except admins and editor * */ function renew_page_restrictions_CIPF(){ $slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD; + $slug_page_redirection = PLGNTLS_class::SLUG_PAGE_REDIRECTION; + $acf_prof_can_renew = PLGNTLS_class::ACF_PROF_CAN_RENEW; + if (!is_page($slug_renew_card)) return; @@ -54,30 +61,36 @@ function renew_page_restrictions_CIPF(){ $base_url = home_url(); $current_user_id = get_current_user_id(); - /* - if (current_user_can('professeur__professeure')) { - $user_page = get_author_posts_url($current_user_id); - wp_redirect($user_page, 301); - } - else if (current_user_can('partenaire')) { - $args = array( - 'post_type' => 'post', - 'author' => $current_user_id, - 'posts_per_page' => 1, - ); - $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; - */ + $current_user = wp_get_current_user(); + $acf_id = 'user_'.$current_user_id; + + /* + * check multiple user roles + * https://developer.wordpress.org/reference/functions/current_user_can/#div-comment-4083 + * + */ + $allowed_roles = array('administrator', 'editor', 'professeur__professeure'); + if (!array_intersect($allowed_roles, $current_user->roles)) + return; + + /* + * if prof, check card state + * + */ + if (current_user_can('professeur__professeure')) { + $can_renew = get_field($acf_prof_can_renew, $acf_id); + if ($can_renew === false) { + $redirect_url = home_url() . '/' . $slug_page_redirection; + wp_redirect($redirect_url, 301); + exit; + } + } + + /* + * if prof can renew its card, reset some form fields before + * + */ reset_some_fields_CIPF(); } add_action('template_redirect', 'renew_page_restrictions_CIPF'); @@ -88,8 +101,13 @@ add_action('template_redirect', 'renew_page_restrictions_CIPF'); /* +* on the renew card page for prof +* output the right message, depending of the status of the card +* 'renouveler' or 'commander' +* * #cipf_prof_carte_commande -> default display: block; * #cipf_prof_carte_renouvellement -> default display: none; +* */ function renew_page_filter_message_CIPF(){ $slug_renew_card = PLGNTLS_class::SLUG_RENEW_CARD; @@ -103,7 +121,7 @@ function renew_page_filter_message_CIPF(){ $cipf_renew = new PLGNTLS_class(); - $card_state = get_field($acf_card_state, $acf_id); + $card_state = get_field($acf_card_state['_name'], $acf_id); error_log("card_state"); error_log($card_state); diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/utils/plgntls_class.php index 4f4d35c..bde970a 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/utils/plgntls_class.php @@ -56,33 +56,118 @@ if (!defined('ABSPATH')) { class PLGNTLS_class { + /* + * const declarations + * + */ + // ACF + const ACF_CARD_STATE = ['_name'=>'etat_carte', 'new'=>'Commande', 'renew'=>'Renouvellement']; + const ACF_CARD_PAYMENT_METHOD = ['_name'=>'paiement', 'paypal'=>'Paypal', 'transfert'=>'Virement']; + const ACF_CARD_PRICE_CHOICE = ['_name'=>'tarif', 'low'=>'10', 'high'=>'15']; + const ACF_CARD_PRICE_DELIVERY = ['_name'=>'livraison', 'pdf'=>'PDF', 'post'=>'Fabrication']; + const ACF_PROF_IS_ACTIV = ['_name'=>'compte-actif', 'activ'=>'Actif', 'inactiv'=>'Inactif']; + const ACF_PROF_CGV = ['_name'=>'cgv', 'cgv'=>'cgv']; + const ACF_PROF_CAN_RENEW = ['_name'=>'renouvellement_possible', 'can'=>true, 'cannot'=>false]; + const ACF_CARD_PRICE_TOTAL = ['_name'=>'somme_a_regler']; // input number + const ACF_CARD_EXPIRATION = ['_name'=>'fin_de_validite']; // input date + + /* + const ACF_CARD_STATE = [ + '_name' => 'etat_carte', + 'new' => 'Commande', + 'renew' => 'Renouvellement', + ]; + + const ACF_CARD_PAYMENT_METHOD = [ + '_name' => 'paiement', + 'paypal' => 'Paypal', + 'transfert' => 'Virement', + ]; + + const ACF_CARD_PRICE_CHOICE = [ + '_name' => 'tarif', + 'low' => '10', + 'high' => '15', + ]; + + const ACF_CARD_PRICE_DELIVERY = [ + '_name' => 'livraison', + 'pdf' => 'PDF', + 'post' => 'Fabrication', + ]; + + const ACF_PROF_IS_ACTIV = [ + '_name' => 'compte-actif', + 'activ' => 'Actif', + 'inactiv' => 'Inactif', + ]; + + const ACF_PROF_CGV = [ + '_name' => 'cgv', + 'cgv' => 'cgv', + ]; + + const ACF_PROF_CAN_RENEW = [ + '_name' => 'renouvellement_possible', + 'can' => true, + 'cannot' => false, + ]; + + const ACF_CARD_PRICE_TOTAL = ['_name' => 'somme_a_regler']; // input number + const ACF_CARD_EXPIRATION = ['_name' => 'fin_de_validite']; // input date + */ + +/* const ACF_CARD_STATE = 'etat_carte'; + const ACF_CARD_STATE__FIRST = 'Commande'; + const ACF_CARD_STATE__RENEW = 'Renouvellement'; + const ACF_CARD_PAYMENT_METHOD = 'paiement'; + const ACF_CARD_PAYMENT_METHOD__PAYPAL = 'Paypal'; + const ACF_CARD_PAYMENT_METHOD__TRANSFERT = 'Virement'; + const ACF_CARD_PRICE_CHOICE = 'tarif'; + const ACF_CARD_PRICE_CHOICE__LOW = '10'; + const ACF_CARD_PRICE_CHOICE__HIGH = '15'; + const ACF_CARD_PRICE_DELIVERY = 'livraison'; - const ACF_CARD_PRICE_TOTAL = 'somme_a_regler'; - const ACF_CARD_EXPIRATION = 'fin_de_validite'; + const ACF_CARD_PRICE_DELIVERY__PDF = 'PDF'; + const ACF_CARD_PRICE_DELIVERY__POST = 'Fabrication'; + const ACF_PROF_IS_ACTIV = 'compte-actif'; - const ACF_PROF_CAN_RENEW = 'renouvellement_possible'; + const ACF_PROF_IS_ACTIV__ACTIV = 'Actif'; + const ACF_PROF_IS_ACTIV__INACTIV = 'Inactif'; + const ACF_PROF_CGV = 'cgv'; + const ACF_PROF_CGV__CGV = 'cgv'; - 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') + const ACF_CARD_PRICE_TOTAL = 'somme_a_regler'; // input number + const ACF_CARD_EXPIRATION = 'fin_de_validite'; // input date + const ACF_PROF_CAN_RENEW = 'renouvellement_possible'; // input true/false +*/ + // META + const META_PAYEMENT_STATUS = 'cipf_payement_status'; + + // SLUG const SLUG_PROF_INACTIV = 'validation-en-cours'; const SLUG_RENEW_CARD = 'commande'; const SLUG_PAGE_REDIRECTION = 'redirection_cipf'; const SLUG_PAYPAL_REDIRECTION_SUCCESS = self::SLUG_PAGE_REDIRECTION; const SLUG_PAYPAL_REDIRECTION_FAILURE = self::SLUG_PAGE_REDIRECTION; - const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php) - + // PAYPAL const PAYPAL_CLIENT_ID = "AfcmwxIXlG2ZxaMdjazX57I70BXz__aEqNWaTnqfSCI34a0V7nMbytswx7EViUjlpHs7opyrRwaH9YLl"; const PAYPAL_CLIENT_SECRET = "EGunIhGRjPvn0Z8wXO0JsdhET30OStTAH_IyRsmhimEN23_qiRSFD-ql4tvnulKJw6TitZ-vU-ytc4A-"; const PAYPAL_API_BASE_URL = "https://api-m.sandbox.paypal.com"; const PAYPAL_MESSAGE_SUCCESS = '`paiement reussi`'; const PAYPAL_MESSAGE_FAILURE = '`paiement raté`'; + // OTHER + 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') + const USER_INFO_DATE_FORMAT = 'd/m/Y'; // for user_infos.php (date format : https://www.php.net/manual/fr/datetime.format.php) + private static $_root_path; diff --git a/private b/private index a135dcb..3004ec5 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit a135dcbe03376cbc3412f2ca921a1457bcf29ebd +Subproject commit 3004ec54284338e204084be6de10150b5a8971cb