<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\Service;
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use PayPal\Exception\PayPalConnectionException;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use PayPalHttp\HttpRequest;
use PayPalHttp\HttpResponse;
class PaypalService
{
private ApiContext $apiContext;
public function __construct(string $clientID, string $clientSecret)
{
/*$this->apiContext = new ApiContext(
new OAuthTokenCredential($clientID, $clientSecret)
);
$this->apiContext->setConfig(
array('mode' => 'live')
);*/
$this->environment = new ProductionEnvironment($clientID, $clientSecret); // Use `ProductionEnvironment` for production
$this->client = new PayPalHttpClient($this->environment);
}
public function getPayment(string $idPayment, string $totalAmount, string $currency, string $returnUrl, $cancelUrl):HttpResponse
{
$clientID = 'Abe0ZVGKbSU6RPW6PtZ2M0lkQjVhig7ncpa57yvjPnhVJBvztXPrXa6EWak8ndaK53AvjjUBKakmk_gE';
$clientSecret = 'EBOz0K16ZMkxvxDrPnER_y3mEzEqcu3asM7yp_gZ92hdXs6r-pE9_3k2qEItUrseGwsVv5IroLnKvHm1';
$environment = new ProductionEnvironment($clientID, $clientSecret); // Use `ProductionEnvironment` for production
$client = new PayPalHttpClient($environment);
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => $idPayment,
"amount" => [
"value" => $totalAmount,
"currency_code" => "EUR"
]
]],
"application_context" => [
"user_action" => "PAY_NOW",
"cancel_url" => $cancelUrl,
"return_url" => $returnUrl
]
];
try {
$response = $client->execute($request);
// Here, OrdersCaptureRequest() creates a POST request to /v2/checkout/orders
// $response->result->id gives the orderId of the order created above
return ($response);
} catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
return ($ex);
}
}
}