diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index c962e23..17d466a 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -116,7 +116,8 @@ class Cipf { //const PAYPAL_CLIENT_ID = self::PAYPAL_HUGO_LIVE_CLIENT_ID; //const PAYPAL_CLIENT_SECRET = self::PAYPAL_HUGO_LIVE_CLIENT_SECRET; //const PAYPAL_API_BASE_URL = self::PAYPAL_HUGO_LIVE_API_BASE_URL; - const PAYPAL_MESSAGE_SUCCESS = 'Paiement reussi, vous allez être redirigé-es vers votre espace'; + const PAYPAL_MESSAGE_SUCCESS = 'Paiement réussi, vous allez être redirigé-es vers votre espace'; + const PAYPAL_MESSAGE_PROBLEM = "Paiement réussi, mais une erreure est survenue dans le traitement de la commande, si vous voyez que votre compte n'est pas mis à jour correctement, contactez la fipf directement"; const PAYPAL_MESSAGE_FAILURE = 'Paiement raté, vous allez être redirigé-es vers votre espace'; // ROLES @@ -147,6 +148,11 @@ vous allez être redirigés vers votre espace', 'failure'=> 'Paiement échoué, vous allez être redirigés vers votre espace', + 'problem'=> +"Paiement réussi, +mais une erreur est survenue dans le traitement de la commande, +si vous voyez que votre compte n'est pas mis à jour correctement, +contactez la fipf directement", ], ]; const OPTION_EMAILS = [ @@ -154,103 +160,94 @@ vous allez être redirigés vers votre espace', '_callback'=>'update_emails_settings_option_CIPF', '_default'=>[ // 1. profs : email : payment_success : validation payment prof reussi, send email - [ - 'action'=>'payment_success', + 'payment_success'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"paiement réussi", 'notification_message'=>"par ici la monnaie", 'confirmation_send'=>true, 'confirmation_subject'=>"paiement réussi", - 'confirmation_mesage'=>"donne l'argent", + 'confirmation_message'=>"donne l'argent", ], // 2. profs : email : payment_failure : validation payment prof echec, send email - [ - 'action'=>'payment_echec', + 'payment_echec'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 3. profs : email : transfert_success : validation transfert prof reussi, send email - [ - 'action'=>'transfert_success', + 'transfert_success'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 4. profs : email : transfert_failures : validation transfert prof echec, send email - [ - 'action'=>'transfert_echec', + 'transfert_echec'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 5. partners : email : offer_expired : offres temporaires -> gerer qu'elles disparaissent apres la date de validite -> la passer en masquer - [ - 'action'=>'offer_expired', + 'offer_expired'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 6. partners : email : offer_will_expire : la gestion des offres à échéance - [ - 'action'=>'offer_will_expire', + 'offer_will_expire'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", -// 7. payments : email : account_deleted : schedule event pour supprimer le compte xx temps (6 mois ?) apres fin de validite de la carte + 'confirmation_message'=>"", ], - [ - 'action'=>'account_deleted', +// 7. payments : email : account_deleted : schedule event pour supprimer le compte xx temps (6 mois ?) apres fin de validite de la carte + 'account_deleted'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 8. payments : email : account_will_expire : faire rappels emails avant expiration - [ - 'action'=>'eccount_will_expire', + 'eccount_will_expire'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], // 9. payments : email : account_expired : desactiver carte expiree - [ - 'action'=>'account_expired', + 'account_expired'=>[ 'notification_send'=>true, 'notification_to'=>'$$__admin_email__$$', 'notification_subject'=>"", 'notification_message'=>"", 'confirmation_send'=>true, 'confirmation_subject'=>"", - 'confirmation_mesage'=>"", + 'confirmation_message'=>"", ], ], ]; diff --git a/plugins/cipf_plugin/html/menu/cipf_menu.html b/plugins/cipf_plugin/html/menu/cipf_menu.html index 6ee99bf..69b3764 100644 --- a/plugins/cipf_plugin/html/menu/cipf_menu.html +++ b/plugins/cipf_plugin/html/menu/cipf_menu.html @@ -59,12 +59,17 @@
+ + diff --git a/plugins/cipf_plugin/js/paypal/on_approve.js b/plugins/cipf_plugin/js/paypal/on_approve.js index 8361776..ff73786 100644 --- a/plugins/cipf_plugin/js/paypal/on_approve.js +++ b/plugins/cipf_plugin/js/paypal/on_approve.js @@ -20,6 +20,7 @@ export async function onApprove(data, actions) { // (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() // (2) Other non-recoverable errors -> Show a failure message // (3) Successful transaction -> Show confirmation or thank you message + // + hugogogo treatment error but success order const errorDetail = orderData?.details?.[0]; @@ -33,7 +34,13 @@ export async function onApprove(data, actions) { throw new Error(`${errorDetail.description} (${orderData.debug_id})`); } else if (!orderData.purchase_units) { - throw new Error(JSON.stringify(orderData)); + if (orderData.ERROR_TREATMENT) { + console.log("Capture result",orderData); + resultMessage(PLGNTLS_data.paypal_message_problem); + } + else { + throw new Error(JSON.stringify(orderData)); + } } else { // (3) Successful transaction -> Show confirmation or thank you message @@ -45,7 +52,8 @@ export async function onApprove(data, actions) { resultMessage(PLGNTLS_data.paypal_message_success); actions.redirect(PLGNTLS_data.paypal_redirection_success); } - } catch (error) { + } + catch (error) { console.error(error); resultMessage(PLGNTLS_data.paypal_message_failure); actions.redirect(PLGNTLS_data.paypal_redirection_failure); diff --git a/plugins/cipf_plugin/php/admin_menu.php b/plugins/cipf_plugin/php/admin_menu.php index 8f53350..b97fdb9 100644 --- a/plugins/cipf_plugin/php/admin_menu.php +++ b/plugins/cipf_plugin/php/admin_menu.php @@ -59,14 +59,19 @@ function update_payment_messages_option_CIPF($request, $option_name, $option_dat */ $success = ''; $failure = ''; + $problem = ''; if (!isset($request['message_success'])) { return; } if (!isset($request['message_failure'])) { return; } + if (!isset($request['message_problem'])) { + return; + } $success = $request['message_success']; $failure = $request['message_failure']; + $problem = $request['message_problem']; /* @@ -77,6 +82,7 @@ function update_payment_messages_option_CIPF($request, $option_name, $option_dat $data = array( 'success' => $success, 'failure' => $failure, + 'problem' => $problem, ); Plgntls::update_option_safe($option_name, $data); } diff --git a/plugins/cipf_plugin/php/emails.php b/plugins/cipf_plugin/php/emails.php index 910e152..6e666c2 100644 --- a/plugins/cipf_plugin/php/emails.php +++ b/plugins/cipf_plugin/php/emails.php @@ -23,50 +23,179 @@ if (!defined('ABSPATH')) { * 8. payments : email : account_will_expire : faire rappels emails avant expiration * 9. payments : email : account_expired : desactiver carte expiree * -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* -* */ - -function email_success_payment_CIPF($user_id) { +function prepare_emails_CIPF($email_name, $user_id) { Plgntls::debug_infos(); -error_log("in email success"); + $emails_option_object = Cipf::OPTION_EMAILS; + + if (!isset($emails_option_object['_default'][$email_name])) { + return false; + } - $user = get_user_by('id', $user_id); -// $to = $user->user_email; - $to = '$$__admin_email__$$'; - $subject = 'My Custom Email Subject'; - $message = 'Hello, This is a test email sent from my WordPress plugin!'; - $headers = array('Content-Type: text/html; charset=UTF-8'); + if (is_null($user_id)) { + $user_id = get_current_user_id(); + } - $sent = wp_mail($to, $subject, $message, $headers); + + $emails_option = Plgntls::get_option_safe($emails_option_object); + if (!$emails_option) { + return false; + } + if (!isset($emails_option[$email_name])) { + return false; + } + $email = $emails_option[$email_name]; - // Check if the email was sent successfully + + $emails = array(); + if ($email['notification_send']) { + $tmp_email = array(); + $tmp_email['to'] = $email['notification_to']; + $tmp_email['subject'] = $email['notification_subject']; + $tmp_email['message'] = $email['notification_message']; + $tmp_email['headers'] = array('Content-Type: text/html; charset=UTF-8'); + $emails[] = $tmp_email; + } + if ($email['confirmation_send']) { + $tmp_email = array(); + $tmp_email['to'] = $user_id; + $tmp_email['subject'] = $email['confirmation_subject']; + $tmp_email['message'] = $email['confirmation_message']; + $tmp_email['headers'] = array('Content-Type: text/html; charset=UTF-8'); + $emails[] = $tmp_email; + } + return $emails; +} + + + +function send_emails_CIPF($email_name, $user_id = null) { + Plgntls::debug_infos(); + $emails = prepare_emails_CIPF($email_name, $user_id); + if (false === $emails) { + error_log('Email preparing failed!: ' . json_encode($emails)); + return; + } + + foreach ($emails as $email) { + $sent = wp_mail($email['to'], $email['subject'], $email['message'], $email['headers']); + } if (!$sent) { - error_log('Email sending failed!'); + error_log('Email sending failed!: ' . json_encode($email)); } } +//function email_payment_success_CIPF($user_id = null) { +// Plgntls::debug_infos(); +//error_log("email payment success: " . json_encode($email_payment_success)); +// +// send_email_CIPF($email_payment_success, $user_id); +// $user = get_user_by('id', $user_id); +//// $to = $user->user_email; +// $to = '$$__admin_email__$$'; +// $subject = 'My Custom Email Subject'; +// $message = 'Hello, This is a test email sent from my WordPress plugin!'; +// $headers = array('Content-Type: text/html; charset=UTF-8'); +// +// +// // Check if the email was sent successfully +//} + +/* + 'payment_success'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"paiement réussi", + 'notification_message'=>"par ici la monnaie", + 'confirmation_send'=>true, + 'confirmation_subject'=>"paiement réussi", + 'confirmation_mesage'=>"donne l'argent", + ], +// 2. profs : email : payment_failure : validation payment prof echec, send email + 'payment_echec'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 3. profs : email : transfert_success : validation transfert prof reussi, send email + 'transfert_success'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 4. profs : email : transfert_failures : validation transfert prof echec, send email + 'transfert_echec'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 5. partners : email : offer_expired : offres temporaires -> gerer qu'elles disparaissent apres la date de validite -> la passer en masquer + 'offer_expired'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 6. partners : email : offer_will_expire : la gestion des offres à échéance + 'offer_will_expire'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 7. payments : email : account_deleted : schedule event pour supprimer le compte xx temps (6 mois ?) apres fin de validite de la carte + 'account_deleted'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 8. payments : email : account_will_expire : faire rappels emails avant expiration + 'eccount_will_expire'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +// 9. payments : email : account_expired : desactiver carte expiree + 'account_expired'=>[ + 'notification_send'=>true, + 'notification_to'=>'$$__admin_email__$$', + 'notification_subject'=>"", + 'notification_message'=>"", + 'confirmation_send'=>true, + 'confirmation_subject'=>"", + 'confirmation_mesage'=>"", + ], +*/ + diff --git a/plugins/cipf_plugin/php/payments.php b/plugins/cipf_plugin/php/payments.php index 6892abe..d3327ef 100644 --- a/plugins/cipf_plugin/php/payments.php +++ b/plugins/cipf_plugin/php/payments.php @@ -113,18 +113,21 @@ function get_payment_messages_option_CIPF() { return Plgntls::get_option_safe($option_payment); } - function get_payment_message_success_CIPF() { Plgntls::debug_infos(); $payment_option = get_payment_messages_option_CIPF(); return $payment_option['success']; } - function get_payment_message_failure_CIPF() { Plgntls::debug_infos(); $payment_option = get_payment_messages_option_CIPF(); return $payment_option['failure']; } +function get_payment_message_problem_CIPF() { + Plgntls::debug_infos(); + $payment_option = get_payment_messages_option_CIPF(); + return $payment_option['problem']; +} ?> 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 430df06..4619122 100644 --- a/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php +++ b/plugins/cipf_plugin/php/paypal/route_api_orders_capture.php @@ -55,15 +55,16 @@ function handle_orders_capture_request_CIPF($request) { try { update_user_post_capture_CIPF($json_response, 'end'); +throw new HttpException('cannot find user with this order_id', 502); return new WP_REST_Response($json_response, $http_status_code); } catch (HttpException $error) { $status_code = $error->getStatusCode(); - $message = 'Failed to handle order after capture in server :' . $error->getMessage(); + $message = array('ERROR_TREATMENT'=>true, 'message'=>'Failed to handle order after capture in server: ' . $error->getMessage()); return new WP_REST_Response($message, $status_code); } catch (Exception $error) { - $message = 'Failed to handle order after capture in server :' . $error->getMessage(); + $message = array('ERROR_TREATMENT'=>true, 'message'=>'Failed to handle order after capture in server: ' . $error->getMessage()); return new WP_REST_Response($message, 500); } diff --git a/plugins/cipf_plugin/php/paypal/shortcode.php b/plugins/cipf_plugin/php/paypal/shortcode.php index bdf403b..48ee262 100644 --- a/plugins/cipf_plugin/php/paypal/shortcode.php +++ b/plugins/cipf_plugin/php/paypal/shortcode.php @@ -22,6 +22,7 @@ function paypal_shortcode_content_CIPF() { $paypal_client_id = get_paypal_client_id_CIPF(); $paypal_message_success = get_payment_message_success_CIPF(); $paypal_message_failure = get_payment_message_failure_CIPF(); + $paypal_message_problem = get_payment_message_problem_CIPF(); // if (!can_pay_now_CIPF()) // return no_payment_CIPF(); @@ -45,6 +46,7 @@ function paypal_shortcode_content_CIPF() { 'paypal_redirection_failure', 'paypal_message_success', 'paypal_message_failure', + 'paypal_message_problem', ), ); diff --git a/plugins/cipf_plugin/php/paypal/update_user_payment.php b/plugins/cipf_plugin/php/paypal/update_user_payment.php index 85545e6..46876b0 100644 --- a/plugins/cipf_plugin/php/paypal/update_user_payment.php +++ b/plugins/cipf_plugin/php/paypal/update_user_payment.php @@ -171,7 +171,7 @@ function success_payment_for_user_CIPF($user_id, $order_id) { set_payment_success_CIPF($user_id); set_account_valid_CIPF($user_id); - email_success_payment_CIPF($user_id); + send_emails_CIPF('payment_success', $user_id); } diff --git a/plugins/cipf_plugin/plgntls_class.php b/plugins/cipf_plugin/plgntls_class.php index 94eef7f..d79121e 100644 --- a/plugins/cipf_plugin/plgntls_class.php +++ b/plugins/cipf_plugin/plgntls_class.php @@ -1223,8 +1223,9 @@ class Plgntls { /* * override saved option with new one - * or we could compare each fields and update them if needed + * or we could try to compare each fields and update them if needed * - but is it not more heavy in computing actions ? + * - and what if the field is multi-nested array for example ? * also add the field '_db' equal to anything (null by default) * to mark it as coming from this database * and differentiate it from new options