diff --git a/admin/widget_edit.php b/admin/widget_edit.php index a66b5fb..b8d54d4 100644 --- a/admin/widget_edit.php +++ b/admin/widget_edit.php @@ -65,6 +65,12 @@ + @@ -117,14 +123,14 @@
-

+

-

+

-

+

-

+


@@ -168,6 +174,9 @@
  • {amount} le montant du don
  • {message} si le donateur a laissé un message
  • + Vous pouvez ensuite formater le texte avec du html. +
    +
    diff --git a/app/ApiWrapper.php b/app/ApiWrapper.php index d46fc66..bdd0180 100644 --- a/app/ApiWrapper.php +++ b/app/ApiWrapper.php @@ -24,29 +24,69 @@ public function __construct($repository, $haAuthUrl, $apiUrl, $apiAuthUrl, $clie // Authentification - function getAccessTokensAndRefreshIfNecessary($organization_slug) + function getGlobalTokensAndRefreshIfNecessary() { - $tokenData = $this->repository->getAccessTokensDB($organization_slug); - - if ($tokenData == null) { - if ($organization_slug == null) { - $tokenData = $this->generateGlobalAccessToken(); - return $tokenData; - } else { - return null; - } - } else { + $tokenData = $this->repository->getAccessTokensDB(null); + + if ($tokenData == null) + { + $tokenData = $this->generateGlobalAccessToken(); + return $tokenData; + } + else + { $tokenData['access_token'] = Helpers::decryptToken($tokenData['access_token']); $tokenData['refresh_token'] = Helpers::decryptToken($tokenData['refresh_token']); - if ($tokenData['access_token_expires_at'] < date('Y-m-d H:i:s')) { - $tokenData = $this->refreshToken($tokenData['refresh_token'], null); - return $tokenData; + if ($tokenData['access_token_expires_at'] < date('Y-m-d H:i:s')) + { + try + { + $tokenData = $this->refreshToken($tokenData['refresh_token'], null); + return $tokenData; + } + catch (Exception $e) + { + // Impossible de refresh le token global, il faut en générer un nouveau + $newToken = $this->generateGlobalAccessToken(); + + $accessTokenExpiresAt = (new DateTime())->add(new DateInterval('PT28M')); + $refreshTokenExpiresAt = (new DateTime())->add(new DateInterval('P28D')); + + $this->repository->updateAccessTokenDB( + Helpers::encryptToken($newToken['access_token']), + Helpers::encryptToken($newToken['refresh_token']), + null, + $accessTokenExpiresAt, + $refreshTokenExpiresAt + ); + } + } return $tokenData; } } + function getOrganizationTokensAndRefreshIfNecessary($organization_slug) + { + $tokenData = $this->repository->getAccessTokensDB($organization_slug); + + if ($tokenData == null) + { + throw new Exception("Aucun token trouvé pour cette association"); + } + + $tokenData['access_token'] = Helpers::decryptToken($tokenData['access_token']); + $tokenData['refresh_token'] = Helpers::decryptToken($tokenData['refresh_token']); + + if ($tokenData['access_token_expires_at'] < date('Y-m-d H:i:s')) + { + $tokenData = $this->refreshToken($tokenData['refresh_token'], null); + return $tokenData; + } + return $tokenData; + } + function generateGlobalAccessToken() { $curl = curl_init(); @@ -99,14 +139,28 @@ function generateGlobalAccessToken() $accessTokenExpiresAt = (new DateTime())->add(new DateInterval('PT1700S')); $refreshTokenExpiresAt = (new DateTime())->add(new DateInterval('P29D')); - // Insérer les tokens en base de données - $this->repository->insertAccessTokenDB( - Helpers::encryptToken($responseData['access_token']), - Helpers::encryptToken($responseData['refresh_token']), - null, - $accessTokenExpiresAt, - $refreshTokenExpiresAt - ); + //Nous devons définir si nous devons insérer le token global ou le mettre à jour en l'écrasant (possible si le refresh est expiré) + $existingGlobalToken = $this->repository->getAccessTokensDB(null); + if($existingGlobalToken == null) + { + $this->repository->insertAccessTokenDB( + Helpers::encryptToken($responseData['access_token']), + Helpers::encryptToken($responseData['refresh_token']), + null, + $accessTokenExpiresAt, + $refreshTokenExpiresAt + ); + } + else + { + $this->repository->updateAccessTokenDB( + Helpers::encryptToken($responseData['access_token']), + Helpers::encryptToken($responseData['refresh_token']), + null, + $accessTokenExpiresAt, + $refreshTokenExpiresAt + ); + } return $responseData; } @@ -134,7 +188,8 @@ function refreshToken($refreshToken, $organization_slug) $response = curl_exec($curl); // Gérer les erreurs cURL - if (curl_errno($curl)) { + if (curl_errno($curl)) + { $error_msg = curl_error($curl); curl_close($curl); throw new Exception("Erreur cURL : $error_msg"); @@ -285,7 +340,7 @@ function exchangeAuthorizationCode($code, $redirect_uri, $codeVerifier) function GetDonationForm($organizationSlug, $donationSlug) { - $accessToken = $this->getAccessTokensAndRefreshIfNecessary(null); + $accessToken = $this->getGlobalTokensAndRefreshIfNecessary(); if (!$accessToken || !isset($accessToken['access_token'])) { http_response_code(401); echo json_encode(['error' => 'Jeton d\'accès API non trouvé ou expiré.']); @@ -356,7 +411,7 @@ function GetAllOrders($organizationSlug, $formSlug, $currentAmount = 0, $continu $previousToken = ''; $donations = []; - $organizationAccessToken = $this->getAccessTokensAndRefreshIfNecessary($organizationSlug); + $organizationAccessToken = $this->getOrganizationTokensAndRefreshIfNecessary($organizationSlug); if (!$organizationAccessToken || !isset($organizationAccessToken['access_token'])) { http_response_code(401); @@ -384,16 +439,23 @@ function GetAllOrders($organizationSlug, $formSlug, $currentAmount = 0, $continu $pseudo = "anonyme"; $message = ""; - foreach ($order['items'] as $item) { - foreach ($item['customFields'] as $field) { - if ($field['name'] == 'pseudo') { - $pseudo = $field['answer']; - } - if ($field['name'] == 'message') { - $message = $field['answer']; + foreach ($order['items'] as $item) + { + if (array_key_exists('customFields', $item)) + { + foreach ($item['customFields'] as $field) + { + if (strcasecmp($field['name'], 'pseudo') == 0) + { + $pseudo = $field['answer']; + } + if (strcasecmp($field['name'], 'message') == 0) + { + $message = $field['answer']; + } } } - } + } $amount = isset($order['amount']['total']) && is_numeric($order['amount']['total']) ? $order['amount']['total'] : 0; $currentAmount += $amount; diff --git a/app/Config.php b/app/Config.php index f495954..e4de633 100644 --- a/app/Config.php +++ b/app/Config.php @@ -1,7 +1,6 @@ repo; $apiWrapper = Config::getInstance()->apiWrapper; $organizationSlug = $_GET['organizationSlug']; +if($organizationSlug == null) +{ + throw new Exception("Erreur : OrganizationSlug introuvable"); +} -$partnerTokenData = $apiWrapper->getAccessTokensAndRefreshIfNecessary(null); -$accessToken = $partnerTokenData['access_token']; +//Vérification si l'association à déjà lié son compte +//Récupération du refresh_token de l'association en BDD pour voir si c'est nécessaire de générer une URL de mire +$organizationToken = $repository->getAccessTokensDB($organizationSlug); -$apiWrapper->setClientDomain(Config::getInstance()->webSiteDomain, $accessToken); +if ($organizationToken != null) +{ + //Nous avons réussi à récupérer un token de l'association + //Si on peut rafraichir ce token c'est qu'il est encore valide + try + { + $decryptedOrganizationRefreshToken = Helpers::decryptToken($organizationToken['refresh_token']); + $refreshToken = $apiWrapper->refreshToken( $decryptedOrganizationRefreshToken, $organizationSlug); + echo 'Nous possédons déjà un token pour le compte ' . $organizationSlug . ' et nous l\'avons rafraichi, vous pouvez fermer cette page.'; + } + catch (Exception $e) + { + redirectionToAuthorizationUrl(); + } +} +else +{ + redirectionToAuthorizationUrl(); +} -// Générer l'URL d'autorisation -$authorizationUrl = $apiWrapper->generateAuthorizationUrl($organizationSlug); +function redirectionToAuthorizationUrl() +{ + global $apiWrapper; + global $organizationSlug; -// Rediriger vers l'URL générée -header('Location: ' . $authorizationUrl); -exit; + // Nous ne possédons pas de Refresh valide pour cette association, nous allons donc générer une Url pour la liaison + // Récupération du token global HelloassoCharityStream pour set le domain (important pour la mire) + $globalTokens = $apiWrapper->getGlobalTokensAndRefreshIfNecessary(); + $globalAccessToken = $globalTokens['access_token']; + $apiWrapper->setClientDomain(Config::getInstance()->webSiteDomain, $globalAccessToken); + + // Générer l'URL d'autorisation + $authorizationUrl = $apiWrapper->generateAuthorizationUrl($organizationSlug); + + // Rediriger vers l'URL générée + header('Location: ' . $authorizationUrl); +} diff --git a/validate_grant_authorization.php b/validate_grant_authorization.php index 28f8cf9..fe3518a 100644 --- a/validate_grant_authorization.php +++ b/validate_grant_authorization.php @@ -5,26 +5,34 @@ $apiWrapper = Config::getInstance()->apiWrapper; // Récupérer le paramètre 'state' depuis l'URL +// Nous y avons mit l'ID de l'authorizationCode en BDD +// Cela nous permet de récupérer les informartions necessaire pour la mire $state = $_GET['state']; - -// Récupérer la randomString depuis le cache avec l'id 'state' $authorizationCodeData = $repository->getAuthorizationCodeByIdDB($state); +if($authorizationCodeData == null) +{ + throw new Exception("Erreur : Nous n'avons pas trouvé d'authorizationCode correspondant."); +} + $redirect_uri = $authorizationCodeData['redirect_uri']; $codeVerifier = $authorizationCodeData['code_verifier']; -// Le code est normalement renvoyé par la mire d'authentication +// Le code est renvoyé par la mire d'autorisation $code = $_GET['code']; +// Une fois que nous avons toutes les informations necessaire nous pouvons procéder à l'échange $tokenDataGrantAuthorization = $apiWrapper->exchangeAuthorizationCode($code, $redirect_uri, $codeVerifier); // Calculer les dates d'expiration des tokens $accessTokenExpiresAt = (new DateTime())->add(new DateInterval('PT28M')); $refreshTokenExpiresAt = (new DateTime())->add(new DateInterval('P28D')); -$existingOrganizationToken = $apiWrapper->getAccessTokensAndRefreshIfNecessary($tokenDataGrantAuthorization['organization_slug']); +$existingOrganizationToken = $repository->getAccessTokensDB($tokenDataGrantAuthorization['organization_slug']); -if ($existingOrganizationToken != null) { - try { +if ($existingOrganizationToken != null) +{ + try + { $repository->updateAccessTokenDB( Helpers::encryptToken($tokenDataGrantAuthorization['access_token']), Helpers::encryptToken($tokenDataGrantAuthorization['refresh_token']), @@ -34,11 +42,16 @@ ); echo 'Votre compte ' . $tokenDataGrantAuthorization['organization_slug'] . ' été déjà lié à HelloAssoCharityStream, vous pouvez fermer cette page.'; - } catch (Exception $e) { + } + catch (Exception $e) + { throw new Exception("Erreur de MAJ en base de données : $e->getMessage()"); } -} else { - try { +} +else +{ + try + { $repository->insertAccessTokenDB( Helpers::encryptToken($tokenDataGrantAuthorization['access_token']), Helpers::encryptToken($tokenDataGrantAuthorization['refresh_token']), @@ -48,7 +61,9 @@ ); echo 'Votre compte ' . $tokenDataGrantAuthorization['organization_slug'] . ' à bien été lié à HelloAssoCharityStream, vous pouvez fermer cette page.'; - } catch (Exception $e) { + } + catch (Exception $e) + { throw new Exception("Erreur lors de l'insertion en base de données : $e->getMessage()"); } } \ No newline at end of file