-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace client token by user id token
- Loading branch information
Showing
9 changed files
with
438 additions
and
41 deletions.
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
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,88 @@ | ||
<?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\PrestashopCheckout\PayPal\Customer; | ||
|
||
use Db; | ||
use DbQuery; | ||
use Exception; | ||
use PrestaShop\Module\PrestashopCheckout\Customer\ValueObject\CustomerId; | ||
use PrestaShop\Module\PrestashopCheckout\PayPal\Customer\ValueObject\PayPalCustomerId; | ||
|
||
class PayPalCustomerRepository | ||
{ | ||
/** | ||
* @var Db | ||
*/ | ||
private $db; | ||
|
||
/** | ||
* @param Db $db | ||
*/ | ||
public function __construct(Db $db) | ||
{ | ||
$this->db = $db; | ||
} | ||
|
||
/** | ||
* @param CustomerId $customerId | ||
* | ||
* @return PayPalCustomerId|null | ||
* | ||
* @throws Exception | ||
*/ | ||
public function findPayPalCustomerIdByCustomerId(CustomerId $customerId) | ||
{ | ||
try { | ||
$query = new DbQuery(); | ||
$query->select('`paypal_customer_id`'); | ||
$query->from('pscheckout_customer'); | ||
$query->where('`id_customer` = ' . (int) $customerId->getValue()); | ||
$customerIdPayPal = $this->db->getValue($query); | ||
|
||
return $customerIdPayPal ? new PayPalCustomerId($customerIdPayPal) : null; | ||
} catch (Exception $exception) { | ||
throw new Exception('Failed to find PayPal Customer ID', 0, $exception); | ||
} | ||
} | ||
|
||
/** | ||
* @param CustomerId $customerId | ||
* @param PayPalCustomerId $paypalCustomerId | ||
* | ||
* @return void | ||
* | ||
* @throws Exception | ||
*/ | ||
public function save(CustomerId $customerId, PayPalCustomerId $paypalCustomerId) | ||
{ | ||
try { | ||
$this->db->insert( | ||
'pscheckout_customer', | ||
[ | ||
'id_customer' => (int) $customerId->getValue(), | ||
'paypal_customer_id' => pSQL($paypalCustomerId->getValue()), | ||
] | ||
); | ||
} catch (Exception $exception) { | ||
throw new Exception('Failed to save PayPal Customer ID', 0, $exception); | ||
} | ||
} | ||
} |
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,74 @@ | ||
<?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\PrestashopCheckout\PayPal\Customer\ValueObject; | ||
|
||
use InvalidArgumentException; | ||
|
||
class PayPalCustomerId | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $customerId; | ||
|
||
/** | ||
* @param string $customerId | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct($customerId) | ||
{ | ||
$this->assertIsValid($customerId); | ||
$this->customerId = $customerId; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->customerId; | ||
} | ||
|
||
/** | ||
* @param string $customerId | ||
* | ||
* @return void | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function assertIsValid($customerId) | ||
{ | ||
if (!is_string($customerId)) { | ||
throw new InvalidArgumentException('PayPal Customer ID must be a string.'); | ||
} | ||
|
||
$length = strlen($customerId); | ||
|
||
if ($length < 1 || $length > 22) { | ||
throw new InvalidArgumentException('PayPal Customer ID must be between 1 and 22 characters long.'); | ||
} | ||
|
||
if (preg_match('/^[0-9a-zA-Z_-]+$/', $customerId) !== 1) { | ||
throw new InvalidArgumentException('PayPal Customer ID must be alphanumeric.'); | ||
} | ||
} | ||
} |
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,66 @@ | ||
<?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\PrestashopCheckout\PayPal\OAuth; | ||
|
||
use Exception; | ||
use GuzzleHttp\Psr7\Request; | ||
use PrestaShop\Module\PrestashopCheckout\PayPal\Customer\ValueObject\PayPalCustomerId; | ||
|
||
class OAuthService | ||
{ | ||
private $httpClient; | ||
|
||
public function __construct($httpClient) | ||
{ | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
/** | ||
* @param PayPalCustomerId|null $customerId | ||
* | ||
* @return string | ||
* | ||
* @throws Exception | ||
*/ | ||
public function getUserIdToken(PayPalCustomerId $customerId = null) | ||
{ | ||
try { | ||
$body = 'grant_type=client_credentials&response_type=id_token'; | ||
|
||
if ($customerId) { | ||
$body .= '&target_customer_id=' . $customerId->getValue(); | ||
} | ||
|
||
$request = new Request('POST', '', [], $body); | ||
$response = $this->httpClient->sendRequest($request); | ||
|
||
$data = json_decode($response->getBody()->getContents(), true); | ||
|
||
if (empty($data['id_token'])) { | ||
throw new Exception('Failed to get PayPal User ID token from response.'); | ||
} | ||
|
||
return $data['id_token']; | ||
} catch (Exception $exception) { | ||
throw new Exception('Failed to get PayPal User ID token.', 0, $exception); | ||
} | ||
} | ||
} |
Oops, something went wrong.