Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
srv
/
data
/
web
/
vhosts
/
716cf56cb2.url-de-test.ws
/
htdocs
/
src
/
Service
/
/srv/data/web/vhosts/716cf56cb2.url-de-test.ws/htdocs/src/Service/PaypalPayment.php
<?php namespace App\Service; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; class PaypalPayment { private $parameterBag; protected $client; protected $clientId; protected $clientSecret; protected $base; public function __construct(ParameterBagInterface $parameterBag, HttpClientInterface $client) { $this->parameterBag = $parameterBag; $this->client = $client; $this->clientId = $this->parameterBag->get('paypal_client_id'); $this->clientSecret = $this->parameterBag->get('paypal_client_secret'); $this->base = $this->parameterBag->get('paypal_endpoint'); } public function generateAccessToken(): string { // Remplacez 'votre_client_id' et 'votre_client_secret' par vos véritables informations $clientId = $this->clientId; $clientSecret = $this->clientSecret; // Effectuez la demande try { $response = $this->client->request('POST', 'https://api-m.sandbox.paypal.com/v1/oauth2/token', [ 'auth_basic' => [$clientId, $clientSecret], 'body' => [ 'grant_type' => 'client_credentials', ], ]); } catch (\Exception $e) { throw new \Exception($e); } // Obtenez le contenu de la réponse sous forme de tableau associatif $content = $response->toArray(); // Vous pouvez traiter les données de la réponse ici, par exemple, renvoyer la réponse en tant que JSON return $content['access_token']; } public function createOrder(string $price): JsonResponse { $accessToken = $this->generateAccessToken(); $url = $this->base . "/v2/checkout/orders"; $payload = [ 'intent' => 'CAPTURE', 'purchase_units' => [ [ 'amount' => [ 'currency_code' => 'EUR', 'value' => $price, ], ], ], ]; try { $response = $this->client->request('POST', $url, [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => "Bearer " . $accessToken, ], 'body' => json_encode($payload), ]); } catch (\Exception $e) { throw new \Exception($e); } return $this->handleResponse($response); } public function captureOrder(string $orderID): JsonResponse { try { $accessToken = $this->generateAccessToken(); $url = $this->base . "/v2/checkout/orders/" . $orderID . "/capture"; $response = $this->client->request('POST', $url, [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$accessToken}", ], ]); return $this->handleResponse($response); } catch (\Exception $e) { $errorMessage = $response->getContent(); throw new \Exception($errorMessage, $response->getStatusCode()); } } private function handleResponse($response): JsonResponse { try { $jsonResponse = $response->toArray(); return new JsonResponse($jsonResponse, $response->getStatusCode()); } catch (\Exception $e) { $errorMessage = $response->getContent(); throw new \Exception($errorMessage, $response->getStatusCode()); } } }