From 90b5e8f01c72c688c43bd5de97fdeafade54a556 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 23 Sep 2020 15:31:06 +0930 Subject: [PATCH 1/7] Add Orders API wireframe --- src/BigCommerce/Orders/OrdersApi.php | 20 ++++++++++++++++++++ tests/BigCommerce/Orders/OrdersApiTest.php | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/BigCommerce/Orders/OrdersApi.php create mode 100644 tests/BigCommerce/Orders/OrdersApiTest.php diff --git a/src/BigCommerce/Orders/OrdersApi.php b/src/BigCommerce/Orders/OrdersApi.php new file mode 100644 index 00000000..c5f83078 --- /dev/null +++ b/src/BigCommerce/Orders/OrdersApi.php @@ -0,0 +1,20 @@ +getApi()->order($orderId); + $this->assertInstanceOf(OrdersApi::class, $ordersApi); + $this->assertEquals($orderId, $ordersApi->getResourceId()); + } +} From 06359926bab87b27dc1f06df5534337247d8dc19 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Wed, 23 Sep 2020 15:45:17 +0930 Subject: [PATCH 2/7] Implement order transactions endpoint --- src/BigCommerce/Client.php | 6 ++++ src/BigCommerce/Orders/OrdersApi.php | 11 ++++++- .../ResourceModels/Order/Transaction.php | 31 +++++++++++++++++++ .../Order/TransactionsResponse.php | 25 +++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/BigCommerce/ResourceModels/Order/Transaction.php create mode 100644 src/BigCommerce/ResponseModels/Order/TransactionsResponse.php diff --git a/src/BigCommerce/Client.php b/src/BigCommerce/Client.php index 2dc45df4..50d14eeb 100644 --- a/src/BigCommerce/Client.php +++ b/src/BigCommerce/Client.php @@ -2,6 +2,7 @@ namespace BigCommerce\ApiV3; +use BigCommerce\ApiV3\Orders\OrdersApi; use BigCommerce\ApiV3\Customers\CustomersApi; use BigCommerce\ApiV3\PriceLists\PriceListsApi; use BigCommerce\ApiV3\Themes\ThemesApi; @@ -122,4 +123,9 @@ public function theme(string $uuid): ThemesApi $api->setUuid($uuid); return $api; } + + public function order(int $orderId): OrdersApi + { + return new OrdersApi($this, $orderId); + } } diff --git a/src/BigCommerce/Orders/OrdersApi.php b/src/BigCommerce/Orders/OrdersApi.php index c5f83078..93d7f5e0 100644 --- a/src/BigCommerce/Orders/OrdersApi.php +++ b/src/BigCommerce/Orders/OrdersApi.php @@ -3,11 +3,20 @@ namespace BigCommerce\ApiV3\Orders; use BigCommerce\ApiV3\Api\V3ApiBase; +use BigCommerce\ApiV3\ResponseModels\Order\TransactionsResponse; class OrdersApi extends V3ApiBase { - public function transactions() + private const ORDER_ENDPOINT = 'orders/%d'; + private const TRANSACTIONS_ENDPOINT = self::ORDER_ENDPOINT.'/transactions'; + + public function transactions(): TransactionsResponse { + $response = $this->getClient()->getRestClient()->get( + sprintf(self::TRANSACTIONS_ENDPOINT, $this->getResourceId()) + ); + + return new TransactionsResponse($response); } public function refunds() diff --git a/src/BigCommerce/ResourceModels/Order/Transaction.php b/src/BigCommerce/ResourceModels/Order/Transaction.php new file mode 100644 index 00000000..1c5256fc --- /dev/null +++ b/src/BigCommerce/ResourceModels/Order/Transaction.php @@ -0,0 +1,31 @@ +getData(); + } + + protected function resourceClass(): string + { + return Transaction::class; + } +} From 18fa1ca82f90c4618892a21a80c52608bd084de0 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 28 Sep 2020 13:47:28 +0930 Subject: [PATCH 3/7] Implement Get Transactions with test --- src/BigCommerce/Orders/OrdersApi.php | 8 ++-- tests/BigCommerce/Orders/OrdersApiTest.php | 10 ++++ .../responses/orders__transactions__get.json | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tests/BigCommerce/responses/orders__transactions__get.json diff --git a/src/BigCommerce/Orders/OrdersApi.php b/src/BigCommerce/Orders/OrdersApi.php index 93d7f5e0..7a201ffa 100644 --- a/src/BigCommerce/Orders/OrdersApi.php +++ b/src/BigCommerce/Orders/OrdersApi.php @@ -8,7 +8,7 @@ class OrdersApi extends V3ApiBase { private const ORDER_ENDPOINT = 'orders/%d'; - private const TRANSACTIONS_ENDPOINT = self::ORDER_ENDPOINT.'/transactions'; + private const TRANSACTIONS_ENDPOINT = self::ORDER_ENDPOINT . '/transactions'; public function transactions(): TransactionsResponse { @@ -19,11 +19,13 @@ public function transactions(): TransactionsResponse return new TransactionsResponse($response); } - public function refunds() + public function refunds(): RefundsApi { + return new RefundsApi($this->getClient(), null, $this->getResourceId()); } - public function refund() + public function refund(): RefundsApi { + return $this->refunds(); } } diff --git a/tests/BigCommerce/Orders/OrdersApiTest.php b/tests/BigCommerce/Orders/OrdersApiTest.php index dc444002..58902a24 100644 --- a/tests/BigCommerce/Orders/OrdersApiTest.php +++ b/tests/BigCommerce/Orders/OrdersApiTest.php @@ -15,4 +15,14 @@ public function testCanGetOrdersApi() $this->assertInstanceOf(OrdersApi::class, $ordersApi); $this->assertEquals($orderId, $ordersApi->getResourceId()); } + + public function testCanGetOrderTransactions() + { + $this->setReturnData('orders__transactions__get.json'); + $orderId = 121; + + $transactionsResponse = $this->getApi()->order($orderId)->transactions(); + $this->assertCount(1, $transactionsResponse->getTransactions()); + $this->assertEquals(1, $transactionsResponse->getPagination()->total); + } } diff --git a/tests/BigCommerce/responses/orders__transactions__get.json b/tests/BigCommerce/responses/orders__transactions__get.json new file mode 100644 index 00000000..e0e449fa --- /dev/null +++ b/tests/BigCommerce/responses/orders__transactions__get.json @@ -0,0 +1,48 @@ +{ + "data": [ + { + "id": 85926313, + "order_id": "121", + "event": "purchase", + "method": "nonce", + "amount": 1, + "currency": "USD", + "gateway": "squarev2", + "gateway_transaction_id": "pN5Kd7R9ilEI2ygBawCy7tMF|qwnAFAxRZ7tYRtIpZULg1yMF", + "status": "ok", + "test": false, + "fraud_review": false, + "reference_transaction_id": null, + "date_created": "2018-05-08T15:06:12+00:00", + "avs_result": { + "code": "", + "message": "", + "street_match": "", + "postal_match": "" + }, + "cvv_result": { + "code": "", + "message": "" + }, + "credit_card": {}, + "gift_certificate": {}, + "store_credit": {}, + "offline": {}, + "custom": {}, + "payment_instrument_token": "qwsdfghyuio", + "payment_method_id": "squarev2.card" + } + ], + "meta": { + "pagination": { + "total": 1, + "count": 1, + "per_page": 50, + "current_page": 1, + "total_pages": 1, + "links": { + "current": "?page=1&limit=50" + } + } + } +} \ No newline at end of file From e7abe6568b05d098f6d6c9fc05cc56ab5cfec16e Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 28 Sep 2020 14:17:17 +0930 Subject: [PATCH 4/7] Implement Order Create Refund --- src/BigCommerce/Orders/RefundsApi.php | 56 +++++++++++++++++++ .../ResourceModels/Order/OrderRefundItem.php | 21 +++++++ .../ResourceModels/Order/Refund.php | 29 ++++++++++ .../ResourceModels/Order/RefundQuote.php | 16 ++++++ .../Order/RefundQuoteResponse.php | 22 ++++++++ .../ResponseModels/Order/RefundResponse.php | 22 ++++++++ .../ResponseModels/Order/RefundsResponse.php | 21 +++++++ tests/BigCommerce/Orders/RefundsApiTest.php | 55 ++++++++++++++++++ .../orders__order_refunds__create.json | 31 ++++++++++ .../orders__order_refunds__create_quote.json | 14 +++++ 10 files changed, 287 insertions(+) create mode 100644 src/BigCommerce/Orders/RefundsApi.php create mode 100644 src/BigCommerce/ResourceModels/Order/OrderRefundItem.php create mode 100644 src/BigCommerce/ResourceModels/Order/Refund.php create mode 100644 src/BigCommerce/ResourceModels/Order/RefundQuote.php create mode 100644 src/BigCommerce/ResponseModels/Order/RefundQuoteResponse.php create mode 100644 src/BigCommerce/ResponseModels/Order/RefundResponse.php create mode 100644 src/BigCommerce/ResponseModels/Order/RefundsResponse.php create mode 100644 tests/BigCommerce/Orders/RefundsApiTest.php create mode 100644 tests/BigCommerce/responses/orders__order_refunds__create.json create mode 100644 tests/BigCommerce/responses/orders__order_refunds__create_quote.json diff --git a/src/BigCommerce/Orders/RefundsApi.php b/src/BigCommerce/Orders/RefundsApi.php new file mode 100644 index 00000000..aaa5cf08 --- /dev/null +++ b/src/BigCommerce/Orders/RefundsApi.php @@ -0,0 +1,56 @@ +getClient()->getRestClient()->post( + sprintf(self::REFUND_ENDPOINT, $this->getParentResourceId()), + [ + RequestOptions::JSON => [] + ] + ); + + return new RefundResponse($response); + } + + public function createQuote(): RefundQuoteResponse + { + $response = $this->getClient()->getRestClient()->post( + sprintf(self::REFUND_QUOTE_ENDPOINT, $this->getParentResourceId()), + [ + RequestOptions::JSON => [] + ] + ); + + return new RefundQuoteResponse($response); + } + + public function getAll(): RefundsResponse + { + $response = $this->getClient()->getRestClient()->get( + sprintf(self::REFUND_ENDPOINT, $this->getParentResourceId()) + ); + + return new RefundsResponse($response); + } +} diff --git a/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php b/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php new file mode 100644 index 00000000..3a8c7984 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php @@ -0,0 +1,21 @@ +items = array_map(fn($i) => new OrderRefundItem($i), $optionObject->items); + unset($optionObject->items); + parent::__construct($optionObject); + } +} diff --git a/src/BigCommerce/ResourceModels/Order/RefundQuote.php b/src/BigCommerce/ResourceModels/Order/RefundQuote.php new file mode 100644 index 00000000..1ddf8157 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Order/RefundQuote.php @@ -0,0 +1,16 @@ +quote; + } + + protected function addData(stdClass $rawData): void + { + $this->quote = new RefundQuote($rawData); + } +} diff --git a/src/BigCommerce/ResponseModels/Order/RefundResponse.php b/src/BigCommerce/ResponseModels/Order/RefundResponse.php new file mode 100644 index 00000000..bd7a33dd --- /dev/null +++ b/src/BigCommerce/ResponseModels/Order/RefundResponse.php @@ -0,0 +1,22 @@ +refund; + } + + protected function addData(stdClass $rawData): void + { + $this->refund = new Refund($rawData); + } +} diff --git a/src/BigCommerce/ResponseModels/Order/RefundsResponse.php b/src/BigCommerce/ResponseModels/Order/RefundsResponse.php new file mode 100644 index 00000000..6f023769 --- /dev/null +++ b/src/BigCommerce/ResponseModels/Order/RefundsResponse.php @@ -0,0 +1,21 @@ +getData(); + } + protected function resourceClass(): string + { + return RefundQuote::class; + } +} diff --git a/tests/BigCommerce/Orders/RefundsApiTest.php b/tests/BigCommerce/Orders/RefundsApiTest.php new file mode 100644 index 00000000..c06d312f --- /dev/null +++ b/tests/BigCommerce/Orders/RefundsApiTest.php @@ -0,0 +1,55 @@ +getApi()->order($orderId)->refunds(); + $this->assertInstanceOf(RefundsApi::class, $refundsApi); + $this->assertEquals($orderId, $refundsApi->getParentResourceId()); + } + + public function testCanCreateQuoteRefund() + { + $this->setReturnData('orders__order_refunds__create_quote.json', 201); + $orderId = 1; + + $response = $this->getApi()->order($orderId)->refunds()->createQuote(); + $this->assertEquals($orderId, $response->getQuote()->order_id); + } + + public function testCanGetAll() + { + $this->markTestIncomplete(); + } + + public function testCanCreateRefund() + { + $this->setReturnData('orders__order_refunds__create.json', 201); + $orderId = 1; + $reason = 'test reason'; + + $item = new OrderRefundItem(); + $item->item_id = 1; + $item->item_type = OrderRefundItem::ITEM_TYPE__HANDLING; + $item->quantity = 1; + $item->reason = ""; + + $response = $this->getApi()->order($orderId)->refunds()->create([$item], $reason); + $refund = $response->getRefund(); + $responseItem = $refund->items[0]; + + $this->assertEquals($reason, $refund->reason); + $this->assertInstanceOf(OrderRefundItem::class, $responseItem); + $this->assertEquals($item->item_id, $responseItem->item_id); + $this->assertEquals($item->item_type, $responseItem->item_type); + } +} diff --git a/tests/BigCommerce/responses/orders__order_refunds__create.json b/tests/BigCommerce/responses/orders__order_refunds__create.json new file mode 100644 index 00000000..724e56e2 --- /dev/null +++ b/tests/BigCommerce/responses/orders__order_refunds__create.json @@ -0,0 +1,31 @@ +{ + "data": { + "id": 1, + "order_id": 1, + "user_id": 1, + "created": "", + "reason": "test reason", + "total_amount": 1.99, + "total_tax": 1, + "items": [ + { + "item_type": "HANDLING", + "item_id": 1, + "reason": "", + "quantity": 1, + "requested_amount": 0.05 + } + ], + "payments": [ + { + "id": 1, + "provider_id": "checkout_paypalexpress", + "amount": 1.99, + "offline": true, + "is_declined": true, + "declined_message": "" + } + ] + }, + "meta": {} +} diff --git a/tests/BigCommerce/responses/orders__order_refunds__create_quote.json b/tests/BigCommerce/responses/orders__order_refunds__create_quote.json new file mode 100644 index 00000000..30b135be --- /dev/null +++ b/tests/BigCommerce/responses/orders__order_refunds__create_quote.json @@ -0,0 +1,14 @@ +{ + "data": { + "order_id": 1, + "total_refund_amount": 1.99, + "total_refund_tax_amount": 1.95, + "rounding": 1, + "adjustment": 1.99, + "tax_inclusive": true, + "refund_methods": [ + "" + ] + }, + "meta": {} +} \ No newline at end of file From 6074d84b71b622df2219d1cd5487abc1fb1a6b1a Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 28 Sep 2020 14:17:52 +0930 Subject: [PATCH 5/7] Fix codestyle --- src/BigCommerce/ResourceModels/Order/OrderRefundItem.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php b/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php index 3a8c7984..72bb2e1a 100644 --- a/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php +++ b/src/BigCommerce/ResourceModels/Order/OrderRefundItem.php @@ -1,9 +1,7 @@ Date: Mon, 28 Sep 2020 14:25:41 +0930 Subject: [PATCH 6/7] Implement test for get all refunds --- .../ResponseModels/Order/RefundsResponse.php | 6 +-- tests/BigCommerce/Orders/RefundsApiTest.php | 8 +++- .../orders__order_refunds__get_all.json | 42 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 tests/BigCommerce/responses/orders__order_refunds__get_all.json diff --git a/src/BigCommerce/ResponseModels/Order/RefundsResponse.php b/src/BigCommerce/ResponseModels/Order/RefundsResponse.php index 6f023769..74aae1f1 100644 --- a/src/BigCommerce/ResponseModels/Order/RefundsResponse.php +++ b/src/BigCommerce/ResponseModels/Order/RefundsResponse.php @@ -2,13 +2,13 @@ namespace BigCommerce\ApiV3\ResponseModels\Order; -use BigCommerce\ApiV3\ResourceModels\Order\RefundQuote; +use BigCommerce\ApiV3\ResourceModels\Order\Refund; use BigCommerce\ApiV3\ResponseModels\PaginatedResponse; class RefundsResponse extends PaginatedResponse { /** - * @return RefundQuote[] + * @return Refund[] */ public function refunds(): array { @@ -16,6 +16,6 @@ public function refunds(): array } protected function resourceClass(): string { - return RefundQuote::class; + return Refund::class; } } diff --git a/tests/BigCommerce/Orders/RefundsApiTest.php b/tests/BigCommerce/Orders/RefundsApiTest.php index c06d312f..8a4ddbda 100644 --- a/tests/BigCommerce/Orders/RefundsApiTest.php +++ b/tests/BigCommerce/Orders/RefundsApiTest.php @@ -28,7 +28,13 @@ public function testCanCreateQuoteRefund() public function testCanGetAll() { - $this->markTestIncomplete(); + $this->setReturnData('orders__order_refunds__get_all.json', 201); + $orderId = 1; + + $response = $this->getApi()->order($orderId)->refunds()->getAll(); + $this->assertCount(1, $response->refunds()); + $this->assertCount(1, $response->refunds()[0]->items); + $this->assertInstanceOf(OrderRefundItem::class, $response->refunds()[0]->items[0]); } public function testCanCreateRefund() diff --git a/tests/BigCommerce/responses/orders__order_refunds__get_all.json b/tests/BigCommerce/responses/orders__order_refunds__get_all.json new file mode 100644 index 00000000..95f20b47 --- /dev/null +++ b/tests/BigCommerce/responses/orders__order_refunds__get_all.json @@ -0,0 +1,42 @@ +{ + "data": [{ + "id": 1, + "order_id": 1, + "user_id": 1, + "created": "", + "reason": "test reason", + "total_amount": 1.99, + "total_tax": 1, + "items": [ + { + "item_type": "HANDLING", + "item_id": 1, + "reason": "", + "quantity": 1, + "requested_amount": 0.05 + } + ], + "payments": [ + { + "id": 1, + "provider_id": "checkout_paypalexpress", + "amount": 1.99, + "offline": true, + "is_declined": true, + "declined_message": "" + } + ] + }], + "meta": { + "pagination": { + "total": 1, + "count": 1, + "per_page": 50, + "current_page": 1, + "total_pages": 1, + "links": { + "current": "?page=1&limit=50" + } + } + } +} From 30accb317cff63a4cf3951d09b75babf39a20aa7 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 28 Sep 2020 14:26:15 +0930 Subject: [PATCH 7/7] Update README and release notes, noting the coverage of the Orders V3 API --- README.md | 4 ++-- RELEASE_NOTES.md | 11 +++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 65bdfe3a..f483b89a 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,8 @@ Running tests: `composer run-script test` #### Orders (V3) -- ☐ Transactions -- ☐ Order Refunds +- ☑️ Transactions +- ☑️ Order Refunds #### Payment Methods diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 00a2ab67..f55391ba 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,13 +1,8 @@ #### New Features -Add support for the [Themes API](https://developer.bigcommerce.com/api-reference/store-management/themes) +Add support for the [Orders v3 API](https://developer.bigcommerce.com/api-reference/store-management/order-transactions) Includes -- Themes -- Theme Actions -- Theme Jobs - -#### Code Improvements - -- Refactor PaginatedResponse to be simpler to implement \ No newline at end of file +- Transactions +- Order refunds