mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
✨ Standardize REST response format
This commit is contained in:
parent
a0910962b4
commit
b7ef3242bf
5 changed files with 62 additions and 31 deletions
|
@ -109,11 +109,7 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
$this->field_map
|
||||
);
|
||||
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'data' => $js_data,
|
||||
)
|
||||
);
|
||||
return $this->return_success( $js_data );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -126,33 +126,22 @@ class ConnectManualRestEndpoint extends RestEndpoint {
|
|||
$use_sandbox = (bool) ( $data['use_sandbox'] ?? false );
|
||||
|
||||
if ( empty( $client_id ) || empty( $client_secret ) ) {
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'success' => false,
|
||||
'message' => 'No client ID or secret provided.',
|
||||
)
|
||||
);
|
||||
return $this->return_error( 'No client ID or secret provided.' );
|
||||
}
|
||||
|
||||
try {
|
||||
$payee = $this->request_payee( $client_id, $client_secret, $use_sandbox );
|
||||
} catch ( Exception $exception ) {
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'success' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
)
|
||||
);
|
||||
|
||||
return $this->return_error( $exception->getMessage() );
|
||||
}
|
||||
|
||||
$result = array(
|
||||
'merchantId' => $payee->merchant_id,
|
||||
'email' => $payee->email_address,
|
||||
'success' => true,
|
||||
return $this->return_success(
|
||||
array(
|
||||
'merchantId' => $payee->merchant_id,
|
||||
'email' => $payee->email_address,
|
||||
'success' => true,
|
||||
)
|
||||
);
|
||||
|
||||
return rest_ensure_response( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -93,12 +93,9 @@ class LoginLinkRestEndpoint extends RestEndpoint {
|
|||
try {
|
||||
$url = $url_generator->generate( $products );
|
||||
|
||||
return rest_ensure_response( $url );
|
||||
return $this->return_success( $url );
|
||||
} catch ( \Exception $e ) {
|
||||
return new WP_REST_Response(
|
||||
array( 'error' => $e->getMessage() ),
|
||||
500
|
||||
);
|
||||
return $this->return_error( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -131,9 +131,9 @@ class OnboardingRestEndpoint extends RestEndpoint {
|
|||
$this->flag_map
|
||||
);
|
||||
|
||||
return rest_ensure_response(
|
||||
return $this->return_success(
|
||||
$js_data,
|
||||
array(
|
||||
'data' => $js_data,
|
||||
'flags' => $js_flags,
|
||||
)
|
||||
);
|
||||
|
|
|
@ -10,6 +10,7 @@ declare( strict_types = 1 );
|
|||
namespace WooCommerce\PayPalCommerce\Settings\Endpoint;
|
||||
|
||||
use WC_REST_Controller;
|
||||
use WP_REST_Response;
|
||||
|
||||
/**
|
||||
* Base class for REST controllers in the settings module.
|
||||
|
@ -31,6 +32,54 @@ abstract class RestEndpoint extends WC_REST_Controller {
|
|||
return current_user_can( 'manage_woocommerce' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a successful REST API response.
|
||||
*
|
||||
* @param mixed $data The main response data.
|
||||
* @param array $extra Optional, additional response data.
|
||||
*
|
||||
* @return WP_REST_Response The successful response.
|
||||
*/
|
||||
protected function return_success( $data, array $extra = array() ) : WP_REST_Response {
|
||||
$response = array(
|
||||
'success' => true,
|
||||
'data' => $data,
|
||||
);
|
||||
|
||||
if ( $extra ) {
|
||||
foreach ( $extra as $key => $value ) {
|
||||
if ( isset( $response[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$response[ $key ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an error REST API response.
|
||||
*
|
||||
* @param string $reason The reason for the error.
|
||||
* @param mixed $details Optional details about the error.
|
||||
*
|
||||
* @return WP_REST_Response The error response.
|
||||
*/
|
||||
protected function return_error( string $reason, $details = null ) : WP_REST_Response {
|
||||
$response = array(
|
||||
'success' => false,
|
||||
'message' => $reason,
|
||||
);
|
||||
|
||||
if ( ! is_null( $details ) ) {
|
||||
$response['details'] = $details;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes parameters based on a field mapping.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue