diff --git a/plugins/cipf_plugin/php/paypal/route_api_orders.php b/plugins/cipf_plugin/php/paypal/route_api_orders.php index b38b8c0..c5991f4 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders.php @@ -58,14 +58,6 @@ function handle_orders_request_CIPF($request_data) { * if it fails here : the order was created in paypal * but a pbm occured in treating it with the user * - * 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 - * */ try { update_user_pre_order_CIPF($json_response, 'start'); 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 87ccf6d..631be0d 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php @@ -43,13 +43,13 @@ function handle_orders_capture_request_CIPF($request) { * update the client situation * and return the response of paypal * - * 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 + * capture status : https://developer.paypal.com/docs/api/orders/v2/#definition-order_status + * - COMPLETED + * - DECLINED + * - PARTIALLY_REFUNDED + * - PENDING + * - REFUNDED + * - FAILED * */ try { diff --git a/plugins/cipf_plugin/php/paypal/update_user_payment.php b/plugins/cipf_plugin/php/paypal/update_user_payment.php index 4aa7539..4592da1 100644 --- a/plugins/cipf_plugin/php/paypal/update_user_payment.php +++ b/plugins/cipf_plugin/php/paypal/update_user_payment.php @@ -1,6 +1,5 @@ id; $user_id = get_current_user_id(); + $acf_id = 'user_'.$user_id; /* * - delete previous order ids @@ -58,9 +60,10 @@ function update_user_pre_order_CIPF($message) { * - 'failure' -> at capture failure (on author page, it means failure, then make it empty) * - 'nothing' -> output nothing on author page * - */ delete_user_meta($user_id, $meta_payement_status); add_user_meta($user_id, $meta_payement_status, 'started'); + */ + update_field($acf_payment_status['_name'], $acf_payment_status['started'], $acf_id); } @@ -72,7 +75,6 @@ function update_user_pre_order_CIPF($message) { */ function update_user_post_capture_CIPF($message) { PLGNTLS_class::debug_infos(); - $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; $order_id = $message->id; $user_id = get_current_user_id(); @@ -81,13 +83,13 @@ function update_user_post_capture_CIPF($message) { $status = $message->status; /* - * order status : https://developer.paypal.com/docs/api/orders/v2/#definition-order - * CREATED - * SAVED - * APPROVED ? - * VOIDED - * - COMPLETED - * PAYER_ACTION_REQUIRED + * capture status : https://developer.paypal.com/docs/api/orders/v2/#definition-order_status + * - COMPLETED + * - DECLINED + * - PARTIALLY_REFUNDED + * - PENDING + * - REFUNDED + * - FAILED */ /* @@ -97,16 +99,19 @@ function update_user_post_capture_CIPF($message) { * */ // find the user containing the order_id and delete this order_id - find_user_with_order_id_CIPF($user_id, $order_id); - $user_id_to_update = delete_order_id_on_success_CIPF($user_id, $order_id); + $user_id_to_update = find_user_with_order_id_CIPF($user_id, $order_id); if ($user_id_to_update === false) - return; + throw new HttpException('cannot find user with this order_id', 502); if ($status === 'COMPLETED') { // proceed to validate payment for user validate_payment_for_user_CIPF($user_id_to_update, $order_id); } + else if ($status === 'PENDING') { + // i don't know what to do yet, maybe make more checks and output a message to contact diego ? + // https://developer.paypal.com/docs/api/orders/v2/#definition-capture_status_details + } else { - failure_payement_for_user_CIPF($user_id_to_update, $order_id); + failure_payment_for_user_CIPF($user_id_to_update, $order_id, $status); } } @@ -114,10 +119,108 @@ function update_user_post_capture_CIPF($message) { /* * things to do when a payment has failed +* failed payement means order status is not COMPLETED or PENDING +* it can be : +* - DECLINED +* - PARTIALLY_REFUNDED +* - REFUNDED +* - FAILED +* -> capture status : https://developer.paypal.com/docs/api/orders/v2/#definition-order_status +* in any cases, the price was not paid * */ -function failure_payment_for_user_CIPF($user_id, $order_id) { +function failure_payment_for_user_CIPF($user_id, $order_id, $status) { PLGNTLS_class::debug_infos(); + + $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; + //$meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + $acf_payment_status = PLGNTLS_class::ACF_CARD_PAYMENT_STATE; + $meta_order_id = PLGNTLS_class::META_ORDER_ID; + + $acf_id = 'user_'.$user_id; + + /* + * remove the order_id from user meta + * + */ + delete_user_meta($user_id, $meta_order_id, $order_id); + + /* + * change payement status to failed + * - 'started' -> at order creation + * - 'success' -> at capture success (on author page, it means success, then mark it 'nothing') + * - 'failure' -> at capture failure (on author page, it means failure, then mark it 'nothing') + * - 'nothing' -> output nothing on author page + * + update_user_meta($user_id, $meta_payement_status, 'failure'); + */ + update_field($acf_payment_status['_name'], $acf_payment_status['failure'], $acf_id); + + /* + * acf uses 'Y-m-d H:i:s' format : https://www.advancedcustomfields.com/resources/date-time-picker/ + * + */ + $acf_date_format = 'Y-m-d H:i:s'; + $acf_id = 'user_'.$user_id; + + $date_now = date_create('today'); + + /* + * update purchase date to now + * + update_field(CARD_DATE_PURCHASE, $date_now, $acf_id); + */ + + /* + * get current date limit + * if no date, use now + * if paste date, use now + * + */ + $current_date_limit_object = get_field_object($acf_card_expiration['_name'], $acf_id); + + if ($current_date_limit_object === false) { + $current_date_limit = $date_now; + } + else if (empty($current_date_limit_object['value'])) { + $current_date_limit = $date_now; + } + else + { + $current_date_limit_string = $current_date_limit_object['value']; + $current_format_field = $current_date_limit_object['return_format']; + // 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 alike) + $current_date_limit = date_create_from_format($current_format_field, $current_date_limit_string); + $date_diff = date_diff($date_now, $current_date_limit); + $date_is_in_past = $date_diff->format('%R%a') < 0; + + if ($date_is_in_past) + $current_date_limit = $date_now; + } + + /* + * update date limit validity to add 1 year + * + */ + $date_plus_one_year = $current_date_limit->add(date_interval_create_from_date_string('+'.$card_duration)); + update_field($acf_card_expiration['_name'], $date_plus_one_year->format($acf_date_format), $acf_id); + + /* + * change user profil : + * - to active + * - card state is renewal + * - cannot renew + * + */ + 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); } @@ -132,13 +235,24 @@ function validate_payment_for_user_CIPF($user_id, $order_id) { $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; - $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + //$meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + $acf_payment_status = PLGNTLS_class::ACF_CARD_PAYMENT_STATE; + $meta_order_id = PLGNTLS_class::META_ORDER_ID; + + $acf_id = 'user_'.$user_id; + + /* + * remove the order_id from user meta + * + */ + delete_user_meta($user_id, $meta_order_id, $order_id); /* * change payement status to success * - */ update_user_meta($user_id, $meta_payement_status, 'success'); + */ + update_field($acf_payment_status['_name'], $acf_payment_status['success'], $acf_id); /* * acf uses 'Y-m-d H:i:s' format : https://www.advancedcustomfields.com/resources/date-time-picker/ @@ -252,23 +366,18 @@ function find_user_with_order_id_CIPF($current_user_id, $order_id) { 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, $meta_order_id, $order_id); -// if ($del_ret === true) -// return $user_id; -// } - - // https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/ - $users = get_users(array( - 'role' => $role_prof, - 'meta_key' => $meta_order_id, - 'meta_value' => $order_id, - 'fields' => 'ID', - )); + /* + * it means the current user didn't have this order_id + * so we look for another user + * https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/ + * + */ + $users = get_users(array( + 'role' => $role_prof, + 'meta_key' => $meta_order_id, + 'meta_value' => $order_id, + 'fields' => 'ID', + )); if (count($users) > 1) throw new HttpException('multiple users with same order_id', 502); else if (count($users) === 1) diff --git a/plugins/cipf_plugin/php/prof_check_page.php b/plugins/cipf_plugin/php/prof_check_page.php index 52ab07a..9fd1a63 100644 --- a/plugins/cipf_plugin/php/prof_check_page.php +++ b/plugins/cipf_plugin/php/prof_check_page.php @@ -52,16 +52,17 @@ function handle_prof_is_activ_CIPF($author_id) { /* -* check meta field META_PAYEMENT_STATUS +* check acf field payment_status * if field value is 'success' -* - hide block 'failure' -* - and update field to '', so it will not show next time +* - show block 'failure' +* - and update field to 'nothing', so it will not show next time +* if field value is 'failure' +* - show bloc success +* - and update field to 'nothing', so it will not show next time +* if field value is 'nothing' +* - do nothing (keep blocs hidden) * if field value is 'started' -* - hide bloc success -* (we assume it means the order didn't go well) -* (it does not really makes sens, but ok for the moment) -* if field value is '' -* - hide both 'success' and 'failure' blocs +* - do nothing (keep blocs hidden) * * .cipf_prof_paiement_message -> on row, added display none in page css * #cipf_prof_paiement_reussi -> on row @@ -71,13 +72,15 @@ function handle_prof_is_activ_CIPF($author_id) { function show_prof_paiement_messages_CIPF($user_id) { PLGNTLS_class::debug_infos(); $acf_prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV; - $meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + //$meta_payement_status = PLGNTLS_class::META_PAYEMENT_STATUS; + $acf_payment_status = PLGNTLS_class::ACF_CARD_PAYMENT_STATE; + + $acf_id = 'user_' . $user_id; /* * if prof is inactive, do nothing more * */ - $acf_id = 'user_' . $user_id; $is_activ = get_field($acf_prof_is_activ['_name'], $acf_id); if (is_null($is_activ) || empty($is_activ)) return; @@ -86,19 +89,23 @@ function show_prof_paiement_messages_CIPF($user_id) { $cipf_prof_payement = new PLGNTLS_class(); - $payement_status = get_user_meta($user_id, $meta_payement_status, true); - if ($payement_status === 'success') { + //$payement_status = get_user_meta($user_id, $meta_payement_status, true); + $payement_status = get_field($acf_payment_status['_name'], $acf_id); + //if ($payement_status === 'success') { + if ($payement_status === $acf_payment_status['success']) { $cipf_prof_payement->add_to_front(array( array( 'css' => '#cipf_prof_paiement_reussi {display: block;}' ) )); } - else if ($payement_status === 'started') { + //else if ($payement_status === 'started') { + else if ($payement_status === $acf_payment_status['failure']) { $cipf_prof_payement->add_to_front(array( array( 'css' => '#cipf_prof_paiement_echoue {display: block;}' ) )); } - update_user_meta($user_id, $meta_payement_status, ''); + //update_user_meta($user_id, $meta_payement_status, ''); + update_field($acf_payment_status['_name'], $acf_payment_status['nothing'], $acf_id); } diff --git a/plugins/cipf_plugin/utils/plgntls_class.php b/plugins/cipf_plugin/utils/plgntls_class.php index 24763aa..8c9428c 100644 --- a/plugins/cipf_plugin/utils/plgntls_class.php +++ b/plugins/cipf_plugin/utils/plgntls_class.php @@ -71,6 +71,7 @@ class PLGNTLS_class const ACF_CARD_PRICE_TOTAL = ['_name'=>'somme_a_regler']; // number const ACF_CARD_CARD_NUMBER = ['_name'=>'numero_de_la_carte']; // number const ACF_CARD_EXPIRATION = ['_name'=>'fin_de_validite']; // date picker + const ACF_CARD_PAYMENT_STATE = ['_name'=>'etat_paiement', 'started'=>'en_cours', 'success'=>'reussi', 'failure'=>'echec', 'nothing'=>'aucun']; // radio button // META const META_PAYEMENT_STATUS = 'cipf_payement_status'; diff --git a/private b/private index 3004ec5..f90162b 160000 --- a/private +++ b/private @@ -1 +1 @@ -Subproject commit 3004ec54284338e204084be6de10150b5a8971cb +Subproject commit f90162bddccbd22426755f11eace37504f779f87