Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/composer/symfony/process-5.4.46
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan202 authored Dec 2, 2024
2 parents 3184d73 + cca834e commit 9ea6a0a
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 813 deletions.
13 changes: 13 additions & 0 deletions classes/API/ExtensionSDK/AccessTokenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class AccessTokenRequest implements HttpRequestInterface, WrapperInterface
*/
protected $paypalCustomerId;

protected $body = null;

public function __construct($paypalCustomerId = null)
{
$this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
Expand Down Expand Up @@ -78,8 +80,19 @@ public function setHeaders($headers)
return $this;
}

public function setBody($body)
{
$this->body = $body;

return $this;
}

public function getBody()
{
if (false === empty($this->body)) {
return $this->body;
}

$body = [
'grant_type' => 'client_credentials',
];
Expand Down
93 changes: 93 additions & 0 deletions classes/API/ExtensionSDK/GetCredentialsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/*
* Since 2007 PayPal
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author Since 2007 PayPal
* @author 202 ecommerce <tech@202-ecommerce.com>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @copyright PayPal
*
*/

namespace PaypalAddons\classes\API\ExtensionSDK;

if (!defined('_PS_VERSION_')) {
exit;
}

use PaypalAddons\classes\API\HttpAdoptedResponse;
use PaypalAddons\classes\API\HttpResponse;
use PaypalAddons\classes\API\Request\HttpRequestInterface;
use PaypalAddons\classes\API\WrapperInterface;

class GetCredentialsRequest implements HttpRequestInterface, WrapperInterface
{
protected $headers = [];
/** @var string */
protected $partnerId;

public function __construct($partnerId)
{
$this->partnerId = (string) $partnerId;
}

public function getPath()
{
return sprintf('/v1/customer/partners/%s/merchant-integrations/credentials', $this->partnerId);
}

/** @return array*/
public function getHeaders()
{
return $this->headers;
}

/**
* @param array $headers
*
* @return self
*/
public function setHeaders($headers)
{
if (is_array($headers)) {
$this->headers = $headers;
}

return $this;
}

public function getMethod()
{
return 'GET';
}

public function wrap($object)
{
if ($object instanceof HttpResponse) {
return new HttpAdoptedResponse($object);
}

return $object;
}

public function getBody()
{
return null;
}
}
56 changes: 27 additions & 29 deletions classes/API/Onboarding/PaypalGetAuthToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
namespace PaypalAddons\classes\API\Onboarding;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use PaypalAddons\classes\AbstractMethodPaypal;
use PaypalAddons\classes\API\ExtensionSDK\AccessTokenRequest;
use PaypalAddons\classes\API\HttpAdoptedResponse;
use PaypalAddons\classes\API\PaypalClient;
use PaypalAddons\classes\API\Response\Error;
use PaypalAddons\classes\API\Response\ResponseGetAuthToken;
use Throwable;
Expand All @@ -40,8 +42,8 @@

class PaypalGetAuthToken
{
/** @var */
protected $httpClient;
/** @var PaypalClient */
protected $client;

/** @var string */
protected $authCode;
Expand All @@ -54,11 +56,9 @@ class PaypalGetAuthToken

public function __construct($authCode, $sharedId, $sellerNonce, $sandbox)
{
// Depending on the guzzle version, Client take 'base_uri' or 'base_url' parameter
$this->httpClient = new Client([
'base_uri' => $sandbox ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com',
'base_url' => $sandbox ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com',
]);
$method = AbstractMethodPaypal::load();
$method->setSandbox($sandbox);
$this->client = PaypalClient::get($method);
$this->authCode = $authCode;
$this->sharedId = $sharedId;
$this->sellerNonce = $sellerNonce;
Expand All @@ -70,38 +70,36 @@ public function __construct($authCode, $sharedId, $sellerNonce, $sandbox)
public function execute()
{
$returnResponse = new ResponseGetAuthToken();
$body = sprintf('grant_type=authorization_code&code=%s&code_verifier=%s', $this->authCode, $this->sellerNonce);
$request = new AccessTokenRequest();
$request->setBody(
sprintf('grant_type=authorization_code&code=%s&code_verifier=%s', $this->authCode, $this->sellerNonce)
);
$request->setHeaders([
'Content-Type' => 'text/plain',
'Authorization' => 'Basic ' . base64_encode($this->sharedId),
]);

try {
$response = $this->httpClient->post(
'/v1/oauth2/token',
[
RequestOptions::BODY => $body,
RequestOptions::HEADERS => [
'Content-Type' => 'text/plain',
'Authorization' => 'Basic ' . base64_encode($this->sharedId),
],
]
);

$responseDecode = json_decode($response->getBody()->getContents());
/** @var HttpAdoptedResponse $response */
$response = $this->client->execute($request);
$responseDecode = $response->getAdoptedResponse();
$returnResponse->setSuccess(true)
->setData($returnResponse)
->setAuthToken($responseDecode->access_token)
->setRefreshToken($responseDecode->refresh_token)
->setTokenType($responseDecode->token_type)
->setNonce($responseDecode->nonce);
->setData($response)
->setAuthToken($responseDecode->result->access_token)
->setRefreshToken($responseDecode->result->refresh_token)
->setTokenType($responseDecode->result->token_type)
->setNonce($responseDecode->result->nonce);
} catch (Throwable $e) {
$error = new Error();
$error
->setMessage($e->getMessage())
->setErrorCode(empty($e->statusCode) ? $e->getCode() : $e->statusCode);
->setErrorCode($e->getCode());
$returnResponse->setError($error)->setSuccess(false);
} catch (Exception $e) {
$error = new Error();
$error
->setMessage($e->getMessage())
->setErrorCode(empty($e->statusCode) ? $e->getCode() : $e->statusCode);
->setErrorCode($e->getCode());
$returnResponse->setError($error)->setSuccess(false);
}

Expand Down
48 changes: 22 additions & 26 deletions classes/API/Onboarding/PaypalGetCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
namespace PaypalAddons\classes\API\Onboarding;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use PaypalAddons\classes\AbstractMethodPaypal;
use PaypalAddons\classes\API\ExtensionSDK\GetCredentialsRequest;
use PaypalAddons\classes\API\HttpAdoptedResponse;
use PaypalAddons\classes\API\PaypalClient;
use PaypalAddons\classes\API\Response\Error;
use PaypalAddons\classes\API\Response\ResponseGetCredentials;
use Throwable;
Expand All @@ -40,8 +42,8 @@

class PaypalGetCredentials
{
/** @var */
protected $httpClient;
/** @var PaypalClient */
protected $client;

/** @var string */
protected $authToken;
Expand All @@ -51,47 +53,41 @@ class PaypalGetCredentials

public function __construct($authToken, $partnerId, $sandbox)
{
// Depending on the guzzle version, Client take 'base_uri' or 'base_url' parameter
$this->httpClient = new Client([
'base_uri' => $sandbox ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com',
'base_url' => $sandbox ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com',
]);
$method = AbstractMethodPaypal::load();
$method->setSandbox($sandbox);
$this->client = PaypalClient::get($method);
$this->authToken = $authToken;
$this->partnerId = $partnerId;
}

public function execute()
{
$returnResponse = new ResponseGetCredentials();
$uri = sprintf('/v1/customer/partners/%s/merchant-integrations/credentials', $this->partnerId);
$request = new GetCredentialsRequest($this->partnerId);
$request->setHeaders([
'Authorization' => 'Bearer ' . $this->authToken,
]);

try {
$response = $this->httpClient->get(
$uri,
[
RequestOptions::HEADERS => [
'Authorization' => 'Bearer ' . $this->authToken,
],
]
);

$responseDecode = json_decode($response->getBody()->getContents());
/** @var HttpAdoptedResponse $response */
$response = $this->client->execute($request);
$responseDecode = $response->getAdoptedResponse();
$returnResponse->setSuccess(true)
->setClientId($responseDecode->client_id)
->setSecret($responseDecode->client_secret)
->setMerchantId($responseDecode->payer_id)
->setData($returnResponse);
->setClientId($responseDecode->result->client_id)
->setSecret($responseDecode->result->client_secret)
->setMerchantId($responseDecode->result->payer_id)
->setData($response);
} catch (Throwable $e) {
$error = new Error();
$error
->setMessage($e->getMessage())
->setErrorCode(empty($e->statusCode) ? $e->getCode() : $e->statusCode);
->setErrorCode($e->getCode());
$returnResponse->setError($error)->setSuccess(false);
} catch (Exception $e) {
$error = new Error();
$error
->setMessage($e->getMessage())
->setErrorCode(empty($e->statusCode) ? $e->getCode() : $e->statusCode);
->setErrorCode($e->getCode());
$returnResponse->setError($error)->setSuccess(false);
}

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
]
},
"require": {
"php": ">=5.6.0",
"guzzlehttp/guzzle": "6.*"
"php": ">=5.6.0"
},
"require-dev": {
"php": "^7.3",
Expand Down
Loading

0 comments on commit 9ea6a0a

Please sign in to comment.