<?php
namespace Srmklive\PayPal\Traits;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException as HttpClientException;
use GuzzleHttp\Utils;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Srmklive\PayPal\Services\Str;
trait PayPalHttpClient
{
/**
* Http Client class object.
*
* @var HttpClient
*/
private $client;
/**
* Http Client configuration.
*
* @var array
*/
private $httpClientConfig;
/**
* PayPal API Endpoint.
*
* @var string
*/
private $apiUrl;
/**
* PayPal API Endpoint.
*
* @var string
*/
private $apiEndPoint;
/**
* IPN notification url for PayPal.
*
* @var string
*/
private $notifyUrl;
/**
* Http Client request body parameter name.
*
* @var string
*/
private $httpBodyParam;
/**
* Default payment action for PayPal.
*
* @var string
*/
private $paymentAction;
/**
* Default locale for PayPal.
*
* @var string
*/
private $locale;
/**
* Validate SSL details when creating HTTP client.
*
* @var bool
*/
private $validateSSL;
/**
* Request type.
*
* @var string
*/
protected $verb = 'post';
/**
* Set curl constants if not defined.
*
* @return void
*/
protected function setCurlConstants()
{
$constants = [
'CURLOPT_SSLVERSION' => 32,
'CURL_SSLVERSION_TLSv1_2' => 6,
'CURLOPT_SSL_VERIFYPEER' => 64,
'CURLOPT_SSLCERT' => 10025,
];
foreach ($constants as $key => $item) {
$this->defineCurlConstant($key, $item);
}
}
/**
* Declare a curl constant.
*
* @param string $key
* @param string $value
*
* @return bool
*/
protected function defineCurlConstant(string $key, string $value)
{
return defined($key) ? true : define($key, $value);
}
/**
* Function to initialize/override Http Client.
*
* @param \GuzzleHttp\Client|null $client
*
* @return void
*/
public function setClient(HttpClient $client = null)
{
if ($client instanceof HttpClient) {
$this->client = $client;
return;
}
$this->client = new HttpClient([
'curl' => $this->httpClientConfig,
]);
}
/**
* Function to set Http Client configuration.
*
* @return void
*/
protected function setHttpClientConfiguration()
{
$this->setCurlConstants();
$this->httpClientConfig = [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_SSL_VERIFYPEER => $this->validateSSL,
];
// Initialize Http Client
$this->setClient();
// Set default values.
$this->setDefaultValues();
// Set PayPal IPN Notification URL
$this->notifyUrl = $this->config['notify_url'];
}
/**
* Set default values for configuration.
*
* @return void
*/
private function setDefaultValues()
{
$paymentAction = empty($this->paymentAction) ? 'Sale' : $this->paymentAction;
$this->paymentAction = $paymentAction;
$locale = empty($this->locale) ? 'en_US' : $this->locale;
$this->locale = $locale;
$validateSSL = empty($this->validateSSL) ? true : $this->validateSSL;
$this->validateSSL = $validateSSL;
$this->show_totals = var_export($this->show_totals, true);
}
/**
* Perform PayPal API request & return response.
*
* @throws \Throwable
*
* @return StreamInterface
*/
private function makeHttpRequest(): StreamInterface
{
try {
return $this->client->{$this->verb}(
$this->apiUrl,
$this->options
)->getBody();
} catch (HttpClientException $e) {
throw new RuntimeException($e->getResponse()->getBody());
}
}
/**
* Function To Perform PayPal API Request.
*
* @param bool $decode
*
* @throws \Throwable
*
* @return array|StreamInterface|string
*/
private function doPayPalRequest(bool $decode = true)
{
try {
$this->apiUrl = collect([$this->config['api_url'], $this->apiEndPoint])->implode('/');
// Perform PayPal HTTP API request.
$response = $this->makeHttpRequest();
return ($decode === false) ? $response->getContents() : Utils::jsonDecode($response, true);
} catch (RuntimeException $t) {
$error = ($decode === false) || (Str::isJson($t->getMessage()) === false) ? $t->getMessage() : Utils::jsonDecode($t->getMessage(), true);
return ['error' => $error];
}
}
}
Get access token to initiates payment transaction.
generate-token
| Parameter | Type | Comments |
|---|---|---|
| client_id | string | Enter merchant API client/primary key |
| secret_id | string | Enter merchant API secret key |
| env | string | Enter merchant API environment |
| merchant_id | string | Enter merchant API merchant id |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'client_id' => '$client_id',
'secret_id' => 'secret_id',
'env' => 'env',
'merchant_id' => 'merchant_id',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Successfully token is generated"
]
},
"data": {
"token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9PSIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtnVkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2xoU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtcG9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRiQ0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3JzbnIyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYzkzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid credentials."
]
},
"data": null,
"type": "error"
}