Skip to content

Commit f909717

Browse files
committed
add log and change version
1 parent a0be0bb commit f909717

6 files changed

Lines changed: 382 additions & 12 deletions

helloasso-api/helloasso-woocommerce-api.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@
55

66
function helloasso_get_oauth_token($client_id, $client_secret, $api_url)
77
{
8+
helloasso_log_debug('Vérification du token OAuth2', array(
9+
'api_url' => $api_url,
10+
'client_id' => substr($client_id, 0, 10) . '...'
11+
));
12+
813
$access_token = get_option('helloasso_access_token');
914
$refresh_token = get_option('helloasso_refresh_token');
1015
$token_expires_in = get_option('helloasso_token_expires_in');
1116
$refresh_token_expires_in = get_option('helloasso_refresh_token_expires_in');
1217

1318
if ($access_token && time() < $token_expires_in) {
19+
helloasso_log_debug('Token OAuth2 encore valide', array(
20+
'token_preview' => substr($access_token, 0, 10) . '...',
21+
'expires_in' => $token_expires_in - time()
22+
));
1423
return $access_token;
1524
}
1625

1726
if ($refresh_token && time() < $refresh_token_expires_in) {
27+
helloasso_log_info('Rafraîchissement du token OAuth2', array(
28+
'refresh_token_preview' => substr($refresh_token, 0, 10) . '...'
29+
));
30+
1831
$url = $api_url . 'oauth2/token';
1932

2033
$data = array(
@@ -27,24 +40,48 @@ function helloasso_get_oauth_token($client_id, $client_secret, $api_url)
2740
$response = wp_remote_post($url, helloasso_get_args_post_urlencode($data));
2841

2942
if (is_wp_error($response)) {
43+
helloasso_log_error('Erreur lors du rafraîchissement du token OAuth2', array(
44+
'error' => $response->get_error_message(),
45+
'error_code' => $response->get_error_code()
46+
));
3047
return null;
3148
}
3249

3350
$response_body = wp_remote_retrieve_body($response);
51+
$response_code = wp_remote_retrieve_response_code($response);
3452
$data = json_decode($response_body);
3553

54+
helloasso_log_info('Réponse rafraîchissement token OAuth2', array(
55+
'response_code' => $response_code,
56+
'has_access_token' => isset($data->access_token)
57+
));
58+
3659
if (isset($data->access_token)) {
3760
update_option('helloasso_access_token', $data->access_token);
3861
update_option('helloasso_refresh_token', $data->refresh_token);
3962
update_option('helloasso_token_expires_in', $data->expires_in);
4063
update_option('helloasso_refresh_token_expires_in', time() + 2629800);
64+
65+
helloasso_log_info('Token OAuth2 rafraîchi avec succès', array(
66+
'new_token_preview' => substr($data->access_token, 0, 10) . '...',
67+
'expires_in' => $data->expires_in
68+
));
69+
4170
return $data->access_token;
4271
} else {
72+
helloasso_log_error('Réponse invalide lors du rafraîchissement du token', array(
73+
'response_code' => $response_code,
74+
'response_body' => $response_body
75+
));
4376
return null;
4477
}
4578
}
4679

4780
if (!$refresh_token) {
81+
helloasso_log_info('Demande de nouveau token OAuth2 avec client_credentials', array(
82+
'api_url' => $api_url
83+
));
84+
4885
$url = $api_url . 'oauth2/token';
4986

5087
$data = array(
@@ -56,20 +93,43 @@ function helloasso_get_oauth_token($client_id, $client_secret, $api_url)
5693
$response = wp_remote_post($url, helloasso_get_args_post_urlencode($data));
5794

5895
if (is_wp_error($response)) {
96+
helloasso_log_error('Erreur lors de la demande de token OAuth2', array(
97+
'error' => $response->get_error_message(),
98+
'error_code' => $response->get_error_code()
99+
));
59100
return null;
60101
}
61102

62103
$response_body = wp_remote_retrieve_body($response);
104+
$response_code = wp_remote_retrieve_response_code($response);
63105
$data = json_decode($response_body);
64106

107+
helloasso_log_info('Réponse demande token OAuth2', array(
108+
'response_code' => $response_code,
109+
'has_access_token' => isset($data->access_token)
110+
));
111+
65112
if (isset($data->access_token)) {
66113
add_option('helloasso_access_token', $data->access_token);
67114
add_option('helloasso_refresh_token', $data->refresh_token);
68115
add_option('helloasso_token_expires_in', $data->expires_in);
69116
add_option('helloasso_refresh_token_expires_in', time() + 2629800);
117+
118+
helloasso_log_info('Nouveau token OAuth2 obtenu avec succès', array(
119+
'token_preview' => substr($data->access_token, 0, 10) . '...',
120+
'expires_in' => $data->expires_in
121+
));
122+
70123
return $data->access_token;
71124
} else {
125+
helloasso_log_error('Réponse invalide lors de la demande de token', array(
126+
'response_code' => $response_code,
127+
'response_body' => $response_body
128+
));
72129
return null;
73130
}
74131
}
132+
133+
helloasso_log_warning('Aucun token OAuth2 disponible et aucun refresh token valide');
134+
return null;
75135
}

helloasso-woocommerce-gateway.php

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Plugin Name: HelloAsso Payments for WooCommerce
55
* Description: Recevez 100% de vos paiements gratuitement. HelloAsso est la seule solution de paiement gratuite du secteur associatif. Nous sommes financés librement par la solidarité de celles et ceux qui choisissent de laisser une contribution volontaire au moment du paiement à une association.
6-
* Version: 1.0.10
6+
* Version: 1.0.11
77
* Requires at least: 5.0
88
* WC requires at least: 7.7
99
* Requires PHP: 7.2.34
@@ -123,6 +123,11 @@ class WC_HelloAsso_Gateway extends WC_Payment_Gateway
123123
{
124124
public function __construct()
125125
{
126+
helloasso_log_info('Initialisation du gateway HelloAsso', array(
127+
'plugin_version' => '1.0.11',
128+
'wc_version' => defined('WC_VERSION') ? WC_VERSION : 'unknown'
129+
));
130+
126131
$this->id = 'helloasso';
127132
$this->icon = null;
128133
$this->has_fields = true;
@@ -606,8 +611,26 @@ public function validate_fields()
606611

607612
public function process_payment($order_id)
608613
{
614+
helloasso_log_info('Début du traitement de paiement', array(
615+
'order_id' => $order_id,
616+
'user_id' => get_current_user_id(),
617+
'payment_method' => 'helloasso'
618+
));
619+
609620
helloasso_refresh_token_asso();
610621
$order = wc_get_order($order_id);
622+
623+
if (!$order) {
624+
helloasso_log_error('Commande introuvable', array('order_id' => $order_id));
625+
return array('result' => 'failure', 'messages' => 'Commande introuvable');
626+
}
627+
628+
helloasso_log_info('Récupération des données client', array(
629+
'order_id' => $order_id,
630+
'order_status' => $order->get_status(),
631+
'order_total' => $order->get_total()
632+
));
633+
611634
if (isset($_GET['pay_for_order'])) {
612635
$firstName = $order->get_billing_first_name();
613636
$lastName = $order->get_billing_last_name();
@@ -617,9 +640,17 @@ public function process_payment($order_id)
617640
$zipCode = $order->get_billing_postcode();
618641
$countryIso = helloasso_convert_country_code($order->get_billing_country());
619642
$company = $order->get_billing_company();
643+
644+
helloasso_log_debug('Données client récupérées depuis la commande existante', array(
645+
'first_name' => $firstName,
646+
'last_name' => $lastName,
647+
'email' => $email,
648+
'country' => $countryIso
649+
));
620650
} else {
621651
if (isset($_POST['billing_first_name'])) {
622652
if (!isset($_POST['woocommerce-process-checkout-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['woocommerce-process-checkout-nonce'])), 'woocommerce-process_checkout')) {
653+
helloasso_log_error('Nonce invalide lors du checkout', array('order_id' => $order_id));
623654
wc_add_notice('La commande ne peut être finalisé', 'error');
624655
}
625656

@@ -670,6 +701,13 @@ public function process_payment($order_id)
670701
} else {
671702
$company = '';
672703
}
704+
705+
helloasso_log_debug('Données client récupérées depuis POST', array(
706+
'first_name' => $firstName,
707+
'last_name' => $lastName,
708+
'email' => $email,
709+
'country' => $countryIso
710+
));
673711
} else {
674712
$json = file_get_contents('php://input');
675713
$data = json_decode($json, true);
@@ -682,13 +720,26 @@ public function process_payment($order_id)
682720
$zipCode = $data['billing_address']['postcode'];
683721
$countryIso = helloasso_convert_country_code($data['billing_address']['country']);
684722
$company = $data['billing_address']['company'];
723+
724+
helloasso_log_debug('Données client récupérées depuis JSON', array(
725+
'first_name' => $firstName,
726+
'last_name' => $lastName,
727+
'email' => $email,
728+
'country' => $countryIso
729+
));
685730
}
686731
}
687732

688733

689734
$items = $order->get_items();
690735
$total = $order->get_total();
691736

737+
helloasso_log_info('Préparation des données de paiement', array(
738+
'order_id' => $order_id,
739+
'total' => $total,
740+
'items_count' => count($items)
741+
));
742+
692743
$woocommerceOrderId = $order_id;
693744
$userId = $order->get_user_id();
694745
$nonce = wp_create_nonce('helloasso_order');
@@ -763,6 +814,13 @@ public function process_payment($order_id)
763814
}
764815
}
765816

817+
helloasso_log_info('Type de paiement détecté', array(
818+
'order_id' => $order_id,
819+
'payment_type' => $payment_type,
820+
'multi_3_enabled' => $this->get_option('multi_3_enabled'),
821+
'multi_12_enabled' => $this->get_option('multi_12_enabled')
822+
));
823+
766824
$order->update_meta_data('helloasso_payment_type', $payment_type === 'three_times' ? '3 fois (prochaine écheance à suivre sur HelloAsso)' : ($payment_type === 'twelve_times' ? '12 fois (prochaine écheance à suivre sur HelloAsso)' : 'une fois'));
767825
$order->save();
768826

@@ -789,6 +847,13 @@ public function process_payment($order_id)
789847
'amount' => $thirdAmount,
790848
],
791849
];
850+
851+
helloasso_log_info('Paiement en 3 fois configuré', array(
852+
'order_id' => $order_id,
853+
'first_amount' => $firstAmount,
854+
'second_amount' => $secondAmount,
855+
'third_amount' => $thirdAmount
856+
));
792857
} elseif ($payment_type === 'twelve_times') {
793858
$totalCents = round($total * 100);
794859

@@ -807,26 +872,80 @@ public function process_payment($order_id)
807872
'amount' => $monthlyAmount,
808873
];
809874
}
875+
876+
helloasso_log_info('Paiement en 12 fois configuré', array(
877+
'order_id' => $order_id,
878+
'first_amount' => $firstAmount,
879+
'monthly_amount' => $monthlyAmount
880+
));
810881
}
811882
}
812883

813884
$bearerToken = get_option('helloasso_access_token_asso');
814885
$isInTestMode = get_option('helloasso_testmode');
815886

887+
helloasso_log_info('Configuration API HelloAsso', array(
888+
'order_id' => $order_id,
889+
'test_mode' => $isInTestMode,
890+
'has_token' => !empty($bearerToken),
891+
'organization_slug' => get_option('helloasso_organization_slug')
892+
));
893+
816894
if ('yes' === $isInTestMode) {
817895
$api_url = HELLOASSO_WOOCOMMERCE_API_URL_TEST;
818896
} else {
819897
$api_url = HELLOASSO_WOOCOMMERCE_API_URL_PROD;
820898
}
821899

822900
$url = $api_url . 'v5/organizations/' . get_option('helloasso_organization_slug') . '/checkout-intents';
901+
902+
helloasso_log_info('Appel API HelloAsso', array(
903+
'order_id' => $order_id,
904+
'url' => $url,
905+
'api_url' => $api_url
906+
));
907+
823908
$response = wp_remote_post($url, helloasso_get_args_post_token($data, $bearerToken));
824909

825910
if (is_wp_error($response)) {
911+
helloasso_log_error('Erreur lors de l\'appel API HelloAsso', array(
912+
'order_id' => $order_id,
913+
'error' => $response->get_error_message(),
914+
'error_code' => $response->get_error_code()
915+
));
826916
echo 'Erreur : ' . esc_html($response->get_error_message());
827917
}
828918

829919
$response_body = wp_remote_retrieve_body($response);
920+
$response_code = wp_remote_retrieve_response_code($response);
921+
922+
helloasso_log_info('Réponse API HelloAsso reçue', array(
923+
'order_id' => $order_id,
924+
'response_code' => $response_code,
925+
'response_body_length' => strlen($response_body)
926+
));
927+
928+
if ($response_code !== 200) {
929+
helloasso_log_error('Erreur API HelloAsso', array(
930+
'order_id' => $order_id,
931+
'response_code' => $response_code,
932+
'response_body' => $response_body
933+
));
934+
}
935+
936+
$response_data = json_decode($response_body);
937+
938+
if (!$response_data || !isset($response_data->redirectUrl)) {
939+
helloasso_log_error('Réponse API invalide', array(
940+
'order_id' => $order_id,
941+
'response_body' => $response_body
942+
));
943+
}
944+
945+
helloasso_log_info('Paiement traité avec succès', array(
946+
'order_id' => $order_id,
947+
'redirect_url' => $response_data->redirectUrl ?? 'unknown'
948+
));
830949

831950
return array(
832951
'result' => 'success',

0 commit comments

Comments
 (0)