update acf field handling to centerize every call
This commit is contained in:
@@ -38,20 +38,17 @@ if (!defined('ABSPATH')) {
|
||||
function update_user_pre_order_CIPF($message) {
|
||||
Plgntls::debug_infos();
|
||||
$acf_payment_status = Cipf::ACF_CARD_PAYMENT_STATE;
|
||||
$meta_order_id = Cipf::META_ORDER_ID;
|
||||
|
||||
$order_id = $message->id;
|
||||
$user_id = get_current_user_id();
|
||||
$acf_id = 'user_'.$user_id;
|
||||
|
||||
/*
|
||||
* - delete previous order ids
|
||||
* ->if we are here it's because a new order will try to be created
|
||||
* - addind order_id to cipf_order_id meta field
|
||||
* set new order_id
|
||||
* -> it will delete the previous one
|
||||
* -> if we are here it's because a new order will try to be created
|
||||
*
|
||||
*/
|
||||
delete_user_meta($user_id, $meta_order_id);
|
||||
add_user_meta($user_id, $meta_order_id, $order_id);
|
||||
set_acf_order_id_CIPF($order_id, $user_id);
|
||||
|
||||
/*
|
||||
* create a meta field to check states of payements on prof author page :
|
||||
@@ -77,8 +74,9 @@ function update_user_post_capture_CIPF($message) {
|
||||
$order_id = $message->id;
|
||||
$user_id = get_current_user_id();
|
||||
$status = null;
|
||||
if (is_object($message) && isset($message->status))
|
||||
if (is_object($message) && isset($message->status)) {
|
||||
$status = $message->status;
|
||||
}
|
||||
|
||||
/*
|
||||
* capture status : https://developer.paypal.com/docs/api/orders/v2/#definition-order_status
|
||||
@@ -96,18 +94,14 @@ function update_user_post_capture_CIPF($message) {
|
||||
* -> it is at least necessary to find the user who did the purchase
|
||||
*
|
||||
*/
|
||||
// find the user containing the order_id and delete this order_id
|
||||
$user_id_to_update = find_user_with_order_id_CIPF($user_id, $order_id);
|
||||
if ($user_id_to_update === false)
|
||||
if ($user_id_to_update === false) {
|
||||
throw new HttpException('cannot find user with this order_id', 502);
|
||||
}
|
||||
if ($status === 'COMPLETED') {
|
||||
// proceed to validate payment for user
|
||||
success_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_payment_for_user_CIPF($user_id_to_update, $order_id, $status);
|
||||
}
|
||||
@@ -129,17 +123,10 @@ function update_user_post_capture_CIPF($message) {
|
||||
*/
|
||||
function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
|
||||
Plgntls::debug_infos();
|
||||
$meta_order_id = Cipf::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);
|
||||
|
||||
|
||||
// schedule_delete_orderid_CIPF($user_id, $order_id)
|
||||
set_payment_failure_CIPF($user_id);
|
||||
set_account_to_pay_CIPF($user_id);
|
||||
send_emails_CIPF('payment_echec', $user_id);
|
||||
@@ -152,18 +139,11 @@ function failure_payment_for_user_CIPF($user_id, $order_id, $status) {
|
||||
*/
|
||||
function success_payment_for_user_CIPF($user_id, $order_id) {
|
||||
Plgntls::debug_infos();
|
||||
$meta_order_id = Cipf::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);
|
||||
|
||||
|
||||
// schedule_delete_orderid_CIPF($user_id, $order_id)
|
||||
update_card_expiration_CIPF($user_id);
|
||||
|
||||
if (is_card_new_CIPF()) {
|
||||
@@ -188,10 +168,10 @@ function success_payment_for_user_CIPF($user_id, $order_id) {
|
||||
*/
|
||||
function find_user_with_order_id_CIPF($current_user_id, $order_id) {
|
||||
Plgntls::debug_infos();
|
||||
$meta_order_id = Cipf::META_ORDER_ID;
|
||||
$acf_order_id = Cipf::ACF_CARD_ORDER_ID;
|
||||
$role_prof = Cipf::ROLE_PROF;
|
||||
|
||||
$user_metas = get_user_meta($current_user_id, $meta_order_id, false);
|
||||
$user_metas = get_user_meta($current_user_id, $acf_order_id['_name'], false);
|
||||
if (in_array($order_id, $user_metas)) {
|
||||
return $current_user_id;
|
||||
}
|
||||
@@ -202,16 +182,18 @@ function find_user_with_order_id_CIPF($current_user_id, $order_id) {
|
||||
* https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/
|
||||
*
|
||||
*/
|
||||
$users = get_users(array(
|
||||
$user = get_users(array(
|
||||
'role' => $role_prof,
|
||||
'meta_key' => $meta_order_id,
|
||||
'meta_key' => $acf_order_id,
|
||||
'meta_value' => $order_id,
|
||||
'fields' => 'ID',
|
||||
));
|
||||
if (count($users) > 1)
|
||||
if (count($user) > 1) {
|
||||
throw new HttpException('multiple users with same order_id', 502);
|
||||
else if (count($users) === 1)
|
||||
return reset($users);
|
||||
}
|
||||
else if (count($user) === 1) {
|
||||
return reset($user);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,6 +54,10 @@ function check_can_pay_CIPF() {
|
||||
$user_id = get_current_user_id();
|
||||
$acf_id = 'user_' . $user_id;
|
||||
|
||||
|
||||
|
||||
schedule_prof_account_suppression_CIPF($user_id);
|
||||
|
||||
/*
|
||||
* check if payment is virement or immediat
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user