-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add HttpClient & fix billing api calls #1843
Merged
kseghair
merged 5 commits into
PrestaShopCorp:master
from
PoulainMaxime:feat/billing-wrapper-module
Jan 17, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Copyright since 2007 PrestaShop SA and Contributors | ||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Academic Free License version 3.0 | ||
* that is bundled with this package in the file LICENSE.md. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/AFL-3.0 | ||
* 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 [email protected] so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Adapter; | ||
|
||
use PrestaShop\Module\PsxMarketingWithGoogle\Http\HttpClient; | ||
|
||
class BillingAdapter | ||
{ | ||
private const BILLING_URL = 'https://billing-api.distribution.prestashop.net/v1/'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $jwt; | ||
|
||
public function __construct($jwt) | ||
{ | ||
$this->jwt = $jwt; | ||
} | ||
|
||
public function getCurrentSubscription($shopId, $productId) | ||
{ | ||
$httpClient = new HttpClient(self::BILLING_URL); | ||
$httpClient->setHeaders([ | ||
'Accept: application/json', | ||
'Authorization: Bearer ' . $this->jwt, | ||
'Content-Type: application/json', | ||
'User-Agent : module-lib-billing v3 (' . $productId . ')', | ||
]); | ||
|
||
return $httpClient->get('/customers/' . $shopId . '/subscriptions/' . $productId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright since 2007 PrestaShop SA and Contributors | ||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Academic Free License version 3.0 | ||
* that is bundled with this package in the file LICENSE.md. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/AFL-3.0 | ||
* 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 [email protected] so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Http; | ||
|
||
use RuntimeException; | ||
|
||
class HttpClient | ||
{ | ||
private $curl; | ||
/** @var array<int|string,string> */ | ||
private $headers = []; | ||
/** @var array<int|string,bool|int> */ | ||
private $options = []; | ||
/** @var string */ | ||
private $baseUrl = ''; | ||
|
||
/** | ||
* Constructor initializes cURL | ||
* | ||
* @param string $baseUrl Optional base URL for all requests | ||
* | ||
* @throws RuntimeException if cURL extension is not loaded | ||
*/ | ||
public function __construct(string $baseUrl = '') | ||
{ | ||
if (!extension_loaded('curl')) { | ||
throw new RuntimeException('cURL extension is not loaded'); | ||
} | ||
|
||
$this->baseUrl = rtrim($baseUrl, '/'); | ||
$this->curl = curl_init(); | ||
|
||
// Set default options | ||
$this->setDefaultOptions(); | ||
} | ||
|
||
/** | ||
* Set default cURL options | ||
*/ | ||
private function setDefaultOptions(): void | ||
{ | ||
$this->options = [ | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_FOLLOWLOCATION => true, | ||
CURLOPT_MAXREDIRS => 5, | ||
CURLOPT_TIMEOUT => 30, | ||
CURLOPT_SSL_VERIFYPEER => true, | ||
CURLOPT_SSL_VERIFYHOST => 2, | ||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | ||
]; | ||
} | ||
|
||
/** | ||
* Set custom headers for the request | ||
* | ||
* @param array<int|string,string> $headers | ||
* | ||
* @return self | ||
*/ | ||
public function setHeaders(array $headers): self | ||
{ | ||
$this->headers = $headers; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a single header | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* | ||
* @return self | ||
*/ | ||
public function addHeader(string $name, string $value): self | ||
{ | ||
$this->headers[] = "$name: $value"; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set custom cURL options | ||
* | ||
* @param array<string,bool|int> $options | ||
* | ||
* @return self | ||
*/ | ||
public function setOptions(array $options): self | ||
{ | ||
$this->options = $options + $this->options; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Execute HTTP request | ||
* | ||
* @param string $method HTTP method | ||
* @param string $url URL endpoint | ||
* @param array<string,string|bool|int>|string|null $data Request data | ||
* | ||
* @return Response | ||
* | ||
* @throws RuntimeException on cURL errors | ||
*/ | ||
public function request(string $method, string $url, $data = null) | ||
{ | ||
$url = $this->baseUrl . '/' . ltrim($url, '/'); | ||
|
||
$options = $this->options + [ | ||
CURLOPT_URL => $url, | ||
CURLOPT_CUSTOMREQUEST => strtoupper($method), | ||
CURLOPT_HEADER => true, | ||
CURLOPT_HTTPHEADER => $this->headers, | ||
]; | ||
|
||
if ($data !== null) { | ||
if (is_array($data)) { | ||
$data = http_build_query($data); | ||
} | ||
|
||
if ($method === 'GET') { | ||
/* @phpstan-ignore-next-line */ | ||
$options[CURLOPT_URL] .= '?' . $data; | ||
} else { | ||
$options[CURLOPT_POSTFIELDS] = $data; | ||
} | ||
} | ||
|
||
curl_setopt_array($this->curl, $options); | ||
|
||
$response = curl_exec($this->curl); | ||
|
||
if ($response === false) { | ||
throw new \RuntimeException(sprintf('cURL error (%s): %s', curl_errno($this->curl), curl_error($this->curl))); | ||
} | ||
|
||
$headerSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); | ||
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); | ||
|
||
// Split response into headers and body | ||
$headerStr = substr((string) $response, 0, $headerSize); | ||
$body = substr((string) $response, $headerSize); | ||
$error = curl_error($this->curl); | ||
|
||
// Parse headers | ||
$headers = []; | ||
foreach (explode("\r\n", $headerStr) as $line) { | ||
if (preg_match('/^([^:]+):(.+)$/', $line, $matches)) { | ||
$headers[trim($matches[1])] = trim($matches[2]); | ||
} | ||
} | ||
|
||
return new Response( | ||
$httpCode, | ||
$body, | ||
$headers, | ||
$error | ||
); | ||
} | ||
|
||
/** | ||
* Convenience method for GET requests | ||
* | ||
* @param string $url | ||
* @param array<string, string> $params Query parameters | ||
* | ||
* @return Response | ||
*/ | ||
public function get(string $url, array $params = []) | ||
{ | ||
return $this->request('GET', $url, $params); | ||
} | ||
|
||
/** | ||
* Convenience method for POST requests | ||
* | ||
* @param string $url | ||
* @param array<string, string>|string $data | ||
* | ||
* @return Response | ||
*/ | ||
public function post(string $url, $data = []) | ||
{ | ||
return $this->request('POST', $url, $data); | ||
} | ||
|
||
/** | ||
* Convenience method for PUT requests | ||
* | ||
* @param string $url | ||
* @param array<string, string>|string $data | ||
* | ||
* @return Response | ||
*/ | ||
public function put(string $url, $data = []) | ||
{ | ||
return $this->request('PUT', $url, $data); | ||
} | ||
|
||
/** | ||
* Convenience method for DELETE requests | ||
* | ||
* @param string $url | ||
* @param array<string, string> $params | ||
* | ||
* @return Response | ||
*/ | ||
public function delete(string $url, array $params = []) | ||
{ | ||
return $this->request('DELETE', $url, $params); | ||
} | ||
|
||
/** | ||
* Destructor closes cURL connection | ||
*/ | ||
public function __destruct() | ||
{ | ||
if ($this->curl) { | ||
curl_close($this->curl); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright since 2007 PrestaShop SA and Contributors | ||
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Academic Free License version 3.0 | ||
* that is bundled with this package in the file LICENSE.md. | ||
* It is also available through the world-wide-web at this URL: | ||
* https://opensource.org/licenses/AFL-3.0 | ||
* 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 [email protected] so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <[email protected]> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\Http; | ||
|
||
class Response | ||
{ | ||
/** @var int */ | ||
private $statusCode; | ||
/** @var string */ | ||
private $body; | ||
/** @var array<string,string> */ | ||
private $headers; | ||
/** @var string|null */ | ||
private $error; | ||
|
||
/** | ||
* @param int $statusCode | ||
* @param string $body | ||
* @param array<string,string> $headers | ||
* @param string|null $error | ||
* | ||
**/ | ||
public function __construct($statusCode, $body, $headers = [], $error = null) | ||
{ | ||
$this->statusCode = $statusCode; | ||
$this->body = $body; | ||
$this->headers = $headers; | ||
$this->error = $error; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStatusCode() | ||
{ | ||
return $this->statusCode; | ||
} | ||
|
||
/** | ||
* @return array<string,string> | ||
*/ | ||
public function getHeaders() | ||
{ | ||
return $this->headers; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBody() | ||
{ | ||
return $this->body; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getError() | ||
{ | ||
return $this->error; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ca fonctionne avec le mode sandbox de Billing ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acutellement non mais je peux rajouté le mode sandbox. Il me semble que c'est juste un header en plus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Si tu peux ça serait top. Le bon header suivant l'env où on est