From e128bc17fd39f4429a705defbe7d3fd57d658431 Mon Sep 17 00:00:00 2001 From: Mohammed Elhaouari <9967336+mohammed-elhaouari@users.noreply.github.com> Date: Tue, 12 Oct 2021 11:52:06 +0100 Subject: [PATCH] Release v3.0.0 (#22) * update namespace * add upgrading steps * add api docs * add other api docs --- README.md | 12 +- composer.json | 6 +- composer.lock | 101 +++++- docs/api/amount-breakdown.md | 91 ++++- docs/api/amount.md | 91 ++++- docs/api/application-context.md | 319 +++++++++++++++++- docs/api/item.md | 264 ++++++++++++++- docs/basic-usage/order-capture.md | 2 +- docs/basic-usage/order-create.md | 2 +- docs/basic-usage/order-show.md | 2 +- docs/installation.md | 8 +- docs/upgrading.md | 19 ++ src/Concerns/HasCollection.php | 1 - src/Contracts/Environment.php | 21 -- src/Contracts/HttpClient.php | 20 -- src/Environment/PayPalEnvironment.php | 36 -- src/Environment/ProductionEnvironment.php | 27 -- src/Environment/SandboxEnvironment.php | 27 -- src/Http/AccessToken.php | 74 ---- src/Http/AccessTokenRequest.php | 28 -- src/Http/PayPalClient.php | 172 ---------- src/Http/PaypalRequest.php | 10 - .../OrderAuthorizeRequest.php | 4 +- .../OrderCaptureRequest.php | 4 +- src/{Http => Requests}/OrderCreateRequest.php | 3 +- src/{Http => Requests}/OrderShowRequest.php | 4 +- tests/Environment/PayPalEnvironmentTest.php | 24 -- tests/Http/AccessTokenRequestTest.php | 69 ---- tests/Http/AccessTokenTest.php | 23 -- tests/Http/PayPalClientTest.php | 146 -------- .../OrderAuthorizeRequestTest.php | 4 +- .../OrderCaptureRequestTest.php | 4 +- .../OrderCreateRequestTest.php | 4 +- .../OrderShowRequestTest.php | 4 +- 34 files changed, 906 insertions(+), 720 deletions(-) delete mode 100644 src/Contracts/Environment.php delete mode 100644 src/Contracts/HttpClient.php delete mode 100644 src/Environment/PayPalEnvironment.php delete mode 100644 src/Environment/ProductionEnvironment.php delete mode 100644 src/Environment/SandboxEnvironment.php delete mode 100644 src/Http/AccessToken.php delete mode 100644 src/Http/AccessTokenRequest.php delete mode 100644 src/Http/PayPalClient.php delete mode 100644 src/Http/PaypalRequest.php rename src/{Http => Requests}/OrderAuthorizeRequest.php (87%) rename src/{Http => Requests}/OrderCaptureRequest.php (86%) rename src/{Http => Requests}/OrderCreateRequest.php (87%) rename src/{Http => Requests}/OrderShowRequest.php (86%) delete mode 100644 tests/Environment/PayPalEnvironmentTest.php delete mode 100644 tests/Http/AccessTokenRequestTest.php delete mode 100644 tests/Http/AccessTokenTest.php delete mode 100644 tests/Http/PayPalClientTest.php rename tests/{Http => Requests}/OrderAuthorizeRequestTest.php (95%) rename tests/{Http => Requests}/OrderCaptureRequestTest.php (95%) rename tests/{Http => Requests}/OrderCreateRequestTest.php (97%) rename tests/{Http => Requests}/OrderShowRequestTest.php (94%) diff --git a/README.md b/README.md index cb97732..d0c10de 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ Inorder to communicate with PayPal platform we need to set up a client first : ```php // import namespace -use PayPal\Checkout\Environment\SandboxEnvironment; -use PayPal\Checkout\Http\PayPalClient; +use PayPal\Http\Environment\SandboxEnvironment; +use PayPal\Http\PayPalClient; // client id and client secret retrieved from PayPal $clientId = "<>"; @@ -58,8 +58,8 @@ $client = new PayPalClient($environment); ```php // import namespace -use PayPal\Checkout\Environment\ProductionEnvironment; -use PayPal\Checkout\Http\PayPalClient; +use PayPal\Http\Environment\ProductionEnvironment; +use PayPal\Http\PayPalClient; // client id and client secret retrieved from PayPal $clientId = "<>"; @@ -80,7 +80,7 @@ $client = new PayPalClient($environment); ```php // Import namespace -use PayPal\Checkout\Http\OrderCreateRequest; +use PayPal\Checkout\Requests\OrderCreateRequest; use PayPal\Checkout\Orders\AmountBreakdown; use PayPal\Checkout\Orders\Item; use PayPal\Checkout\Orders\Order; @@ -117,7 +117,7 @@ echo $result->status; // CREATED ```php // Import namespace -use PayPal\Checkout\Http\OrderCaptureRequest; +use PayPal\Checkout\Requests\OrderCaptureRequest; // Create an order capture http request $request = new OrderCaptureRequest($order_id); diff --git a/composer.json b/composer.json index 2b875ff..205429e 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,10 @@ "require": { "php": "^7.4|^8.0", "ext-json": "*", - "guzzlehttp/psr7": "^1.6|^2.0", - "guzzlehttp/guzzle": "^7.0", - "brick/money": "^0.5.2" + "brick/money": "^0.5.2", + "phpjuice/paypal-http-client": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^9.0", "squizlabs/php_codesniffer": "^3.4", "phpstan/phpstan": "^0.12", "pestphp/pest": "^1.18" diff --git a/composer.lock b/composer.lock index 50e4423..64f1a91 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "77a352a58227411d51a641b3038d8856", + "content-hash": "33b3a6cae82fdbd68b938cab96c352a7", "packages": [ { "name": "brick/math", @@ -232,16 +232,16 @@ }, { "name": "guzzlehttp/promises", - "version": "1.4.1", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d" + "reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d", - "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d", + "url": "https://api.github.com/repos/guzzle/promises/zipball/136a635e2b4a49b9d79e9c8fee267ffb257fdba0", + "reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0", "shasum": "" }, "require": { @@ -253,7 +253,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.4-dev" + "dev-master": "1.5-dev" } }, "autoload": { @@ -269,10 +269,25 @@ "MIT" ], "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" } ], "description": "Guzzle promises library", @@ -281,9 +296,23 @@ ], "support": { "issues": "https://github.com/guzzle/promises/issues", - "source": "https://github.com/guzzle/promises/tree/1.4.1" + "source": "https://github.com/guzzle/promises/tree/1.5.0" }, - "time": "2021-03-07T09:25:29+00:00" + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", + "type": "tidelift" + } + ], + "time": "2021-10-07T13:05:22+00:00" }, { "name": "guzzlehttp/psr7", @@ -400,6 +429,62 @@ ], "time": "2021-10-06T17:43:30+00:00" }, + { + "name": "phpjuice/paypal-http-client", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/phpjuice/paypal-http-client.git", + "reference": "2e230a5186cceae66dffd75847af702e8f637fe9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpjuice/paypal-http-client/zipball/2e230a5186cceae66dffd75847af702e8f637fe9", + "reference": "2e230a5186cceae66dffd75847af702e8f637fe9", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^7.0", + "guzzlehttp/psr7": "^1.6|^2.0", + "php": "^7.4|^8.0" + }, + "require-dev": { + "pestphp/pest": "^1.18", + "phpstan/phpstan": "^0.12", + "squizlabs/php_codesniffer": "^3.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "PayPal\\Http\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHPJuice", + "email": "phpjuice@gmail.com", + "role": "Developer" + } + ], + "description": "PHP Http Client for PayPal's REST API", + "homepage": "https://github.com/phpjuice/paypal-http-client", + "keywords": [ + "client", + "http", + "paypal", + "phpjuice" + ], + "support": { + "issues": "https://github.com/phpjuice/paypal-http-client/issues", + "source": "https://github.com/phpjuice/paypal-http-client/tree/v1.0.0" + }, + "time": "2021-10-08T13:16:38+00:00" + }, { "name": "psr/http-client", "version": "1.0.1", diff --git a/docs/api/amount-breakdown.md b/docs/api/amount-breakdown.md index 4fd9027..2d8b9a9 100644 --- a/docs/api/amount-breakdown.md +++ b/docs/api/amount-breakdown.md @@ -1,4 +1,93 @@ # Amount Breakdown -coming soon. +See https://developer.paypal.com/docs/api/orders/v2/#definition-Amount_breakdown. +## Methods + +### AmountBreakdown::__construct() + +Creates an amount breakdown object using constructor. + +#### Signature + +```php +public function __construct(string $value, string $currency_code = 'USD'); +``` + +#### Example + +```php +$amount = new AmountBreakdown('100.00', 'USD'); +``` + +### AmountBreakdown::of() + +Creates an amount from a value and an optional currency code. + +#### Signature + +```php +public static function of(string $value, string $currency_code = 'USD'): AmountBreakdown; +``` + +#### Example + +```php +$amount = AmountBreakdown::of('100.00', 'USD'); +``` + +### AmountBreakdown::getCurrencyCode() + +Gets an amount currency code. + +#### Signature + +```php +public function getCurrencyCode(): string; +``` + +#### Example + +```php +$amount = AmountBreakdown::of('100.00', 'USD'); +$currency = $amount->getCurrencyCode(); // USD +``` + +### AmountBreakdown::getValue() + +Gets an amount value. + +#### Signature + +```php +public function getValue(): string; +``` + +#### Example + +```php +$amount = AmountBreakdown::of('100.00', 'USD'); +$value = $amount->getValue(); // '100.00' +``` + +### AmountBreakdown::toArray() + +Casts the AmountBreakdown an array representation, used when serializing an amount into http request. + +#### Signature + +```php +public function toArray(): array; +``` + +#### Example + +```php +$amount = AmountBreakdown::of('100.00', 'USD'); +$array = $amount->toArray(); +// result +[ + 'value' => '100.00', + 'currency_code' => 'USD' +]; +``` diff --git a/docs/api/amount.md b/docs/api/amount.md index e1cad4c..f143faf 100644 --- a/docs/api/amount.md +++ b/docs/api/amount.md @@ -1,4 +1,93 @@ # Amount -coming soon. +See https://developer.paypal.com/docs/api/orders/v2/#definition-Amount_breakdown. +## Methods + +### Amount::__construct() + +Creates an amount object using constructor. + +#### Signature + +```php +public function __construct(string $value, string $currency_code = 'USD'); +``` + +#### Example + +```php +$amount = new Amount('100.00', 'USD'); +``` + +### Amount::of() + +Creates an amount from a value and an optional currency code. + +#### Signature + +```php +public static function of(string $value, string $currency_code = 'USD'): Amount; +``` + +#### Example + +```php +$amount = Amount::of('100.00', 'USD'); +``` + +### Amount::getCurrencyCode() + +Gets an amount currency code. + +#### Signature + +```php +public function getCurrencyCode(): string; +``` + +#### Example + +```php +$amount = Amount::of('100.00', 'USD'); +$currency = $amount->getCurrencyCode(); // USD +``` + +### Amount::getValue() + +Gets an amount value. + +#### Signature + +```php +public function getValue(): string; +``` + +#### Example + +```php +$amount = Amount::of('100.00', 'USD'); +$value = $amount->getValue(); // '100.00' +``` + +### Amount::toArray() + +Casts the Amount an array representation, used when serializing an amount into http request. + +#### Signature + +```php +public function toArray(): array; +``` + +#### Example + +```php +$amount = Amount::of('100.00', 'USD'); +$array = $amount->toArray(); +// result +[ + 'value' => '100.00', + 'currency_code' => 'USD' +]; +``` diff --git a/docs/api/application-context.md b/docs/api/application-context.md index eac1a41..8105d18 100644 --- a/docs/api/application-context.md +++ b/docs/api/application-context.md @@ -1,4 +1,319 @@ -# Application Context +# ApplicationContext -coming soon. +See https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context +## Methods + +### ApplicationContext::__construct() + +Creates an application context object using constructor. + +#### Signature + +```php +public function __construct( + ?string $brand_name = null, + ?string $locale = 'en-US', + ?string $landing_page = NO_PREFERENCE, + ?string $shipping_preference = NO_SHIPPING, + ?string $return_url = null, + ?string $cancel_url = null +); +``` + +#### Example + +```php +$application_context = new ApplicationContext('PHPJuice', 'en-US'); +``` + +### ApplicationContext::create() + +Helper method to create an `ApplicationContext` statically. + +#### Signature + +```php +public static function create( + ?string $brand_name = null, + ?string $locale = 'en-US', + ?string $landing_page = NO_PREFERENCE, + ?string $shipping_preference = NO_SHIPPING, + ?string $return_url = null, + ?string $cancel_url = null +): ApplicationContext; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +``` + +### ApplicationContext::getBrandName() + +Returns the brand name value from the application context. + +#### Signature + +```php +public function getBrandName(): ?string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getBrandName() // PHPJuice +``` + +### ApplicationContext::setBrandName() + +Sets the brand name value on an application context. + +#### Signature + +```php +public function setBrandName($brand_name): self; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setBrandName('PHPJuice Inc'); +$application_context->getBrandName(); // PHPJuice Inc +``` + +### ApplicationContext::getLocale() + +Returns the locale value from the application context, default to `en-US`. + +#### Signature + +```php +public function getLocale(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getLocale() // en-US +``` + +### ApplicationContext::setLocale() + +Sets a locale on an application context. + +#### Signature + +```php +public function setLocale($locale): self; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setLocale('en-GB'); +$application_context->getLocale(); // en-GB +``` + +### ApplicationContext::getShippingPreference() + +Returns the shipping preference value from an application context, default to `NO_SHIPPING`. + +#### Signature + +```php +public function getShippingPreference(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US', 'NO_SHIPPING'); +$application_context->getShippingPreference() // NO_SHIPPING +``` + +### ApplicationContext::setShippingPreference() + +Sets a shipping preference value on an application context. + +#### Signature + +```php +public function setShippingPreference($shipping_preference): self; +``` + +**Available shipping preference options** + +```php +const GET_FROM_FILE = 'GET_FROM_FILE'; +const NO_SHIPPING = 'NO_SHIPPING'; +const SET_PROVIDED_ADDRESS = 'SET_PROVIDED_ADDRESS'; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setShippingPreference('GET_FROM_FILE'); +$application_context->getShippingPreference(); // GET_FROM_FILE +``` + +### ApplicationContext::getLandingPage() + +Returns the landing page value from an application context, default to `NO_PREFERENCE`. + +#### Signature + +```php +public function getLandingPage(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getLandingPage() // NO_PREFERENCE +``` + +### ApplicationContext::setLandingPage() + +Sets a landing page value on an application context. + +#### Signature + +```php +public function setLandingPage($landing_page): self; +``` + +**Available landing page options** + +```php +const LOGIN = 'LOGIN'; +const BILLING = 'BILLING'; +const NO_PREFERENCE = 'NO_PREFERENCE'; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setLandingPage('BILLING'); +$application_context->getLandingPage(); // BILLING +``` + +### ApplicationContext::getUserAction() + +Returns the user action value from an application context, default to `CONTINUE`. + +#### Signature + +```php +public function getUserAction(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getUserAction() // CONTINUE +``` + +### ApplicationContext::setUserAction() + +Sets a user action value on an application context. + +#### Signature + +```php +public function setUserAction($user_action): self; +``` + +**Available user action options** + +```php +const ACTION_CONTINUE = 'CONTINUE'; +const ACTION_PAY_NOW = 'PAY_NOW'; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setUserAction('PAY_NOW'); +$application_context->getUserAction(); // PAY_NOW +``` + +### ApplicationContext::getReturnUrl() + +Returns the return url value from an application context, default to `null`. + +#### Signature + +```php +public function getReturnUrl(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getReturnUrl() // null +``` + +### ApplicationContext::setReturnUrl() + +Sets a return url value on an application context. + +#### Signature + +```php +public function setReturnUrl($return_url): self; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setReturnUrl('https://phpjuice.org/success'); +$application_context->getReturnUrl(); // https://phpjuice.org/success +``` + +### ApplicationContext::getCancelUrl() + +Returns the cancel url value from an application context, default to `null`. + +#### Signature + +```php +public function getCancelUrl(): string; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->getCancelUrl() // null +``` + +### ApplicationContext::setCancelUrl() + +Sets a cancel url value on an application context. + +#### Signature + +```php +public function setCancelUrl($cancel_url): self; +``` + +#### Example + +```php +$application_context = ApplicationContext::create('PHPJuice', 'en-US'); +$application_context->setCancelUrl('https://phpjuice.org/cancel'); +$application_context->getCancelUrl(); // https://phpjuice.org/cancel +``` diff --git a/docs/api/item.md b/docs/api/item.md index 37569d1..13406a3 100644 --- a/docs/api/item.md +++ b/docs/api/item.md @@ -1,4 +1,266 @@ # Item -coming soon. +See https://developer.paypal.com/docs/api/orders/v2/#definition-item. +## Methods + +### Item::__construct() + +Creates an item object using constructor. + +#### Signature + +```php +public function __construct(string $name, AmountContract $amount, int $quantity = 1); +``` + +#### Example + +```php +$amount = Amount::of('100', 'USD'); +$item = new Item('Item name', $amount, 2); +``` + +### Item::create() + +A static helper method to create a new item. + +#### Signature + +```php +public static function create(string $name, string $value, string $currency_code = 'USD', int $quantity = 1): Item; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +``` + +### Item::setUnitAmount() + +A method to override a unit amount on an item. + +#### Signature + +```php +public function setUnitAmount(AmountContract $unit_amount): self; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$new_amount = Amount::of('200.00', 'USD'); +$item->setUnitAmount($new_amount); +``` + +### Item::getSku() + +Returns a sku value assigned to an Item, default to `uniqid()`. + +#### Signature + +```php +public function getSku(): string; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->getSku() // random ex:5819f3ad1c0ce +``` + +### Item::setSku() + +Sets an sku value on an item. + +#### Signature + +```php +public function setSku(string $sku): self; +``` + +#### Example + +```php +$item = Item::create('Item 1', '100.00', 'USD', 4); +$item->setSku('item_5819f3ad1c0ce'); +$item->getSku(); // item_5819f3ad1c0ce +``` + +### Item::getName() + +Returns an item name. + +#### Signature + +```php +public function getName(): ?string; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->getName() // Item name +``` + +### Item::setName() + +Sets an item name. + +#### Signature + +```php +public function setName(string $name): self; +``` + +#### Example + +```php +$item = Item::create('Item 1', '100.00', 'USD', 4); +$item->setName('My Item'); +$item->getName(); // My Item +``` + +### Item::getDescription() + +Returns an item description. defaults to `null` + +#### Signature + +```php +public function getDescription(): ?string; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->getDescription() // null +``` + +### Item::setDescription() + +Sets an item description. + +#### Signature + +```php +public function setDescription(string $description): self; +``` + +#### Example + +```php +$item = Item::create('Item 1', '100.00', 'USD', 4); +$item->setDescription('My Item description'); +$item->getDescription(); // My Item description +``` + +### Item::getQuantity() + +Returns an item quantity. defaults to `1` + +#### Signature + +```php +public function getQuantity(): int; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->getQuantity() // 4 +``` + +### Item::setQuantity() + +Sets an item quantity. + +#### Signature + +```php +public function setQuantity(int $quantity): self; +``` + +#### Example + +```php +$item = Item::create('Item 1', '100.00', 'USD', 4); +$item->setQuantity(3); +$item->getQuantity(); // 3 +``` + +### Item::getCategory() + +Returns an item quantity. defaults to `DIGITAL_GOODS` + +#### Signature + +```php +public function getCategory(): ?string; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->getCategory() // DIGITAL_GOODS +``` + +### Item::setCategory() + +Sets an item category. + +#### Signature + +```php +public function setCategory(string $category): self; +``` + +**Available category options** + +```php +const DIGITAL_GOODS = 'DIGITAL_GOODS'; +const PHYSICAL_GOODS = 'PHYSICAL_GOODS'; +``` + +#### Example + +```php +$item = Item::create('Item 1', '100.00', 'USD', 4); +$item->setCategory(PHYSICAL_GOODS); +$item->getCategory(); // PHYSICAL_GOODS +``` + +### Item::toArray() + +Returns an item array representation. used when serializing an item into http request. + +#### Signature + +```php +public function toArray(): array; +``` + +#### Example + +```php +$item = Item::create('Item name', '100.00', 'USD', 4); +$item->toArray(); +// result +[ + 'name' => 'Item name', + 'unit_amount' => [ + 'value' => '100.00', + 'currency_code' => 'USD' + ], + 'quantity' => 4, + 'description' => null, + 'category' => 'DIGITAL_GOODS', +] +``` diff --git a/docs/basic-usage/order-capture.md b/docs/basic-usage/order-capture.md index f204f05..81b285c 100644 --- a/docs/basic-usage/order-capture.md +++ b/docs/basic-usage/order-capture.md @@ -3,7 +3,7 @@ To successfully capture payment for an order, the buyer must first approve the order or a valid payment\_source must be provided in the request. A buyer can approve the order upon being redirected to the `rel: approve` URL that was provided the HATEOAS links in the [Create Order](order-create.md) response. ```php -use PayPal\Checkout\Http\OrderCaptureRequest; +use PayPal\Checkout\Requests\OrderCaptureRequest; // Get order id from database or request $order_id = '8F783829JA718493L'; diff --git a/docs/basic-usage/order-create.md b/docs/basic-usage/order-create.md index 4975152..5d8438d 100644 --- a/docs/basic-usage/order-create.md +++ b/docs/basic-usage/order-create.md @@ -3,7 +3,7 @@ An order represents a payment between two or more parties. and inorder to create a new order we must send an `OrderCreateRequest` to PayPal API. ```php -use PayPal\Checkout\Http\OrderCreateRequest; +use PayPal\Checkout\Requests\OrderCreateRequest; use PayPal\Checkout\Orders\AmountBreakdown; use PayPal\Checkout\Orders\Item; use PayPal\Checkout\Orders\Order; diff --git a/docs/basic-usage/order-show.md b/docs/basic-usage/order-show.md index f0ebf67..11ac7a2 100644 --- a/docs/basic-usage/order-show.md +++ b/docs/basic-usage/order-show.md @@ -3,7 +3,7 @@ Inorder to show an order we must send an `OrderShowRequest` to PayPal API with `order_id` in question. ```php -use PayPal\Checkout\Http\OrderShowRequest; +use PayPal\Checkout\Requests\OrderShowRequest; // Get order id from database or request $order_id = '8F783829JA718493L'; diff --git a/docs/installation.md b/docs/installation.md index e4a1ff7..479b416 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -18,8 +18,8 @@ Inorder to communicate with PayPal platform we need to set up a client first : ```php // import namespace -use PayPal\Checkout\Environment\SandboxEnvironment; -use PayPal\Checkout\Http\PayPalClient; +use PayPal\Http\Environment\SandboxEnvironment; +use PayPal\Http\PayPalClient; // client id and client secret retrieved from PayPal $clientId = "<>"; @@ -36,8 +36,8 @@ $client = new PayPalClient($environment); ```php // import namespace -use PayPal\Checkout\Environment\ProductionEnvironment; -use PayPal\Checkout\Http\PayPalClient; +use PayPal\Http\Environment\ProductionEnvironment; +use PayPal\Http\PayPalClient; // client id and client secret retrieved from PayPal $clientId = "<>"; diff --git a/docs/upgrading.md b/docs/upgrading.md index 7301733..da7ea76 100644 --- a/docs/upgrading.md +++ b/docs/upgrading.md @@ -1,5 +1,24 @@ # Upgrading +## UPGRADING FROM V2 TO V3 + +There are few changes needed for upgrading from v2 to v3. but the bulk of changes can be done by leveraging the power of +find and replace on your editor. + +- Change `^2.xx` (xx can vary) to `^3.0` in your `composer.json` +- Run`composer update`. Of course, your app must meet the minimum requirements as well. +- Change `PayPalClient` namespace from `PayPal\Checkout\Http\PayPalClient` to `PayPal\Http\PayPalClient`. +- Change environments namespace: + - `PayPal\Checkout\Environment\Environment` to `PayPal\Http\Environment\Environment`. + - `PayPal\Checkout\Environment\ProductionEnvironment` to `PayPal\Http\Environment\ProductionEnvironment`. + - `PayPal\Checkout\Environment\SandboxEnvironment` to `PayPal\Http\Environment\SandboxEnvironment`. +- Change requests namespace: + - `PayPal\Checkout\Http\OrderCreateRequest` to `PayPal\Checkout\Requests\OrderCreateRequest`. + - `PayPal\Checkout\Http\OrderCaptureRequest` to `PayPal\Checkout\Requests\OrderCaptureRequest`. + - `PayPal\Checkout\Http\OrderAuthorizeRequest` to `PayPal\Checkout\Requests\OrderAuthorizeRequest`. + - `PayPal\Checkout\Http\OrderShowRequest` to `PayPal\Checkout\Requests\OrderShowRequest`. + - `PayPal\Checkout\Http\PaypalRequest` to `PayPal\Http\PaypalRequest` + ## UPGRADING FROM V1 TO V2 There are no special requirements for upgrading from v1 to v2, other than changing ^1.xx \(xx can vary\) to ^2.0 in your composer. json and running `composer update`. Of course, your app must meet the minimum requirements as well. diff --git a/src/Concerns/HasCollection.php b/src/Concerns/HasCollection.php index 95fe3c9..b791ca8 100644 --- a/src/Concerns/HasCollection.php +++ b/src/Concerns/HasCollection.php @@ -61,7 +61,6 @@ public function offsetSet($offset, $value) /** * @param string $offset - * */ public function offsetGet(string $offset) { diff --git a/src/Contracts/Environment.php b/src/Contracts/Environment.php deleted file mode 100644 index 7628ea7..0000000 --- a/src/Contracts/Environment.php +++ /dev/null @@ -1,21 +0,0 @@ -clientId = $clientId; - $this->clientSecret = $clientSecret; - } - - /** - * @return string - */ - public function basicAuthorizationString(): string - { - return base64_encode($this->clientId.':'.$this->clientSecret); - } -} diff --git a/src/Environment/ProductionEnvironment.php b/src/Environment/ProductionEnvironment.php deleted file mode 100644 index df360e2..0000000 --- a/src/Environment/ProductionEnvironment.php +++ /dev/null @@ -1,27 +0,0 @@ -token = $token; - $this->token_type = $token_type; - $this->expires_in = $expires_in; - $this->created_at = time(); - } - - /** - * gets the token. - */ - public function getToken(): ?string - { - return $this->token; - } - - /** - * gets the token. - */ - public function getTokenType(): ?string - { - return $this->token_type; - } - - /** - * returns authorization string. - */ - public function authorizationString(): ?string - { - return $this->token_type.' '.$this->token; - } - - /** - * returns if a token is expired or not. - */ - public function isExpired(): bool - { - return time() >= $this->created_at + $this->expires_in; - } -} diff --git a/src/Http/AccessTokenRequest.php b/src/Http/AccessTokenRequest.php deleted file mode 100644 index 43bb79a..0000000 --- a/src/Http/AccessTokenRequest.php +++ /dev/null @@ -1,28 +0,0 @@ - 'Basic '.$environment->basicAuthorizationString(), - 'Accept' => 'application/json', - 'Content-Type' => 'application/x-www-form-urlencoded', - ]; - - $body = http_build_query(['grant_type' => 'client_credentials']); - - // if refresh token is provided we use it to get an access token - if (!is_null($refresh_token)) { - $body = http_build_query([ - 'grant_type' => 'refresh_token', - 'refresh_token' => $refresh_token, - ]); - } - parent::__construct('POST', '/v1/oauth2/token', $headers, $body); - } -} diff --git a/src/Http/PayPalClient.php b/src/Http/PayPalClient.php deleted file mode 100644 index f21fde8..0000000 --- a/src/Http/PayPalClient.php +++ /dev/null @@ -1,172 +0,0 @@ -environment = $environment; - $this->client = new Client(['base_uri' => $environment->baseUrl()]); - $this->access_token = null; - } - - /** - * Send http request. - * - * @param Request $request - * @return Response - * @throws GuzzleException - * @throws RequestException - */ - public function send(Request $request): Response - { - // if request doesn't have an authorization header - if (!$this->hasAuthHeader($request)) { - // fetch access token if null or expired - if ($this->hasInvalidToken()) { - $this->fetchAccessToken(); - } - // add Authorization header to request - $request = $request->withHeader('Authorization', $this->access_token->authorizationString()); - } - - // add user agent header to request - $request = $this->injectUserAgentHeaders($request); - - // add sdk headers - $request = $this->injectSdkHeaders($request); - - // add gzip headers - $request = $this->injectGzipHeaders($request); - - // send request and return response - return $this->client->send($request); - } - - /** - * Check if headers contain an auth header. - * - * @param Request $request - * @return bool - */ - public function hasAuthHeader(Request $request): bool - { - return array_key_exists('Authorization', $request->getHeaders()); - } - - /** - * Check if request has a valid token - * - * @return bool - */ - public function hasInvalidToken(): bool - { - return !$this->access_token || $this->access_token->isExpired(); - } - - /** - * Sends a request that fetches the access token. - * - * @return AccessToken - * @throws GuzzleException - */ - public function fetchAccessToken(): AccessToken - { - $response = $this->client->send(new AccessTokenRequest($this->environment)); - $result = Utils::jsonDecode((string) $response->getBody()); - $this->access_token = new AccessToken($result->access_token, $result->token_type, $result->expires_in); - - return $this->access_token; - } - - /** - * Injects default user-agent into the request. - * - * @param Request $request - * @return Request - */ - public function injectUserAgentHeaders(Request $request): Request - { - return $request->withHeader('User-Agent', 'PayPalHttp-PHP HTTP/1.1'); - } - - /** - * Inject PayPal sdk headers into request. - * - * @param Request $request - * @return Request - */ - public function injectSdkHeaders(Request $request): Request - { - $r = $request->withHeader('sdk_name', 'Checkout SDK') - ->withHeader('sdk_version', '1.0.0'); - - /* - * Only inject this header on production - * - * @see https://github.com/phpjuice/paypal-checkout-sdk/issues/6 - */ - if ('production' == $this->environment->name()) { - $r = $r->withHeader('sdk_tech_stack', 'PHP '.PHP_VERSION); - } - - return $r; - } - - /** - * Inject gzip headers into the request. - * - * @param Request $request - * @return Request - */ - public function injectGzipHeaders(Request $request): Request - { - return $request->withHeader('Accept-Encoding', 'gzip'); - } - - /** - * Returns default user-agent. - */ - public function setClient(Client $client): self - { - $this->client = $client; - - return $this; - } -} diff --git a/src/Http/PaypalRequest.php b/src/Http/PaypalRequest.php deleted file mode 100644 index cfacd70..0000000 --- a/src/Http/PaypalRequest.php +++ /dev/null @@ -1,10 +0,0 @@ -baseUrl())->toBe('https://api.sandbox.paypal.com'); -}); - -it('creates production environment', function () { - $env = new ProductionEnvironment('client_id', 'client_secret'); - expect($env->baseUrl())->toBe('https://api.paypal.com'); -}); - -it('has basic authorization string', function () { - $env1 = new SandboxEnvironment('client_id', 'client_secret'); - $env2 = new ProductionEnvironment('client_id', 'client_secret'); - $expected = base64_encode('client_id:client_secret'); - expect($env1->basicAuthorizationString())->toBe($expected); - expect($env2->basicAuthorizationString())->toBe($expected); -}); diff --git a/tests/Http/AccessTokenRequestTest.php b/tests/Http/AccessTokenRequestTest.php deleted file mode 100644 index d1f4423..0000000 --- a/tests/Http/AccessTokenRequestTest.php +++ /dev/null @@ -1,69 +0,0 @@ -environment = new ProductionEnvironment('client_id', 'client_secret'); -}); - -it('has correct request uri', function () { - $request = new AccessTokenRequest($this->environment); - expect((string) $request->getUri())->toBe('/v1/oauth2/token'); -}); - -it('has correct rquest method', function () { - $request = new AccessTokenRequest($this->environment); - expect($request->getMethod())->toBe('POST'); -}); - -it('has correct request headers', function () { - $request = new AccessTokenRequest($this->environment); - expect($request->getHeaderLine('Content-Type'))->toBe('application/x-www-form-urlencoded'); -}); - -it('has basic auth request headers', function () { - $request = new AccessTokenRequest($this->environment); - $expected = 'Basic '.$this->environment->basicAuthorizationString(); - expect($request->getHeaderLine('Authorization'))->toBe($expected); -}); - -it('has correct data with body', function () { - $request = new AccessTokenRequest($this->environment); - $expected = http_build_query(['grant_type' => 'client_credentials']); - expect((string) $request->getBody())->toBe($expected); -}); - -it('can excute a request', function () { - $mockResponse = json_encode([ - 'access_token' => 'A21AAFSO5otrlVigoJUQ1p', - 'token_type' => 'Bearer', - 'expires_in' => 32400, - ]); - $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], $mockResponse), - ]); - $handlerStack = HandlerStack::create($mock); - $client = new Client(['handler' => $handlerStack]); - - $response = $client->send(new AccessTokenRequest($this->environment)); - - expect($response->getStatusCode())->toBe(200); - - $result = Utils::jsonDecode((string) $response->getBody(), true); - expect($result)->toBe([ - 'access_token' => 'A21AAFSO5otrlVigoJUQ1p', - 'token_type' => 'Bearer', - 'expires_in' => 32400, - ]); -}); diff --git a/tests/Http/AccessTokenTest.php b/tests/Http/AccessTokenTest.php deleted file mode 100644 index 48e3e2d..0000000 --- a/tests/Http/AccessTokenTest.php +++ /dev/null @@ -1,23 +0,0 @@ -getToken())->toBe('A21AAFSO5otrlVigoJUQ1p'); - expect($accessToken->getTokenType())->toBe('Bearer'); -}); - -it('testTokenIsExpired', function () { - $accessToken = new AccessToken('A21AAFSO5otrlVigoJUQ1p', 'Bearer', 32400); - expect($accessToken->isExpired())->toBeFalse(); - $accessToken = new AccessToken('A21AAFSO5otrlVigoJUQ1p', 'Bearer', 0); - expect($accessToken->isExpired())->toBeTrue(); -}); - -it('testTokenAuthorizationString', function () { - $accessToken = new AccessToken('Token', 'Bearer', 32400); - expect($accessToken->authorizationString())->toEqual('Bearer Token'); -}); diff --git a/tests/Http/PayPalClientTest.php b/tests/Http/PayPalClientTest.php deleted file mode 100644 index 83739d2..0000000 --- a/tests/Http/PayPalClientTest.php +++ /dev/null @@ -1,146 +0,0 @@ -setClient($this->client); - /** @noinspection PhpUnhandledExceptionInspection */ - $accessToken = $paypalClient->fetchAccessToken(); - expect($accessToken->authorizationString())->toBe('Bearer A21AAFSO5otrlVigoJUQ1p'); -}); - -it("has authorization header", function () { - $env = new ProductionEnvironment('client_id', 'client_secret'); - $paypalClient = new PayPalClient($env); - $paypalClient->setClient($this->client); - - $request = new AccessTokenRequest($env); - expect($paypalClient->hasAuthHeader($request))->toBeTrue(); - - $request = new OrderCaptureRequest('1KC5501443316171H'); - - expect($paypalClient->hasAuthHeader($request))->toBeFalse(); -}); - - -it("has all sdk headers on production", function () { - $env = new ProductionEnvironment('client_id', 'client_secret'); - $paypalClient = new PayPalClient($env); - $paypalClient->setClient($this->client); - - $request = new AccessTokenRequest($env); - $request = $paypalClient->injectSdkHeaders($request); - expect($request->getHeaders())->toBe([ - 'Authorization' => [ - 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=', - ], - 'Accept' => [ - 'application/json', - ], - 'Content-Type' => [ - 'application/x-www-form-urlencoded', - ], - 'sdk_name' => [ - 'Checkout SDK', - ], - 'sdk_version' => [ - '1.0.0', - ], - 'sdk_tech_stack' => [ - 'PHP '.PHP_VERSION, - ], - ]); -}); - - -it("has only a subset of sdk headers on sandbox", function () { - $env = new SandboxEnvironment('client_id', 'client_secret'); - $paypalClient = new PayPalClient($env); - $paypalClient->setClient($this->client); - - $request = new AccessTokenRequest($env); - $request = $paypalClient->injectSdkHeaders($request); - expect($request->getHeaders())->toBe([ - 'Authorization' => [ - 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=', - ], - 'Accept' => [ - 'application/json', - ], - 'Content-Type' => [ - 'application/x-www-form-urlencoded', - ], - 'sdk_name' => [ - 'Checkout SDK', - ], - 'sdk_version' => [ - '1.0.0', - ], - ]); -}); - - -it("tests has invalid token method", function () { - $env = new ProductionEnvironment('client_id', 'client_secret'); - $paypalClient = new PayPalClient($env); - $paypalClient->setClient($this->client); - - expect($paypalClient->hasInvalidToken())->toBeTrue(); - - /** @noinspection PhpUnhandledExceptionInspection */ - $paypalClient->fetchAccessToken(); - - expect($paypalClient->hasInvalidToken())->toBeFalse(); -}); - - -it("can execute request", function () { - $env = new ProductionEnvironment('client_id', 'client_secret'); - $paypalClient = new PayPalClient($env); - $paypalClient->setClient($this->client); - - $request = new OrderCaptureRequest('1KC5501443316171H'); - - /** @noinspection PhpUnhandledExceptionInspection */ - $response = $paypalClient->send($request); - - $result = Utils::jsonDecode((string) $response->getBody(), true); - expect($result['id'])->toBe('1KC5501443316171H'); -}); - - -beforeEach(function () { - $response1 = json_encode([ - 'access_token' => 'A21AAFSO5otrlVigoJUQ1p', - 'token_type' => 'Bearer', - 'expires_in' => 32400, - ]); - - $response2 = json_encode([ - 'id' => '1KC5501443316171H', - 'intent' => 'CAPTURE', - 'status' => 'CREATED', - ]); - - $mock = new MockHandler([ - new Response(200, ['Content-Type' => 'application/json'], $response1), - new Response(200, ['Content-Type' => 'application/json'], $response2), - ]); - - $handlerStack = HandlerStack::create($mock); - - $this->client = new Client(['handler' => $handlerStack]); -}); diff --git a/tests/Http/OrderAuthorizeRequestTest.php b/tests/Requests/OrderAuthorizeRequestTest.php similarity index 95% rename from tests/Http/OrderAuthorizeRequestTest.php rename to tests/Requests/OrderAuthorizeRequestTest.php index 1754154..fd5e0ee 100644 --- a/tests/Http/OrderAuthorizeRequestTest.php +++ b/tests/Requests/OrderAuthorizeRequestTest.php @@ -1,6 +1,6 @@