Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authorizations-related endpoints #4775

Merged
merged 13 commits into from
Oct 12, 2022
4 changes: 4 additions & 0 deletions changelog/add-authorizations-endpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Add authorizations endpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Class WC_REST_Payments_Authorizations_Controller
*
* @package WooCommerce\Payments\Admin
*/

defined( 'ABSPATH' ) || exit;

/**
* REST controller for authorizations.
*/
class WC_REST_Payments_Authorizations_Controller extends WC_Payments_REST_Controller {

/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'payments/authorizations';

/**
* Configure REST API routes.
*/
public function register_routes() {
register_rest_route(
$this->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<payment_intent_id>\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( WP_REST_Request $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( 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.
*/
public function get_authorizations_summary() {
return $this->forward_request( 'get_authorizations_summary', [] );
}
}
4 changes: 4 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
45 changes: 45 additions & 0 deletions includes/wc-payment-api/class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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' ) {
mgascam marked this conversation as resolved.
Show resolved Hide resolved
kalessil marked this conversation as resolved.
Show resolved Hide resolved
$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 ) {
kalessil marked this conversation as resolved.
Show resolved Hide resolved
return $this->request( [], self::AUTHORIZATIONS_API . '/' . $payment_intent_id, self::GET );
}
}
95 changes: 94 additions & 1 deletion tests/unit/wc-payment-api/test-class-wc-payments-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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'] );
kalessil marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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,
kalessil marked this conversation as resolved.
Show resolved Hide resolved
'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.
*
Expand Down