src/Service/PaypalService.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * Unauthorized copying of this file, via any medium is strictly prohibited
  4.  * Proprietary and confidential.
  5.  *
  6.  * @author Bilel AZRI          <azri.bilel@gmail.com>
  7.  * @author Assma BEN SASSI     <bensassiasma.bws@gmail.com>
  8.  *
  9.  * Bicking man (c) 2019-present.
  10.  */
  11. declare(strict_types=1);
  12. namespace App\Service;
  13. use PayPal\Api\Amount;
  14. use PayPal\Api\Payer;
  15. use PayPal\Api\Payment;
  16. use PayPal\Api\RedirectUrls;
  17. use PayPal\Api\Transaction;
  18. use PayPal\Auth\OAuthTokenCredential;
  19. use PayPal\Rest\ApiContext;
  20. use PayPal\Exception\PayPalConnectionException;
  21. use PayPalCheckoutSdk\Core\PayPalHttpClient;
  22. use PayPalCheckoutSdk\Core\ProductionEnvironment;
  23. use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
  24. use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  25. use PayPalHttp\HttpRequest;
  26. use PayPalHttp\HttpResponse;
  27. class PaypalService
  28. {
  29.     private ApiContext $apiContext;
  30.     public function __construct(string $clientIDstring $clientSecret)
  31.     {
  32.         /*$this->apiContext = new ApiContext(
  33.           new OAuthTokenCredential($clientID, $clientSecret)
  34.         );
  35.         $this->apiContext->setConfig(
  36.             array('mode' => 'live')
  37.         );*/
  38.         $this->environment = new ProductionEnvironment($clientID$clientSecret); // Use `ProductionEnvironment` for production
  39.         $this->client = new PayPalHttpClient($this->environment);
  40.     }
  41.     public function getPayment(string $idPaymentstring $totalAmountstring $currencystring $returnUrl$cancelUrl):HttpResponse
  42.     {
  43.         
  44.         $clientID 'Abe0ZVGKbSU6RPW6PtZ2M0lkQjVhig7ncpa57yvjPnhVJBvztXPrXa6EWak8ndaK53AvjjUBKakmk_gE';
  45.         $clientSecret 'EBOz0K16ZMkxvxDrPnER_y3mEzEqcu3asM7yp_gZ92hdXs6r-pE9_3k2qEItUrseGwsVv5IroLnKvHm1';
  46.        
  47.         $environment = new ProductionEnvironment($clientID$clientSecret); // Use `ProductionEnvironment` for production
  48.         $client = new PayPalHttpClient($environment);
  49.         $request = new OrdersCreateRequest();
  50.         $request->prefer('return=representation');
  51.         $request->body = [
  52.                             "intent" => "CAPTURE",
  53.                             "purchase_units" => [[
  54.                                 "reference_id" => $idPayment,
  55.                                 "amount" => [
  56.                                     "value" => $totalAmount,
  57.                                     "currency_code" => "EUR"
  58.                                 ]
  59.                             ]],
  60.                             "application_context" => [
  61.                                 "user_action" => "PAY_NOW",
  62.                                 "cancel_url" => $cancelUrl,
  63.                                 "return_url" => $returnUrl
  64.                             
  65.                         ];
  66.                         try {
  67.                             $response $client->execute($request);
  68.                             // Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
  69.                             // $response->result->id gives the orderId of the order created above
  70.                             return ($response); 
  71.                             
  72.                         } catch (HttpException $ex) {
  73.                             echo $ex->statusCode;
  74.                             print_r($ex->getMessage());
  75.                             return ($ex);
  76.                         } 
  77.     }
  78. }