Skip to content

Commit

Permalink
Merge pull request #21 from OXID-eSales/PSPAYPAL-806_UniquePayPalRequ…
Browse files Browse the repository at this point in the history
…estID

Pspaypal 806 unique pay pal request
  • Loading branch information
mariolorenz authored Aug 8, 2024
2 parents 4916a8a + b600cc3 commit f9e7251
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 28 deletions.
20 changes: 13 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.0.14] -2024-04-26
## [2.0.15] - 2024-08-08

### FIX

- introduce ActionHash to make the PayPal-Request-ID more unique

## [2.0.14] - 2024-04-26

- introduce GooglePay-Classes

## [2.0.13] -2024-04-09
## [2.0.13] - 2024-04-09

- PayPal-Request-Id based on serialized body, no extra PayPal-Request-Id necessary anymore

## [2.0.12] -2024-03-19
## [2.0.12] - 2024-03-19

- introduce central log functionality

## [2.0.11] -2024-03-15
## [2.0.11] - 2024-03-15

- remove 500-redirect if some API Calls not work correctly
- introduce PayPal-Request-Id for api-calls

## [2.0.10] -2024-01-26
## [2.0.10] - 2024-01-26

- add Vaulting to API-Calls
- add Debug-Logging for send-Requests

## [2.0.9] -2023-11-17
## [2.0.9] - 2023-11-17

- add PartnerAttributionId to API-Calls

## [2.0.8] -2023-09-08
## [2.0.8] - 2023-09-08

- Avoid PHP warning on non existing cache file

Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Catalog extends BaseService
{
protected $basePath = '/v1/catalogs';
protected string $basePath = '/v1/catalogs';

/**
* Creates a product.
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Disputes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

class Disputes extends BaseService
{
protected $basePath = '/v1/customer';
protected string $basePath = '/v1/customer';

/**
* Send a dispute event notification. The event format should conform to the CloudEvents specification.Currently
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class Orders extends BaseService
{
protected $basePath = '/v2/checkout';
protected string $basePath = '/v2/checkout';

/**
* Creates an order.<blockquote><strong>Note:</strong> For error handling and troubleshooting, see <a
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Partner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Partner extends BaseService
{
protected $basePath = '/v2/customer';
protected string $basePath = '/v2/customer';

/**
* Creates a partner referral that is shared by the API caller. The referrals contains the client's personal,
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class Payments extends BaseService
{
protected $basePath = '/v2/payments';
protected string $basePath = '/v2/payments';

/**
* Authorizes partial or full amount of a saved order payment, by order ID. Authorize partial or full amount of a
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class Subscriptions extends BaseService
{
protected $basePath = '/v1/billing';
protected string $basePath = '/v1/billing';

/**
* Creates a plan that defines pricing and billing cycle details for subscriptions.
Expand Down
2 changes: 1 addition & 1 deletion generated/Service/TransactionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TransactionSearch extends BaseService
{
protected $basePath = '/v1/reporting';
protected string $basePath = '/v1/reporting';

/**
* Lists transactions. Specify one or more query parameters to filter the transaction that appear in the
Expand Down
29 changes: 21 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Client
*/
private $merchantPayerId;

/**
* @var string
*/
private $actionHash;

/**
* @var string
*/
Expand All @@ -74,6 +79,7 @@ class Client
* seller/merchant or partner clientSecret depending on if it is a first party or a third party
* request. Usually you want to use the sellers credentials if they are available
* @param string $tokenCacheFilename the filename for the cached token
* @param string $actionHash - An hash to help generate a unique PayPal-Request-Id
* @param string $payerId the technical oxid paypal account client id used as meta information in requests
* @param bool $debug
*/
Expand All @@ -83,6 +89,7 @@ public function __construct(
$clientId,
$clientSecret,
$tokenCacheFilename,
$actionHash = "",
$payerId = "",
$debug = false
) {
Expand All @@ -91,6 +98,7 @@ public function __construct(
$this->merchantClientSecret = $clientSecret;
$this->merchantPayerId = $payerId;
$this->tokenCacheFilename = $tokenCacheFilename;
$this->actionHash = $actionHash;
$stack = HandlerStack::create();
if ($debug) {
$stack->push(
Expand Down Expand Up @@ -128,7 +136,7 @@ public function send(RequestInterface $request): ResponseInterface
try {
$method = $request->getMethod();
assert(
(array_search($method, ['POST','PATCH','PUT','GET','DELETE']) !== false),
(in_array($method, ['POST','PATCH','PUT','GET','DELETE'])),
"not valid http method '$method' for paypal client"
);

Expand All @@ -138,16 +146,14 @@ public function send(RequestInterface $request): ResponseInterface
//clear tokens to force re-auth
$this->setTokenResponse(null);
return $this->sendWithAuth($request);
} else {
throw $e;
}
throw $e;
}
}

public function request($method, $uri = '', $options = [])
{
$res = $this->httpClient->request($method, $this->endpoint . $uri, $options);
return $res;
return $this->httpClient->request($method, $this->endpoint . $uri, $options);
}

/**
Expand Down Expand Up @@ -175,7 +181,7 @@ public function auth()
]
]);

$rawTokenResponse = json_decode('' . $res->getBody(), true);
$rawTokenResponse = json_decode('' . $res->getBody(), true, 512, JSON_THROW_ON_ERROR);

$this->setTokenResponse($rawTokenResponse['access_token']);

Expand Down Expand Up @@ -252,8 +258,7 @@ protected function injectAuthHeaders(RequestInterface $request)
protected function sendWithAuth(RequestInterface $request)
{
$request = $this->injectAuthHeaders($request);
$res = $this->httpClient->send($request);
return $res;
return $this->httpClient->send($request);
}


Expand All @@ -280,4 +285,12 @@ public function getEndpoint(): string
{
return $this->endpoint;
}

/**
* @return string
*/
public function getActionHash(): string
{
return $this->actionHash;
}
}
16 changes: 15 additions & 1 deletion src/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Onboarding extends Client
* @param string $oxidPartnerId for getting credentials and information for successful onboarding
* @param string $sellerId for getting information for successful onboarding
* @param string $tokenCacheFilename the filename for the cached token
* @param string $actionHash - An hash to help generate a unique PayPal-Request-Id
* @param bool $debug set to true to debug request sent to PayPal on the console
*/
public function __construct(
Expand All @@ -44,11 +45,21 @@ public function __construct(
$oxidPartnerId,
$sellerId,
$tokenCacheFilename,
$actionHash,
$debug = false
) {
$this->partnerId = $oxidPartnerId;
$this->sellerId = $sellerId;
parent::__construct($logger, $endpoint, $oxidClientId, $oxidClientSecret, $tokenCacheFilename, '', $debug);
parent::__construct(
$logger,
$endpoint,
$oxidClientId,
$oxidClientSecret,
$tokenCacheFilename,
$actionHash,
'',
$debug
);
}

/**
Expand Down Expand Up @@ -124,6 +135,9 @@ public function getCredentials(): array
return $result;
}

/**
* @throws ApiException
*/
public function getMerchantInformations(): array
{
$partnerId = $this->partnerId;
Expand Down
9 changes: 4 additions & 5 deletions src/Service/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class BaseService
{
use ServiceContainer;

/** @var Client */
public $client;
public Client $client;

protected string $basePath = '';

/**
* @param $client Client
Expand Down Expand Up @@ -46,9 +47,7 @@ protected function send($method, $path, $params = [], $headers = [], $body = nul
}
$fullPath = $this->basePath . $path;

if ($body) {
$headers['PayPal-Request-Id'] = md5(serialize($body));
}
$headers['PayPal-Request-Id'] = md5($path . serialize($body) . $this->client->getActionHash());

$request = $this->client->createRequest($method, $fullPath, $headers, $body);

Expand Down

0 comments on commit f9e7251

Please sign in to comment.