From 539c3997652a6d0297f9155f8e941163b13fb29c Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 12 Nov 2020 14:27:31 +1030 Subject: [PATCH 1/3] Implement the V3 APIs for payment processing --- src/BigCommerce/Client.php | 6 +++ .../Payments/PaymentsProcessingApi.php | 42 +++++++++++++++ .../ResourceModels/Order/Order.php | 16 ++++++ .../Payment/PaymentAccessToken.php | 10 ++++ .../ResourceModels/Payment/PaymentMethod.php | 15 ++++++ .../AcceptedPaymentMethodsResponse.php | 22 ++++++++ .../Payment/PaymentAccessTokenResponse.php | 22 ++++++++ .../Payments/PaymentsProcessingApiTest.php | 27 ++++++++++ ...payments__create_payment_access_token.json | 5 ++ ...ayments__get_accepted_payment_methods.json | 53 +++++++++++++++++++ 10 files changed, 218 insertions(+) create mode 100644 src/BigCommerce/Payments/PaymentsProcessingApi.php create mode 100644 src/BigCommerce/ResourceModels/Order/Order.php create mode 100644 src/BigCommerce/ResourceModels/Payment/PaymentAccessToken.php create mode 100644 src/BigCommerce/ResourceModels/Payment/PaymentMethod.php create mode 100644 src/BigCommerce/ResponseModels/Payment/AcceptedPaymentMethodsResponse.php create mode 100644 src/BigCommerce/ResponseModels/Payment/PaymentAccessTokenResponse.php create mode 100644 tests/BigCommerce/Payments/PaymentsProcessingApiTest.php create mode 100644 tests/BigCommerce/responses/payments__create_payment_access_token.json create mode 100644 tests/BigCommerce/responses/payments__get_accepted_payment_methods.json diff --git a/src/BigCommerce/Client.php b/src/BigCommerce/Client.php index 50d14eeb..d72fee63 100644 --- a/src/BigCommerce/Client.php +++ b/src/BigCommerce/Client.php @@ -4,6 +4,7 @@ use BigCommerce\ApiV3\Orders\OrdersApi; use BigCommerce\ApiV3\Customers\CustomersApi; +use BigCommerce\ApiV3\Payments\PaymentsProcessingApi; use BigCommerce\ApiV3\PriceLists\PriceListsApi; use BigCommerce\ApiV3\Themes\ThemesApi; use GuzzleHttp\HandlerStack; @@ -128,4 +129,9 @@ public function order(int $orderId): OrdersApi { return new OrdersApi($this, $orderId); } + + public function payments(): PaymentsProcessingApi + { + return new PaymentsProcessingApi($this); + } } diff --git a/src/BigCommerce/Payments/PaymentsProcessingApi.php b/src/BigCommerce/Payments/PaymentsProcessingApi.php new file mode 100644 index 00000000..93096796 --- /dev/null +++ b/src/BigCommerce/Payments/PaymentsProcessingApi.php @@ -0,0 +1,42 @@ +getClient()->getRestClient()->post( + self::ACCESS_TOKENS_ENDPOINT, + [ + RequestOptions::JSON => json_encode($order), + ] + ); + + return new PaymentAccessTokenResponse($response); + } + + public function paymentMethods(int $orderId): AcceptedPaymentMethodsResponse + { + $response = $this->getClient()->getRestClient()->get( + self::PAYMENT_METHODS_ENDPOINT, + [ + RequestOptions::QUERY => [ + 'order_id' => $orderId, + ] + ] + ); + + return new AcceptedPaymentMethodsResponse($response); + } +} diff --git a/src/BigCommerce/ResourceModels/Order/Order.php b/src/BigCommerce/ResourceModels/Order/Order.php new file mode 100644 index 00000000..43b2fb77 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Order/Order.php @@ -0,0 +1,16 @@ +id = $id; + $this->is_recurring = $is_recurring; + } +} diff --git a/src/BigCommerce/ResourceModels/Payment/PaymentAccessToken.php b/src/BigCommerce/ResourceModels/Payment/PaymentAccessToken.php new file mode 100644 index 00000000..ac720582 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Payment/PaymentAccessToken.php @@ -0,0 +1,10 @@ +getData(); + } + + protected function resourceClass(): string + { + return PaymentMethod::class; + } +} diff --git a/src/BigCommerce/ResponseModels/Payment/PaymentAccessTokenResponse.php b/src/BigCommerce/ResponseModels/Payment/PaymentAccessTokenResponse.php new file mode 100644 index 00000000..9d1fb950 --- /dev/null +++ b/src/BigCommerce/ResponseModels/Payment/PaymentAccessTokenResponse.php @@ -0,0 +1,22 @@ +token; + } + + protected function addData(stdClass $rawData): void + { + $this->token = new PaymentAccessToken($rawData); + } +} diff --git a/tests/BigCommerce/Payments/PaymentsProcessingApiTest.php b/tests/BigCommerce/Payments/PaymentsProcessingApiTest.php new file mode 100644 index 00000000..dbf464d3 --- /dev/null +++ b/tests/BigCommerce/Payments/PaymentsProcessingApiTest.php @@ -0,0 +1,27 @@ +setReturnData('payments__create_payment_access_token.json', 201); + $order = new Order(1, false); + + $token = $this->getApi()->payments()->createToken($order)->getToken(); + $this->assertEquals('abcdefg', $token->id); + } + + public function testCanGetPaymentMethodsForOrder(): void + { + $this->setReturnData('payments__get_accepted_payment_methods.json'); + $methods = $this->getApi()->payments()->paymentMethods(123)->getMethods(); + + $this->assertCount(1, $methods); + $this->assertEquals('stripe.card', $methods[0]->id); + } +} diff --git a/tests/BigCommerce/responses/payments__create_payment_access_token.json b/tests/BigCommerce/responses/payments__create_payment_access_token.json new file mode 100644 index 00000000..8198abf5 --- /dev/null +++ b/tests/BigCommerce/responses/payments__create_payment_access_token.json @@ -0,0 +1,5 @@ +{ + "data": { + "id": "abcdefg" + } +} \ No newline at end of file diff --git a/tests/BigCommerce/responses/payments__get_accepted_payment_methods.json b/tests/BigCommerce/responses/payments__get_accepted_payment_methods.json new file mode 100644 index 00000000..ac3a586e --- /dev/null +++ b/tests/BigCommerce/responses/payments__get_accepted_payment_methods.json @@ -0,0 +1,53 @@ +{ + "data": [ + { + "id": "stripe.card", + "name": "Stripe", + "test_mode": true, + "type": "card", + "supported_instruments": [ + { + "instrument_type": "VISA", + "verification_value_required": true + }, + { + "instrument_type": "MASTERCARD", + "verification_value_required": true + }, + { + "instrument_type": "AMEX", + "verification_value_required": true + }, + { + "instrument_type": "DISCOVER", + "verification_value_required": true + }, + { + "instrument_type": "JCB", + "verification_value_required": true + }, + { + "instrument_type": "DINERS_CLUB", + "verification_value_required": true + }, + { + "instrument_type": "STORED_CARD", + "verification_value_required": true + } + ], + "stored_instruments": [ + { + "type": "stored_card", + "brand": "VISA", + "expiry_month": 9, + "expiry_year": 2020, + "issuer_identification_number": "424242", + "last_4": "4242", + "token": "050a1e5c982e5905288ec5ec33f292772762033a0704f46fccb16bf1940b51ef", + "is_default": true + } + ] + } + ], + "meta": {} +} From ecf275b4982178f42b1031fa0a600b8a9148b1fb Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 12 Nov 2020 14:37:54 +1030 Subject: [PATCH 2/3] Update readme and release notes --- README.md | 4 ++-- RELEASE_NOTES.md | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f483b89a..6dc69ab6 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,8 @@ Running tests: `composer run-script test` #### Payment Methods -- ☐ Payment Access Token -- ☐ Payment Methods +- ☑️ Payment Access Token +- ☑️ Payment Methods #### Scripts diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index a2353235..9bc9f3f1 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,8 @@ +## New Features -# Minor Update +- Implement V3 Payment endpoints (Create Payment Access Token, Get Accepted Payment Methods) + +## Minor Update - Bump guzzlehttp/guzzle from 7.1.1 to 7.2.0 From 070e771d1990818c4f9f4851b49e484b99abf45e Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 12 Nov 2020 14:54:49 +1030 Subject: [PATCH 3/3] Fix code style issues --- src/BigCommerce/Payments/PaymentsProcessingApi.php | 4 ++-- src/BigCommerce/ResourceModels/Order/Order.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/BigCommerce/Payments/PaymentsProcessingApi.php b/src/BigCommerce/Payments/PaymentsProcessingApi.php index 93096796..79bdfebe 100644 --- a/src/BigCommerce/Payments/PaymentsProcessingApi.php +++ b/src/BigCommerce/Payments/PaymentsProcessingApi.php @@ -11,8 +11,8 @@ class PaymentsProcessingApi extends V3ApiBase { private const PAYMENTS_ENDPOINT = 'payments/'; - private const ACCESS_TOKENS_ENDPOINT = self::PAYMENTS_ENDPOINT.'access_tokens'; - private const PAYMENT_METHODS_ENDPOINT = self::PAYMENTS_ENDPOINT.'methods'; + private const ACCESS_TOKENS_ENDPOINT = self::PAYMENTS_ENDPOINT . 'access_tokens'; + private const PAYMENT_METHODS_ENDPOINT = self::PAYMENTS_ENDPOINT . 'methods'; public function createToken(Order $order): PaymentAccessTokenResponse { diff --git a/src/BigCommerce/ResourceModels/Order/Order.php b/src/BigCommerce/ResourceModels/Order/Order.php index 43b2fb77..7ebcc3f6 100644 --- a/src/BigCommerce/ResourceModels/Order/Order.php +++ b/src/BigCommerce/ResourceModels/Order/Order.php @@ -2,7 +2,6 @@ namespace BigCommerce\ApiV3\ResourceModels\Order; - class Order { public int $id;