created functions to handle all state changes

This commit is contained in:
asus
2024-03-19 12:37:16 +01:00
parent f221e43807
commit 5010b70d79
7 changed files with 317 additions and 97 deletions

View File

@@ -44,6 +44,7 @@ include_once(PLGNTLS_class::root_path() . 'php/author_restriction.php');
include_once(PLGNTLS_class::root_path() . 'php/profs_profil.php');
include_once(PLGNTLS_class::root_path() . 'php/profs_form.php');
include_once(PLGNTLS_class::root_path() . 'php/profs_dates.php');
include_once(PLGNTLS_class::root_path() . 'php/profs_states.php');
include_once(PLGNTLS_class::root_path() . 'php/partners_register.php');
include_once(PLGNTLS_class::root_path() . 'php/partners_page.php');

View File

@@ -60,7 +60,7 @@ function update_user_pre_order_CIPF($message) {
* - 'nothing' -> output nothing on author page
*
*/
update_field($acf_payment_status['_name'], $acf_payment_status['started'], $acf_id);
set_payment_started($user_id);
}
@@ -128,18 +128,10 @@ function update_user_post_capture_CIPF($message) {
*/
function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;
$card_duration = PLGNTLS_class::CARD_VALIDITY_TIME;
$prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
$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
*
@@ -147,25 +139,8 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
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_field($acf_payment_status['_name'], $acf_payment_status['failure'], $acf_id);
/*
* other changes on user :
* //- pofil is inactive
* - account_stat is to_pay
*
*/
//update_field($prof_is_activ['_name'], $prof_is_activ['inactiv'], $acf_id);
update_field($acf_account_state['_name'], $acf_account_state['to_pay'], $acf_id);
set_payment_failure($user_id);
set_account_to_pay_CIPF($user_id);
}
@@ -176,26 +151,10 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
function success_payment_for_user_CIPF($user_id, $order_id) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;
$acf_card_number = PLGNTLS_class::ACF_CARD_NUMBER;
$card_duration = PLGNTLS_class::CARD_VALIDITY_TIME;
$prof_is_activ = PLGNTLS_class::ACF_PROF_IS_ACTIV;
$acf_payment_status = PLGNTLS_class::ACF_CARD_PAYMENT_STATE;
$meta_order_id = PLGNTLS_class::META_ORDER_ID;
/*
$acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
$acf_payement = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
$acf_delivery = PLGNTLS_class::ACF_CARD_PRICE_DELIVERY;
$acf_price = PLGNTLS_class::ACF_CARD_PRICE_CHOICE;
*/
/*
* acf uses 'Y-m-d H:i:s' format : https://www.advancedcustomfields.com/resources/date-time-picker/
*
*/
$acf_id = 'user_'.$user_id;
@@ -206,53 +165,14 @@ function success_payment_for_user_CIPF($user_id, $order_id) {
delete_user_meta($user_id, $meta_order_id, $order_id);
/*
* update date limit validity to add 1 year
*
*/
update_card_expiration_CIPF($user_id, $card_duration);
/*
* - if card status is command, create card id
* - make card status to renewal
*/
$card_status = get_field($acf_card_state['_name'], $acf_id);
if ($card_status === $acf_card_state['new']) {
$card_id = $date_now->format('Ymd') . $user_id;
error_log("-------");
error_log("card_id");
error_log($card_id);
update_field($acf_card_number['_name'], $card_id, $acf_id);
if (is_card_new_CIPF()) {
set_card_number_CIPF($user_id);
}
update_field($acf_card_state['_name'], $acf_card_state['renew'], $acf_id);
set_card_renew_CIPF($user_id);
/*
* change user :
* //- profile is active
* - payement status is success
* - account_state is valid
*
*/
//update_field($prof_is_activ['_name'], $prof_is_activ['activ'], $acf_id);
update_field($acf_payment_status['_name'], $acf_payment_status['success'], $acf_id);
update_field($acf_account_state['_name'], $acf_account_state['valid'], $acf_id);
/*
* reset some fields for the form to buy the card
* - cgv
* - paiement
* - livraison
* - tarif
*
* could make sense to put it here,
* but actually it needs to be put at begining of form
*
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);
*/
set_payment_success($user_id);
}

View File

@@ -65,11 +65,11 @@ function check_can_pay_CIPF() {
/*
* ABANDONNED : formbuilder disfunction when acf field has value:label and return on label, so it's easier for us to just believe their calculation
* calculate price
* update the price even if form builder already did it
* in case it was changed from admin pannel
*
*/
$tarif = get_acf_value_CIPF($acf_card_price_choice['_name'], $acf_id);
$livraison = get_acf_value_CIPF($acf_card_price_delivery['_name'], $acf_id);
if ($tarif !== null && $livraison !== null) {
@@ -79,7 +79,6 @@ function check_can_pay_CIPF() {
$price = 0;
}
update_field($acf_card_price_total['_name'], $price, $acf_id);
*/
/*

View File

@@ -126,7 +126,11 @@ function is_card_date_expired_CIPF($user_id = null) {
/*
* will add card_duration to card
* card_duration is expected in a string format : https://www.php.net/manual/en/class.dateinterval.php
*
*/
function update_card_expiration_CIPF($user_id = null, $card_duration = '0') {
PLGNTLS_class::debug_infos();
$acf_card_expiration = PLGNTLS_class::ACF_CARD_EXPIRATION;

View File

@@ -15,7 +15,7 @@ if (!defined('ABSPATH')) {
* actions after prof form is validated
*
*/
function prof_form_reset_fields_CIPF($form_id, $post_array, $form_type) {
function prof_after_form_CIPF($form_id, $post_array, $form_type) {
PLGNTLS_class::debug_infos();
$acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
@@ -32,15 +32,21 @@ function prof_form_reset_fields_CIPF($form_id, $post_array, $form_type) {
/*
* if new prof, change status to 'to pay'
* change status to :
* if new prof & paypal : 'to pay'
* if expired card & paypal : 'to_pay'
*
*/
$is_new = get_field($acf_account_state['_name'], $acf_id);
if ($is_new === $acf_account_state['new']) {
update_field($acf_account_state['_name'], $acf_account_state['to_pay'], $acf_id);
if (is_payment_method_paypal_CIPF()) {
if (is_account_new_CIPF()) {
set_account_to_pay_CIPF();
}
if (is_account_expired_CIPF()) {
set_account_to_pay_CIPF();
}
}
}
add_action('df_after_process', 'prof_form_reset_fields_CIPF', 10, 3);
add_action('df_after_process', 'prof_after_form_CIPF', 10, 3);

View File

@@ -0,0 +1,290 @@
<?php
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
* [/] etat compte ('etat_compte') :
* 1. new . 'nouveau prof' -> ok : [1: a l'inscription - ok]
* 2. to_pay . 'doit payer' -> ko : [1: apres premier form - ok], [2: apres form qui suit carte expiree - ok], [3: apres paiement immediat echoue - ko], [4: apres virement echoue - ko]
* 3. valid . 'carte valide' ->
* 4. waiting_invalid . 'en attente invalide' ->
* 5. waiting_valid . 'en attente valide' ->
* 6. expired . 'carte expiree' ->
*
* [/] etat carte ('etat_carte') :
* - 'Commande'
* - 'Renouvellement'
*
* [ ] activation du compte ('compte-actif') :
* - 'Actif' -> equivalent a etat compte [3] carte valide
* - 'Inactif' -> equivalent a etat compte [2] doit payer
*
* [/] etat paiement ('etat_paiement') :
* - 'en_cours'
* - 'reussi'
* - 'echec'
* - 'aucun'
*
* [/] type paiement ('paiement') :
* - 'Paypal'
* - 'Virement'
*
* [/] numero de carte ('numero_de_la_carte')
*
*/
/*
* global 'setter' and 'izzer' for this file
*
*/
function is_acf_state_CIPF($user_id = null, $acf_field, $state_name) {
PLGNTLS_class::debug_infos();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
$acf_state = get_field($acf_field['_name'], $acf_id);
if ($acf_state === $acf_field[$state_name]) {
return true;
}
return false;
}
function set_acf_state_CIPF($user_id = null, $acf_field, $state_name) {
PLGNTLS_class::debug_infos();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
update_field($acf_field['_name'], $acf_field[$state_name], $acf_id);
}
/*
* etat compte ('etat_compte') :
* 1. new . 'nouveau prof' -> ok : [1: a l'inscription - ok]
* 2. to_pay . 'doit payer' -> ko : [1: apres premier form - ok], [2: apres form qui suit carte expiree - ok], [3: apres paiement immediat echoue - ko], [4: apres virement echoue - ko]
* 3. valid . 'carte valide' ->
* 4. waiting_invalid . 'en attente invalide' ->
* 5. waiting_valid . 'en attente valide' ->
* 6. expired . 'carte expiree' ->
*
* receives the name of the state, and compare it to the current state
* either use the is_acf_card_state_CIPF() function directly,
* or one of the specific functions
*
*/
function is_account_state_CIPF($user_id = null, $state_name) {
PLGNTLS_class::debug_infos();
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
return is_acf_state_CIPF($user_id, $acf_account_state, $state_name);
}
function is_account_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'new');
}
function is_account_to_pay_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'to_pay');
}
function is_account_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'valid');
}
function is_account_waiting_invalid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'waiting_invalid');
}
function is_account_waiting_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'waiting_valid');
}
function is_account_expired_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF($user_id, 'expired');
}
/*
* setters :
*/
function set_account_state_CIPF($user_id = null, $state_name) {
PLGNTLS_class::debug_infos();
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
set_acf_state_CIPF($user_id, $acf_account_state, $state_name);
}
function set_account_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'new');
}
function set_account_to_pay_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'to_pay');
}
function set_account_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'valid');
}
function set_account_waiting_invalid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'waiting_invalid');
}
function set_account_waiting_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'waiting_valid');
}
function set_account_expired_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF($user_id, 'expired');
}
/*
* etat carte ('etat_carte') :
* - 'Commande'
* - 'Renouvellement'
*
*/
function is_card_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
return is_acf_state_CIPF($user_id, $acf_card_state, 'new');
}
function is_card_renew_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
return is_acf_state_CIPF($user_id, $acf_card_state, 'renew');
}
function set_card_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
set_acf_state_CIPF($user_id, $acf_card_state, 'new');
}
function set_card_renew_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
set_acf_state_CIPF($user_id, $acf_card_state, 'renew');
}
/*
* type paiement ('paiement') :
* - 'paypal'=>'Paypal'
* - 'transfert'=>'Virement'
*
*/
function is_payment_method_paypal_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
return is_acf_state_CIPF($user_id, $acf_card_payment_method, 'paypal');
}
function is_payment_method_transfert_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
return is_acf_state_CIPF($user_id, $acf_card_payment_method, 'transfert');
}
/*
* etat paiement ('etat_paiement') :
* - 'en_cours'
* - 'reussi'
* - 'echec'
* - 'aucun'
*
* 'started'=>'en_cours', 'success'=>'reussi', 'failure'=>'echec', 'nothing'=>'aucun'
*
*/
function is_payment_state_CIPF($user_id = null, $type) {
PLGNTLS_class::debug_infos();
$acf_card_payment_state = PLGNTLS_class::ACF_CARD_PAYMENT_STATE;
return is_acf_state_CIPF($user_id, $acf_card_payment_state, $type);
}
function is_payment_started($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF($user_id, 'started');
}
function is_payment_success($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF($user_id, 'success');
}
function is_payment_failure($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF($user_id, 'failure');
}
function is_payment_nothing($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF($user_id, 'nothing');
}
/*
* setters
*/
function set_payment_state_CIPF($user_id = null, $type) {
PLGNTLS_class::debug_infos();
$acf_card_payment_state = PLGNTLS_class::ACF_CARD_PAYMENT_STATE;
set_acf_state_CIPF($user_id, $acf_card_payment_state, $type);
}
function set_payment_started($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF($user_id, 'started');
}
function set_payment_success($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF($user_id, 'success');
}
function set_payment_failure($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF($user_id, 'failure');
}
function set_payment_nothing($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF($user_id, 'nothing');
}
/*
*
*/
function set_card_number_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_number = PLGNTLS_class::ACF_CARD_NUMBER;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$card_id = $date_now->format('Ymd') . $user_id;
update_field($acf_card_number['_name'], $card_id, $acf_id);
}
?>

View File

@@ -70,7 +70,7 @@ class PLGNTLS_class {
const ACF_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
const ACF_ACCOUNT_STATE = ['_name'=>'etat_compte', 'new'=>'nouveau prof', 'to_pay'=>'doit payer', 'valid'=>'carte valide', 'waiting'=>'en attente', 'expired'=>'carte expiree', ];
const ACF_ACCOUNT_STATE = ['_name'=>'etat_compte', 'new'=>'nouveau prof', 'to_pay'=>'doit payer', 'valid'=>'carte valide', 'waiting_invalid'=>'en attente invalide', 'waiting_valid'=>'en attente valide', 'expired'=>'carte expiree', ];
// META
const META_PAYEMENT_STATUS = 'cipf_payement_status';