From 6d676c6cb7d40860d679fda9844a59c432cd8613 Mon Sep 17 00:00:00 2001 From: Mike Edinger Date: Fri, 30 May 2025 15:48:43 +0200 Subject: [PATCH 01/30] Fix stan for php-stan --- src/Mappers/Tiers/TierMapper.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Mappers/Tiers/TierMapper.php b/src/Mappers/Tiers/TierMapper.php index 55f4baf..52a005d 100644 --- a/src/Mappers/Tiers/TierMapper.php +++ b/src/Mappers/Tiers/TierMapper.php @@ -7,11 +7,7 @@ class TierMapper { - /** - * @param $data - * @return Tier - */ - public function map($data): ?Tier + public function map(stdClass $data): ?Tier { if (!$data->name) { return null; From 3da00d0cf397c74830737c43822423da8d620a46 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 9 Jul 2025 13:17:04 +0200 Subject: [PATCH 02/30] wip --- src/Mappers/Products/ProductMapper.php | 19 +++ src/Mappers/Products/ProductsMapper.php | 25 ++++ src/Models/Products/Product.php | 130 ++++++++++++++++++ .../OAuth/Products/ProductsResource.php | 89 ++++++++++++ .../Register/Products/ProductsResource.php | 92 +++++++++++++ 5 files changed, 355 insertions(+) create mode 100644 src/Mappers/Products/ProductMapper.php create mode 100644 src/Mappers/Products/ProductsMapper.php create mode 100644 src/Models/Products/Product.php create mode 100644 src/Resources/OAuth/Products/ProductsResource.php create mode 100644 src/Resources/Register/Products/ProductsResource.php diff --git a/src/Mappers/Products/ProductMapper.php b/src/Mappers/Products/ProductMapper.php new file mode 100644 index 0000000..60fc1cc --- /dev/null +++ b/src/Mappers/Products/ProductMapper.php @@ -0,0 +1,19 @@ +uuid, + $data->externalIdentifier, + $data->name, + $data->description + ); + } +} diff --git a/src/Mappers/Products/ProductsMapper.php b/src/Mappers/Products/ProductsMapper.php new file mode 100644 index 0000000..045cef7 --- /dev/null +++ b/src/Mappers/Products/ProductsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $perks; + } +} diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php new file mode 100644 index 0000000..d8fa878 --- /dev/null +++ b/src/Models/Products/Product.php @@ -0,0 +1,130 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->name = $name; + $this->description = $description; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): string + { + return $this->externalIdentifier; + } + + public function getName(): string + { + return $this->name; + } + + public function getDescription(): string + { + return $this->description; + } + + /** + * @param mixed[] $params + * @return Product[] + * + * @throws MaintenanceModeException|GuzzleException|PiggyRequestException + */ + public static function list(array $params = []): array + { + $response = ApiClient::get(self::resourceUri, $params); + + return ProductsMapper::map((array) $response->getData()); + } + + /** + * @param mixed[] $body + * + * @throws MaintenanceModeException|GuzzleException|PiggyRequestException + */ + public static function create(array $body): Product + { + $response = ApiClient::post(self::resourceUri, $body); + + return ProductMapper::map($response->getData()); + } + + /** + * @param mixed[] $params + * + * @throws MaintenanceModeException|GuzzleException|PiggyRequestException + */ + public static function get(string $perkUuid, array $params = []): Product + { + $response = ApiClient::get(self::resourceUri."/$perkUuid", $params); + + return ProductMapper::map($response->getData()); + } + + /** + * @param mixed[] $body + * + * @throws MaintenanceModeException|GuzzleException|PiggyRequestException + */ + public static function update(string $perkUuid, array $body): Product + { + $response = ApiClient::put(self::resourceUri."/$perkUuid", $body); + + return ProductMapper::map($response->getData()); + } + + /** + * @param mixed[] $params + * + * @throws MaintenanceModeException|GuzzleException|PiggyRequestException + */ + public static function delete(string $perkUuid, array $params = []): Response + { + return ApiClient::delete(self::resourceUri."/$perkUuid", $params); + } +} diff --git a/src/Resources/OAuth/Products/ProductsResource.php b/src/Resources/OAuth/Products/ProductsResource.php new file mode 100644 index 0000000..bdf0159 --- /dev/null +++ b/src/Resources/OAuth/Products/ProductsResource.php @@ -0,0 +1,89 @@ +client->get($this->resourceUri, $params); + + $mapper = new ProductsMapper(); + + return $mapper->map((array) $response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function create(string $externalIdentifier, string $name, ?string $description): Product + { + $response = $this->client->post($this->resourceUri, [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param mixed[] $params + * + * @throws PiggyRequestException + */ + public function get(string $perkUuid, array $params = []): Product + { + $response = $this->client->get("$this->resourceUri/$perkUuid", $params); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $perkUuid, string $label): Product + { + $response = $this->client->put("$this->resourceUri/$perkUuid", [ + 'label' => $label, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param mixed[] $params + * @return null + * + * @throws PiggyRequestException + */ + public function delete(string $perkUuid, array $params = []) + { + $response = $this->client->destroy("$this->resourceUri/$perkUuid", $params); + + return $response->getData(); + } +} \ No newline at end of file diff --git a/src/Resources/Register/Products/ProductsResource.php b/src/Resources/Register/Products/ProductsResource.php new file mode 100644 index 0000000..4f86ea9 --- /dev/null +++ b/src/Resources/Register/Products/ProductsResource.php @@ -0,0 +1,92 @@ +client->get($this->resourceUri, $params); + + $mapper = new ProductsMapper(); + + return $mapper->map((array) $response->getData()); + } + + /** + * @param mixed[] $options + * + * @throws PiggyRequestException + */ + public function create(string $label, string $name, string $dataType, array $options): Product + { + $response = $this->client->post($this->resourceUri, [ + 'label' => $label, + 'name' => $name, + 'dataType' => $dataType, + 'options' => $options, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param mixed[] $params + * + * @throws PiggyRequestException + */ + public function get(string $perkUuid, array $params = []): Product + { + $response = $this->client->get("$this->resourceUri/$perkUuid", $params); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $perkUuid, string $label): Product + { + $response = $this->client->put("$this->resourceUri/$perkUuid", [ + 'label' => $label, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param mixed[] $params + * @return null + * + * @throws PiggyRequestException + */ + public function delete(string $perkUuid, array $params = []) + { + $response = $this->client->destroy("$this->resourceUri/$perkUuid", $params); + + return $response->getData(); + } +} \ No newline at end of file From b8d5628f035b97a139af95ef5bb7e2c6df7e9aa9 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 10:56:29 +0200 Subject: [PATCH 03/30] wip --- src/Mappers/Products/ProductMapper.php | 3 +- src/Mappers/Products/ProductsMapper.php | 6 +- .../OAuth/Products/ProductsResource.php | 73 +++++++++++++---- .../Register/Products/ProductsResource.php | 80 ++++++++++++++----- 4 files changed, 119 insertions(+), 43 deletions(-) diff --git a/src/Mappers/Products/ProductMapper.php b/src/Mappers/Products/ProductMapper.php index 60fc1cc..0b6e73f 100644 --- a/src/Mappers/Products/ProductMapper.php +++ b/src/Mappers/Products/ProductMapper.php @@ -13,7 +13,8 @@ public function map(stdClass $data): Product $data->uuid, $data->externalIdentifier, $data->name, - $data->description + $data->description, + $data->categories ); } } diff --git a/src/Mappers/Products/ProductsMapper.php b/src/Mappers/Products/ProductsMapper.php index 045cef7..bb9f104 100644 --- a/src/Mappers/Products/ProductsMapper.php +++ b/src/Mappers/Products/ProductsMapper.php @@ -15,11 +15,11 @@ public function map(array $data): array { $mapper = new ProductMapper(); - $perks = []; + $products = []; foreach ($data as $item) { - $perks[] = $mapper->map($item); + $products[] = $mapper->map($item); } - return $perks; + return $products; } } diff --git a/src/Resources/OAuth/Products/ProductsResource.php b/src/Resources/OAuth/Products/ProductsResource.php index bdf0159..d8b458b 100644 --- a/src/Resources/OAuth/Products/ProductsResource.php +++ b/src/Resources/OAuth/Products/ProductsResource.php @@ -13,12 +13,9 @@ class ProductsResource extends BaseResource /** * @var string */ - protected $resourceUri = '/api/v3/oauth/clients/external-products'; + protected $resourceUri = '/api/v3/oauth/clients/products'; /** - * @param mixed[] $params - * @return Product[] - * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -33,12 +30,13 @@ public function list(array $params = []): array /** * @throws PiggyRequestException */ - public function create(string $externalIdentifier, string $name, ?string $description): Product + public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product { $response = $this->client->post($this->resourceUri, [ 'external_identifier' => $externalIdentifier, 'name' => $name, 'description' => $description, + 'categories' => $categories, ]); $mapper = new ProductMapper(); @@ -47,13 +45,11 @@ public function create(string $externalIdentifier, string $name, ?string $descri } /** - * @param mixed[] $params - * * @throws PiggyRequestException */ - public function get(string $perkUuid, array $params = []): Product + public function get(string $uuid, array $params = []): Product { - $response = $this->client->get("$this->resourceUri/$perkUuid", $params); + $response = $this->client->get("$this->resourceUri/$uuid", $params); $mapper = new ProductMapper(); @@ -63,10 +59,44 @@ public function get(string $perkUuid, array $params = []): Product /** * @throws PiggyRequestException */ - public function update(string $perkUuid, string $label): Product + public function find(string $externalIdentifier): Product { - $response = $this->client->put("$this->resourceUri/$perkUuid", [ - 'label' => $label, + $response = $this->client->get("$this->resourceUri/find", [ + 'external_identifier' => $externalIdentifier, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + { + $response = $this->client->post("$this->resourceUri/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + { + $response = $this->client->put("$this->resourceUri/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, ]); $mapper = new ProductMapper(); @@ -75,14 +105,23 @@ public function update(string $perkUuid, string $label): Product } /** - * @param mixed[] $params - * @return null - * * @throws PiggyRequestException */ - public function delete(string $perkUuid, array $params = []) + public function delete(string $uuid, array $params = []) { - $response = $this->client->destroy("$this->resourceUri/$perkUuid", $params); + $response = $this->client->destroy("$this->resourceUri/$uuid", $params); + + return $response->getData(); + } + + /** + * @throws PiggyRequestException + */ + public function batch(array $products): Product + { + $response = $this->client->put("$this->resourceUri/batch", [ + 'products' => $products, + ]); return $response->getData(); } diff --git a/src/Resources/Register/Products/ProductsResource.php b/src/Resources/Register/Products/ProductsResource.php index 4f86ea9..79d1e05 100644 --- a/src/Resources/Register/Products/ProductsResource.php +++ b/src/Resources/Register/Products/ProductsResource.php @@ -13,12 +13,9 @@ class ProductsResource extends BaseResource /** * @var string */ - protected $resourceUri = '/api/v3/register/external-products'; + protected $resourceUri = '/api/v3/oauth/registers/products'; /** - * @param mixed[] $params - * @return Product[] - * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -31,17 +28,15 @@ public function list(array $params = []): array } /** - * @param mixed[] $options - * * @throws PiggyRequestException */ - public function create(string $label, string $name, string $dataType, array $options): Product + public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product { $response = $this->client->post($this->resourceUri, [ - 'label' => $label, + 'external_identifier' => $externalIdentifier, 'name' => $name, - 'dataType' => $dataType, - 'options' => $options, + 'description' => $description, + 'categories' => $categories, ]); $mapper = new ProductMapper(); @@ -50,13 +45,11 @@ public function create(string $label, string $name, string $dataType, array $opt } /** - * @param mixed[] $params - * * @throws PiggyRequestException */ - public function get(string $perkUuid, array $params = []): Product + public function get(string $uuid, array $params = []): Product { - $response = $this->client->get("$this->resourceUri/$perkUuid", $params); + $response = $this->client->get("$this->resourceUri/$uuid", $params); $mapper = new ProductMapper(); @@ -66,10 +59,10 @@ public function get(string $perkUuid, array $params = []): Product /** * @throws PiggyRequestException */ - public function update(string $perkUuid, string $label): Product + public function find(string $externalIdentifier): Product { - $response = $this->client->put("$this->resourceUri/$perkUuid", [ - 'label' => $label, + $response = $this->client->get("$this->resourceUri/find", [ + 'external_identifier' => $externalIdentifier, ]); $mapper = new ProductMapper(); @@ -78,14 +71,57 @@ public function update(string $perkUuid, string $label): Product } /** - * @param mixed[] $params - * @return null - * * @throws PiggyRequestException */ - public function delete(string $perkUuid, array $params = []) + public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product { - $response = $this->client->destroy("$this->resourceUri/$perkUuid", $params); + $response = $this->client->post("$this->resourceUri/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + { + $response = $this->client->put("$this->resourceUri/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + $mapper = new ProductMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function delete(string $uuid, array $params = []) + { + $response = $this->client->destroy("$this->resourceUri/$uuid", $params); + + return $response->getData(); + } + + /** + * @throws PiggyRequestException + */ + public function batch(array $products): Product + { + $response = $this->client->put("$this->resourceUri/batch", [ + 'products' => $products, + ]); return $response->getData(); } From 681e1752f0e2f3c11eb7cc982e7ba347e5213a09 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 11:35:18 +0200 Subject: [PATCH 04/30] wip --- src/Models/Products/Product.php | 71 ++++++--------------------------- 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php index d8fa878..ecba0ba 100644 --- a/src/Models/Products/Product.php +++ b/src/Models/Products/Product.php @@ -32,21 +32,28 @@ class Product */ protected $description; + /** + * @var array|null + */ + protected $categories; + /** * @var string */ - const resourceUri = '/api/v3/oauth/clients/external-products'; + const resourceUri = '/api/v3/oauth/clients/products'; public function __construct( string $uuid, string $externalIdentifier, string $name, - ?string $description + ?string $description, + ?array $categories ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; $this->name = $name; $this->description = $description; + $this->categories = $categories; } public function getUuid(): string @@ -64,67 +71,13 @@ public function getName(): string return $this->name; } - public function getDescription(): string + public function getDescription(): ?string { return $this->description; } - /** - * @param mixed[] $params - * @return Product[] - * - * @throws MaintenanceModeException|GuzzleException|PiggyRequestException - */ - public static function list(array $params = []): array - { - $response = ApiClient::get(self::resourceUri, $params); - - return ProductsMapper::map((array) $response->getData()); - } - - /** - * @param mixed[] $body - * - * @throws MaintenanceModeException|GuzzleException|PiggyRequestException - */ - public static function create(array $body): Product - { - $response = ApiClient::post(self::resourceUri, $body); - - return ProductMapper::map($response->getData()); - } - - /** - * @param mixed[] $params - * - * @throws MaintenanceModeException|GuzzleException|PiggyRequestException - */ - public static function get(string $perkUuid, array $params = []): Product - { - $response = ApiClient::get(self::resourceUri."/$perkUuid", $params); - - return ProductMapper::map($response->getData()); - } - - /** - * @param mixed[] $body - * - * @throws MaintenanceModeException|GuzzleException|PiggyRequestException - */ - public static function update(string $perkUuid, array $body): Product - { - $response = ApiClient::put(self::resourceUri."/$perkUuid", $body); - - return ProductMapper::map($response->getData()); - } - - /** - * @param mixed[] $params - * - * @throws MaintenanceModeException|GuzzleException|PiggyRequestException - */ - public static function delete(string $perkUuid, array $params = []): Response + public function getCategories(): ?array { - return ApiClient::delete(self::resourceUri."/$perkUuid", $params); + return $this->categories; } } From 8665d8ed21abda4ad7bcc76dc8e1234824087b3f Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 13:10:03 +0200 Subject: [PATCH 05/30] wip --- src/Http/Traits/SetsOAuthResources.php | 7 + src/Http/Traits/SetsRegisterResources.php | 7 + src/Mappers/Categories/CategoriesMapper.php | 25 ++ src/Mappers/Categories/CategoryMapper.php | 18 ++ src/Mappers/Products/ProductMapper.php | 11 +- src/Models/Categories/Category.php | 50 +++ src/Models/Products/Product.php | 7 +- .../OAuth/Products/ProductsResource.php | 2 +- tests/OAuth/Products/ProductsResourceTest.php | 303 ++++++++++++++++++ 9 files changed, 426 insertions(+), 4 deletions(-) create mode 100644 src/Mappers/Categories/CategoriesMapper.php create mode 100644 src/Mappers/Categories/CategoryMapper.php create mode 100644 src/Models/Categories/Category.php create mode 100644 tests/OAuth/Products/ProductsResourceTest.php diff --git a/src/Http/Traits/SetsOAuthResources.php b/src/Http/Traits/SetsOAuthResources.php index 8caa050..3f8e1be 100644 --- a/src/Http/Traits/SetsOAuthResources.php +++ b/src/Http/Traits/SetsOAuthResources.php @@ -28,6 +28,7 @@ use Piggy\Api\Resources\OAuth\Perks\PerksResource; use Piggy\Api\Resources\OAuth\PortalSessions\PortalSessionsResource; use Piggy\Api\Resources\OAuth\PrepaidTransactionsResource; +use Piggy\Api\Resources\OAuth\Products\ProductsResource; use Piggy\Api\Resources\OAuth\Shops\ShopsResource; use Piggy\Api\Resources\OAuth\SubscriptionTypesResource; use Piggy\Api\Resources\OAuth\Tiers\TiersResource; @@ -203,6 +204,11 @@ trait SetsOAuthResources */ public $customAttributes; + /** + * @var ProductsResource + */ + public $products; + protected function setResources(BaseClient $client): void { $this->contacts = new ContactsResource($client); @@ -238,5 +244,6 @@ protected function setResources(BaseClient $client): void $this->contactsPortalAuthUrl = new ContactsPortalAuthUrlResource($client); $this->collectableRewards = new CollectableRewardsResource($client); $this->customAttributes = new CustomAttributeResource($client); + $this->products = new ProductsResource($client); } } diff --git a/src/Http/Traits/SetsRegisterResources.php b/src/Http/Traits/SetsRegisterResources.php index 7e49526..31d30d5 100644 --- a/src/Http/Traits/SetsRegisterResources.php +++ b/src/Http/Traits/SetsRegisterResources.php @@ -3,6 +3,7 @@ namespace Piggy\Api\Http\Traits; use Piggy\Api\Http\BaseClient; +use Piggy\Api\Resources\Register\Products\ProductsResource; use Piggy\Api\Resources\Register\Loyalty\Tokens\LoyaltyTokensResource; use Piggy\Api\Resources\Register\ContactIdentifiersResource; use Piggy\Api\Resources\Register\Contacts\ContactsResource; @@ -99,6 +100,11 @@ trait SetsRegisterResources */ public $promotion; + /** + * @var ProductsResource + */ + public $products; + protected function setResources(BaseClient $client): void { $this->registers = new RegisterResource($client); @@ -116,5 +122,6 @@ protected function setResources(BaseClient $client): void $this->subscriptionTypes = new SubscriptionTypesResource($client); $this->voucher = new VouchersResource($client); $this->promotion = new PromotionsResource($client); + $this->products = new ProductsResource($client); } } diff --git a/src/Mappers/Categories/CategoriesMapper.php b/src/Mappers/Categories/CategoriesMapper.php new file mode 100644 index 0000000..7391450 --- /dev/null +++ b/src/Mappers/Categories/CategoriesMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $categories; + } +} diff --git a/src/Mappers/Categories/CategoryMapper.php b/src/Mappers/Categories/CategoryMapper.php new file mode 100644 index 0000000..82b53a7 --- /dev/null +++ b/src/Mappers/Categories/CategoryMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->external_identifier, + $data->name + ); + } +} diff --git a/src/Mappers/Products/ProductMapper.php b/src/Mappers/Products/ProductMapper.php index 0b6e73f..ae0bf45 100644 --- a/src/Mappers/Products/ProductMapper.php +++ b/src/Mappers/Products/ProductMapper.php @@ -2,6 +2,7 @@ namespace Piggy\Api\Mappers\Products; +use Piggy\Api\Mappers\Categories\CategoriesMapper; use Piggy\Api\Models\Products\Product; use stdClass; @@ -9,12 +10,18 @@ class ProductMapper { public function map(stdClass $data): Product { + $categories = null; + if (isset($data->categories)) { + $mapper = new CategoriesMapper(); + $categories = $mapper->map($data->categories); + } + return new Product( $data->uuid, - $data->externalIdentifier, + $data->external_identifier, $data->name, $data->description, - $data->categories + $categories ); } } diff --git a/src/Models/Categories/Category.php b/src/Models/Categories/Category.php new file mode 100644 index 0000000..d3954ca --- /dev/null +++ b/src/Models/Categories/Category.php @@ -0,0 +1,50 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->name = $name; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): string + { + return $this->externalIdentifier; + } + + public function getName(): string + { + return $this->name; + } +} \ No newline at end of file diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php index ecba0ba..4e775e1 100644 --- a/src/Models/Products/Product.php +++ b/src/Models/Products/Product.php @@ -9,6 +9,8 @@ use Piggy\Api\Http\Responses\Response; use Piggy\Api\Mappers\Products\ProductMapper; use Piggy\Api\Mappers\Products\ProductsMapper; +use Piggy\Api\Models\Categories\Category; +use Piggy\Api\Models\Contacts\Subscription; class Product { @@ -33,7 +35,7 @@ class Product protected $description; /** - * @var array|null + * @var ?Category[] */ protected $categories; @@ -76,6 +78,9 @@ public function getDescription(): ?string return $this->description; } + /** + * @return ?Category[] + */ public function getCategories(): ?array { return $this->categories; diff --git a/src/Resources/OAuth/Products/ProductsResource.php b/src/Resources/OAuth/Products/ProductsResource.php index d8b458b..b8b276a 100644 --- a/src/Resources/OAuth/Products/ProductsResource.php +++ b/src/Resources/OAuth/Products/ProductsResource.php @@ -117,7 +117,7 @@ public function delete(string $uuid, array $params = []) /** * @throws PiggyRequestException */ - public function batch(array $products): Product + public function batch(array $products) { $response = $this->client->put("$this->resourceUri/batch", [ 'products' => $products, diff --git a/tests/OAuth/Products/ProductsResourceTest.php b/tests/OAuth/Products/ProductsResourceTest.php new file mode 100644 index 0000000..8d409d1 --- /dev/null +++ b/tests/OAuth/Products/ProductsResourceTest.php @@ -0,0 +1,303 @@ +addExpectedResponse([ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ], + [ + 'uuid' => '456', + 'external_identifier' => '456', + 'name' => 'Product 2 name', + 'description' => 'Product 2 description', + 'categories' => [ + [ + 'uuid' => '456', + 'external_identifier' => '456', + 'name' => 'Category 2', + ] + ] + ], + ]); + + $products = $this->mockedClient->products->list(); + + $this->assertEquals('123', $products[0]->getUuid()); + $this->assertEquals('123', $products[0]->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $products[0]->getName()); + $this->assertEquals('Product 1 description', $products[0]->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $products[0]->getCategories()); + + $this->assertEquals('456', $products[1]->getUuid()); + $this->assertEquals('456', $products[1]->getExternalIdentifier());; + $this->assertEquals('Product 2 name', $products[1]->getName()); + $this->assertEquals('Product 2 description', $products[1]->getDescription()); + $this->assertEquals([ + new Category('456', '456', 'Category 2'), + ], $products[1]->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_create_a_product(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->create( + '123', + 'Product 1 name', + 'Product 1 description', + [ + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ); + + $this->assertEquals('123', $product->getUuid()); + $this->assertEquals('123', $product->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $product->getName()); + $this->assertEquals('Product 1 description', $product->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $product->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_get_a_product(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->get('123'); + + $this->assertEquals('123', $product->getUuid()); + $this->assertEquals('123', $product->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $product->getName()); + $this->assertEquals('Product 1 description', $product->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $product->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_find_a_product(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->find('123'); + + $this->assertEquals('123', $product->getUuid()); + $this->assertEquals('123', $product->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $product->getName()); + $this->assertEquals('Product 1 description', $product->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $product->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_find_or_create_a_product(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->findOrCreate( + '123', + 'Product 1 name', + 'Product 1 description', + [ + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ); + + $this->assertEquals('123', $product->getUuid()); + $this->assertEquals('123', $product->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $product->getName()); + $this->assertEquals('Product 1 description', $product->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $product->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_update_a_product(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->update( + '123', + '123', + 'Product 1 name', + 'Product 1 description', + [ + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ); + + $this->assertEquals('123', $product->getUuid()); + $this->assertEquals('123', $product->getExternalIdentifier());; + $this->assertEquals('Product 1 name', $product->getName()); + $this->assertEquals('Product 1 description', $product->getDescription()); + $this->assertEquals([ + new Category('123', '123', 'Category 1'), + ], $product->getCategories()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_delete_a_product(): void + { + $this->addExpectedResponse(null); + + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Product 1 name', + 'description' => 'Product 1 description', + 'categories' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1', + ] + ] + ]); + + $product = $this->mockedClient->products->delete('123'); + + $this->assertNull($product); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_batch_create_products(): void + { + $this->addExpectedResponse([ + 'status' => 'processing', + ]); + + $response = $this->mockedClient->products->batch([ + '456', + '456', + 'Product 1 name', + 'Product 1 description', + [ + 'external_identifier' => '123', + 'name' => 'Category 1', + ], + '456', + '456', + 'Product 2 name', + 'Product 2 description', + [ + 'external_identifier' => '456', + 'name' => 'Category 2', + ] + ]); + + $this->assertEquals('processing', $response->status); + } +} \ No newline at end of file From 884e06cfa16a30a5f6825dbaf59d7bd99dc67c75 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 13:12:57 +0200 Subject: [PATCH 06/30] wip --- src/Resources/Register/Products/ProductsResource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Register/Products/ProductsResource.php b/src/Resources/Register/Products/ProductsResource.php index 79d1e05..10a749a 100644 --- a/src/Resources/Register/Products/ProductsResource.php +++ b/src/Resources/Register/Products/ProductsResource.php @@ -13,7 +13,7 @@ class ProductsResource extends BaseResource /** * @var string */ - protected $resourceUri = '/api/v3/oauth/registers/products'; + protected $resourceUri = '/api/v3/register/products'; /** * @throws PiggyRequestException From 55bba53fb5b0aa744f27c37498a67b03167e9196 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 13:23:41 +0200 Subject: [PATCH 07/30] wip --- src/Http/Traits/SetsOAuthResources.php | 7 + src/Http/Traits/SetsRegisterResources.php | 7 + .../OAuth/Categories/CategoriesResource.php | 122 ++++++++++++ .../Categories/CategoriesResource.php | 122 ++++++++++++ .../Categories/CategoriesResourceTest.php | 184 ++++++++++++++++++ tests/OAuth/Products/ProductsResourceTest.php | 42 ++-- 6 files changed, 458 insertions(+), 26 deletions(-) create mode 100644 src/Resources/OAuth/Categories/CategoriesResource.php create mode 100644 src/Resources/Register/Categories/CategoriesResource.php create mode 100644 tests/OAuth/Categories/CategoriesResourceTest.php diff --git a/src/Http/Traits/SetsOAuthResources.php b/src/Http/Traits/SetsOAuthResources.php index 3f8e1be..3f55506 100644 --- a/src/Http/Traits/SetsOAuthResources.php +++ b/src/Http/Traits/SetsOAuthResources.php @@ -5,6 +5,7 @@ use Piggy\Api\Http\BaseClient; use Piggy\Api\Resources\OAuth\Automations\AutomationsResource; use Piggy\Api\Resources\OAuth\Brandkit\BrandkitResource; +use Piggy\Api\Resources\OAuth\Categories\CategoriesResource; use Piggy\Api\Resources\OAuth\Contacts\ContactAttributesResource; use Piggy\Api\Resources\OAuth\Contacts\ContactIdentifiersResource; use Piggy\Api\Resources\OAuth\Contacts\ContactsResource; @@ -209,6 +210,11 @@ trait SetsOAuthResources */ public $products; + /** + * @var CategoriesResource + */ + public $categories; + protected function setResources(BaseClient $client): void { $this->contacts = new ContactsResource($client); @@ -245,5 +251,6 @@ protected function setResources(BaseClient $client): void $this->collectableRewards = new CollectableRewardsResource($client); $this->customAttributes = new CustomAttributeResource($client); $this->products = new ProductsResource($client); + $this->categories = new CategoriesResource($client); } } diff --git a/src/Http/Traits/SetsRegisterResources.php b/src/Http/Traits/SetsRegisterResources.php index 31d30d5..c04ae2a 100644 --- a/src/Http/Traits/SetsRegisterResources.php +++ b/src/Http/Traits/SetsRegisterResources.php @@ -3,6 +3,7 @@ namespace Piggy\Api\Http\Traits; use Piggy\Api\Http\BaseClient; +use Piggy\Api\Resources\Register\Categories\CategoriesResource; use Piggy\Api\Resources\Register\Products\ProductsResource; use Piggy\Api\Resources\Register\Loyalty\Tokens\LoyaltyTokensResource; use Piggy\Api\Resources\Register\ContactIdentifiersResource; @@ -105,6 +106,11 @@ trait SetsRegisterResources */ public $products; + /** + * @var CategoriesResource + */ + public $categories; + protected function setResources(BaseClient $client): void { $this->registers = new RegisterResource($client); @@ -123,5 +129,6 @@ protected function setResources(BaseClient $client): void $this->voucher = new VouchersResource($client); $this->promotion = new PromotionsResource($client); $this->products = new ProductsResource($client); + $this->categories = new CategoriesResource($client); } } diff --git a/src/Resources/OAuth/Categories/CategoriesResource.php b/src/Resources/OAuth/Categories/CategoriesResource.php new file mode 100644 index 0000000..77e1bbd --- /dev/null +++ b/src/Resources/OAuth/Categories/CategoriesResource.php @@ -0,0 +1,122 @@ +client->get($this->resourceUri, $params); + + $mapper = new CategoriesMapper(); + + return $mapper->map((array) $response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function create(string $externalIdentifier, string $name): Category + { + $response = $this->client->post($this->resourceUri, [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Category + { + $response = $this->client->get("$this->resourceUri/$uuid", $params); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function find(string $externalIdentifier): Category + { + $response = $this->client->get("$this->resourceUri/find", [ + 'external_identifier' => $externalIdentifier, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function findOrCreate(string $externalIdentifier, ?string $name): Category + { + $response = $this->client->post("$this->resourceUri/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category + { + $response = $this->client->put("$this->resourceUri/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function delete(string $uuid, array $params = []) + { + $response = $this->client->destroy("$this->resourceUri/$uuid", $params); + + return $response->getData(); + } + + /** + * @throws PiggyRequestException + */ + public function batch(array $categories) + { + $response = $this->client->put("$this->resourceUri/batch", [ + 'categories' => $categories, + ]); + + return $response->getData(); + } +} \ No newline at end of file diff --git a/src/Resources/Register/Categories/CategoriesResource.php b/src/Resources/Register/Categories/CategoriesResource.php new file mode 100644 index 0000000..a9cf93d --- /dev/null +++ b/src/Resources/Register/Categories/CategoriesResource.php @@ -0,0 +1,122 @@ +client->get($this->resourceUri, $params); + + $mapper = new CategoriesMapper(); + + return $mapper->map((array) $response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function create(string $externalIdentifier, string $name): Category + { + $response = $this->client->post($this->resourceUri, [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Category + { + $response = $this->client->get("$this->resourceUri/$uuid", $params); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function find(string $externalIdentifier): Category + { + $response = $this->client->get("$this->resourceUri/find", [ + 'external_identifier' => $externalIdentifier, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function findOrCreate(string $externalIdentifier, ?string $name): Category + { + $response = $this->client->post("$this->resourceUri/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category + { + $response = $this->client->put("$this->resourceUri/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + $mapper = new CategoryMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @throws PiggyRequestException + */ + public function delete(string $uuid, array $params = []) + { + $response = $this->client->destroy("$this->resourceUri/$uuid", $params); + + return $response->getData(); + } + + /** + * @throws PiggyRequestException + */ + public function batch(array $categories) + { + $response = $this->client->put("$this->resourceUri/batch", [ + 'categories' => $categories, + ]); + + return $response->getData(); + } +} \ No newline at end of file diff --git a/tests/OAuth/Categories/CategoriesResourceTest.php b/tests/OAuth/Categories/CategoriesResourceTest.php new file mode 100644 index 0000000..cca7cb7 --- /dev/null +++ b/tests/OAuth/Categories/CategoriesResourceTest.php @@ -0,0 +1,184 @@ +addExpectedResponse([ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ], + [ + 'uuid' => '456', + 'external_identifier' => '456', + 'name' => 'Category 2 name', + ], + ]); + + $categories = $this->mockedClient->categories->list(); + + $this->assertEquals('123', $categories[0]->getUuid()); + $this->assertEquals('123', $categories[0]->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $categories[0]->getName()); + + $this->assertEquals('456', $categories[1]->getUuid()); + $this->assertEquals('456', $categories[1]->getExternalIdentifier());; + $this->assertEquals('Category 2 name', $categories[1]->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_create_a_category(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ]); + + $category = $this->mockedClient->categories->create( + '123', + 'Category 1 name' + ); + + $this->assertEquals('123', $category->getUuid()); + $this->assertEquals('123', $category->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $category->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_get_a_category(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ]); + + $category = $this->mockedClient->categories->get('123'); + + $this->assertEquals('123', $category->getUuid()); + $this->assertEquals('123', $category->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $category->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_find_a_category(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ]); + + $category = $this->mockedClient->categories->find('123'); + + $this->assertEquals('123', $category->getUuid()); + $this->assertEquals('123', $category->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $category->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_find_or_create_a_category(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ]); + + $category = $this->mockedClient->categories->findOrCreate( + '123', + 'Category 1 name', + ); + + $this->assertEquals('123', $category->getUuid()); + $this->assertEquals('123', $category->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $category->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_update_a_category(): void + { + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Category 1 name', + ]); + + $category = $this->mockedClient->categories->update( + '123', + '123', + 'Category 1 name' + ); + + $this->assertEquals('123', $category->getUuid()); + $this->assertEquals('123', $category->getExternalIdentifier());; + $this->assertEquals('Category 1 name', $category->getName()); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_delete_a_category(): void + { + $this->addExpectedResponse(null); + + $category = $this->mockedClient->categories->delete('123'); + + $this->assertNull($category); + } + + /** @test + * + * @throws PiggyRequestException + */ + public function it_can_batch_create_categories(): void + { + $this->addExpectedResponse([ + 'status' => 'processing', + ]); + + $response = $this->mockedClient->categories->batch([ + [ + '123', + '123', + 'Category 1 name', + ], + [ + '456', + '456', + 'Category 2 name', + ] + ]); + + $this->assertEquals('processing', $response->status); + } +} \ No newline at end of file diff --git a/tests/OAuth/Products/ProductsResourceTest.php b/tests/OAuth/Products/ProductsResourceTest.php index 8d409d1..464ba14 100644 --- a/tests/OAuth/Products/ProductsResourceTest.php +++ b/tests/OAuth/Products/ProductsResourceTest.php @@ -250,20 +250,6 @@ public function it_can_delete_a_product(): void { $this->addExpectedResponse(null); - $this->addExpectedResponse([ - 'uuid' => '123', - 'external_identifier' => '123', - 'name' => 'Product 1 name', - 'description' => 'Product 1 description', - 'categories' => [ - [ - 'uuid' => '123', - 'external_identifier' => '123', - 'name' => 'Category 1', - ] - ] - ]); - $product = $this->mockedClient->products->delete('123'); $this->assertNull($product); @@ -280,21 +266,25 @@ public function it_can_batch_create_products(): void ]); $response = $this->mockedClient->products->batch([ - '456', - '456', - 'Product 1 name', - 'Product 1 description', [ - 'external_identifier' => '123', - 'name' => 'Category 1', + '123', + '123', + 'Product 1 name', + 'Product 1 description', + [ + 'external_identifier' => '123', + 'name' => 'Category 1', + ], ], - '456', - '456', - 'Product 2 name', - 'Product 2 description', [ - 'external_identifier' => '456', - 'name' => 'Category 2', + '456', + '456', + 'Product 2 name', + 'Product 2 description', + [ + 'external_identifier' => '456', + 'name' => 'Category 2', + ] ] ]); From 2a13ba25a1cfc9002ef7094d6c3deabcf5593734 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 13:42:40 +0200 Subject: [PATCH 08/30] wip --- src/Models/Categories/Category.php | 115 +++++++++++++++++ src/Models/Products/Product.php | 118 +++++++++++++++++- .../Register/Products/ProductsResource.php | 2 +- .../Categories/CategoriesMapper.php | 25 ++++ .../Categories/CategoryMapper.php | 18 +++ src/StaticMappers/Products/ProductMapper.php | 27 ++++ src/StaticMappers/Products/ProductsMapper.php | 25 ++++ 7 files changed, 326 insertions(+), 4 deletions(-) create mode 100644 src/StaticMappers/Categories/CategoriesMapper.php create mode 100644 src/StaticMappers/Categories/CategoryMapper.php create mode 100644 src/StaticMappers/Products/ProductMapper.php create mode 100644 src/StaticMappers/Products/ProductsMapper.php diff --git a/src/Models/Categories/Category.php b/src/Models/Categories/Category.php index d3954ca..1d66eed 100644 --- a/src/Models/Categories/Category.php +++ b/src/Models/Categories/Category.php @@ -2,6 +2,14 @@ namespace Piggy\Api\Models\Categories; +use GuzzleHttp\Exception\GuzzleException; +use Piggy\Api\ApiClient; +use Piggy\Api\Exceptions\MaintenanceModeException; +use Piggy\Api\Exceptions\PiggyRequestException; +use Piggy\Api\Http\Responses\Response; +use Piggy\Api\StaticMappers\Categories\CategoryMapper; +use Piggy\Api\StaticMappers\Categories\CategoriesMapper; + class Category { /** @@ -47,4 +55,111 @@ public function getName(): string { return $this->name; } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function list(array $params = []): array + { + $response = ApiClient::get(self::resourceUri, $params); + + return CategoriesMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function create(string $externalIdentifier, string $name): Category + { + $response = ApiClient::post(self::resourceUri, [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + return CategoryMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Category + { + $response = ApiClient::get(self::resourceUri."/$uuid", $params); + + return CategoryMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function find(string $externalIdentifier): Category + { + $response = ApiClient::get(self::resourceUri."/find", [ + 'external_identifier' => $externalIdentifier, + ]); + + return CategoryMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function findOrCreate(string $externalIdentifier, ?string $name): Category + { + $response = ApiClient::post(self::resourceUri."/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + return CategoryMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category + { + $response = ApiClient::put(self::resourceUri."/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + ]); + + return CategoryMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function delete(string $uuid, array $params = []): Response + { + return ApiClient::delete(self::resourceUri."/$uuid", $params); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function batch(array $categories) + { + $response = ApiClient::put(self::resourceUri."/batch", [ + 'categories' => $categories, + ]); + + return $response->getData(); + } } \ No newline at end of file diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php index 4e775e1..6b9de71 100644 --- a/src/Models/Products/Product.php +++ b/src/Models/Products/Product.php @@ -7,10 +7,9 @@ use Piggy\Api\Exceptions\MaintenanceModeException; use Piggy\Api\Exceptions\PiggyRequestException; use Piggy\Api\Http\Responses\Response; -use Piggy\Api\Mappers\Products\ProductMapper; -use Piggy\Api\Mappers\Products\ProductsMapper; +use Piggy\Api\StaticMappers\Products\ProductMapper; +use Piggy\Api\StaticMappers\Products\ProductsMapper; use Piggy\Api\Models\Categories\Category; -use Piggy\Api\Models\Contacts\Subscription; class Product { @@ -85,4 +84,117 @@ public function getCategories(): ?array { return $this->categories; } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function list(array $params = []): array + { + $response = ApiClient::get(self::resourceUri, $params); + + return ProductsMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product + { + $response = ApiClient::post(self::resourceUri, [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + return ProductMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Product + { + $response = ApiClient::get(self::resourceUri."/$uuid", $params); + + return ProductMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function find(string $externalIdentifier): Product + { + $response = ApiClient::get(self::resourceUri."/find", [ + 'external_identifier' => $externalIdentifier, + ]); + + return ProductMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + { + $response = ApiClient::post(self::resourceUri."/find-or-create", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + return ProductMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + { + $response = ApiClient::put(self::resourceUri."/$uuid", [ + 'external_identifier' => $externalIdentifier, + 'name' => $name, + 'description' => $description, + 'categories' => $categories, + ]); + + return ProductMapper::map($response->getData()); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function delete(string $uuid, array $params = []): Response + { + return ApiClient::delete(self::resourceUri."/$uuid", $params); + } + + /** + * @throws GuzzleException + * @throws MaintenanceModeException + * @throws PiggyRequestException + */ + public function batch(array $products) + { + $response = ApiClient::put(self::resourceUri."/batch", [ + 'products' => $products, + ]); + + return $response->getData(); + } } diff --git a/src/Resources/Register/Products/ProductsResource.php b/src/Resources/Register/Products/ProductsResource.php index 10a749a..d465bb3 100644 --- a/src/Resources/Register/Products/ProductsResource.php +++ b/src/Resources/Register/Products/ProductsResource.php @@ -117,7 +117,7 @@ public function delete(string $uuid, array $params = []) /** * @throws PiggyRequestException */ - public function batch(array $products): Product + public function batch(array $products) { $response = $this->client->put("$this->resourceUri/batch", [ 'products' => $products, diff --git a/src/StaticMappers/Categories/CategoriesMapper.php b/src/StaticMappers/Categories/CategoriesMapper.php new file mode 100644 index 0000000..e946f38 --- /dev/null +++ b/src/StaticMappers/Categories/CategoriesMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $categories; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Categories/CategoryMapper.php b/src/StaticMappers/Categories/CategoryMapper.php new file mode 100644 index 0000000..a12dad2 --- /dev/null +++ b/src/StaticMappers/Categories/CategoryMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->external_identifier, + $data->name + ); + } +} diff --git a/src/StaticMappers/Products/ProductMapper.php b/src/StaticMappers/Products/ProductMapper.php new file mode 100644 index 0000000..6e8cb49 --- /dev/null +++ b/src/StaticMappers/Products/ProductMapper.php @@ -0,0 +1,27 @@ +categories)) { + $mapper = new CategoriesMapper(); + $categories = $mapper->map($data->categories); + } + + return new Product( + $data->uuid, + $data->external_identifier, + $data->name, + $data->description, + $categories + ); + } +} diff --git a/src/StaticMappers/Products/ProductsMapper.php b/src/StaticMappers/Products/ProductsMapper.php new file mode 100644 index 0000000..54a7b40 --- /dev/null +++ b/src/StaticMappers/Products/ProductsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $products; + } +} From f5069302f117aa39ec2d8f0a01a580bf2719fa05 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 14:13:55 +0200 Subject: [PATCH 09/30] wip --- src/Models/Categories/Category.php | 103 +++++++++-------- src/Models/Products/Product.php | 107 +++++++++--------- .../OAuth/Categories/CategoriesResource.php | 38 +++++++ .../OAuth/Products/ProductsResource.php | 44 +++++++ .../Categories/CategoriesResource.php | 38 +++++++ .../Register/Products/ProductsResource.php | 44 +++++++ 6 files changed, 273 insertions(+), 101 deletions(-) diff --git a/src/Models/Categories/Category.php b/src/Models/Categories/Category.php index 1d66eed..29d9e87 100644 --- a/src/Models/Categories/Category.php +++ b/src/Models/Categories/Category.php @@ -27,7 +27,8 @@ class Category */ protected $name; - /*** @var string + /** + * @var string */ const resourceUri = '/api/v3/oauth/clients/categories'; @@ -57,9 +58,11 @@ public function getName(): string } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $params + * + * @return Category[] + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function list(array $params = []): array { @@ -69,24 +72,26 @@ public function list(array $params = []): array } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return Category + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function create(string $externalIdentifier, string $name): Category + public function create(array $body): Category { - $response = ApiClient::post(self::resourceUri, [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - ]); + $response = ApiClient::post(self::resourceUri, $body); return CategoryMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $params + * + * @return Category + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function get(string $uuid, array $params = []): Category { @@ -96,53 +101,55 @@ public function get(string $uuid, array $params = []): Category } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $params + * + * @return Category + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function find(string $externalIdentifier): Category + public function find(array $params): Category { - $response = ApiClient::get(self::resourceUri."/find", [ - 'external_identifier' => $externalIdentifier, - ]); + $response = ApiClient::get(self::resourceUri."/find", $params); return CategoryMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return Category + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function findOrCreate(string $externalIdentifier, ?string $name): Category + public function findOrCreate(array $body): Category { - $response = ApiClient::post(self::resourceUri."/find-or-create", [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - ]); + $response = ApiClient::post(self::resourceUri."/find-or-create", $body); return CategoryMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $body + * + * @return Category + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category + public function update(string $uuid, array $body): Category { - $response = ApiClient::put(self::resourceUri."/$uuid", [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - ]); + $response = ApiClient::put(self::resourceUri."/$uuid", $body); return CategoryMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $params + * + * @return Response + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function delete(string $uuid, array $params = []): Response { @@ -150,15 +157,15 @@ public function delete(string $uuid, array $params = []): Response } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return array + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function batch(array $categories) + public function batch(array $body): array { - $response = ApiClient::put(self::resourceUri."/batch", [ - 'categories' => $categories, - ]); + $response = ApiClient::put(self::resourceUri."/batch", $body); return $response->getData(); } diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php index 6b9de71..be976f3 100644 --- a/src/Models/Products/Product.php +++ b/src/Models/Products/Product.php @@ -43,6 +43,7 @@ class Product */ const resourceUri = '/api/v3/oauth/clients/products'; + /** @param Category[]|null $categories */ public function __construct( string $uuid, string $externalIdentifier, @@ -86,9 +87,11 @@ public function getCategories(): ?array } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $params + * + * @return Product[] + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function list(array $params = []): array { @@ -98,26 +101,26 @@ public function list(array $params = []): array } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return Product + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product + public function create(array $body): Product { - $response = ApiClient::post(self::resourceUri, [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - 'description' => $description, - 'categories' => $categories, - ]); + $response = ApiClient::post(self::resourceUri, $body); return ProductMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $params + * + * @return Product + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function get(string $uuid, array $params = []): Product { @@ -127,57 +130,55 @@ public function get(string $uuid, array $params = []): Product } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $params + * + * @return Product + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function find(string $externalIdentifier): Product + public function find(array $params): Product { - $response = ApiClient::get(self::resourceUri."/find", [ - 'external_identifier' => $externalIdentifier, - ]); + $response = ApiClient::get(self::resourceUri."/find", $params); return ProductMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return Product + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + public function findOrCreate(array $body): Product { - $response = ApiClient::post(self::resourceUri."/find-or-create", [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - 'description' => $description, - 'categories' => $categories, - ]); + $response = ApiClient::post(self::resourceUri."/find-or-create", $body); return ProductMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $body + * + * @return Product + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product + public function update(string $uuid, array $body): Product { - $response = ApiClient::put(self::resourceUri."/$uuid", [ - 'external_identifier' => $externalIdentifier, - 'name' => $name, - 'description' => $description, - 'categories' => $categories, - ]); + $response = ApiClient::put(self::resourceUri."/$uuid", $body); return ProductMapper::map($response->getData()); } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param string $uuid + * @param array $params + * + * @return Response + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ public function delete(string $uuid, array $params = []): Response { @@ -185,15 +186,15 @@ public function delete(string $uuid, array $params = []): Response } /** - * @throws GuzzleException - * @throws MaintenanceModeException - * @throws PiggyRequestException + * @param array $body + * + * @return array + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function batch(array $products) + public function batch(array $body): array { - $response = ApiClient::put(self::resourceUri."/batch", [ - 'products' => $products, - ]); + $response = ApiClient::put(self::resourceUri."/batch", $body); return $response->getData(); } diff --git a/src/Resources/OAuth/Categories/CategoriesResource.php b/src/Resources/OAuth/Categories/CategoriesResource.php index 77e1bbd..bec1cdc 100644 --- a/src/Resources/OAuth/Categories/CategoriesResource.php +++ b/src/Resources/OAuth/Categories/CategoriesResource.php @@ -16,6 +16,10 @@ class CategoriesResource extends BaseResource protected $resourceUri = '/api/v3/oauth/clients/categories'; /** + * @param array $params + * + * @return Category[] + * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -28,6 +32,11 @@ public function list(array $params = []): array } /** + * @param string $externalIdentifier + * @param string $name + * + * @return Category + * * @throws PiggyRequestException */ public function create(string $externalIdentifier, string $name): Category @@ -43,6 +52,11 @@ public function create(string $externalIdentifier, string $name): Category } /** + * @param string $uuid + * @param array $params + * + * @return Category + * * @throws PiggyRequestException */ public function get(string $uuid, array $params = []): Category @@ -55,6 +69,10 @@ public function get(string $uuid, array $params = []): Category } /** + * @param string $externalIdentifier + * + * @return Category + * * @throws PiggyRequestException */ public function find(string $externalIdentifier): Category @@ -69,6 +87,11 @@ public function find(string $externalIdentifier): Category } /** + * @param string $externalIdentifier + * @param string|null $name + * + * @return Category + * * @throws PiggyRequestException */ public function findOrCreate(string $externalIdentifier, ?string $name): Category @@ -84,6 +107,12 @@ public function findOrCreate(string $externalIdentifier, ?string $name): Categor } /** + * @param string $uuid + * @param string|null $externalIdentifier + * @param string|null $name + * + * @return Category + * * @throws PiggyRequestException */ public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category @@ -99,6 +128,11 @@ public function update(string $uuid, ?string $externalIdentifier, ?string $name) } /** + * @param string $uuid + * @param array $params + * + * @return mixed + * * @throws PiggyRequestException */ public function delete(string $uuid, array $params = []) @@ -109,6 +143,10 @@ public function delete(string $uuid, array $params = []) } /** + * @param array $categories + * + * @return array + * * @throws PiggyRequestException */ public function batch(array $categories) diff --git a/src/Resources/OAuth/Products/ProductsResource.php b/src/Resources/OAuth/Products/ProductsResource.php index b8b276a..0b69544 100644 --- a/src/Resources/OAuth/Products/ProductsResource.php +++ b/src/Resources/OAuth/Products/ProductsResource.php @@ -16,6 +16,10 @@ class ProductsResource extends BaseResource protected $resourceUri = '/api/v3/oauth/clients/products'; /** + * @param array $params + * + * @return Product[] + * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -28,6 +32,13 @@ public function list(array $params = []): array } /** + * @param string $externalIdentifier + * @param string $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product @@ -45,6 +56,11 @@ public function create(string $externalIdentifier, string $name, ?string $descri } /** + * @param string $uuid + * @param array $params + * + * @return Product + * * @throws PiggyRequestException */ public function get(string $uuid, array $params = []): Product @@ -57,6 +73,10 @@ public function get(string $uuid, array $params = []): Product } /** + * @param string $externalIdentifier + * + * @return Product + * * @throws PiggyRequestException */ public function find(string $externalIdentifier): Product @@ -71,6 +91,13 @@ public function find(string $externalIdentifier): Product } /** + * @param string $externalIdentifier + * @param string|null $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product @@ -88,6 +115,14 @@ public function findOrCreate(string $externalIdentifier, ?string $name, ?string } /** + * @param string $uuid + * @param string|null $externalIdentifier + * @param string|null $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product @@ -105,6 +140,11 @@ public function update(string $uuid, ?string $externalIdentifier, ?string $name, } /** + * @param string $uuid + * @param array $params + * + * @return mixed + * * @throws PiggyRequestException */ public function delete(string $uuid, array $params = []) @@ -115,6 +155,10 @@ public function delete(string $uuid, array $params = []) } /** + * @param array $products + * + * @return array + * * @throws PiggyRequestException */ public function batch(array $products) diff --git a/src/Resources/Register/Categories/CategoriesResource.php b/src/Resources/Register/Categories/CategoriesResource.php index a9cf93d..f8c304c 100644 --- a/src/Resources/Register/Categories/CategoriesResource.php +++ b/src/Resources/Register/Categories/CategoriesResource.php @@ -16,6 +16,10 @@ class CategoriesResource extends BaseResource protected $resourceUri = '/api/v3/register/categories'; /** + * @param array $params + * + * @return Category[] + * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -28,6 +32,11 @@ public function list(array $params = []): array } /** + * @param string $externalIdentifier + * @param string $name + * + * @return Category + * * @throws PiggyRequestException */ public function create(string $externalIdentifier, string $name): Category @@ -43,6 +52,11 @@ public function create(string $externalIdentifier, string $name): Category } /** + * @param string $uuid + * @param array $params + * + * @return Category + * * @throws PiggyRequestException */ public function get(string $uuid, array $params = []): Category @@ -55,6 +69,10 @@ public function get(string $uuid, array $params = []): Category } /** + * @param string $externalIdentifier + * + * @return Category + * * @throws PiggyRequestException */ public function find(string $externalIdentifier): Category @@ -69,6 +87,11 @@ public function find(string $externalIdentifier): Category } /** + * @param string $externalIdentifier + * @param string|null $name + * + * @return Category + * * @throws PiggyRequestException */ public function findOrCreate(string $externalIdentifier, ?string $name): Category @@ -84,6 +107,12 @@ public function findOrCreate(string $externalIdentifier, ?string $name): Categor } /** + * @param string $uuid + * @param string|null $externalIdentifier + * @param string|null $name + * + * @return Category + * * @throws PiggyRequestException */ public function update(string $uuid, ?string $externalIdentifier, ?string $name): Category @@ -99,6 +128,11 @@ public function update(string $uuid, ?string $externalIdentifier, ?string $name) } /** + * @param string $uuid + * @param array $params + * + * @return mixed + * * @throws PiggyRequestException */ public function delete(string $uuid, array $params = []) @@ -109,6 +143,10 @@ public function delete(string $uuid, array $params = []) } /** + * @param array $categories + * + * @return array + * * @throws PiggyRequestException */ public function batch(array $categories) diff --git a/src/Resources/Register/Products/ProductsResource.php b/src/Resources/Register/Products/ProductsResource.php index d465bb3..0c5ec72 100644 --- a/src/Resources/Register/Products/ProductsResource.php +++ b/src/Resources/Register/Products/ProductsResource.php @@ -16,6 +16,10 @@ class ProductsResource extends BaseResource protected $resourceUri = '/api/v3/register/products'; /** + * @param array $params + * + * @return Product[] + * * @throws PiggyRequestException */ public function list(array $params = []): array @@ -28,6 +32,13 @@ public function list(array $params = []): array } /** + * @param string $externalIdentifier + * @param string $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function create(string $externalIdentifier, string $name, ?string $description, ?array $categories): Product @@ -45,6 +56,11 @@ public function create(string $externalIdentifier, string $name, ?string $descri } /** + * @param string $uuid + * @param array $params + * + * @return Product + * * @throws PiggyRequestException */ public function get(string $uuid, array $params = []): Product @@ -57,6 +73,10 @@ public function get(string $uuid, array $params = []): Product } /** + * @param string $externalIdentifier + * + * @return Product + * * @throws PiggyRequestException */ public function find(string $externalIdentifier): Product @@ -71,6 +91,13 @@ public function find(string $externalIdentifier): Product } /** + * @param string $externalIdentifier + * @param string|null $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function findOrCreate(string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product @@ -88,6 +115,14 @@ public function findOrCreate(string $externalIdentifier, ?string $name, ?string } /** + * @param string $uuid + * @param string|null $externalIdentifier + * @param string|null $name + * @param string|null $description + * @param array>|null $categories + * + * @return Product + * * @throws PiggyRequestException */ public function update(string $uuid, ?string $externalIdentifier, ?string $name, ?string $description, ?array $categories): Product @@ -105,6 +140,11 @@ public function update(string $uuid, ?string $externalIdentifier, ?string $name, } /** + * @param string $uuid + * @param array $params + * + * @return mixed + * * @throws PiggyRequestException */ public function delete(string $uuid, array $params = []) @@ -115,6 +155,10 @@ public function delete(string $uuid, array $params = []) } /** + * @param array $products + * + * @return array + * * @throws PiggyRequestException */ public function batch(array $products) From 319ae3c3938e26c9c31f409869e12a93e06be7e8 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 16:00:50 +0200 Subject: [PATCH 10/30] wip --- src/Models/Categories/Category.php | 16 ++--- src/Models/Orders/Order.php | 72 +++++++++++++++++++++++ src/Models/Products/Product.php | 14 ++--- src/StaticMappers/Orders/OrderMapper.php | 24 ++++++++ src/StaticMappers/Orders/OrdersMapper.php | 25 ++++++++ 5 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 src/Models/Orders/Order.php create mode 100644 src/StaticMappers/Orders/OrderMapper.php create mode 100644 src/StaticMappers/Orders/OrdersMapper.php diff --git a/src/Models/Categories/Category.php b/src/Models/Categories/Category.php index 29d9e87..d2099d3 100644 --- a/src/Models/Categories/Category.php +++ b/src/Models/Categories/Category.php @@ -64,7 +64,7 @@ public function getName(): string * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function list(array $params = []): array + public static function list(array $params = []): array { $response = ApiClient::get(self::resourceUri, $params); @@ -78,7 +78,7 @@ public function list(array $params = []): array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function create(array $body): Category + public static function create(array $body): Category { $response = ApiClient::post(self::resourceUri, $body); @@ -93,7 +93,7 @@ public function create(array $body): Category * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function get(string $uuid, array $params = []): Category + public static function get(string $uuid, array $params = []): Category { $response = ApiClient::get(self::resourceUri."/$uuid", $params); @@ -107,7 +107,7 @@ public function get(string $uuid, array $params = []): Category * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function find(array $params): Category + public static function find(array $params): Category { $response = ApiClient::get(self::resourceUri."/find", $params); @@ -121,7 +121,7 @@ public function find(array $params): Category * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function findOrCreate(array $body): Category + public static function findOrCreate(array $body): Category { $response = ApiClient::post(self::resourceUri."/find-or-create", $body); @@ -136,7 +136,7 @@ public function findOrCreate(array $body): Category * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function update(string $uuid, array $body): Category + public static function update(string $uuid, array $body): Category { $response = ApiClient::put(self::resourceUri."/$uuid", $body); @@ -151,7 +151,7 @@ public function update(string $uuid, array $body): Category * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function delete(string $uuid, array $params = []): Response + public static function delete(string $uuid, array $params = []): Response { return ApiClient::delete(self::resourceUri."/$uuid", $params); } @@ -163,7 +163,7 @@ public function delete(string $uuid, array $params = []): Response * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function batch(array $body): array + public static function batch(array $body): array { $response = ApiClient::put(self::resourceUri."/batch", $body); diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php new file mode 100644 index 0000000..3f0c015 --- /dev/null +++ b/src/Models/Orders/Order.php @@ -0,0 +1,72 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->contact = $contact; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): string + { + return $this->externalIdentifier; + } + + public function getContact(): Contact + { + return $this->contact; + } + + /** + * @param array $params + * + * @return Order[] + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function list(array $params = []): array + { + $response = ApiClient::get(self::resourceUri, $params); + + return OrdersMapper::map($response->getData());z + } +} \ No newline at end of file diff --git a/src/Models/Products/Product.php b/src/Models/Products/Product.php index be976f3..2703bb5 100644 --- a/src/Models/Products/Product.php +++ b/src/Models/Products/Product.php @@ -93,7 +93,7 @@ public function getCategories(): ?array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function list(array $params = []): array + public static function list(array $params = []): array { $response = ApiClient::get(self::resourceUri, $params); @@ -107,7 +107,7 @@ public function list(array $params = []): array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function create(array $body): Product + public static function create(array $body): Product { $response = ApiClient::post(self::resourceUri, $body); @@ -122,7 +122,7 @@ public function create(array $body): Product * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function get(string $uuid, array $params = []): Product + public static function get(string $uuid, array $params = []): Product { $response = ApiClient::get(self::resourceUri."/$uuid", $params); @@ -136,7 +136,7 @@ public function get(string $uuid, array $params = []): Product * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function find(array $params): Product + public static function find(array $params): Product { $response = ApiClient::get(self::resourceUri."/find", $params); @@ -150,7 +150,7 @@ public function find(array $params): Product * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function findOrCreate(array $body): Product + public static function findOrCreate(array $body): Product { $response = ApiClient::post(self::resourceUri."/find-or-create", $body); @@ -180,7 +180,7 @@ public function update(string $uuid, array $body): Product * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function delete(string $uuid, array $params = []): Response + public static function delete(string $uuid, array $params = []): Response { return ApiClient::delete(self::resourceUri."/$uuid", $params); } @@ -192,7 +192,7 @@ public function delete(string $uuid, array $params = []): Response * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public function batch(array $body): array + public static function batch(array $body): array { $response = ApiClient::put(self::resourceUri."/batch", $body); diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php new file mode 100644 index 0000000..6d2f303 --- /dev/null +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -0,0 +1,24 @@ +contact)) { + $contact = ContactMapper::map($data->contact); + } + + return new Order( + $data->uuid, + $data->external_identifier, + $contact + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrdersMapper.php b/src/StaticMappers/Orders/OrdersMapper.php new file mode 100644 index 0000000..800e7a9 --- /dev/null +++ b/src/StaticMappers/Orders/OrdersMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $orders; + } +} \ No newline at end of file From f9f9890990acaa595b2657e2703cf517653b0192 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 16:45:31 +0200 Subject: [PATCH 11/30] wip --- src/Models/Orders/Order.php | 72 ++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 3f0c015..962254d 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -7,6 +7,7 @@ use Piggy\Api\Exceptions\MaintenanceModeException; use Piggy\Api\Exceptions\PiggyRequestException; use Piggy\Api\Models\Contacts\Contact; +use Piggy\Api\Models\Shops\Shop; use Piggy\Api\StaticMappers\Orders\OrdersMapper; class Order @@ -26,6 +27,75 @@ class Order */ protected $contact; + /** + * @var Shop + */ + protected $shop; + + /** + * @var string + */ + protected $currency; + + /** + * @var string|null + */ + protected $reference; + + /** + * @var string + */ + protected $status; + + /** + * @var string + */ + protected $paymentStatus; + + /** + * @var string + */ + protected $formattedTotalAmount; + + /** + * @var int|null + */ + protected $orderAmount; + + /** + * @var int + */ + protected $totalChargesAmount; + + /** + * @var int + */ + protected $totalDiscountAmount; + + /** + * @var int + */ + protected $totalOrderAmount; + + /** + * @var string|null + */ + protected $paidAt; + + // TODO: Add Line items + // TODO: Add Applied discounts + // TODO: Add Charges + + /** + * @var string|null + */ + protected $createdAt; + + /** + * @var string + */ + protected $updatedAt; + /** * @var string */ @@ -67,6 +137,6 @@ public static function list(array $params = []): array { $response = ApiClient::get(self::resourceUri, $params); - return OrdersMapper::map($response->getData());z + return OrdersMapper::map($response->getData()); } } \ No newline at end of file From faaf880b83480129173b94bb133ba2505b2bf930 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Thu, 10 Jul 2025 17:11:58 +0200 Subject: [PATCH 12/30] wip --- src/Models/Orders/Order.php | 99 +++++++++++++++++++++++- src/StaticMappers/Orders/OrderMapper.php | 21 ++++- 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 962254d..364fc37 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -55,7 +55,7 @@ class Order /** * @var string */ - protected $formattedTotalAmount; + protected $formattedTotalOrderAmount; /** * @var int|null @@ -82,8 +82,8 @@ class Order */ protected $paidAt; - // TODO: Add Line items - // TODO: Add Applied discounts + // TODO: Add Line Items + // TODO: Add Applied Discounts // TODO: Add Charges /** @@ -104,11 +104,37 @@ class Order public function __construct( string $uuid, string $externalIdentifier, - Contact $contact + Contact $contact, + Shop $shop, + string $currency, + ?string $reference, + string $status, + string $paymentStatus, + string $formattedTotalOrderAmount, + ?int $orderAmount, + int $totalChargesAmount, + int $totalDiscountAmount, + int $totalOrderAmount, + ?string $paidAt, + string $createdAt, + string $updatedAt ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; $this->contact = $contact; + $this->shop = $shop; + $this->currency = $currency; + $this->reference = $reference; + $this->status = $status; + $this->paymentStatus = $paymentStatus; + $this->formattedTotalOrderAmount = $formattedTotalOrderAmount; + $this->orderAmount = $orderAmount; + $this->totalChargesAmount = $totalChargesAmount; + $this->totalDiscountAmount = $totalDiscountAmount; + $this->totalOrderAmount = $totalOrderAmount; + $this->paidAt = $paidAt; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; } public function getUuid(): string @@ -126,6 +152,71 @@ public function getContact(): Contact return $this->contact; } + public function getShop(): Shop + { + return $this->shop; + } + + public function getCurrency(): string + { + return $this->currency; + } + + public function getReference(): ?string + { + return $this->reference; + } + + public function getStatus(): string + { + return $this->status; + } + + public function getPaymentStatus(): string + { + return $this->paymentStatus; + } + + public function getFormattedTotalOrderAmount(): string + { + return $this->formattedTotalOrderAmount; + } + + public function getOrderAmount(): ?int + { + return $this->orderAmount; + } + + public function getTotalChargesAmount(): int + { + return $this->totalChargesAmount; + } + + public function getTotalDiscountAmount(): int + { + return $this->totalDiscountAmount; + } + + public function getTotalOrderAmount(): int + { + return $this->totalOrderAmount; + } + + public function getPaidAt(): ?string + { + return $this->paidAt; + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getUpdatedAt(): string + { + return $this->updatedAt; + } + /** * @param array $params * diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 6d2f303..7eabafb 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -4,6 +4,7 @@ use Piggy\Api\Models\Orders\Order; use Piggy\Api\StaticMappers\Contacts\ContactMapper; +use Piggy\Api\StaticMappers\Shops\ShopMapper; use stdClass; class OrderMapper @@ -15,10 +16,28 @@ public static function map(stdClass $data): Order $contact = ContactMapper::map($data->contact); } + $shop = null; + if (isset($data->business_profile)) { + $shop = ShopMapper::map($data->business_profile); + } + return new Order( $data->uuid, $data->external_identifier, - $contact + $contact, + $shop, + $data->currency, + $data->reference ?? null, + $data->status, + $data->payment_status, + $data->formatted_total_order_amount, + isset($data->order_amount) ? (int) $data->order_amount : null, + (int) $data->total_charges_amount, + (int) $data->total_discount_amount, + (int) $data->total_order_amount, + $data->paid_at ?? null, + $data->created_at, + $data->updated_at ); } } \ No newline at end of file From 40b2c213916288130f30f24a7556d358cff86adb Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Tue, 15 Jul 2025 15:39:04 +0200 Subject: [PATCH 13/30] wip --- src/Models/Orders/LineItem.php | 144 +++++++++++++++++ src/Models/Orders/Order.php | 151 +++++++++++++++--- src/Models/Orders/SubLineItem.php | 144 +++++++++++++++++ src/StaticMappers/Orders/LineItemMapper.php | 32 ++++ src/StaticMappers/Orders/LineItemsMapper.php | 25 +++ src/StaticMappers/Orders/OrderMapper.php | 8 +- .../Orders/SubLineItemMapper.php | 37 +++++ .../Orders/SubLineItemsMapper.php | 25 +++ 8 files changed, 539 insertions(+), 27 deletions(-) create mode 100644 src/Models/Orders/LineItem.php create mode 100644 src/Models/Orders/SubLineItem.php create mode 100644 src/StaticMappers/Orders/LineItemMapper.php create mode 100644 src/StaticMappers/Orders/LineItemsMapper.php create mode 100644 src/StaticMappers/Orders/SubLineItemMapper.php create mode 100644 src/StaticMappers/Orders/SubLineItemsMapper.php diff --git a/src/Models/Orders/LineItem.php b/src/Models/Orders/LineItem.php new file mode 100644 index 0000000..a61353e --- /dev/null +++ b/src/Models/Orders/LineItem.php @@ -0,0 +1,144 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->name = $name; + $this->quantity = $quantity; + $this->price = $price; + $this->discountAmount = $discountAmount; + $this->totalAmount = $totalAmount; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + $this->product = $product; + $this->subLineItems = $subLineItems; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): ?string + { + return $this->externalIdentifier; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getPrice(): string + { + return $this->price; + } + + public function getDiscountAmount(): ?int + { + return $this->discountAmount; + } + + public function getTotalAmount(): int + { + return $this->totalAmount; + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getUpdatedAt(): string + { + return $this->updatedAt; + } + + public function getProduct(): ?Product + { + return $this->product; + } + + public function getSubLineItems(): array + { + return $this->subLineItems; + } +} \ No newline at end of file diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 364fc37..79ed9d2 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -8,7 +8,9 @@ use Piggy\Api\Exceptions\PiggyRequestException; use Piggy\Api\Models\Contacts\Contact; use Piggy\Api\Models\Shops\Shop; +use Piggy\Api\StaticMappers\Orders\OrderMapper; use Piggy\Api\StaticMappers\Orders\OrdersMapper; +use stdClass; class Order { @@ -22,16 +24,6 @@ class Order */ protected $externalIdentifier; - /** - * @var Contact - */ - protected $contact; - - /** - * @var Shop - */ - protected $shop; - /** * @var string */ @@ -82,7 +74,6 @@ class Order */ protected $paidAt; - // TODO: Add Line Items // TODO: Add Applied Discounts // TODO: Add Charges @@ -96,6 +87,21 @@ class Order */ protected $updatedAt; + /** + * @var Contact + */ + protected $contact; + + /** + * @var Shop + */ + protected $shop; + + /** + * @var LineItem[] $lineItems + */ + protected $lineItems; + /** * @var string */ @@ -104,8 +110,6 @@ class Order public function __construct( string $uuid, string $externalIdentifier, - Contact $contact, - Shop $shop, string $currency, ?string $reference, string $status, @@ -117,12 +121,13 @@ public function __construct( int $totalOrderAmount, ?string $paidAt, string $createdAt, - string $updatedAt + string $updatedAt, + Contact $contact, + Shop $shop, + array $lineItems ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; - $this->contact = $contact; - $this->shop = $shop; $this->currency = $currency; $this->reference = $reference; $this->status = $status; @@ -135,6 +140,9 @@ public function __construct( $this->paidAt = $paidAt; $this->createdAt = $createdAt; $this->updatedAt = $updatedAt; + $this->contact = $contact; + $this->shop = $shop; + $this->lineItems = $lineItems; } public function getUuid(): string @@ -147,16 +155,6 @@ public function getExternalIdentifier(): string return $this->externalIdentifier; } - public function getContact(): Contact - { - return $this->contact; - } - - public function getShop(): Shop - { - return $this->shop; - } - public function getCurrency(): string { return $this->currency; @@ -217,6 +215,21 @@ public function getUpdatedAt(): string return $this->updatedAt; } + public function getContact(): Contact + { + return $this->contact; + } + + public function getShop(): Shop + { + return $this->shop; + } + + public function getLineItems(): array + { + return $this->lineItems; + } + /** * @param array $params * @@ -230,4 +243,90 @@ public static function list(array $params = []): array return OrdersMapper::map($response->getData()); } + + /** + * @param string $uuid + * @param array $params + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function get(string $uuid, array $params = []): Order + { + $response = ApiClient::get(self::resourceUri."/$uuid", $params); + + return OrderMapper::map($response->getData()); + } + + /** + * @param array $params + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function find(array $params): Order + { + $response = ApiClient::get(self::resourceUri."/find", $params); + + return OrderMapper::map($response->getData()); + } + + /** + * @param array $body + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function create(array $body): Order + { + $response = ApiClient::post(self::resourceUri, $body); + + return OrderMapper::map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function process(string $uuid, array $body): Order + { + $response = ApiClient::post(self::resourceUri."$uuid/process", $body); + + return OrderMapper::map($response->getData()); + } + + /** + * @param array $body + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function createAndProcess(array $body): Order + { + $response = ApiClient::post(self::resourceUri."/create-and-process", $body); + + return OrderMapper::map($response->getData()); + } + + /** + * @param array $body + * + * @return stdClass + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function calculate(array $body): stdClass + { + $response = ApiClient::post(self::resourceUri."/calculate", $body); + + return $response->getData(); + } } \ No newline at end of file diff --git a/src/Models/Orders/SubLineItem.php b/src/Models/Orders/SubLineItem.php new file mode 100644 index 0000000..39a274b --- /dev/null +++ b/src/Models/Orders/SubLineItem.php @@ -0,0 +1,144 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->name = $name; + $this->quantity = $quantity; + $this->price = $price; + $this->discountAmount = $discountAmount; + $this->totalAmount = $totalAmount; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + $this->lineItem = $lineItem; + $this->product = $product; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): ?string + { + return $this->externalIdentifier; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getPrice(): string + { + return $this->price; + } + + public function getDiscountAmount(): ?int + { + return $this->discountAmount; + } + + public function getTotalAmount(): int + { + return $this->totalAmount; + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getUpdatedAt(): string + { + return $this->updatedAt; + } + + public function getLineItem(): LineItem + { + return $this->lineItem; + } + + public function getProduct(): ?Product + { + return $this->product; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/LineItemMapper.php b/src/StaticMappers/Orders/LineItemMapper.php new file mode 100644 index 0000000..c2fb53d --- /dev/null +++ b/src/StaticMappers/Orders/LineItemMapper.php @@ -0,0 +1,32 @@ +product)) { + $product = ProductMapper::map($data->product); + } + + return new LineItem( + $data->uuid, + $data->external_identifier, + $data->name, + $data->quantity, + $data->price, + $data->discount_amount, + $data->total_amount, + $data->created_at, + $data->updated_at, + $product, + $data->sub_line_items + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/LineItemsMapper.php b/src/StaticMappers/Orders/LineItemsMapper.php new file mode 100644 index 0000000..74cfb12 --- /dev/null +++ b/src/StaticMappers/Orders/LineItemsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $lineItems; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 7eabafb..2ab5541 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -21,6 +21,11 @@ public static function map(stdClass $data): Order $shop = ShopMapper::map($data->business_profile); } + $lineItems = []; + if (isset($data->line_items)) { + $lineItems = LineItemsMapper::map($data->line_items); + } + return new Order( $data->uuid, $data->external_identifier, @@ -37,7 +42,8 @@ public static function map(stdClass $data): Order (int) $data->total_order_amount, $data->paid_at ?? null, $data->created_at, - $data->updated_at + $data->updated_at, + $lineItems ); } } \ No newline at end of file diff --git a/src/StaticMappers/Orders/SubLineItemMapper.php b/src/StaticMappers/Orders/SubLineItemMapper.php new file mode 100644 index 0000000..c6d2ec4 --- /dev/null +++ b/src/StaticMappers/Orders/SubLineItemMapper.php @@ -0,0 +1,37 @@ +line_item)) { + $lineItem = LineItemMapper::map($data->line_item); + } + + $product = null; + if (isset($data->product)) { + $product = ProductMapper::map($data->product); + } + + return new SubLineItem( + $data->uuid, + $data->external_identifier, + $data->name, + $data->quantity, + $data->price, + $data->discount_amount, + $data->total_amount, + $data->created_at, + $data->updated_at, + $lineItem, + $product + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/SubLineItemsMapper.php b/src/StaticMappers/Orders/SubLineItemsMapper.php new file mode 100644 index 0000000..9ee72a6 --- /dev/null +++ b/src/StaticMappers/Orders/SubLineItemsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $subLineItems; + } +} \ No newline at end of file From 8375d9f39292dd87ed2a057e2ff30b2895f6d003 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Tue, 15 Jul 2025 15:45:59 +0200 Subject: [PATCH 14/30] wip --- src/StaticMappers/Orders/LineItemMapper.php | 4 ++-- src/StaticMappers/Orders/OrderMapper.php | 4 ++-- src/StaticMappers/Orders/SubLineItemMapper.php | 4 ++-- src/StaticMappers/Products/ProductMapper.php | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/StaticMappers/Orders/LineItemMapper.php b/src/StaticMappers/Orders/LineItemMapper.php index c2fb53d..37bdbaa 100644 --- a/src/StaticMappers/Orders/LineItemMapper.php +++ b/src/StaticMappers/Orders/LineItemMapper.php @@ -23,8 +23,8 @@ public static function map(stdClass $data): LineItem $data->price, $data->discount_amount, $data->total_amount, - $data->created_at, - $data->updated_at, + $data->created_at ?? '', + $data->updated_at ?? '', $product, $data->sub_line_items ); diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 2ab5541..2fe8405 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -29,8 +29,6 @@ public static function map(stdClass $data): Order return new Order( $data->uuid, $data->external_identifier, - $contact, - $shop, $data->currency, $data->reference ?? null, $data->status, @@ -43,6 +41,8 @@ public static function map(stdClass $data): Order $data->paid_at ?? null, $data->created_at, $data->updated_at, + $contact, + $shop, $lineItems ); } diff --git a/src/StaticMappers/Orders/SubLineItemMapper.php b/src/StaticMappers/Orders/SubLineItemMapper.php index c6d2ec4..56df501 100644 --- a/src/StaticMappers/Orders/SubLineItemMapper.php +++ b/src/StaticMappers/Orders/SubLineItemMapper.php @@ -28,8 +28,8 @@ public static function map(stdClass $data): SubLineItem $data->price, $data->discount_amount, $data->total_amount, - $data->created_at, - $data->updated_at, + $data->created_at ?? '', + $data->updated_at ?? '', $lineItem, $product ); diff --git a/src/StaticMappers/Products/ProductMapper.php b/src/StaticMappers/Products/ProductMapper.php index 6e8cb49..9ea9513 100644 --- a/src/StaticMappers/Products/ProductMapper.php +++ b/src/StaticMappers/Products/ProductMapper.php @@ -17,11 +17,11 @@ public static function map(stdClass $data): Product } return new Product( - $data->uuid, + $data->uuid ?? '', $data->external_identifier, $data->name, - $data->description, - $categories + $data->description ?? null, + $categories ?? null ); } } From 8b4acd57d0d3226cf22d4bc0aef8d9265514d6de Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Tue, 15 Jul 2025 16:20:18 +0200 Subject: [PATCH 15/30] wip --- src/Models/Orders/LineItem.php | 26 +++++++++++++++----------- src/Models/Orders/Order.php | 6 +++++- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Models/Orders/LineItem.php b/src/Models/Orders/LineItem.php index a61353e..4ac335b 100644 --- a/src/Models/Orders/LineItem.php +++ b/src/Models/Orders/LineItem.php @@ -7,60 +7,61 @@ class LineItem { /** - * @var string $uuid + * @var string */ protected $uuid; /** - * @var string|null $externalIdentifier + * @var string|null */ protected $externalIdentifier; /** - * @var string|null $name + * @var string|null */ protected $name; /** - * @var int $quantity + * @var int */ protected $quantity; /** - * @var string $price + * @var string */ protected $price; /** - * @var int|null $discountAmount + * @var int|null */ protected $discountAmount; /** - * @var int $totalAmount + * @var int */ protected $totalAmount; /** - * @var string $createdAt + * @var string */ protected $createdAt; /** - * @var string $updatedAt + * @var string */ protected $updatedAt; /** - * @var Product|null $product + * @var Product|null */ protected $product; /** - * @var SubLineItem[] $subLineItems + * @var SubLineItem[] */ protected $subLineItems; + /** @param SubLineItem[] $subLineItems */ public function __construct( string $uuid, ?string $externalIdentifier, @@ -137,6 +138,9 @@ public function getProduct(): ?Product return $this->product; } + /** + * @return array|SubLineItem[] + */ public function getSubLineItems(): array { return $this->subLineItems; diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 79ed9d2..da16a07 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -78,7 +78,7 @@ class Order // TODO: Add Charges /** - * @var string|null + * @var string */ protected $createdAt; @@ -107,6 +107,7 @@ class Order */ const resourceUri = '/api/v3/oauth/clients/orders'; + /** @param LineItem[] $lineItems */ public function __construct( string $uuid, string $externalIdentifier, @@ -225,6 +226,9 @@ public function getShop(): Shop return $this->shop; } + /** + * @return array|LineItem[] + */ public function getLineItems(): array { return $this->lineItems; From e59a22e0bf416ac5148c17815b42c2d560f2935e Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Tue, 15 Jul 2025 16:52:28 +0200 Subject: [PATCH 16/30] wip --- src/Models/Orders/AppliedDiscount.php | 128 +++++++++++++++++++++++ src/Models/Orders/Charge.php | 94 +++++++++++++++++ src/Models/Orders/Order.php | 35 ++++++- src/StaticMappers/Orders/OrderMapper.php | 4 +- 4 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 src/Models/Orders/AppliedDiscount.php create mode 100644 src/Models/Orders/Charge.php diff --git a/src/Models/Orders/AppliedDiscount.php b/src/Models/Orders/AppliedDiscount.php new file mode 100644 index 0000000..5d51f01 --- /dev/null +++ b/src/Models/Orders/AppliedDiscount.php @@ -0,0 +1,128 @@ + + */ + protected $lineItems; + + /** + * @var array + */ + protected $subLineItems; + + /** + * @param LineItem[] $lineItems + * @param SubLineItem[] $subLineItems + */ + public function __construct( + string $uuid, + ?string $externalIdentifier, + ?string $name, + int $amount, + string $type, + string $value, + string $appliedTo, + array $lineItems, + array $subLineItems + ) { + $this->uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->name = $name; + $this->amount = $amount; + $this->type = $type; + $this->value = $value; + $this->appliedTo = $appliedTo; + $this->lineItems = $lineItems; + $this->subLineItems = $subLineItems; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): ?string + { + return $this->externalIdentifier; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getAmount(): int + { + return $this->amount; + } + + public function getType(): string + { + return $this->type; + } + + public function getValue(): string + { + return $this->value; + } + + public function getAppliedTo(): string + { + return $this->appliedTo; + } + + /** + * @return array + */ + public function getLineItems(): array + { + return $this->lineItems; + } + + /** + * @return array + */ + public function getSubLineItems(): array + { + return $this->subLineItems; + } +} \ No newline at end of file diff --git a/src/Models/Orders/Charge.php b/src/Models/Orders/Charge.php new file mode 100644 index 0000000..df9f3de --- /dev/null +++ b/src/Models/Orders/Charge.php @@ -0,0 +1,94 @@ +uuid = $uuid; + $this->externalIdentifier = $externalIdentifier; + $this->type = $type; + $this->name = $name; + $this->amount = $amount; + $this->discountAmount = $discountAmount; + $this->totalAmount = $totalAmount; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getExternalIdentifier(): ?string + { + return $this->externalIdentifier; + } + + public function getType(): string + { + return $this->type; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getAmount(): int + { + return $this->amount; + } + + public function getDiscountAmount(): ?int + { + return $this->discountAmount; + } + + public function getTotalAmount(): int + { + return $this->totalAmount; + } +} \ No newline at end of file diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index da16a07..feec26e 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -74,9 +74,6 @@ class Order */ protected $paidAt; - // TODO: Add Applied Discounts - // TODO: Add Charges - /** * @var string */ @@ -102,6 +99,16 @@ class Order */ protected $lineItems; + /** + * @var AppliedDiscount[] $appliedDiscounts + */ + protected $appliedDiscounts = []; + + /** + * @var Charge[] $charges + */ + protected $charges = []; + /** * @var string */ @@ -125,7 +132,9 @@ public function __construct( string $updatedAt, Contact $contact, Shop $shop, - array $lineItems + array $lineItems, + array $appliedDiscounts = [], + array $charges = [] ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; @@ -144,6 +153,8 @@ public function __construct( $this->contact = $contact; $this->shop = $shop; $this->lineItems = $lineItems; + $this->appliedDiscounts = $appliedDiscounts; + $this->charges = $charges; } public function getUuid(): string @@ -234,6 +245,22 @@ public function getLineItems(): array return $this->lineItems; } + /** + * @return array|AppliedDiscount[] + */ + public function getAppliedDiscounts(): array + { + return $this->appliedDiscounts; + } + + /** + * @return array|Charge[] + */ + public function charges(): array + { + return $this->charges; + } + /** * @param array $params * diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 2fe8405..747e18e 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -43,7 +43,9 @@ public static function map(stdClass $data): Order $data->updated_at, $contact, $shop, - $lineItems + $lineItems, + $data->applied_discounts, + $data->charges ); } } \ No newline at end of file From a4307b48df8966814896d4438e20ec55369b65d9 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Tue, 15 Jul 2025 17:02:36 +0200 Subject: [PATCH 17/30] wip --- src/Models/Orders/AppliedDiscount.php | 4 ++-- src/Models/Orders/Order.php | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Models/Orders/AppliedDiscount.php b/src/Models/Orders/AppliedDiscount.php index 5d51f01..4022592 100644 --- a/src/Models/Orders/AppliedDiscount.php +++ b/src/Models/Orders/AppliedDiscount.php @@ -50,8 +50,8 @@ class AppliedDiscount protected $subLineItems; /** - * @param LineItem[] $lineItems - * @param SubLineItem[] $subLineItems + * @param array $lineItems + * @param array $subLineItems */ public function __construct( string $uuid, diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index feec26e..4b81cda 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -114,7 +114,11 @@ class Order */ const resourceUri = '/api/v3/oauth/clients/orders'; - /** @param LineItem[] $lineItems */ + /** + * @param LineItem[] $lineItems + * @param AppliedDiscount[] $appliedDiscounts + * @param Charge[] $charges + */ public function __construct( string $uuid, string $externalIdentifier, From 02ec9d99c570712e653525b4473cc7148a3e30be Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 10:52:40 +0200 Subject: [PATCH 18/30] wip --- src/Models/Orders/AppliedDiscount.php | 20 ++++++------- src/Models/Orders/LineItem.php | 6 ++-- src/Models/Orders/Order.php | 36 +++++++++++++----------- src/Models/Orders/SubLineItem.php | 22 +++++++-------- src/StaticMappers/Orders/OrderMapper.php | 4 +-- 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/Models/Orders/AppliedDiscount.php b/src/Models/Orders/AppliedDiscount.php index 4022592..78d7f4c 100644 --- a/src/Models/Orders/AppliedDiscount.php +++ b/src/Models/Orders/AppliedDiscount.php @@ -40,18 +40,18 @@ class AppliedDiscount protected $appliedTo; /** - * @var array + * @var array */ - protected $lineItems; + protected $lineItems = []; /** - * @var array + * @var array */ - protected $subLineItems; + protected $subLineItems = []; /** - * @param array $lineItems - * @param array $subLineItems + * @param array $lineItems + * @param array $subLineItems */ public function __construct( string $uuid, @@ -61,8 +61,8 @@ public function __construct( string $type, string $value, string $appliedTo, - array $lineItems, - array $subLineItems + array $lineItems = [], + array $subLineItems = [] ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; @@ -111,7 +111,7 @@ public function getAppliedTo(): string } /** - * @return array + * @return array */ public function getLineItems(): array { @@ -119,7 +119,7 @@ public function getLineItems(): array } /** - * @return array + * @return array */ public function getSubLineItems(): array { diff --git a/src/Models/Orders/LineItem.php b/src/Models/Orders/LineItem.php index 4ac335b..1cc10c6 100644 --- a/src/Models/Orders/LineItem.php +++ b/src/Models/Orders/LineItem.php @@ -59,7 +59,7 @@ class LineItem /** * @var SubLineItem[] */ - protected $subLineItems; + protected $subLineItems = []; /** @param SubLineItem[] $subLineItems */ public function __construct( @@ -73,7 +73,7 @@ public function __construct( string $createdAt, string $updatedAt, ?Product $product, - array $subLineItems + array $subLineItems = [] ) { $this->uuid = $uuid; $this->externalIdentifier = $externalIdentifier; @@ -139,7 +139,7 @@ public function getProduct(): ?Product } /** - * @return array|SubLineItem[] + * @return SubLineItem[] */ public function getSubLineItems(): array { diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 4b81cda..0a71156 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -10,7 +10,6 @@ use Piggy\Api\Models\Shops\Shop; use Piggy\Api\StaticMappers\Orders\OrderMapper; use Piggy\Api\StaticMappers\Orders\OrdersMapper; -use stdClass; class Order { @@ -95,17 +94,17 @@ class Order protected $shop; /** - * @var LineItem[] $lineItems + * @var LineItem[] */ - protected $lineItems; + protected $lineItems = []; /** - * @var AppliedDiscount[] $appliedDiscounts + * @var AppliedDiscount[] */ protected $appliedDiscounts = []; /** - * @var Charge[] $charges + * @var Charge[] */ protected $charges = []; @@ -136,7 +135,7 @@ public function __construct( string $updatedAt, Contact $contact, Shop $shop, - array $lineItems, + array $lineItems = [], array $appliedDiscounts = [], array $charges = [] ) { @@ -242,7 +241,7 @@ public function getShop(): Shop } /** - * @return array|LineItem[] + * @return LineItem[] */ public function getLineItems(): array { @@ -250,7 +249,7 @@ public function getLineItems(): array } /** - * @return array|AppliedDiscount[] + * @return AppliedDiscount[] */ public function getAppliedDiscounts(): array { @@ -258,7 +257,7 @@ public function getAppliedDiscounts(): array } /** - * @return array|Charge[] + * @return Charge[] */ public function charges(): array { @@ -326,39 +325,42 @@ public static function create(array $body): Order * @param string $uuid * @param array $body * - * @return Order + * @return array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function process(string $uuid, array $body): Order + public static function process(string $uuid, array $body): array { $response = ApiClient::post(self::resourceUri."$uuid/process", $body); - return OrderMapper::map($response->getData()); + return $response->getData(); } /** * @param array $body * - * @return Order + * @return array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function createAndProcess(array $body): Order + public static function createAndProcess(array $body): array { $response = ApiClient::post(self::resourceUri."/create-and-process", $body); - return OrderMapper::map($response->getData()); + return [ + 'order' => OrderMapper::map($response->getData()->order), + 'result' => $response->getData()->result, + ]; } /** * @param array $body * - * @return stdClass + * @return array * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function calculate(array $body): stdClass + public static function calculate(array $body): array { $response = ApiClient::post(self::resourceUri."/calculate", $body); diff --git a/src/Models/Orders/SubLineItem.php b/src/Models/Orders/SubLineItem.php index 39a274b..8648894 100644 --- a/src/Models/Orders/SubLineItem.php +++ b/src/Models/Orders/SubLineItem.php @@ -7,57 +7,57 @@ class SubLineItem { /** - * @var string $uuid + * @var string */ protected $uuid; /** - * @var string|null $externalIdentifier + * @var string|null */ protected $externalIdentifier; /** - * @var string|null $name + * @var string|null */ protected $name; /** - * @var int $quantity + * @var int */ protected $quantity; /** - * @var string $price + * @var string */ protected $price; /** - * @var int|null $discountAmount + * @var int|null */ protected $discountAmount; /** - * @var int $totalAmount + * @var int */ protected $totalAmount; /** - * @var string $createdAt + * @var string */ protected $createdAt; /** - * @var string $updatedAt + * @var string */ protected $updatedAt; /** - * @var LineItem $lineItem + * @var LineItem */ protected $lineItem; /** - * @var Product|null $product + * @var Product|null */ protected $product; diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 747e18e..18a9d9a 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -39,8 +39,8 @@ public static function map(stdClass $data): Order (int) $data->total_discount_amount, (int) $data->total_order_amount, $data->paid_at ?? null, - $data->created_at, - $data->updated_at, + $data->created_at ?? '', + $data->updated_at ?? '', $contact, $shop, $lineItems, From 4d33c7ad240b89a20d2309dc725dbe7ecc9fbb5a Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 10:56:56 +0200 Subject: [PATCH 19/30] wip --- src/Models/Orders/LineItem.php | 24 ----------------- src/Models/Orders/SubLineItem.php | 26 +------------------ src/StaticMappers/Orders/LineItemMapper.php | 9 ++++--- src/StaticMappers/Orders/OrderMapper.php | 4 +-- .../Orders/SubLineItemMapper.php | 2 -- 5 files changed, 9 insertions(+), 56 deletions(-) diff --git a/src/Models/Orders/LineItem.php b/src/Models/Orders/LineItem.php index 1cc10c6..ad6a937 100644 --- a/src/Models/Orders/LineItem.php +++ b/src/Models/Orders/LineItem.php @@ -41,16 +41,6 @@ class LineItem */ protected $totalAmount; - /** - * @var string - */ - protected $createdAt; - - /** - * @var string - */ - protected $updatedAt; - /** * @var Product|null */ @@ -70,8 +60,6 @@ public function __construct( string $price, ?int $discountAmount, int $totalAmount, - string $createdAt, - string $updatedAt, ?Product $product, array $subLineItems = [] ) { @@ -82,8 +70,6 @@ public function __construct( $this->price = $price; $this->discountAmount = $discountAmount; $this->totalAmount = $totalAmount; - $this->createdAt = $createdAt; - $this->updatedAt = $updatedAt; $this->product = $product; $this->subLineItems = $subLineItems; } @@ -123,16 +109,6 @@ public function getTotalAmount(): int return $this->totalAmount; } - public function getCreatedAt(): string - { - return $this->createdAt; - } - - public function getUpdatedAt(): string - { - return $this->updatedAt; - } - public function getProduct(): ?Product { return $this->product; diff --git a/src/Models/Orders/SubLineItem.php b/src/Models/Orders/SubLineItem.php index 8648894..5bb0566 100644 --- a/src/Models/Orders/SubLineItem.php +++ b/src/Models/Orders/SubLineItem.php @@ -41,16 +41,6 @@ class SubLineItem */ protected $totalAmount; - /** - * @var string - */ - protected $createdAt; - - /** - * @var string - */ - protected $updatedAt; - /** * @var LineItem */ @@ -69,8 +59,6 @@ public function __construct( string $price, ?int $discountAmount, int $totalAmount, - string $createdAt, - string $updatedAt, LineItem $lineItem, ?Product $product ) { @@ -81,8 +69,6 @@ public function __construct( $this->price = $price; $this->discountAmount = $discountAmount; $this->totalAmount = $totalAmount; - $this->createdAt = $createdAt; - $this->updatedAt = $updatedAt; $this->lineItem = $lineItem; $this->product = $product; } @@ -121,17 +107,7 @@ public function getTotalAmount(): int { return $this->totalAmount; } - - public function getCreatedAt(): string - { - return $this->createdAt; - } - - public function getUpdatedAt(): string - { - return $this->updatedAt; - } - + public function getLineItem(): LineItem { return $this->lineItem; diff --git a/src/StaticMappers/Orders/LineItemMapper.php b/src/StaticMappers/Orders/LineItemMapper.php index 37bdbaa..6688b26 100644 --- a/src/StaticMappers/Orders/LineItemMapper.php +++ b/src/StaticMappers/Orders/LineItemMapper.php @@ -15,6 +15,11 @@ public static function map(stdClass $data): LineItem $product = ProductMapper::map($data->product); } + $subLineItems = null; + if (isset($data->sub_line_items)) { + $subLineItems = SubLineItemsMapper::map($data->sub_line_items); + } + return new LineItem( $data->uuid, $data->external_identifier, @@ -23,10 +28,8 @@ public static function map(stdClass $data): LineItem $data->price, $data->discount_amount, $data->total_amount, - $data->created_at ?? '', - $data->updated_at ?? '', $product, - $data->sub_line_items + $subLineItems ); } } \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 18a9d9a..747e18e 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -39,8 +39,8 @@ public static function map(stdClass $data): Order (int) $data->total_discount_amount, (int) $data->total_order_amount, $data->paid_at ?? null, - $data->created_at ?? '', - $data->updated_at ?? '', + $data->created_at, + $data->updated_at, $contact, $shop, $lineItems, diff --git a/src/StaticMappers/Orders/SubLineItemMapper.php b/src/StaticMappers/Orders/SubLineItemMapper.php index 56df501..e382db7 100644 --- a/src/StaticMappers/Orders/SubLineItemMapper.php +++ b/src/StaticMappers/Orders/SubLineItemMapper.php @@ -28,8 +28,6 @@ public static function map(stdClass $data): SubLineItem $data->price, $data->discount_amount, $data->total_amount, - $data->created_at ?? '', - $data->updated_at ?? '', $lineItem, $product ); From bc8a2ca7075c5b7a5cdf79519a00d7715be23b97 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 11:01:27 +0200 Subject: [PATCH 20/30] add mappers --- src/Mappers/Orders/LineItemMapper.php | 37 ++++++++++++++++ src/Mappers/Orders/LineItemsMapper.php | 25 +++++++++++ src/Mappers/Orders/OrderMapper.php | 54 +++++++++++++++++++++++ src/Mappers/Orders/OrdersMapper.php | 25 +++++++++++ src/Mappers/Orders/SubLineItemMapper.php | 37 ++++++++++++++++ src/Mappers/Orders/SubLineItemsMapper.php | 25 +++++++++++ 6 files changed, 203 insertions(+) create mode 100644 src/Mappers/Orders/LineItemMapper.php create mode 100644 src/Mappers/Orders/LineItemsMapper.php create mode 100644 src/Mappers/Orders/OrderMapper.php create mode 100644 src/Mappers/Orders/OrdersMapper.php create mode 100644 src/Mappers/Orders/SubLineItemMapper.php create mode 100644 src/Mappers/Orders/SubLineItemsMapper.php diff --git a/src/Mappers/Orders/LineItemMapper.php b/src/Mappers/Orders/LineItemMapper.php new file mode 100644 index 0000000..6cc11e4 --- /dev/null +++ b/src/Mappers/Orders/LineItemMapper.php @@ -0,0 +1,37 @@ +product)) { + $mapper = new ProductMapper(); + $product = $mapper->map($data->product); + } + + $subLineItems = null; + if (isset($data->sub_line_items)) { + $mapper = new SubLineItemsMapper(); + $subLineItems = $mapper->map($data->sub_line_items); + } + + return new LineItem( + $data->uuid, + $data->external_identifier, + $data->name, + $data->quantity, + $data->price, + $data->discount_amount, + $data->total_amount, + $product, + $subLineItems + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/LineItemsMapper.php b/src/Mappers/Orders/LineItemsMapper.php new file mode 100644 index 0000000..b6ee7f8 --- /dev/null +++ b/src/Mappers/Orders/LineItemsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $lineItems; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/OrderMapper.php b/src/Mappers/Orders/OrderMapper.php new file mode 100644 index 0000000..da6e4b6 --- /dev/null +++ b/src/Mappers/Orders/OrderMapper.php @@ -0,0 +1,54 @@ +contact)) { + $mapper = new ContactMapper(); + $contact = $mapper->map($data->contact); + } + + $shop = null; + if (isset($data->business_profile)) { + $mapper = new ShopMapper(); + $shop = $mapper->map($data->business_profile); + } + + $lineItems = []; + if (isset($data->line_items)) { + $mapper = new LineItemMapper(); + $lineItems = $mapper->map($data->line_items); + } + + return new Order( + $data->uuid, + $data->external_identifier, + $data->currency, + $data->reference ?? null, + $data->status, + $data->payment_status, + $data->formatted_total_order_amount, + isset($data->order_amount) ? (int) $data->order_amount : null, + (int) $data->total_charges_amount, + (int) $data->total_discount_amount, + (int) $data->total_order_amount, + $data->paid_at ?? null, + $data->created_at, + $data->updated_at, + $contact, + $shop, + $lineItems, + $data->applied_discounts, + $data->charges + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/OrdersMapper.php b/src/Mappers/Orders/OrdersMapper.php new file mode 100644 index 0000000..a256d15 --- /dev/null +++ b/src/Mappers/Orders/OrdersMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $orders; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/SubLineItemMapper.php b/src/Mappers/Orders/SubLineItemMapper.php new file mode 100644 index 0000000..a2afc48 --- /dev/null +++ b/src/Mappers/Orders/SubLineItemMapper.php @@ -0,0 +1,37 @@ +line_item)) { + $mapper = new LineItemMapper(); + $lineItem = $mapper->map($data->line_item); + } + + $product = null; + if (isset($data->product)) { + $mapper = new ProductMapper(); + $product = $mapper->map($data->product); + } + + return new SubLineItem( + $data->uuid, + $data->external_identifier, + $data->name, + $data->quantity, + $data->price, + $data->discount_amount, + $data->total_amount, + $lineItem, + $product + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/SubLineItemsMapper.php b/src/Mappers/Orders/SubLineItemsMapper.php new file mode 100644 index 0000000..fe3639d --- /dev/null +++ b/src/Mappers/Orders/SubLineItemsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $subLineItems; + } +} \ No newline at end of file From 5857210ed4f61f4ecf93bc7b960d911fa1ff52cc Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 12:29:28 +0200 Subject: [PATCH 21/30] wip --- src/Http/Traits/SetsOAuthResources.php | 7 + src/Resources/OAuth/Orders/OrdersResource.php | 130 ++++++++++++++++++ .../Register/Orders/OrdersResource.php | 130 ++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 src/Resources/OAuth/Orders/OrdersResource.php create mode 100644 src/Resources/Register/Orders/OrdersResource.php diff --git a/src/Http/Traits/SetsOAuthResources.php b/src/Http/Traits/SetsOAuthResources.php index 3f55506..7d336ee 100644 --- a/src/Http/Traits/SetsOAuthResources.php +++ b/src/Http/Traits/SetsOAuthResources.php @@ -26,6 +26,7 @@ use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardAttributesResource; use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardsResource; use Piggy\Api\Resources\OAuth\Loyalty\Tokens\LoyaltyTokensResource; +use Piggy\Api\Resources\OAuth\Orders\OrdersResource; use Piggy\Api\Resources\OAuth\Perks\PerksResource; use Piggy\Api\Resources\OAuth\PortalSessions\PortalSessionsResource; use Piggy\Api\Resources\OAuth\PrepaidTransactionsResource; @@ -215,6 +216,11 @@ trait SetsOAuthResources */ public $categories; + /** + * @var OrdersResource + */ + public $orders; + protected function setResources(BaseClient $client): void { $this->contacts = new ContactsResource($client); @@ -252,5 +258,6 @@ protected function setResources(BaseClient $client): void $this->customAttributes = new CustomAttributeResource($client); $this->products = new ProductsResource($client); $this->categories = new CategoriesResource($client); + $this->orders = new OrdersResource($client); } } diff --git a/src/Resources/OAuth/Orders/OrdersResource.php b/src/Resources/OAuth/Orders/OrdersResource.php new file mode 100644 index 0000000..5b61caf --- /dev/null +++ b/src/Resources/OAuth/Orders/OrdersResource.php @@ -0,0 +1,130 @@ + $params + * + * @return Order[] + * + * @throws PiggyRequestException + */ + public function list(array $params = []): array + { + $response = $this->client->get($this->resourceUri, $params); + + $mapper = new OrdersMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param string $uuid + * @param array $params + * + * @return Order + * + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Order + { + $response = $this->client->get($this->resourceUri."/$uuid", $params); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param array $params + * + * @return Order + * + * @throws PiggyRequestException + */ + public function find(array $params): Order + { + $response = $this->client->get($this->resourceUri."/find", $params); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param array $body + * + * @return Order + * + * @throws PiggyRequestException + */ + public function create(array $body): Order + { + $response = $this->client->post($this->resourceUri, $body); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function process(string $uuid, array $body): array + { + $response = $this->client->post($this->resourceUri."$uuid/process", $body); + + return $response->getData(); + } + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function createAndProcess(array $body): array + { + $response = $this->client->post($this->resourceUri."/create-and-process", $body); + + $mapper = new OrderMapper(); + + return [ + 'order' => $mapper->map($response->getData()), + 'result' => $response->getData()->result, + ]; + } + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function calculate(array $body): array + { + $response = $this->client->post($this->resourceUri."/calculate", $body); + + return $response->getData(); + } +} \ No newline at end of file diff --git a/src/Resources/Register/Orders/OrdersResource.php b/src/Resources/Register/Orders/OrdersResource.php new file mode 100644 index 0000000..b0949e2 --- /dev/null +++ b/src/Resources/Register/Orders/OrdersResource.php @@ -0,0 +1,130 @@ + $params + * + * @return Order[] + * + * @throws PiggyRequestException + */ + public function list(array $params = []): array + { + $response = $this->client->get($this->resourceUri, $params); + + $mapper = new OrdersMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param string $uuid + * @param array $params + * + * @return Order + * + * @throws PiggyRequestException + */ + public function get(string $uuid, array $params = []): Order + { + $response = $this->client->get($this->resourceUri."/$uuid", $params); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param array $params + * + * @return Order + * + * @throws PiggyRequestException + */ + public function find(array $params): Order + { + $response = $this->client->get($this->resourceUri."/find", $params); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param array $body + * + * @return Order + * + * @throws PiggyRequestException + */ + public function create(array $body): Order + { + $response = $this->client->post($this->resourceUri, $body); + + $mapper = new OrderMapper(); + + return $mapper->map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function process(string $uuid, array $body): array + { + $response = $this->client->post($this->resourceUri."$uuid/process", $body); + + return $response->getData(); + } + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function createAndProcess(array $body): array + { + $response = $this->client->post($this->resourceUri."/create-and-process", $body); + + $mapper = new OrderMapper(); + + return [ + 'order' => $mapper->map($response->getData()), + 'result' => $response->getData()->result, + ]; + } + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function calculate(array $body): array + { + $response = $this->client->post($this->resourceUri."/calculate", $body); + + return $response->getData(); + } +} \ No newline at end of file From 92974209177f6d15ceac2bbe390b59061679a71f Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 12:47:56 +0200 Subject: [PATCH 22/30] wip --- src/Mappers/Orders/LineItemMapper.php | 2 +- src/Mappers/Orders/OrderMapper.php | 2 +- src/Mappers/Products/ProductMapper.php | 6 +-- src/Models/Orders/Order.php | 12 ++--- src/Models/Orders/SubLineItem.php | 6 +-- tests/OAuth/Orders/OrdersResourceTest.php | 64 +++++++++++++++++++++++ 6 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 tests/OAuth/Orders/OrdersResourceTest.php diff --git a/src/Mappers/Orders/LineItemMapper.php b/src/Mappers/Orders/LineItemMapper.php index 6cc11e4..dc8fe8f 100644 --- a/src/Mappers/Orders/LineItemMapper.php +++ b/src/Mappers/Orders/LineItemMapper.php @@ -16,7 +16,7 @@ public static function map(stdClass $data): LineItem $product = $mapper->map($data->product); } - $subLineItems = null; + $subLineItems = []; if (isset($data->sub_line_items)) { $mapper = new SubLineItemsMapper(); $subLineItems = $mapper->map($data->sub_line_items); diff --git a/src/Mappers/Orders/OrderMapper.php b/src/Mappers/Orders/OrderMapper.php index da6e4b6..f2414fc 100644 --- a/src/Mappers/Orders/OrderMapper.php +++ b/src/Mappers/Orders/OrderMapper.php @@ -25,7 +25,7 @@ public function map(stdClass $data): Order $lineItems = []; if (isset($data->line_items)) { - $mapper = new LineItemMapper(); + $mapper = new LineItemsMapper(); $lineItems = $mapper->map($data->line_items); } diff --git a/src/Mappers/Products/ProductMapper.php b/src/Mappers/Products/ProductMapper.php index ae0bf45..b9d7ba6 100644 --- a/src/Mappers/Products/ProductMapper.php +++ b/src/Mappers/Products/ProductMapper.php @@ -17,11 +17,11 @@ public function map(stdClass $data): Product } return new Product( - $data->uuid, + $data->uuid ?? '', $data->external_identifier, $data->name, - $data->description, - $categories + $data->description ?? null, + $categories ?? null ); } } diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 0a71156..162310e 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -84,12 +84,12 @@ class Order protected $updatedAt; /** - * @var Contact + * @var Contact|null */ protected $contact; /** - * @var Shop + * @var Shop|null */ protected $shop; @@ -133,8 +133,8 @@ public function __construct( ?string $paidAt, string $createdAt, string $updatedAt, - Contact $contact, - Shop $shop, + ?Contact $contact, + ?Shop $shop, array $lineItems = [], array $appliedDiscounts = [], array $charges = [] @@ -230,12 +230,12 @@ public function getUpdatedAt(): string return $this->updatedAt; } - public function getContact(): Contact + public function getContact(): ?Contact { return $this->contact; } - public function getShop(): Shop + public function getShop(): ?Shop { return $this->shop; } diff --git a/src/Models/Orders/SubLineItem.php b/src/Models/Orders/SubLineItem.php index 5bb0566..8872e15 100644 --- a/src/Models/Orders/SubLineItem.php +++ b/src/Models/Orders/SubLineItem.php @@ -42,7 +42,7 @@ class SubLineItem protected $totalAmount; /** - * @var LineItem + * @var LineItem|null */ protected $lineItem; @@ -59,7 +59,7 @@ public function __construct( string $price, ?int $discountAmount, int $totalAmount, - LineItem $lineItem, + ?LineItem $lineItem, ?Product $product ) { $this->uuid = $uuid; @@ -108,7 +108,7 @@ public function getTotalAmount(): int return $this->totalAmount; } - public function getLineItem(): LineItem + public function getLineItem(): ?LineItem { return $this->lineItem; } diff --git a/tests/OAuth/Orders/OrdersResourceTest.php b/tests/OAuth/Orders/OrdersResourceTest.php new file mode 100644 index 0000000..c49c641 --- /dev/null +++ b/tests/OAuth/Orders/OrdersResourceTest.php @@ -0,0 +1,64 @@ + Date: Wed, 16 Jul 2025 14:58:10 +0200 Subject: [PATCH 23/30] wip --- src/Mappers/Orders/AppliedDiscountMapper.php | 24 + src/Mappers/Orders/AppliedDiscountsMapper.php | 25 + src/Mappers/Orders/ChargeMapper.php | 22 + src/Mappers/Orders/ChargesMapper.php | 25 + src/Mappers/Orders/OrderMapper.php | 17 +- src/Models/Orders/Order.php | 23 +- src/Resources/OAuth/Orders/OrdersResource.php | 11 +- .../Register/Orders/OrdersResource.php | 9 +- src/StaticMappers/Orders/OrderMapper.php | 1 + tests/OAuth/Orders/OrdersResourceTest.php | 1053 ++++++++++++++++- 10 files changed, 1187 insertions(+), 23 deletions(-) create mode 100644 src/Mappers/Orders/AppliedDiscountMapper.php create mode 100644 src/Mappers/Orders/AppliedDiscountsMapper.php create mode 100644 src/Mappers/Orders/ChargeMapper.php create mode 100644 src/Mappers/Orders/ChargesMapper.php diff --git a/src/Mappers/Orders/AppliedDiscountMapper.php b/src/Mappers/Orders/AppliedDiscountMapper.php new file mode 100644 index 0000000..fc068d9 --- /dev/null +++ b/src/Mappers/Orders/AppliedDiscountMapper.php @@ -0,0 +1,24 @@ +uuid, + $data->external_identifier, + $data->name, + $data->amount, + $data->type, + $data->value, + $data->applied_to, + $data->line_items, + $data->sub_line_items + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/AppliedDiscountsMapper.php b/src/Mappers/Orders/AppliedDiscountsMapper.php new file mode 100644 index 0000000..ad66540 --- /dev/null +++ b/src/Mappers/Orders/AppliedDiscountsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $appliedDiscounts; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/ChargeMapper.php b/src/Mappers/Orders/ChargeMapper.php new file mode 100644 index 0000000..4185858 --- /dev/null +++ b/src/Mappers/Orders/ChargeMapper.php @@ -0,0 +1,22 @@ +uuid, + $data->external_identifier, + $data->type, + $data->name, + $data->amount, + $data->discount_amount, + $data->total_amount + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/ChargesMapper.php b/src/Mappers/Orders/ChargesMapper.php new file mode 100644 index 0000000..bf1f768 --- /dev/null +++ b/src/Mappers/Orders/ChargesMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $charges; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/OrderMapper.php b/src/Mappers/Orders/OrderMapper.php index f2414fc..db6f181 100644 --- a/src/Mappers/Orders/OrderMapper.php +++ b/src/Mappers/Orders/OrderMapper.php @@ -29,6 +29,18 @@ public function map(stdClass $data): Order $lineItems = $mapper->map($data->line_items); } + $appliedDiscounts = []; + if (isset($data->applied_discounts)) { + $mapper = new AppliedDiscountsMapper(); + $appliedDiscounts = $mapper->map($data->applied_discounts); + } + + $charges = []; + if (isset($data->charges)) { + $mapper = new ChargesMapper(); + $charges = $mapper->map($data->charges); + } + return new Order( $data->uuid, $data->external_identifier, @@ -42,13 +54,14 @@ public function map(stdClass $data): Order (int) $data->total_discount_amount, (int) $data->total_order_amount, $data->paid_at ?? null, + $data->completed_at ?? null, $data->created_at, $data->updated_at, $contact, $shop, $lineItems, - $data->applied_discounts, - $data->charges + $appliedDiscounts, // $data->applied_discounts, + $charges // $data->charges ); } } \ No newline at end of file diff --git a/src/Models/Orders/Order.php b/src/Models/Orders/Order.php index 162310e..40213a1 100644 --- a/src/Models/Orders/Order.php +++ b/src/Models/Orders/Order.php @@ -10,6 +10,7 @@ use Piggy\Api\Models\Shops\Shop; use Piggy\Api\StaticMappers\Orders\OrderMapper; use Piggy\Api\StaticMappers\Orders\OrdersMapper; +use stdClass; class Order { @@ -73,6 +74,11 @@ class Order */ protected $paidAt; + /** + * @var string|null + */ + protected $completedAt; + /** * @var string */ @@ -131,6 +137,7 @@ public function __construct( int $totalDiscountAmount, int $totalOrderAmount, ?string $paidAt, + ?string $completedAt, string $createdAt, string $updatedAt, ?Contact $contact, @@ -151,6 +158,7 @@ public function __construct( $this->totalDiscountAmount = $totalDiscountAmount; $this->totalOrderAmount = $totalOrderAmount; $this->paidAt = $paidAt; + $this->completedAt = $completedAt; $this->createdAt = $createdAt; $this->updatedAt = $updatedAt; $this->contact = $contact; @@ -220,6 +228,11 @@ public function getPaidAt(): ?string return $this->paidAt; } + public function getCompletedAt(): ?string + { + return $this->completedAt; + } + public function getCreatedAt(): string { return $this->createdAt; @@ -259,7 +272,7 @@ public function getAppliedDiscounts(): array /** * @return Charge[] */ - public function charges(): array + public function getCharges(): array { return $this->charges; } @@ -325,11 +338,11 @@ public static function create(array $body): Order * @param string $uuid * @param array $body * - * @return array + * @return stdClass * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function process(string $uuid, array $body): array + public static function process(string $uuid, array $body = []): stdClass { $response = ApiClient::post(self::resourceUri."$uuid/process", $body); @@ -356,11 +369,11 @@ public static function createAndProcess(array $body): array /** * @param array $body * - * @return array + * @return stdClass * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function calculate(array $body): array + public static function calculate(array $body): stdClass { $response = ApiClient::post(self::resourceUri."/calculate", $body); diff --git a/src/Resources/OAuth/Orders/OrdersResource.php b/src/Resources/OAuth/Orders/OrdersResource.php index 5b61caf..58ad8c8 100644 --- a/src/Resources/OAuth/Orders/OrdersResource.php +++ b/src/Resources/OAuth/Orders/OrdersResource.php @@ -7,6 +7,7 @@ use Piggy\Api\Mappers\Orders\OrdersMapper; use Piggy\Api\Models\Orders\Order; use Piggy\Api\Resources\BaseResource; +use stdClass; class OrdersResource extends BaseResource { @@ -84,11 +85,11 @@ public function create(array $body): Order * @param string $uuid * @param array $body * - * @return array + * @return stdClass * * @throws PiggyRequestException */ - public function process(string $uuid, array $body): array + public function process(string $uuid, array $body = []): stdClass { $response = $this->client->post($this->resourceUri."$uuid/process", $body); @@ -109,7 +110,7 @@ public function createAndProcess(array $body): array $mapper = new OrderMapper(); return [ - 'order' => $mapper->map($response->getData()), + 'order' => $mapper->map($response->getData()->order), 'result' => $response->getData()->result, ]; } @@ -117,11 +118,11 @@ public function createAndProcess(array $body): array /** * @param array $body * - * @return array + * @return stdClass * * @throws PiggyRequestException */ - public function calculate(array $body): array + public function calculate(array $body): stdClass { $response = $this->client->post($this->resourceUri."/calculate", $body); diff --git a/src/Resources/Register/Orders/OrdersResource.php b/src/Resources/Register/Orders/OrdersResource.php index b0949e2..8c3dda6 100644 --- a/src/Resources/Register/Orders/OrdersResource.php +++ b/src/Resources/Register/Orders/OrdersResource.php @@ -7,6 +7,7 @@ use Piggy\Api\Mappers\Orders\OrdersMapper; use Piggy\Api\Models\Orders\Order; use Piggy\Api\Resources\BaseResource; +use stdClass; class OrdersResource extends BaseResource { @@ -84,11 +85,11 @@ public function create(array $body): Order * @param string $uuid * @param array $body * - * @return array + * @return stdClass * * @throws PiggyRequestException */ - public function process(string $uuid, array $body): array + public function process(string $uuid, array $body = []): stdClass { $response = $this->client->post($this->resourceUri."$uuid/process", $body); @@ -117,11 +118,11 @@ public function createAndProcess(array $body): array /** * @param array $body * - * @return array + * @return stdClass * * @throws PiggyRequestException */ - public function calculate(array $body): array + public function calculate(array $body): stdClass { $response = $this->client->post($this->resourceUri."/calculate", $body); diff --git a/src/StaticMappers/Orders/OrderMapper.php b/src/StaticMappers/Orders/OrderMapper.php index 747e18e..5fa46a1 100644 --- a/src/StaticMappers/Orders/OrderMapper.php +++ b/src/StaticMappers/Orders/OrderMapper.php @@ -39,6 +39,7 @@ public static function map(stdClass $data): Order (int) $data->total_discount_amount, (int) $data->total_order_amount, $data->paid_at ?? null, + $data->completed_at ?? null, $data->created_at, $data->updated_at, $contact, diff --git a/tests/OAuth/Orders/OrdersResourceTest.php b/tests/OAuth/Orders/OrdersResourceTest.php index c49c641..2ac3168 100644 --- a/tests/OAuth/Orders/OrdersResourceTest.php +++ b/tests/OAuth/Orders/OrdersResourceTest.php @@ -11,7 +11,169 @@ class OrdersResourceTest extends OAuthTestCase */ public function it_can_list_orders(): void { - // + $this->addExpectedResponse([ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'formatted_total_order_amount' => '€ 30.00', + 'total_discount_amount' => 500, + 'total_charges_amount' => 1000, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'created_at' => '2025-01-01', + 'updated_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + 'email' => 'customer@example.com', + ], + 'business_profile' => [ + 'uuid' => '123', + 'name' => 'Test Business', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Lungo', + ], + 'sub_line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => $uuid = '123', + 'name' => 'Extra Cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Extra Cream', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'value' => 20, + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'line_items' => [], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ], + ]); + + $orders = $this->mockedClient->orders->list(); + + $order = $orders[0]; + + $this->assertEquals('123', $order->getUuid()); + $this->assertEquals('123', $order->getExternalIdentifier()); + $this->assertEquals('123', $order->getReference()); + $this->assertEquals('CREATED', $order->getStatus()); + $this->assertEquals('PAID', $order->getPaymentStatus()); + $this->assertEquals(2500, $order->getTotalOrderAmount()); + $this->assertEquals(3000, $order->getOrderAmount()); + $this->assertEquals('€ 30.00', $order->getFormattedTotalOrderAmount()); + $this->assertEquals(500, $order->getTotalDiscountAmount()); + $this->assertEquals(1000, $order->getTotalChargesAmount()); + $this->assertEquals('EUR', $order->getCurrency()); + $this->assertEquals('2025-01-01', $order->getPaidAt()); + $this->assertEquals('2025-01-01', $order->getCompletedAt()); + $this->assertEquals('2025-01-01', $order->getCreatedAt()); + $this->assertEquals('2025-01-01', $order->getUpdatedAt()); + + $contact = $order->getContact(); + $this->assertEquals('123', $contact->getUuid()); + $this->assertEquals('customer@example.com', $contact->getEmail()); + + $profile = $order->getShop(); + $this->assertEquals('123', $profile->getUuid()); + $this->assertEquals('Test Business', $profile->getName()); + + $lineItems = $order->getLineItems(); + $this->assertCount(1, $lineItems); + $lineItem = $lineItems[0]; + $this->assertEquals('123', $lineItem->getUuid()); + $this->assertEquals('123', $lineItem->getExternalIdentifier()); + $this->assertEquals('Lungo', $lineItem->getName()); + $this->assertEquals(5, $lineItem->getQuantity()); + $this->assertEquals(600, $lineItem->getPrice()); + $this->assertEquals(3000, $lineItem->getTotalAmount()); + $this->assertEquals(0, $lineItem->getDiscountAmount()); + + $product = $lineItem->getProduct(); + $this->assertEquals('123', $product->getExternalIdentifier()); + $this->assertEquals('Lungo', $product->getName()); + + $subLineItems = $lineItem->getSubLineItems(); + $this->assertCount(1, $subLineItems); + $subLineItem = $subLineItems[0]; + $this->assertEquals('123', $subLineItem->getUuid()); + $this->assertEquals('123', $subLineItem->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subLineItem->getName()); + $this->assertEquals(1, $subLineItem->getQuantity()); + $this->assertEquals(1000, $subLineItem->getPrice()); + $this->assertEquals(1000, $subLineItem->getTotalAmount()); + $this->assertEquals(0, $subLineItem->getDiscountAmount()); + + $subProduct = $subLineItem->getProduct(); + $this->assertEquals('123', $subProduct->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subProduct->getName()); + + $discounts = $order->getAppliedDiscounts(); + $this->assertCount(1, $discounts); + $discount = $discounts[0]; + $this->assertEquals('123', $discount->getExternalIdentifier()); + $this->assertEquals('ABSOLUTE', $discount->getType()); + $this->assertEquals('Summer Sale', $discount->getName()); + $this->assertEquals(20, $discount->getAmount()); + $this->assertEquals('SUB_LINE_ITEMS', $discount->getAppliedTo()); + $this->assertEquals('123', $discount->getSubLineItems()[0]->external_identifier); + + $charges = $order->getCharges(); + $this->assertCount(1, $charges); + $charge = $charges[0]; + $this->assertEquals('123', $charge->getExternalIdentifier()); + $this->assertEquals('SERVICE_CHARGE', $charge->getType()); + $this->assertEquals('Service charge', $charge->getName()); + $this->assertEquals(1000, $charge->getAmount()); + $this->assertEquals(0, $charge->getDiscountAmount()); + $this->assertEquals(1000, $charge->getTotalAmount()); } /** @@ -19,7 +181,166 @@ public function it_can_list_orders(): void */ public function it_can_get_an_order(): void { - // + + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'formatted_total_order_amount' => '€ 30.00', + 'total_discount_amount' => 500, + 'total_charges_amount' => 1000, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'created_at' => '2025-01-01', + 'updated_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + 'email' => 'customer@example.com', + ], + 'business_profile' => [ + 'uuid' => '123', + 'name' => 'Test Business', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Lungo', + ], + 'sub_line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => $uuid = '123', + 'name' => 'Extra Cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Extra Cream', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'value' => 20, + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'line_items' => [], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $order = $this->mockedClient->orders->get('123'); + + $this->assertEquals('123', $order->getUuid()); + $this->assertEquals('123', $order->getExternalIdentifier()); + $this->assertEquals('123', $order->getReference()); + $this->assertEquals('CREATED', $order->getStatus()); + $this->assertEquals('PAID', $order->getPaymentStatus()); + $this->assertEquals(2500, $order->getTotalOrderAmount()); + $this->assertEquals(3000, $order->getOrderAmount()); + $this->assertEquals('€ 30.00', $order->getFormattedTotalOrderAmount()); + $this->assertEquals(500, $order->getTotalDiscountAmount()); + $this->assertEquals(1000, $order->getTotalChargesAmount()); + $this->assertEquals('EUR', $order->getCurrency()); + $this->assertEquals('2025-01-01', $order->getPaidAt()); + $this->assertEquals('2025-01-01', $order->getCompletedAt()); + $this->assertEquals('2025-01-01', $order->getCreatedAt()); + $this->assertEquals('2025-01-01', $order->getUpdatedAt()); + + $contact = $order->getContact(); + $this->assertEquals('123', $contact->getUuid()); + $this->assertEquals('customer@example.com', $contact->getEmail()); + + $profile = $order->getShop(); + $this->assertEquals('123', $profile->getUuid()); + $this->assertEquals('Test Business', $profile->getName()); + + $lineItems = $order->getLineItems(); + $this->assertCount(1, $lineItems); + $lineItem = $lineItems[0]; + $this->assertEquals('123', $lineItem->getUuid()); + $this->assertEquals('123', $lineItem->getExternalIdentifier()); + $this->assertEquals('Lungo', $lineItem->getName()); + $this->assertEquals(5, $lineItem->getQuantity()); + $this->assertEquals(600, $lineItem->getPrice()); + $this->assertEquals(3000, $lineItem->getTotalAmount()); + $this->assertEquals(0, $lineItem->getDiscountAmount()); + + $product = $lineItem->getProduct(); + $this->assertEquals('123', $product->getExternalIdentifier()); + $this->assertEquals('Lungo', $product->getName()); + + $subLineItems = $lineItem->getSubLineItems(); + $this->assertCount(1, $subLineItems); + $subLineItem = $subLineItems[0]; + $this->assertEquals('123', $subLineItem->getUuid()); + $this->assertEquals('123', $subLineItem->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subLineItem->getName()); + $this->assertEquals(1, $subLineItem->getQuantity()); + $this->assertEquals(1000, $subLineItem->getPrice()); + $this->assertEquals(1000, $subLineItem->getTotalAmount()); + $this->assertEquals(0, $subLineItem->getDiscountAmount()); + + $subProduct = $subLineItem->getProduct(); + $this->assertEquals('123', $subProduct->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subProduct->getName()); + + $discounts = $order->getAppliedDiscounts(); + $this->assertCount(1, $discounts); + $discount = $discounts[0]; + $this->assertEquals('123', $discount->getExternalIdentifier()); + $this->assertEquals('ABSOLUTE', $discount->getType()); + $this->assertEquals('Summer Sale', $discount->getName()); + $this->assertEquals(20, $discount->getAmount()); + $this->assertEquals('SUB_LINE_ITEMS', $discount->getAppliedTo()); + $this->assertEquals('123', $discount->getSubLineItems()[0]->external_identifier); + + $charges = $order->getCharges(); + $this->assertCount(1, $charges); + $charge = $charges[0]; + $this->assertEquals('123', $charge->getExternalIdentifier()); + $this->assertEquals('SERVICE_CHARGE', $charge->getType()); + $this->assertEquals('Service charge', $charge->getName()); + $this->assertEquals(1000, $charge->getAmount()); + $this->assertEquals(0, $charge->getDiscountAmount()); + $this->assertEquals(1000, $charge->getTotalAmount()); } /** @@ -27,7 +348,168 @@ public function it_can_get_an_order(): void */ public function it_can_find_an_order(): void { - // + + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'formatted_total_order_amount' => '€ 30.00', + 'total_discount_amount' => 500, + 'total_charges_amount' => 1000, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'created_at' => '2025-01-01', + 'updated_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + 'email' => 'customer@example.com', + ], + 'business_profile' => [ + 'uuid' => '123', + 'name' => 'Test Business', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Lungo', + ], + 'sub_line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => $uuid = '123', + 'name' => 'Extra Cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Extra Cream', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'value' => 20, + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'line_items' => [], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $order = $this->mockedClient->orders->find([ + 'external_identifier' => '123', + ]); + + $this->assertEquals('123', $order->getUuid()); + $this->assertEquals('123', $order->getExternalIdentifier()); + $this->assertEquals('123', $order->getReference()); + $this->assertEquals('CREATED', $order->getStatus()); + $this->assertEquals('PAID', $order->getPaymentStatus()); + $this->assertEquals(2500, $order->getTotalOrderAmount()); + $this->assertEquals(3000, $order->getOrderAmount()); + $this->assertEquals('€ 30.00', $order->getFormattedTotalOrderAmount()); + $this->assertEquals(500, $order->getTotalDiscountAmount()); + $this->assertEquals(1000, $order->getTotalChargesAmount()); + $this->assertEquals('EUR', $order->getCurrency()); + $this->assertEquals('2025-01-01', $order->getPaidAt()); + $this->assertEquals('2025-01-01', $order->getCompletedAt()); + $this->assertEquals('2025-01-01', $order->getCreatedAt()); + $this->assertEquals('2025-01-01', $order->getUpdatedAt()); + + $contact = $order->getContact(); + $this->assertEquals('123', $contact->getUuid()); + $this->assertEquals('customer@example.com', $contact->getEmail()); + + $profile = $order->getShop(); + $this->assertEquals('123', $profile->getUuid()); + $this->assertEquals('Test Business', $profile->getName()); + + $lineItems = $order->getLineItems(); + $this->assertCount(1, $lineItems); + $lineItem = $lineItems[0]; + $this->assertEquals('123', $lineItem->getUuid()); + $this->assertEquals('123', $lineItem->getExternalIdentifier()); + $this->assertEquals('Lungo', $lineItem->getName()); + $this->assertEquals(5, $lineItem->getQuantity()); + $this->assertEquals(600, $lineItem->getPrice()); + $this->assertEquals(3000, $lineItem->getTotalAmount()); + $this->assertEquals(0, $lineItem->getDiscountAmount()); + + $product = $lineItem->getProduct(); + $this->assertEquals('123', $product->getExternalIdentifier()); + $this->assertEquals('Lungo', $product->getName()); + + $subLineItems = $lineItem->getSubLineItems(); + $this->assertCount(1, $subLineItems); + $subLineItem = $subLineItems[0]; + $this->assertEquals('123', $subLineItem->getUuid()); + $this->assertEquals('123', $subLineItem->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subLineItem->getName()); + $this->assertEquals(1, $subLineItem->getQuantity()); + $this->assertEquals(1000, $subLineItem->getPrice()); + $this->assertEquals(1000, $subLineItem->getTotalAmount()); + $this->assertEquals(0, $subLineItem->getDiscountAmount()); + + $subProduct = $subLineItem->getProduct(); + $this->assertEquals('123', $subProduct->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subProduct->getName()); + + $discounts = $order->getAppliedDiscounts(); + $this->assertCount(1, $discounts); + $discount = $discounts[0]; + $this->assertEquals('123', $discount->getExternalIdentifier()); + $this->assertEquals('ABSOLUTE', $discount->getType()); + $this->assertEquals('Summer Sale', $discount->getName()); + $this->assertEquals(20, $discount->getAmount()); + $this->assertEquals('SUB_LINE_ITEMS', $discount->getAppliedTo()); + $this->assertEquals('123', $discount->getSubLineItems()[0]->external_identifier); + + $charges = $order->getCharges(); + $this->assertCount(1, $charges); + $charge = $charges[0]; + $this->assertEquals('123', $charge->getExternalIdentifier()); + $this->assertEquals('SERVICE_CHARGE', $charge->getType()); + $this->assertEquals('Service charge', $charge->getName()); + $this->assertEquals(1000, $charge->getAmount()); + $this->assertEquals(0, $charge->getDiscountAmount()); + $this->assertEquals(1000, $charge->getTotalAmount()); } /** @@ -35,7 +517,232 @@ public function it_can_find_an_order(): void */ public function it_can_create_orders(): void { - // + $this->addExpectedResponse([ + 'uuid' => '123', + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'formatted_total_order_amount' => '€ 30.00', + 'total_discount_amount' => 500, + 'total_charges_amount' => 1000, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'created_at' => '2025-01-01', + 'updated_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + 'email' => 'customer@example.com', + ], + 'business_profile' => [ + 'uuid' => '123', + 'name' => 'Test Business', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Lungo', + ], + 'sub_line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => $uuid = '123', + 'name' => 'Extra Cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Extra Cream', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'value' => 20, + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'line_items' => [], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $order = $this->mockedClient->orders->create([ + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'total_discount_amount' => 500, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + ], + 'business_profile' => [ + 'uuid' => '123', + ], + 'line_items' => [ + [ + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid = '123', + 'name' => 'Extra cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $this->assertEquals('123', $order->getUuid()); + $this->assertEquals('123', $order->getExternalIdentifier()); + $this->assertEquals('123', $order->getReference()); + $this->assertEquals('CREATED', $order->getStatus()); + $this->assertEquals('PAID', $order->getPaymentStatus()); + $this->assertEquals(2500, $order->getTotalOrderAmount()); + $this->assertEquals(3000, $order->getOrderAmount()); + $this->assertEquals('€ 30.00', $order->getFormattedTotalOrderAmount()); + $this->assertEquals(500, $order->getTotalDiscountAmount()); + $this->assertEquals(1000, $order->getTotalChargesAmount()); + $this->assertEquals('EUR', $order->getCurrency()); + $this->assertEquals('2025-01-01', $order->getPaidAt()); + $this->assertEquals('2025-01-01', $order->getCompletedAt()); + $this->assertEquals('2025-01-01', $order->getCreatedAt()); + $this->assertEquals('2025-01-01', $order->getUpdatedAt()); + + $contact = $order->getContact(); + $this->assertEquals('123', $contact->getUuid()); + $this->assertEquals('customer@example.com', $contact->getEmail()); + + $profile = $order->getShop(); + $this->assertEquals('123', $profile->getUuid()); + $this->assertEquals('Test Business', $profile->getName()); + + $lineItems = $order->getLineItems(); + $this->assertCount(1, $lineItems); + $lineItem = $lineItems[0]; + $this->assertEquals('123', $lineItem->getUuid()); + $this->assertEquals('123', $lineItem->getExternalIdentifier()); + $this->assertEquals('Lungo', $lineItem->getName()); + $this->assertEquals(5, $lineItem->getQuantity()); + $this->assertEquals(600, $lineItem->getPrice()); + $this->assertEquals(3000, $lineItem->getTotalAmount()); + $this->assertEquals(0, $lineItem->getDiscountAmount()); + + $product = $lineItem->getProduct(); + $this->assertEquals('123', $product->getExternalIdentifier()); + $this->assertEquals('Lungo', $product->getName()); + + $subLineItems = $lineItem->getSubLineItems(); + $this->assertCount(1, $subLineItems); + $subLineItem = $subLineItems[0]; + $this->assertEquals('123', $subLineItem->getUuid()); + $this->assertEquals('123', $subLineItem->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subLineItem->getName()); + $this->assertEquals(1, $subLineItem->getQuantity()); + $this->assertEquals(1000, $subLineItem->getPrice()); + $this->assertEquals(1000, $subLineItem->getTotalAmount()); + $this->assertEquals(0, $subLineItem->getDiscountAmount()); + + $subProduct = $subLineItem->getProduct(); + $this->assertEquals('123', $subProduct->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subProduct->getName()); + + $discounts = $order->getAppliedDiscounts(); + $this->assertCount(1, $discounts); + $discount = $discounts[0]; + $this->assertEquals('123', $discount->getExternalIdentifier()); + $this->assertEquals('ABSOLUTE', $discount->getType()); + $this->assertEquals('Summer Sale', $discount->getName()); + $this->assertEquals(20, $discount->getAmount()); + $this->assertEquals('SUB_LINE_ITEMS', $discount->getAppliedTo()); + $this->assertEquals('123', $discount->getSubLineItems()[0]->external_identifier); + + $charges = $order->getCharges(); + $this->assertCount(1, $charges); + $charge = $charges[0]; + $this->assertEquals('123', $charge->getExternalIdentifier()); + $this->assertEquals('SERVICE_CHARGE', $charge->getType()); + $this->assertEquals('Service charge', $charge->getName()); + $this->assertEquals(1000, $charge->getAmount()); + $this->assertEquals(0, $charge->getDiscountAmount()); + $this->assertEquals(1000, $charge->getTotalAmount()); } /** @@ -43,7 +750,18 @@ public function it_can_create_orders(): void */ public function it_can_process_orders(): void { - // + $this->addExpectedResponse([ + 'type' => 'points_transaction', + 'data' => [ + 'points' => 40, + 'new_balance' => 40, + ], + ]); + + $response = $this->mockedClient->orders->process('123'); + + $this->assertEquals(40, $response->data->points); + $this->assertEquals(40, $response->data->new_balance); } /** @@ -51,7 +769,248 @@ public function it_can_process_orders(): void */ public function it_can_create_and_process_orders(): void { - // + $this->addExpectedResponse([ + 'order' => [ + 'uuid' => '123', + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'formatted_total_order_amount' => '€ 30.00', + 'total_discount_amount' => 500, + 'total_charges_amount' => 1000, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'created_at' => '2025-01-01', + 'updated_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + 'email' => 'customer@example.com', + ], + 'business_profile' => [ + 'uuid' => '123', + 'name' => 'Test Business', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Lungo', + ], + 'sub_line_items' => [ + [ + 'uuid' => '123', + 'external_identifier' => $uuid = '123', + 'name' => 'Extra Cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + 'name' => 'Extra Cream', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'value' => 20, + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'line_items' => [], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'uuid' => '123', + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ], + 'result' => [ + 'type' => 'points_transaction', + 'data' => [ + 'points' => 40, + 'new_balance' => 40, + ], + ], + ]); + + $response = $this->mockedClient->orders->createAndProcess([ + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'total_discount_amount' => 500, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + ], + 'business_profile' => [ + 'uuid' => '123', + ], + 'line_items' => [ + [ + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid = '123', + 'name' => 'Extra cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $order = $response['order']; + + $this->assertEquals('123', $order->getUuid()); + $this->assertEquals('123', $order->getExternalIdentifier()); + $this->assertEquals('123', $order->getReference()); + $this->assertEquals('CREATED', $order->getStatus()); + $this->assertEquals('PAID', $order->getPaymentStatus()); + $this->assertEquals(2500, $order->getTotalOrderAmount()); + $this->assertEquals(3000, $order->getOrderAmount()); + $this->assertEquals('€ 30.00', $order->getFormattedTotalOrderAmount()); + $this->assertEquals(500, $order->getTotalDiscountAmount()); + $this->assertEquals(1000, $order->getTotalChargesAmount()); + $this->assertEquals('EUR', $order->getCurrency()); + $this->assertEquals('2025-01-01', $order->getPaidAt()); + $this->assertEquals('2025-01-01', $order->getCompletedAt()); + $this->assertEquals('2025-01-01', $order->getCreatedAt()); + $this->assertEquals('2025-01-01', $order->getUpdatedAt()); + + $contact = $order->getContact(); + $this->assertEquals('123', $contact->getUuid()); + $this->assertEquals('customer@example.com', $contact->getEmail()); + + $profile = $order->getShop(); + $this->assertEquals('123', $profile->getUuid()); + $this->assertEquals('Test Business', $profile->getName()); + + $lineItems = $order->getLineItems(); + $this->assertCount(1, $lineItems); + $lineItem = $lineItems[0]; + $this->assertEquals('123', $lineItem->getUuid()); + $this->assertEquals('123', $lineItem->getExternalIdentifier()); + $this->assertEquals('Lungo', $lineItem->getName()); + $this->assertEquals(5, $lineItem->getQuantity()); + $this->assertEquals(600, $lineItem->getPrice()); + $this->assertEquals(3000, $lineItem->getTotalAmount()); + $this->assertEquals(0, $lineItem->getDiscountAmount()); + + $product = $lineItem->getProduct(); + $this->assertEquals('123', $product->getExternalIdentifier()); + $this->assertEquals('Lungo', $product->getName()); + + $subLineItems = $lineItem->getSubLineItems(); + $this->assertCount(1, $subLineItems); + $subLineItem = $subLineItems[0]; + $this->assertEquals('123', $subLineItem->getUuid()); + $this->assertEquals('123', $subLineItem->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subLineItem->getName()); + $this->assertEquals(1, $subLineItem->getQuantity()); + $this->assertEquals(1000, $subLineItem->getPrice()); + $this->assertEquals(1000, $subLineItem->getTotalAmount()); + $this->assertEquals(0, $subLineItem->getDiscountAmount()); + + $subProduct = $subLineItem->getProduct(); + $this->assertEquals('123', $subProduct->getExternalIdentifier()); + $this->assertEquals('Extra Cream', $subProduct->getName()); + + $discounts = $order->getAppliedDiscounts(); + $this->assertCount(1, $discounts); + $discount = $discounts[0]; + $this->assertEquals('123', $discount->getExternalIdentifier()); + $this->assertEquals('ABSOLUTE', $discount->getType()); + $this->assertEquals('Summer Sale', $discount->getName()); + $this->assertEquals(20, $discount->getAmount()); + $this->assertEquals('SUB_LINE_ITEMS', $discount->getAppliedTo()); + $this->assertEquals('123', $discount->getSubLineItems()[0]->external_identifier); + + $charges = $order->getCharges(); + $this->assertCount(1, $charges); + $charge = $charges[0]; + $this->assertEquals('123', $charge->getExternalIdentifier()); + $this->assertEquals('SERVICE_CHARGE', $charge->getType()); + $this->assertEquals('Service charge', $charge->getName()); + $this->assertEquals(1000, $charge->getAmount()); + $this->assertEquals(0, $charge->getDiscountAmount()); + $this->assertEquals(1000, $charge->getTotalAmount()); + + $result = $response['result']; + + $this->assertEquals(40, $result->data->points); + $this->assertEquals(40, $result->data->new_balance); } /** @@ -59,6 +1018,86 @@ public function it_can_create_and_process_orders(): void */ public function it_can_calculate_orders(): void { - // + $this->addExpectedResponse([ + 'result' => [ + 'type' => 'points_transaction', + 'data' => [ + 'points' => 40, + 'new_balance' => 40, + ], + ], + ]); + + $response = $this->mockedClient->orders->calculate([ + 'external_identifier' => '123', + 'reference' => '123', + 'status' => 'CREATED', + 'payment_status' => 'PAID', + 'total_order_amount' => 2500, + 'order_amount' => 3000, + 'total_discount_amount' => 500, + 'currency' => 'EUR', + 'paid_at' => '2025-01-01', + 'completed_at' => '2025-01-01', + 'contact' => [ + 'uuid' => '123', + ], + 'business_profile' => [ + 'uuid' => '123', + ], + 'line_items' => [ + [ + 'external_identifier' => '123', + 'name' => 'Lungo', + 'quantity' => 5, + 'price' => 600, + 'total_amount' => 3000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid = '123', + 'name' => 'Extra cream', + 'quantity' => 1, + 'price' => 1000, + 'total_amount' => 1000, + 'discount_amount' => 0, + 'product' => [ + 'external_identifier' => '123', + ], + ], + ], + ], + ], + 'applied_discounts' => [ + [ + 'external_identifier' => '123', + 'type' => 'ABSOLUTE', + 'name' => 'Summer Sale', + 'amount' => 20, + 'applied_to' => 'SUB_LINE_ITEMS', + 'sub_line_items' => [ + [ + 'external_identifier' => $uuid, + ], + ], + ], + ], + 'charges' => [ + [ + 'external_identifier' => '123', + 'type' => 'SERVICE_CHARGE', + 'name' => 'Service charge', + 'amount' => 1000, + 'discount_amount' => 0, + 'total_amount' => 1000, + ], + ], + ]); + + $this->assertEquals(40, $response->result->data->points); + $this->assertEquals(40, $response->result->data->new_balance); } } \ No newline at end of file From c1c58b7d548cbdae926ae7eb685890e3336463d7 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 15:05:45 +0200 Subject: [PATCH 24/30] wip --- src/Mappers/Orders/AppliedDiscountsMapper.php | 4 ++-- src/Mappers/Orders/ChargesMapper.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Mappers/Orders/AppliedDiscountsMapper.php b/src/Mappers/Orders/AppliedDiscountsMapper.php index ad66540..30bccba 100644 --- a/src/Mappers/Orders/AppliedDiscountsMapper.php +++ b/src/Mappers/Orders/AppliedDiscountsMapper.php @@ -2,14 +2,14 @@ namespace Piggy\Api\Mappers\Orders; -use Piggy\Api\Models\Orders\LineItem; +use Piggy\Api\Models\Orders\AppliedDiscount; use stdClass; class AppliedDiscountsMapper { /** * @param stdClass[] $data - * @return LineItem[] + * @return AppliedDiscount[] */ public static function map(array $data): array { diff --git a/src/Mappers/Orders/ChargesMapper.php b/src/Mappers/Orders/ChargesMapper.php index bf1f768..1ae81ce 100644 --- a/src/Mappers/Orders/ChargesMapper.php +++ b/src/Mappers/Orders/ChargesMapper.php @@ -2,14 +2,14 @@ namespace Piggy\Api\Mappers\Orders; -use Piggy\Api\Models\Orders\LineItem; +use Piggy\Api\Models\Orders\Charge; use stdClass; class ChargesMapper { /** * @param stdClass[] $data - * @return LineItem[] + * @return Charge[] */ public static function map(array $data): array { From 4cb92c01648c29ec691ad2c7b8644afa98a4a2dc Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 15:08:32 +0200 Subject: [PATCH 25/30] wip --- tests/OAuth/Categories/CategoriesResourceTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/OAuth/Categories/CategoriesResourceTest.php b/tests/OAuth/Categories/CategoriesResourceTest.php index cca7cb7..6aa04ff 100644 --- a/tests/OAuth/Categories/CategoriesResourceTest.php +++ b/tests/OAuth/Categories/CategoriesResourceTest.php @@ -3,7 +3,6 @@ namespace Piggy\Api\Tests\OAuth\Categories; use Piggy\Api\Exceptions\PiggyRequestException; -use Piggy\Api\Models\Categories\Category; use Piggy\Api\Tests\OAuthTestCase; class CategoriesResourceTest extends OAuthTestCase @@ -112,7 +111,7 @@ public function it_can_find_or_create_a_category(): void $category = $this->mockedClient->categories->findOrCreate( '123', - 'Category 1 name', + 'Category 1 name' ); $this->assertEquals('123', $category->getUuid()); From 48fa3fc3646fb472c58c0d33c7d036fae9891027 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 15:27:06 +0200 Subject: [PATCH 26/30] wip --- src/Models/Orders/LineItemReturn.php | 47 +++++++++ src/Models/Orders/OrderReturn.php | 96 +++++++++++++++++++ src/Models/Orders/SubLineItemReturn.php | 47 +++++++++ .../Orders/LineItemReturnMapper.php | 18 ++++ .../Orders/LineItemReturnsMapper.php | 25 +++++ .../Orders/OrderReturnMapper.php | 30 ++++++ .../Orders/OrderReturnsMapper.php | 35 +++++++ .../Orders/SubLineItemReturnMapper.php | 18 ++++ .../Orders/SubLineItemReturnsMapper.php | 26 +++++ 9 files changed, 342 insertions(+) create mode 100644 src/Models/Orders/LineItemReturn.php create mode 100644 src/Models/Orders/OrderReturn.php create mode 100644 src/Models/Orders/SubLineItemReturn.php create mode 100644 src/StaticMappers/Orders/LineItemReturnMapper.php create mode 100644 src/StaticMappers/Orders/LineItemReturnsMapper.php create mode 100644 src/StaticMappers/Orders/OrderReturnMapper.php create mode 100644 src/StaticMappers/Orders/OrderReturnsMapper.php create mode 100644 src/StaticMappers/Orders/SubLineItemReturnMapper.php create mode 100644 src/StaticMappers/Orders/SubLineItemReturnsMapper.php diff --git a/src/Models/Orders/LineItemReturn.php b/src/Models/Orders/LineItemReturn.php new file mode 100644 index 0000000..3c18be1 --- /dev/null +++ b/src/Models/Orders/LineItemReturn.php @@ -0,0 +1,47 @@ +uuid = $uuid; + $this->quantity = $quantity; + $this->lineItem = $lineItem; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getLineItem(): array + { + return $this->lineItem; + } +} \ No newline at end of file diff --git a/src/Models/Orders/OrderReturn.php b/src/Models/Orders/OrderReturn.php new file mode 100644 index 0000000..1eb578f --- /dev/null +++ b/src/Models/Orders/OrderReturn.php @@ -0,0 +1,96 @@ +uuid = $uuid; + $this->status = $status; + $this->order = $order; + $this->lineItemReturns = $lineItemReturns; + $this->subLineItemReturns = $subLineItemReturns; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getStatus(): string + { + return $this->status; + } + + public function getOrder(): array + { + return $this->order; + } + + public function getLineItemReturns(): array + { + return $this->lineItemReturns; + } + + public function getSubLineItemReturns(): array + { + return $this->subLineItemReturns; + } + + /** + * @param array $body + * + * @return Order + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function create(array $body): Order + { + $response = ApiClient::post(self::resourceUri, $body); + + return OrderMapper::map($response->getData()); + } +} \ No newline at end of file diff --git a/src/Models/Orders/SubLineItemReturn.php b/src/Models/Orders/SubLineItemReturn.php new file mode 100644 index 0000000..84d6fff --- /dev/null +++ b/src/Models/Orders/SubLineItemReturn.php @@ -0,0 +1,47 @@ +uuid = $uuid; + $this->quantity = $quantity; + $this->subLineItem = $subLineItem; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getSubLineItem(): array + { + return $this->subLineItem; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/LineItemReturnMapper.php b/src/StaticMappers/Orders/LineItemReturnMapper.php new file mode 100644 index 0000000..6e3b0ea --- /dev/null +++ b/src/StaticMappers/Orders/LineItemReturnMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->quantity, + $data->line_item + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/LineItemReturnsMapper.php b/src/StaticMappers/Orders/LineItemReturnsMapper.php new file mode 100644 index 0000000..d5167d4 --- /dev/null +++ b/src/StaticMappers/Orders/LineItemReturnsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $lineItemReturns; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrderReturnMapper.php b/src/StaticMappers/Orders/OrderReturnMapper.php new file mode 100644 index 0000000..2de5edc --- /dev/null +++ b/src/StaticMappers/Orders/OrderReturnMapper.php @@ -0,0 +1,30 @@ +line_item_returns)) { + $lineItemReturns = SubLineItemReturnsMapper::map($data->line_item_returns); + } + + $subLineItemReturns = []; + if (isset($data->sub_line_item_returns)) { + $subLineItemReturns = SubLineItemReturnsMapper::map($data->sub_line_item_returns); + } + + return new OrderReturn( + $data->uuid, + $data->status, + $data->order, + $lineItemReturns, + $subLineItemReturns + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrderReturnsMapper.php b/src/StaticMappers/Orders/OrderReturnsMapper.php new file mode 100644 index 0000000..a8320dc --- /dev/null +++ b/src/StaticMappers/Orders/OrderReturnsMapper.php @@ -0,0 +1,35 @@ +product)) { + $product = ProductMapper::map($data->product); + } + + $subLineItems = null; + if (isset($data->sub_line_items)) { + $subLineItems = SubLineItemsMapper::map($data->sub_line_items); + } + + return new LineItem( + $data->uuid, + $data->external_identifier, + $data->name, + $data->quantity, + $data->price, + $data->discount_amount, + $data->total_amount, + $product, + $subLineItems + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/SubLineItemReturnMapper.php b/src/StaticMappers/Orders/SubLineItemReturnMapper.php new file mode 100644 index 0000000..2639552 --- /dev/null +++ b/src/StaticMappers/Orders/SubLineItemReturnMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->quantity, + $data->sub_line_item + ); + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/SubLineItemReturnsMapper.php b/src/StaticMappers/Orders/SubLineItemReturnsMapper.php new file mode 100644 index 0000000..5fe1561 --- /dev/null +++ b/src/StaticMappers/Orders/SubLineItemReturnsMapper.php @@ -0,0 +1,26 @@ +map($item); + } + + return $subLineItemReturns; + } +} \ No newline at end of file From 20117a784d0fb6502a783aea844c89d55e09ae9e Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 15:33:53 +0200 Subject: [PATCH 27/30] wip --- src/Models/Orders/LineItemReturn.php | 14 +++++++++++++- src/Models/Orders/OrderReturn.php | 22 +++++++++++++++++++++- src/Models/Orders/SubLineItemReturn.php | 14 +++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Models/Orders/LineItemReturn.php b/src/Models/Orders/LineItemReturn.php index 3c18be1..71fdf7a 100644 --- a/src/Models/Orders/LineItemReturn.php +++ b/src/Models/Orders/LineItemReturn.php @@ -15,10 +15,13 @@ class LineItemReturn protected $quantity; /** - * @var array + * @var array */ protected $lineItem = []; + /** + * @param array $lineItem + */ public function __construct( string $uuid, int $quantity, @@ -30,16 +33,25 @@ public function __construct( $this->lineItem = $lineItem; } + /** + * @return string + */ public function getUuid(): string { return $this->uuid; } + /** + * @return int + */ public function getQuantity(): int { return $this->quantity; } + /** + * @return array + */ public function getLineItem(): array { return $this->lineItem; diff --git a/src/Models/Orders/OrderReturn.php b/src/Models/Orders/OrderReturn.php index 1eb578f..ca8c303 100644 --- a/src/Models/Orders/OrderReturn.php +++ b/src/Models/Orders/OrderReturn.php @@ -21,7 +21,7 @@ class OrderReturn protected $status; /** - * @var array + * @var array */ protected $order = []; @@ -40,6 +40,11 @@ class OrderReturn */ const resourceUri = '/api/v3/oauth/clients/order-returns'; + /** + * @param array $order + * @param LineItemReturn[] $lineItemReturns + * @param SubLineItemReturn[] $subLineItemReturns + */ public function __construct( string $uuid, string $status, @@ -55,26 +60,41 @@ public function __construct( $this->subLineItemReturns = $subLineItemReturns; } + /** + * @return string + */ public function getUuid(): string { return $this->uuid; } + /** + * @return string + */ public function getStatus(): string { return $this->status; } + /** + * @return array + */ public function getOrder(): array { return $this->order; } + /** + * @return LineItemReturn[] + */ public function getLineItemReturns(): array { return $this->lineItemReturns; } + /** + * @return SubLineItemReturn[] + */ public function getSubLineItemReturns(): array { return $this->subLineItemReturns; diff --git a/src/Models/Orders/SubLineItemReturn.php b/src/Models/Orders/SubLineItemReturn.php index 84d6fff..de5b452 100644 --- a/src/Models/Orders/SubLineItemReturn.php +++ b/src/Models/Orders/SubLineItemReturn.php @@ -15,10 +15,13 @@ class SubLineItemReturn protected $quantity; /** - * @var array + * @var array */ protected $subLineItem = []; + /** + * @param array $subLineItem + */ public function __construct( string $uuid, int $quantity, @@ -30,16 +33,25 @@ public function __construct( $this->subLineItem = $subLineItem; } + /** + * @return string + */ public function getUuid(): string { return $this->uuid; } + /** + * @return int + */ public function getQuantity(): int { return $this->quantity; } + /** + * @return array + */ public function getSubLineItem(): array { return $this->subLineItem; From 7b29f75a8659642c73fd465c56d3c966d102e55f Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 16:33:10 +0200 Subject: [PATCH 28/30] wip --- src/Models/Orders/OrderReturn.php | 42 ++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Models/Orders/OrderReturn.php b/src/Models/Orders/OrderReturn.php index ca8c303..db1b27a 100644 --- a/src/Models/Orders/OrderReturn.php +++ b/src/Models/Orders/OrderReturn.php @@ -6,7 +6,8 @@ use Piggy\Api\ApiClient; use Piggy\Api\Exceptions\MaintenanceModeException; use Piggy\Api\Exceptions\PiggyRequestException; -use Piggy\Api\StaticMappers\Orders\OrderMapper; +use Piggy\Api\StaticMappers\Orders\OrderReturnMapper; +use stdClass; class OrderReturn { @@ -103,14 +104,47 @@ public function getSubLineItemReturns(): array /** * @param array $body * - * @return Order + * @return OrderReturn * * @throws GuzzleException|MaintenanceModeException|PiggyRequestException */ - public static function create(array $body): Order + public static function create(array $body): OrderReturn { $response = ApiClient::post(self::resourceUri, $body); - return OrderMapper::map($response->getData()); + return OrderReturnMapper::map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return stdClass + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function process(string $uuid, array $body = []): stdClass + { + $response = ApiClient::post(self::resourceUri."$uuid/process", $body); + + return $response->getData(); + } + + + /** + * @param array $body + * + * @return array + * + * @throws GuzzleException|MaintenanceModeException|PiggyRequestException + */ + public static function createAndProcess(array $body): array + { + $response = ApiClient::post(self::resourceUri."/create-and-process", $body); + + return [ + 'return' => OrderReturnMapper::map($response->getData()->return), + 'result' => $response->getData()->result, + ]; } } \ No newline at end of file From da9e1361687781009575595201ba36a9342554ce Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 16:54:28 +0200 Subject: [PATCH 29/30] wip --- src/Mappers/Orders/LineItemReturnMapper.php | 18 ++++++ src/Mappers/Orders/LineItemReturnsMapper.php | 25 ++++++++ src/Mappers/Orders/OrderReturnMapper.php | 30 +++++++++ src/Mappers/Orders/OrderReturnsMapper.php | 25 ++++++++ .../Orders/SubLineItemReturnMapper.php | 18 ++++++ .../Orders/SubLineItemReturnsMapper.php | 25 ++++++++ src/Resources/OAuth/Orders/OrderReturns.php | 64 +++++++++++++++++++ .../Register/Orders/OrderReturns.php | 64 +++++++++++++++++++ .../Orders/OrderReturnMapper.php | 2 +- .../Orders/OrderReturnsMapper.php | 33 ++++------ 10 files changed, 282 insertions(+), 22 deletions(-) create mode 100644 src/Mappers/Orders/LineItemReturnMapper.php create mode 100644 src/Mappers/Orders/LineItemReturnsMapper.php create mode 100644 src/Mappers/Orders/OrderReturnMapper.php create mode 100644 src/Mappers/Orders/OrderReturnsMapper.php create mode 100644 src/Mappers/Orders/SubLineItemReturnMapper.php create mode 100644 src/Mappers/Orders/SubLineItemReturnsMapper.php create mode 100644 src/Resources/OAuth/Orders/OrderReturns.php create mode 100644 src/Resources/Register/Orders/OrderReturns.php diff --git a/src/Mappers/Orders/LineItemReturnMapper.php b/src/Mappers/Orders/LineItemReturnMapper.php new file mode 100644 index 0000000..dbc86a3 --- /dev/null +++ b/src/Mappers/Orders/LineItemReturnMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->quantity, + $data->line_item + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/LineItemReturnsMapper.php b/src/Mappers/Orders/LineItemReturnsMapper.php new file mode 100644 index 0000000..d538b85 --- /dev/null +++ b/src/Mappers/Orders/LineItemReturnsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $lineItemReturns; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/OrderReturnMapper.php b/src/Mappers/Orders/OrderReturnMapper.php new file mode 100644 index 0000000..cc2b51c --- /dev/null +++ b/src/Mappers/Orders/OrderReturnMapper.php @@ -0,0 +1,30 @@ +line_item_returns)) { + $lineItemReturns = LineItemReturnsMapper::map($data->line_item_returns); + } + + $subLineItemReturns = []; + if (isset($data->sub_line_item_returns)) { + $subLineItemReturns = SubLineItemReturnsMapper::map($data->sub_line_item_returns); + } + + return new OrderReturn( + $data->uuid, + $data->status, + $data->order, + $lineItemReturns, + $subLineItemReturns + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/OrderReturnsMapper.php b/src/Mappers/Orders/OrderReturnsMapper.php new file mode 100644 index 0000000..0ff92b6 --- /dev/null +++ b/src/Mappers/Orders/OrderReturnsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $orderReturns; + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/SubLineItemReturnMapper.php b/src/Mappers/Orders/SubLineItemReturnMapper.php new file mode 100644 index 0000000..6223207 --- /dev/null +++ b/src/Mappers/Orders/SubLineItemReturnMapper.php @@ -0,0 +1,18 @@ +uuid, + $data->quantity, + $data->sub_line_item + ); + } +} \ No newline at end of file diff --git a/src/Mappers/Orders/SubLineItemReturnsMapper.php b/src/Mappers/Orders/SubLineItemReturnsMapper.php new file mode 100644 index 0000000..fb9611c --- /dev/null +++ b/src/Mappers/Orders/SubLineItemReturnsMapper.php @@ -0,0 +1,25 @@ +map($item); + } + + return $subLineItemReturns; + } +} \ No newline at end of file diff --git a/src/Resources/OAuth/Orders/OrderReturns.php b/src/Resources/OAuth/Orders/OrderReturns.php new file mode 100644 index 0000000..b0d5d03 --- /dev/null +++ b/src/Resources/OAuth/Orders/OrderReturns.php @@ -0,0 +1,64 @@ + $body + * + * @return OrderReturn + * + * @throws PiggyRequestException + */ + public function create(array $body): OrderReturn + { + $response = $this->client->post($this->resourceUri, $body); + + return OrderReturnMapper::map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return stdClass + * + * @throws PiggyRequestException + */ + public function process(string $uuid, array $body = []): stdClass + { + $response = $this->client->post($this->resourceUri."$uuid/process", $body); + + return $response->getData(); + } + + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function createAndProcess(array $body): array + { + $response = $this->client->post($this->resourceUri."/create-and-process", $body); + + return [ + 'return' => OrderReturnMapper::map($response->getData()->return), + 'result' => $response->getData()->result, + ]; + } +} \ No newline at end of file diff --git a/src/Resources/Register/Orders/OrderReturns.php b/src/Resources/Register/Orders/OrderReturns.php new file mode 100644 index 0000000..66c091b --- /dev/null +++ b/src/Resources/Register/Orders/OrderReturns.php @@ -0,0 +1,64 @@ + $body + * + * @return OrderReturn + * + * @throws PiggyRequestException + */ + public function create(array $body): OrderReturn + { + $response = $this->client->post($this->resourceUri, $body); + + return OrderReturnMapper::map($response->getData()); + } + + /** + * @param string $uuid + * @param array $body + * + * @return stdClass + * + * @throws PiggyRequestException + */ + public function process(string $uuid, array $body = []): stdClass + { + $response = $this->client->post($this->resourceUri."$uuid/process", $body); + + return $response->getData(); + } + + + /** + * @param array $body + * + * @return array + * + * @throws PiggyRequestException + */ + public function createAndProcess(array $body): array + { + $response = $this->client->post($this->resourceUri."/create-and-process", $body); + + return [ + 'return' => OrderReturnMapper::map($response->getData()->return), + 'result' => $response->getData()->result, + ]; + } +} \ No newline at end of file diff --git a/src/StaticMappers/Orders/OrderReturnMapper.php b/src/StaticMappers/Orders/OrderReturnMapper.php index 2de5edc..34d69ea 100644 --- a/src/StaticMappers/Orders/OrderReturnMapper.php +++ b/src/StaticMappers/Orders/OrderReturnMapper.php @@ -11,7 +11,7 @@ public static function map(stdClass $data): OrderReturn { $lineItemReturns = []; if (isset($data->line_item_returns)) { - $lineItemReturns = SubLineItemReturnsMapper::map($data->line_item_returns); + $lineItemReturns = LineItemReturnMapper::map($data->line_item_returns); } $subLineItemReturns = []; diff --git a/src/StaticMappers/Orders/OrderReturnsMapper.php b/src/StaticMappers/Orders/OrderReturnsMapper.php index a8320dc..b4ac526 100644 --- a/src/StaticMappers/Orders/OrderReturnsMapper.php +++ b/src/StaticMappers/Orders/OrderReturnsMapper.php @@ -2,34 +2,25 @@ namespace Piggy\Api\StaticMappers\Orders; -use Piggy\Api\Models\Orders\LineItem; -use Piggy\Api\StaticMappers\Products\ProductMapper; +use Piggy\Api\Mappers\Orders\OrderReturnMapper; +use Piggy\Api\Models\Orders\OrderReturn; use stdClass; class OrderReturnsMapper { - public static function map(stdClass $data): LineItem + /** + * @param stdClass[] $data + * @return OrderReturn[] + */ + public static function map(array $data): array { - $product = null; - if (isset($data->product)) { - $product = ProductMapper::map($data->product); - } + $mapper = new OrderReturnMapper(); - $subLineItems = null; - if (isset($data->sub_line_items)) { - $subLineItems = SubLineItemsMapper::map($data->sub_line_items); + $orderReturns = []; + foreach ($data as $item) { + $orderReturns[] = $mapper->map($item); } - return new LineItem( - $data->uuid, - $data->external_identifier, - $data->name, - $data->quantity, - $data->price, - $data->discount_amount, - $data->total_amount, - $product, - $subLineItems - ); + return $orderReturns; } } \ No newline at end of file From 0b9a4a88e5da7f15fe05d62cf2e6d2a6571f2889 Mon Sep 17 00:00:00 2001 From: Lars Wolters Date: Wed, 16 Jul 2025 17:19:05 +0200 Subject: [PATCH 30/30] wip --- src/Http/Traits/SetsOAuthResources.php | 7 + src/Http/Traits/SetsRegisterResources.php | 18 ++- src/Models/Orders/LineItemReturn.php | 15 +-- src/Models/Orders/OrderReturn.php | 11 +- src/Models/Orders/SubLineItemReturn.php | 15 +-- ...erReturns.php => OrderReturnsResource.php} | 2 +- ...erReturns.php => OrderReturnsResource.php} | 2 +- .../Orders/OrderReturnMapper.php | 2 +- .../OAuth/Orders/OrderReturnsResourceTest.php | 120 ++++++++++++++++++ 9 files changed, 165 insertions(+), 27 deletions(-) rename src/Resources/OAuth/Orders/{OrderReturns.php => OrderReturnsResource.php} (96%) rename src/Resources/Register/Orders/{OrderReturns.php => OrderReturnsResource.php} (96%) create mode 100644 tests/OAuth/Orders/OrderReturnsResourceTest.php diff --git a/src/Http/Traits/SetsOAuthResources.php b/src/Http/Traits/SetsOAuthResources.php index 7d336ee..dd2e41a 100644 --- a/src/Http/Traits/SetsOAuthResources.php +++ b/src/Http/Traits/SetsOAuthResources.php @@ -26,6 +26,7 @@ use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardAttributesResource; use Piggy\Api\Resources\OAuth\Loyalty\Rewards\RewardsResource; use Piggy\Api\Resources\OAuth\Loyalty\Tokens\LoyaltyTokensResource; +use Piggy\Api\Resources\OAuth\Orders\OrderReturnsResource; use Piggy\Api\Resources\OAuth\Orders\OrdersResource; use Piggy\Api\Resources\OAuth\Perks\PerksResource; use Piggy\Api\Resources\OAuth\PortalSessions\PortalSessionsResource; @@ -221,6 +222,11 @@ trait SetsOAuthResources */ public $orders; + /** + * @var OrderReturnsResource + */ + public $orderReturns; + protected function setResources(BaseClient $client): void { $this->contacts = new ContactsResource($client); @@ -259,5 +265,6 @@ protected function setResources(BaseClient $client): void $this->products = new ProductsResource($client); $this->categories = new CategoriesResource($client); $this->orders = new OrdersResource($client); + $this->orderReturns = new OrderReturnsResource($client); } } diff --git a/src/Http/Traits/SetsRegisterResources.php b/src/Http/Traits/SetsRegisterResources.php index c04ae2a..c104f98 100644 --- a/src/Http/Traits/SetsRegisterResources.php +++ b/src/Http/Traits/SetsRegisterResources.php @@ -4,8 +4,6 @@ use Piggy\Api\Http\BaseClient; use Piggy\Api\Resources\Register\Categories\CategoriesResource; -use Piggy\Api\Resources\Register\Products\ProductsResource; -use Piggy\Api\Resources\Register\Loyalty\Tokens\LoyaltyTokensResource; use Piggy\Api\Resources\Register\ContactIdentifiersResource; use Piggy\Api\Resources\Register\Contacts\ContactsResource; use Piggy\Api\Resources\Register\ContactSubscriptionsResource; @@ -15,7 +13,11 @@ use Piggy\Api\Resources\Register\Loyalty\Receptions\CreditReceptionsResource; use Piggy\Api\Resources\Register\Loyalty\Receptions\RewardReceptionsResource; use Piggy\Api\Resources\Register\Loyalty\Rewards\RewardsResource; +use Piggy\Api\Resources\Register\Loyalty\Tokens\LoyaltyTokensResource; +use Piggy\Api\Resources\Register\Orders\OrderReturnsResource; +use Piggy\Api\Resources\Register\Orders\OrdersResource; use Piggy\Api\Resources\Register\PrepaidTransactionResource; +use Piggy\Api\Resources\Register\Products\ProductsResource; use Piggy\Api\Resources\Register\Registers\RegisterResource; use Piggy\Api\Resources\Register\SubscriptionTypesResource; use Piggy\Api\Resources\Register\Vouchers\PromotionsResource; @@ -111,6 +113,16 @@ trait SetsRegisterResources */ public $categories; + /** + * @var OrdersResource + */ + public $orders; + + /** + * @var OrderReturnsResource + */ + public $orderReturns; + protected function setResources(BaseClient $client): void { $this->registers = new RegisterResource($client); @@ -130,5 +142,7 @@ protected function setResources(BaseClient $client): void $this->promotion = new PromotionsResource($client); $this->products = new ProductsResource($client); $this->categories = new CategoriesResource($client); + $this->orders = new OrdersResource($client); + $this->orderReturns = new OrderReturnsResource($client); } } diff --git a/src/Models/Orders/LineItemReturn.php b/src/Models/Orders/LineItemReturn.php index 71fdf7a..c8a365f 100644 --- a/src/Models/Orders/LineItemReturn.php +++ b/src/Models/Orders/LineItemReturn.php @@ -2,6 +2,8 @@ namespace Piggy\Api\Models\Orders; +use stdClass; + class LineItemReturn { /** @@ -15,17 +17,14 @@ class LineItemReturn protected $quantity; /** - * @var array + * @var stdClass */ - protected $lineItem = []; + protected $lineItem; - /** - * @param array $lineItem - */ public function __construct( string $uuid, int $quantity, - array $lineItem = [] + stdClass $lineItem ) { $this->uuid = $uuid; @@ -50,9 +49,9 @@ public function getQuantity(): int } /** - * @return array + * @return stdClass */ - public function getLineItem(): array + public function getLineItem(): stdClass { return $this->lineItem; } diff --git a/src/Models/Orders/OrderReturn.php b/src/Models/Orders/OrderReturn.php index db1b27a..23dbfda 100644 --- a/src/Models/Orders/OrderReturn.php +++ b/src/Models/Orders/OrderReturn.php @@ -22,9 +22,9 @@ class OrderReturn protected $status; /** - * @var array + * @var stdClass */ - protected $order = []; + protected $order; /** * @var LineItemReturn[] @@ -42,14 +42,13 @@ class OrderReturn const resourceUri = '/api/v3/oauth/clients/order-returns'; /** - * @param array $order * @param LineItemReturn[] $lineItemReturns * @param SubLineItemReturn[] $subLineItemReturns */ public function __construct( string $uuid, string $status, - array $order = [], + stdClass $order, array $lineItemReturns = [], array $subLineItemReturns = [] ) @@ -78,9 +77,9 @@ public function getStatus(): string } /** - * @return array + * @return stdClass */ - public function getOrder(): array + public function getOrder(): stdClass { return $this->order; } diff --git a/src/Models/Orders/SubLineItemReturn.php b/src/Models/Orders/SubLineItemReturn.php index de5b452..287ebda 100644 --- a/src/Models/Orders/SubLineItemReturn.php +++ b/src/Models/Orders/SubLineItemReturn.php @@ -2,6 +2,8 @@ namespace Piggy\Api\Models\Orders; +use stdClass; + class SubLineItemReturn { /** @@ -15,17 +17,14 @@ class SubLineItemReturn protected $quantity; /** - * @var array + * @var stdClass */ - protected $subLineItem = []; + protected $subLineItem; - /** - * @param array $subLineItem - */ public function __construct( string $uuid, int $quantity, - array $subLineItem = [] + stdClass $subLineItem ) { $this->uuid = $uuid; @@ -50,9 +49,9 @@ public function getQuantity(): int } /** - * @return array + * @return stdClass */ - public function getSubLineItem(): array + public function getSubLineItem(): stdClass { return $this->subLineItem; } diff --git a/src/Resources/OAuth/Orders/OrderReturns.php b/src/Resources/OAuth/Orders/OrderReturnsResource.php similarity index 96% rename from src/Resources/OAuth/Orders/OrderReturns.php rename to src/Resources/OAuth/Orders/OrderReturnsResource.php index b0d5d03..ea2f49d 100644 --- a/src/Resources/OAuth/Orders/OrderReturns.php +++ b/src/Resources/OAuth/Orders/OrderReturnsResource.php @@ -8,7 +8,7 @@ use Piggy\Api\StaticMappers\Orders\OrderReturnMapper; use stdClass; -class OrderReturns extends BaseResource +class OrderReturnsResource extends BaseResource { /** * @var string diff --git a/src/Resources/Register/Orders/OrderReturns.php b/src/Resources/Register/Orders/OrderReturnsResource.php similarity index 96% rename from src/Resources/Register/Orders/OrderReturns.php rename to src/Resources/Register/Orders/OrderReturnsResource.php index 66c091b..9ad533a 100644 --- a/src/Resources/Register/Orders/OrderReturns.php +++ b/src/Resources/Register/Orders/OrderReturnsResource.php @@ -8,7 +8,7 @@ use Piggy\Api\StaticMappers\Orders\OrderReturnMapper; use stdClass; -class OrderReturns extends BaseResource +class OrderReturnsResource extends BaseResource { /** * @var string diff --git a/src/StaticMappers/Orders/OrderReturnMapper.php b/src/StaticMappers/Orders/OrderReturnMapper.php index 34d69ea..3e6c4c2 100644 --- a/src/StaticMappers/Orders/OrderReturnMapper.php +++ b/src/StaticMappers/Orders/OrderReturnMapper.php @@ -11,7 +11,7 @@ public static function map(stdClass $data): OrderReturn { $lineItemReturns = []; if (isset($data->line_item_returns)) { - $lineItemReturns = LineItemReturnMapper::map($data->line_item_returns); + $lineItemReturns = LineItemReturnsMapper::map($data->line_item_returns); } $subLineItemReturns = []; diff --git a/tests/OAuth/Orders/OrderReturnsResourceTest.php b/tests/OAuth/Orders/OrderReturnsResourceTest.php new file mode 100644 index 0000000..560430c --- /dev/null +++ b/tests/OAuth/Orders/OrderReturnsResourceTest.php @@ -0,0 +1,120 @@ +addExpectedResponse([ + 'uuid' => '123', + 'order' => [ + 'uuid' => '123', + ], + 'status' => 'COMPLETED', + 'line_item_returns' => [ + [ + 'uuid' => '123', + 'line_item' => [ + 'uuid' => '123', + ], + 'quantity' => 1, + ] + ], + 'sub_line_item_returns' => [] + ]); + + $return = $this->mockedClient->orderReturns->create([ + 'external_identifier' => '123', + 'status' => 'COMPLETED', + 'order' => [ + 'uuid' => '123', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'quantity' => 5, + 'reason' => 'Broken item' + ], + ], + ]); + + $this->assertEquals('123', $return->getUuid()); + } + + /** + * @test + */ + public function it_can_process_order_returns(): void + { + $this->addExpectedResponse([ + 'type' => 'points_transaction', + 'data' => [ + 'points' => 40, + 'new_balance' => 40, + ], + ]); + + $response = $this->mockedClient->orderReturns->process('123'); + + $this->assertEquals(40, $response->data->points); + $this->assertEquals(40, $response->data->new_balance); + } + + /** + * @test + */ + public function it_can_create_and_process_order_returns(): void + { + $this->addExpectedResponse([ + 'return' => [ + 'uuid' => '123', + 'order' => [ + 'uuid' => '123', + ], + 'status' => 'COMPLETED', + 'line_item_returns' => [ + [ + 'uuid' => '123', + 'line_item' => [ + 'uuid' => '123', + ], + 'quantity' => 1, + ] + ], + 'sub_line_item_returns' => [] + ], + 'result' => [ + 'type' => 'points_transaction', + 'data' => [ + 'points' => 40, + 'new_balance' => 40, + ], + ] + ]); + + $response = $this->mockedClient->orderReturns->createAndProcess([ + 'external_identifier' => '123', + 'status' => 'COMPLETED', + 'order' => [ + 'uuid' => '123', + ], + 'line_items' => [ + [ + 'uuid' => '123', + 'quantity' => 5, + 'reason' => 'Broken item' + ], + ], + ]); + + $this->assertEquals('123', $response['return']->getUuid()); + $this->assertEquals(40, $response['result']->data->points); + $this->assertEquals(40, $response['result']->data->new_balance); + } +} \ No newline at end of file