diff --git a/plugins/fipfcard_plugin/js/paypal/create_order.js b/plugins/fipfcard_plugin/js/paypal/create_order.js index 684a8d8..260a4a5 100644 --- a/plugins/fipfcard_plugin/js/paypal/create_order.js +++ b/plugins/fipfcard_plugin/js/paypal/create_order.js @@ -15,18 +15,17 @@ export async function createOrder() { method: "POST", headers: { "Content-Type": "application/json", - //"X-WP-Nonce": PLGNTLS_data.rest_nonce, }, - // use the "body" param to optionally pass additional order information - // like product ids and quantities - body: JSON.stringify({ - cart: [ - { - id: "1234", - quantity: "1", - }, - ], - }), + //// use the "body" param to optionally pass additional order information + //// like product ids and quantities + //body: JSON.stringify({ + // cart: [ + // { + // id: "1234", + // quantity: "1", + // }, + // ], + //}), }); const orderData = await response.json(); diff --git a/plugins/fipfcard_plugin/php/can_user_pay.php b/plugins/fipfcard_plugin/php/can_user_pay.php new file mode 100644 index 0000000..4834035 --- /dev/null +++ b/plugins/fipfcard_plugin/php/can_user_pay.php @@ -0,0 +1,14 @@ + diff --git a/plugins/fipfcard_plugin/php/paypal/paypal.php b/plugins/fipfcard_plugin/php/paypal/paypal.php index af48377..4492286 100644 --- a/plugins/fipfcard_plugin/php/paypal/paypal.php +++ b/plugins/fipfcard_plugin/php/paypal/paypal.php @@ -1,15 +1,22 @@ add_to_front( - array( - $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', 'paypal_shortcode_content_FIPF'); - - - - -/** - * 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 add_id_to_script_FIPF( $tag, $handle, $src ) { - if ( $handle === 'PLGNTLS_paypal_js' ) { - $tag = ''; - } - return $tag; -} -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 routes_endpoints_FIPF() -{ - $base_rest_route = "fipf_plugin/api/v1"; - register_rest_route($base_rest_route, '/orders', array( - 'methods' => 'POST', - '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' => 'handle_orders_capture_request_FIPF', - )); -}; -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 2d764a4..e35d5d6 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders.php @@ -1,16 +1,13 @@ getMessage()); - return new WP_Error('500', 'Failed to create order.', array('status' => 500)); + error_log('Failed to create order: '); + error_log(json_encode($error)); + return new WP_Error('500', 'Failed to create order :' . $error->getMessage(), array('status' => 500)); } } @@ -43,12 +48,17 @@ function handle_orders_request_FIPF($request_data) { * Create an order to start the transaction. * @see https://developer.paypal.com/docs/api/orders/v2/#orders_create */ -function create_order_FIPF( $cart ) +//function create_order_FIPF($cart) +function create_order_FIPF() { // use the cart information passed from the front-end to calculate the purchase unit details $access_token = generate_access_token_FIPF(); + $user_id = get_current_user_id(); + $acf_id = 'user_' . $user_id; + $price = get_field('somme_a_regler', $acf_id); + $url = PAYPAL_API_BASE_URL . '/v2/checkout/orders'; $payload = array( 'intent' => "CAPTURE", @@ -56,7 +66,7 @@ function create_order_FIPF( $cart ) array( 'amount' => array( 'currency_code' => "EUR", - 'value' => "1.00", + 'value' => $price, ), ), ), 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 a7f97c9..0387158 100644 --- a/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/fipfcard_plugin/php/paypal/route_api_orders_capture.php @@ -1,15 +1,14 @@ 'POST', + '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' => 'handle_orders_capture_request_FIPF', + )); +}; +add_action('rest_api_init', 'routes_endpoints_FIPF'); + + + + +?> diff --git a/plugins/fipfcard_plugin/php/paypal/shortcode.php b/plugins/fipfcard_plugin/php/paypal/shortcode.php new file mode 100644 index 0000000..610b6af --- /dev/null +++ b/plugins/fipfcard_plugin/php/paypal/shortcode.php @@ -0,0 +1,59 @@ +add_to_front( + array( + $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', 'paypal_shortcode_content_FIPF'); + + +function no_payment_CIPF() { + return; +} + + + +?> diff --git a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php index 28a6d32..7312ade 100644 --- a/plugins/fipfcard_plugin/php/paypal/update_user_payment.php +++ b/plugins/fipfcard_plugin/php/paypal/update_user_payment.php @@ -87,7 +87,6 @@ function update_user_payment_FIPF($message, $step) /* -* * 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) @@ -102,26 +101,30 @@ function update_user_payment_FIPF($message, $step) * -> 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; + $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); */ - $post_id = 'user_'.$user_id; - update_field(CARD_IS_VALID, true, $post_id); + update_field($acf_card_state, 'Renouvellement', $acf_id); + $date_now = date($acf_date_format); /* * update purchase date to now + update_field(CARD_DATE_PURCHASE, $date_now, $acf_id); */ - $date_now = date($acf_date_format); - update_field(CARD_DATE_PURCHASE, $date_now, $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); + $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 @@ -147,7 +150,7 @@ function validate_payment_for_user_FIPF($user_id, $order_id) { */ $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); + update_field($acf_card_expiration, $new_date_limit, $acf_id); } diff --git a/plugins/fipfcard_plugin/php/paypal/user_can_pay.php b/plugins/fipfcard_plugin/php/paypal/user_can_pay.php new file mode 100644 index 0000000..99b95d4 --- /dev/null +++ b/plugins/fipfcard_plugin/php/paypal/user_can_pay.php @@ -0,0 +1,94 @@ + false, 'message' => "error: no price selected"); + } + if ($price === 0) { + return array('success' => false, 'message' => "error: price is 0, nothing to purchase"); + } + + /* + * date validity is empty + * or is paste + * or is less than 1 month + * + */ + $validity_field = get_field_object($acf_card_expiration, $acf_id); + $validity = $validity_field['value']; + $format_field = $validity_field['return_format']; + $format_acf = 'Y-m-d H:i:s'; + + if (empty($validity)) + return array('success' => true); + + $date_now = date_create('now'); + $date_validity = date_create_from_format($format_field, $validity); + $diff = date_diff($date_now, $date_validity)->format('%R%a'); + error_log("diff"); + error_log($diff); + if ((int)$diff <= 0) { + // date end of validity in the past + return array('success' => true); + } + else if ((int)$diff <= $card_renew_period) { + // date expiration is in less that renew period time (ex: 30 days) + return array('success' => true); + } + else { + // date end of validity is in more than renew perdio (ex: 3 month) + return array('success' => false, 'message' => "error: it's too soon to renew your card"); + } + + return array('success' => true); +} + + + + +?> diff --git a/plugins/fipfcard_plugin/php/redirections.php b/plugins/fipfcard_plugin/php/redirections.php index 1cea86e..4caefd6 100644 --- a/plugins/fipfcard_plugin/php/redirections.php +++ b/plugins/fipfcard_plugin/php/redirections.php @@ -12,7 +12,7 @@ if (!defined('ABSPATH')) { /* * redirect users to profil * if prof -> author page -* if partenaire -> post page / project page / home page +* if partenaire -> post page || home page */ function redirection_page_CIPF(){ if (!is_page(PROF_PARTENAIRE_REDIRECTION_PAGE)) @@ -29,7 +29,7 @@ function redirection_page_CIPF(){ } else if (current_user_can('partenaire')) { $args = array( - 'post_type' => 'post,project', + 'post_type' => 'post', 'author' => $current_user_id, 'posts_per_page' => 1, ); diff --git a/plugins/fipfcard_plugin/php/reset_acf_fields.php b/plugins/fipfcard_plugin/php/reset_acf_fields.php index ab35251..c53d3e0 100644 --- a/plugins/fipfcard_plugin/php/reset_acf_fields.php +++ b/plugins/fipfcard_plugin/php/reset_acf_fields.php @@ -3,17 +3,14 @@ /* function reset_some_fields_CIPF($form_id, $user_id, $post_array) { +*/ function reset_some_fields_CIPF() { $user_id = get_current_user_id(); update_field('cgv', array(""), 'user_'.$user_id); update_field('paiement', array(""), 'user_'.$user_id); update_field('livraison', array(""), 'user_'.$user_id); } -add_shortcode('test_reset_acf', 'reset_some_fields_CIPF'); -*/ -/* -add_action('df_after_insert_user', 'reinit_some_fields_CIPF', 10, 3); -*/ +add_action('df_after_insert_user', 'reset_some_fields_CIPF'); ?> diff --git a/plugins/fipfcard_plugin/utils/plgntls_class.php b/plugins/fipfcard_plugin/utils/plgntls_class.php index cca8f63..c914727 100644 --- a/plugins/fipfcard_plugin/utils/plgntls_class.php +++ b/plugins/fipfcard_plugin/utils/plgntls_class.php @@ -56,6 +56,14 @@ if (!defined('ABSPATH')) { class PLGNTLS_class { + const ACF_CARD_STATE = 'etat_carte'; + const ACF_CARD_PAYMENT_METHOD = 'paiement'; + const ACF_CARD_PRICE_CHOICE = 'tarif'; + 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 + private static $_root_path; private static $_root_url; diff --git a/private b/private index ff731c1..c04d227 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit ff731c1ad4dcd4ec401f9052c14a3ce1af432a49 +Subproject commit c04d227649c57fa15a8f353b03d91dcfdda04fab diff --git a/wordpress_docker b/wordpress_docker index f45db59..82e6509 160000 --- a/wordpress_docker +++ b/wordpress_docker @@ -1 +1 @@ -Subproject commit f45db59f8b715aa592dd7d977745f55f6a294604 +Subproject commit 82e65092cf4c51318b8bff46384efbacce88e794