From 01d438ee1d1b475d40f559433841bd9a65ca1f86 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 16 Sep 2022 12:19:42 +0100 Subject: [PATCH 01/10] add authorizations endpoint controller --- ...est-payments-authorizations-controller.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 includes/admin/class-wc-rest-payments-authorizations-controller.php diff --git a/includes/admin/class-wc-rest-payments-authorizations-controller.php b/includes/admin/class-wc-rest-payments-authorizations-controller.php new file mode 100644 index 00000000000..7a66941a00e --- /dev/null +++ b/includes/admin/class-wc-rest-payments-authorizations-controller.php @@ -0,0 +1,86 @@ +namespace, + '/' . $this->rest_base, + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_authorizations' ], + 'permission_callback' => [ $this, 'check_permission' ], + ] + ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/summary', + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_authorizations_summary' ], + 'permission_callback' => [ $this, 'check_permission' ], + ] + ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P\w+)', + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'get_authorization' ], + 'permission_callback' => [ $this, 'check_permission' ], + ] + ); + } + + /** + * Retrieve authorizations to respond with via API. + * + * @param WP_REST_Request $request Full data about the request. + */ + public function get_authorizations( $request ) { + $page = (int) $request->get_param( 'page' ); + $page_size = (int) $request->get_param( 'pagesize' ); + $sort = $request->get_param( 'sort' ); + $direction = $request->get_param( 'direction' ); + return $this->forward_request( 'list_authorizations', [ $page, $page_size, $sort, $direction ] ); + } + + /** + * Retrieve authorization to respond with via API. + * + * @param WP_REST_Request $request Full data about the request. + */ + public function get_authorization( $request ) { + $payment_intent_id = $request->get_param( 'payment_intent_id' ); + return $this->forward_request( 'get_authorization', [ $payment_intent_id ] ); + } + + /** + * Retrieve authorizations summary to respond with via API. + * + * @param WP_REST_Request $request Full data about the request. + */ + public function get_authorizations_summary( $request ) { + return $this->forward_request( 'get_authorizations_summary', [] ); + } +} From 633690b6ec4664f7038e240d46858422f92f7e51 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 16 Sep 2022 12:19:55 +0100 Subject: [PATCH 02/10] add new controller to path --- includes/class-wc-payments.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index 9f376b4662d..c99ffe253de 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -740,6 +740,10 @@ public static function init_rest_api() { include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-payment-intents-controller.php'; $payment_intents_controller = new WC_REST_Payments_Payment_Intents_Controller( self::$api_client ); $payment_intents_controller->register_routes(); + + include_once WCPAY_ABSPATH . 'includes/admin/class-wc-rest-payments-authorizations-controller.php'; + $authorizations_controller = new WC_REST_Payments_Authorizations_Controller( self::$api_client ); + $authorizations_controller->register_routes(); } /** From bf827ddd9e6998addef607c2006122bbdf03dba0 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 16 Sep 2022 12:21:25 +0100 Subject: [PATCH 03/10] add new functions to pull data from server --- .../class-wc-payments-api-client.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/includes/wc-payment-api/class-wc-payments-api-client.php b/includes/wc-payment-api/class-wc-payments-api-client.php index ec581b4d289..18e5c35da0b 100644 --- a/includes/wc-payment-api/class-wc-payments-api-client.php +++ b/includes/wc-payment-api/class-wc-payments-api-client.php @@ -65,6 +65,7 @@ class WC_Payments_API_Client { const DOCUMENTS_API = 'documents'; const VAT_API = 'vat'; const LINKS_API = 'links'; + const AUTHORIZATIONS_API = 'authorizations'; /** * Common keys in API requests/responses that we might want to redact. @@ -2562,4 +2563,48 @@ private function get_fingerprint_metadata(): array { return $customer_fingerprint_metadata; } + + /** + * List authorizations + * + * @param int $page The requested page. + * @param int $page_size The size of the requested page. + * @param string $sort The column to be used for sorting. + * @param string $direction The sorting direction. + * + * @return array + * @throws API_Exception - Exception thrown on request failure. + */ + public function list_authorizations( $page = 0, $page_size = 25, $sort = 'created', $direction = 'desc' ) { + $query = [ + 'page' => $page, + 'pagesize' => $page_size, + 'sort' => $sort, + 'direction' => $direction, + ]; + + $authorizations = $this->request( $query, self::AUTHORIZATIONS_API, self::GET ); + + return $authorizations; + } + + /** + * Return summary for authorizations. + * + * @return array The authorizations summary. + * @throws API_Exception Exception thrown on request failure. + */ + public function get_authorizations_summary() { + return $this->request( [], self::AUTHORIZATIONS_API . '/summary', self::GET ); + } + + /** + * Fetch a single authorizations with provided payment intent id. + * + * @param string $payment_intent_id id of requested transaction. + * @return array authorization object. + */ + public function get_authorization( $payment_intent_id ) { + return $this->request( [], self::AUTHORIZATIONS_API . '/' . $payment_intent_id, self::GET ); + } } From 611f70b85b25ddaff24c38e33337f28ddc408d58 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 23 Sep 2022 10:17:03 +0100 Subject: [PATCH 04/10] add tests for authorizations --- .../test-class-wc-payments-api-client.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php index fda5649409e..07e6324d050 100644 --- a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php +++ b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php @@ -1987,6 +1987,99 @@ public function test_get_document_error() { $this->payments_api_client->get_document( 'someDocument' ); } + /** + * Test a successful fetch of a single authorization. + * + * @throws Exception In case of test failure. + */ + public function test_get_authorization_success() { + $payment_intent_id = 'pi_123smtm'; + + $this->set_http_mock_response( + 200, + [ + 'payment_intent_id' => $payment_intent_id, + ] + ); + + $authorization = $this->payments_api_client->get_authorization( $payment_intent_id ); + $this->assertEquals( $payment_intent_id, $authorization['payment_intent_id'] ); + } + + /** + * Test fetching of non existing authorization. + * + * @throws Exception In case of test failure. + */ + public function test_get_authorization_not_found() { + $payment_intent_id = 'pi_123smtm'; + $error_code = 'authorization_missing'; + $error_message = 'The authorization you asked for does not exist'; + + $this->set_http_mock_response( + 404, + [ + 'error' => [ + 'code' => $error_code, + 'message' => $error_message, + ], + ] + ); + $this->expectException( Exception::class ); + $this->expectExceptionMessage( "Error: $error_message" ); + + $this->payments_api_client->get_authorization( $payment_intent_id ); + } + + /** + * Test a successful fetch of a list of authorizations. + * + * @throws Exception In case of test failure. + */ + public function test_list_authorizations_success() { + $payment_intent_id_1 = 'pi_123dytycd'; + $payment_intent_id_2 = 'pi_123dbap'; + + $this->set_http_mock_response( + 200, + [ + 'data' => [ + [ + 'payment_intent_id' => $payment_intent_id_1, + ], + [ + 'payment_intent_id' => $payment_intent_id_2, + ], + ], + ] + ); + + $authorizations = $this->payments_api_client->list_authorizations(); + + $this->assertSame( $payment_intent_id_1, $authorizations['data'][0]['payment_intent_id'] ); + $this->assertSame( $payment_intent_id_2, $authorizations['data'][1]['payment_intent_id'] ); + } + + /** + * Test a successful fetch of authorizations summary. + * + * @throws Exception In case of test failure. + */ + public function test_authorizations_summary_success() { + $this->set_http_mock_response( + 200, + [ + 'count' => 123, + 'total' => 1200, + ] + ); + + $summary = $this->payments_api_client->get_authorizations_summary(); + + $this->assertSame( 123, $summary['count'] ); + $this->assertSame( 1200, $summary['total'] ); + } + /** * Set up http mock response. * From e3d190ac3301e33d901a35e256035bab5c41136d Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 23 Sep 2022 10:17:23 +0100 Subject: [PATCH 05/10] fix small typo in transactions test description --- tests/unit/wc-payment-api/test-class-wc-payments-api-client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php index 07e6324d050..178a9847fb2 100644 --- a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php +++ b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php @@ -508,7 +508,7 @@ public function test_get_transaction_not_found() { } /** - * Test a successful fetch of a single transaction. + * Test a successful fetch of a list of transactions. * * @throws Exception In case of test failure. */ From 4d98d0986aad8b8e7d5ed5a54a8ba45ec9988d51 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Tue, 27 Sep 2022 09:59:31 +0100 Subject: [PATCH 06/10] add type to function params --- .../class-wc-rest-payments-authorizations-controller.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/includes/admin/class-wc-rest-payments-authorizations-controller.php b/includes/admin/class-wc-rest-payments-authorizations-controller.php index 7a66941a00e..aa05ac791b8 100644 --- a/includes/admin/class-wc-rest-payments-authorizations-controller.php +++ b/includes/admin/class-wc-rest-payments-authorizations-controller.php @@ -57,7 +57,7 @@ public function register_routes() { * * @param WP_REST_Request $request Full data about the request. */ - public function get_authorizations( $request ) { + public function get_authorizations( WP_REST_Request $request ) { $page = (int) $request->get_param( 'page' ); $page_size = (int) $request->get_param( 'pagesize' ); $sort = $request->get_param( 'sort' ); @@ -70,17 +70,15 @@ public function get_authorizations( $request ) { * * @param WP_REST_Request $request Full data about the request. */ - public function get_authorization( $request ) { + public function get_authorization( WP_REST_Request $request ) { $payment_intent_id = $request->get_param( 'payment_intent_id' ); return $this->forward_request( 'get_authorization', [ $payment_intent_id ] ); } /** * Retrieve authorizations summary to respond with via API. - * - * @param WP_REST_Request $request Full data about the request. */ - public function get_authorizations_summary( $request ) { + public function get_authorizations_summary() { return $this->forward_request( 'get_authorizations_summary', [] ); } } From bd6f9d37af9611ace2b9587babac27b400a62c18 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Tue, 27 Sep 2022 10:49:31 +0100 Subject: [PATCH 07/10] add changelog --- changelog/add-authorizations-endpoints | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/add-authorizations-endpoints diff --git a/changelog/add-authorizations-endpoints b/changelog/add-authorizations-endpoints new file mode 100644 index 00000000000..2bb328d9387 --- /dev/null +++ b/changelog/add-authorizations-endpoints @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Add authorizations endpoints From b0a6b70cc5903e1ce29dc60fd26faea0de4a7db0 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 30 Sep 2022 12:10:15 +0100 Subject: [PATCH 08/10] Strict check in test using assertSame --- tests/unit/wc-payment-api/test-class-wc-payments-api-client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php index 5f8e73c718f..367a8bdf12c 100644 --- a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php +++ b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php @@ -2003,7 +2003,7 @@ public function test_get_authorization_success() { ); $authorization = $this->payments_api_client->get_authorization( $payment_intent_id ); - $this->assertEquals( $payment_intent_id, $authorization['payment_intent_id'] ); + $this->assertSame( $payment_intent_id, $authorization['payment_intent_id'] ); } /** From f452270d12a7811d320cc0ef3375bc681611495c Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 30 Sep 2022 12:10:46 +0100 Subject: [PATCH 09/10] add strict types to functions --- includes/wc-payment-api/class-wc-payments-api-client.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/includes/wc-payment-api/class-wc-payments-api-client.php b/includes/wc-payment-api/class-wc-payments-api-client.php index b1a0c025ac9..fedd12b3966 100644 --- a/includes/wc-payment-api/class-wc-payments-api-client.php +++ b/includes/wc-payment-api/class-wc-payments-api-client.php @@ -2573,7 +2573,7 @@ private function get_fingerprint_metadata(): array { * @return array * @throws API_Exception - Exception thrown on request failure. */ - public function list_authorizations( $page = 0, $page_size = 25, $sort = 'created', $direction = 'desc' ) { + public function list_authorizations( int $page = 0, int $page_size = 25, string $sort = 'created', string $direction = 'desc' ) { $query = [ 'page' => $page, 'pagesize' => $page_size, @@ -2581,9 +2581,7 @@ public function list_authorizations( $page = 0, $page_size = 25, $sort = 'create 'direction' => $direction, ]; - $authorizations = $this->request( $query, self::AUTHORIZATIONS_API, self::GET ); - - return $authorizations; + return $this->request( $query, self::AUTHORIZATIONS_API, self::GET ); } /** @@ -2602,7 +2600,7 @@ public function get_authorizations_summary() { * @param string $payment_intent_id id of requested transaction. * @return array authorization object. */ - public function get_authorization( $payment_intent_id ) { + public function get_authorization( string $payment_intent_id ) { return $this->request( [], self::AUTHORIZATIONS_API . '/' . $payment_intent_id, self::GET ); } } From f88b5cfd6fe775a6aaa23b94f19d73fab08e086c Mon Sep 17 00:00:00 2001 From: Ahmed Date: Fri, 30 Sep 2022 12:12:17 +0100 Subject: [PATCH 10/10] Inline error code in test --- .../unit/wc-payment-api/test-class-wc-payments-api-client.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php index 367a8bdf12c..59799a61f80 100644 --- a/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php +++ b/tests/unit/wc-payment-api/test-class-wc-payments-api-client.php @@ -2013,14 +2013,13 @@ public function test_get_authorization_success() { */ public function test_get_authorization_not_found() { $payment_intent_id = 'pi_123smtm'; - $error_code = 'authorization_missing'; $error_message = 'The authorization you asked for does not exist'; $this->set_http_mock_response( 404, [ 'error' => [ - 'code' => $error_code, + 'code' => 'authorization_missing', 'message' => $error_message, ], ]