2016-08-31 21:16:52 +00:00
< ? php
/**
* REST API WC Payment gateways controller
*
* Handles requests to the / payment_gateways endpoint .
*
2018-03-06 18:04:58 +00:00
* @ package WooCommerce / API
* @ since 3.0 . 0
2016-08-31 21:16:52 +00:00
*/
2018-03-06 18:04:58 +00:00
defined ( 'ABSPATH' ) || exit ;
2016-08-31 21:16:52 +00:00
/**
2018-03-06 18:04:58 +00:00
* Paymenga gateways controller class .
*
2016-08-31 21:16:52 +00:00
* @ package WooCommerce / API
* @ extends WC_REST_Controller
*/
class WC_REST_Payment_Gateways_Controller extends WC_REST_Controller {
/**
* Endpoint namespace .
*
* @ var string
*/
2017-02-09 17:06:13 +00:00
protected $namespace = 'wc/v2' ;
2016-08-31 21:16:52 +00:00
/**
* Route base .
*
* @ var string
*/
protected $rest_base = 'payment_gateways' ;
/**
* Register the route for / payment_gateways and / payment_gateways /< id >
*/
public function register_routes () {
2018-03-05 20:53:06 +00:00
register_rest_route (
$this -> namespace , '/' . $this -> rest_base , array (
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_items' ),
'permission_callback' => array ( $this , 'get_items_permissions_check' ),
'args' => $this -> get_collection_params (),
2017-01-26 19:22:57 +00:00
),
2018-03-05 20:53:06 +00:00
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
register_rest_route (
$this -> namespace , '/' . $this -> rest_base . '/(?P<id>[\w-]+)' , array (
'args' => array (
'id' => array (
'description' => __ ( 'Unique identifier for the resource.' , 'woocommerce' ),
'type' => 'string' ,
),
2016-08-31 21:16:52 +00:00
),
2018-03-05 20:53:06 +00:00
array (
'methods' => WP_REST_Server :: READABLE ,
'callback' => array ( $this , 'get_item' ),
'permission_callback' => array ( $this , 'get_item_permissions_check' ),
'args' => array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
),
),
array (
'methods' => WP_REST_Server :: EDITABLE ,
'callback' => array ( $this , 'update_item' ),
'permission_callback' => array ( $this , 'update_items_permissions_check' ),
'args' => $this -> get_endpoint_args_for_item_schema ( WP_REST_Server :: EDITABLE ),
),
'schema' => array ( $this , 'get_public_item_schema' ),
)
);
2016-08-31 21:16:52 +00:00
}
/**
* Check whether a given request has permission to view payment gateways .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_items_permissions_check ( $request ) {
if ( ! wc_rest_check_manager_permissions ( 'payment_gateways' , 'read' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot list resources.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check if a given request has access to read a payment gateway .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function get_item_permissions_check ( $request ) {
if ( ! wc_rest_check_manager_permissions ( 'payment_gateways' , 'read' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_view' , __ ( 'Sorry, you cannot view this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Check whether a given request has permission to edit payment gateways .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | boolean
*/
public function update_items_permissions_check ( $request ) {
if ( ! wc_rest_check_manager_permissions ( 'payment_gateways' , 'edit' ) ) {
return new WP_Error ( 'woocommerce_rest_cannot_edit' , __ ( 'Sorry, you are not allowed to edit this resource.' , 'woocommerce' ), array ( 'status' => rest_authorization_required_code () ) );
}
return true ;
}
/**
* Get payment gateways .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return WP_Error | WP_REST_Response
*/
public function get_items ( $request ) {
$payment_gateways = WC () -> payment_gateways -> payment_gateways ();
$response = array ();
foreach ( $payment_gateways as $payment_gateway_id => $payment_gateway ) {
$payment_gateway -> id = $payment_gateway_id ;
$gateway = $this -> prepare_item_for_response ( $payment_gateway , $request );
$gateway = $this -> prepare_response_for_collection ( $gateway );
$response [] = $gateway ;
}
return rest_ensure_response ( $response );
}
/**
* Get a single payment gateway .
*
2018-03-06 18:04:58 +00:00
* @ param WP_REST_Request $request Request data .
2016-08-31 21:16:52 +00:00
* @ return WP_REST_Response | WP_Error
*/
public function get_item ( $request ) {
$gateway = $this -> get_gateway ( $request );
if ( is_null ( $gateway ) ) {
2016-09-06 20:57:30 +00:00
return new WP_Error ( 'woocommerce_rest_payment_gateway_invalid' , __ ( 'Resource does not exist.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-08-31 21:16:52 +00:00
}
$gateway = $this -> prepare_item_for_response ( $gateway , $request );
return rest_ensure_response ( $gateway );
}
/**
2017-06-02 16:58:16 +00:00
* Update A Single Payment Method .
2016-08-31 21:16:52 +00:00
*
2018-03-06 18:04:58 +00:00
* @ param WP_REST_Request $request Request data .
2016-08-31 21:16:52 +00:00
* @ return WP_REST_Response | WP_Error
*/
public function update_item ( $request ) {
$gateway = $this -> get_gateway ( $request );
if ( is_null ( $gateway ) ) {
2016-09-06 20:57:30 +00:00
return new WP_Error ( 'woocommerce_rest_payment_gateway_invalid' , __ ( 'Resource does not exist.' , 'woocommerce' ), array ( 'status' => 404 ) );
2016-08-31 21:16:52 +00:00
}
2017-03-24 13:22:09 +00:00
// Get settings.
$gateway -> init_form_fields ();
$settings = $gateway -> settings ;
// Update settings.
2016-08-31 21:16:52 +00:00
if ( isset ( $request [ 'settings' ] ) ) {
2016-09-08 22:14:40 +00:00
$errors_found = false ;
2016-08-31 21:16:52 +00:00
foreach ( $gateway -> form_fields as $key => $field ) {
if ( isset ( $request [ 'settings' ][ $key ] ) ) {
2016-09-08 22:14:40 +00:00
if ( is_callable ( array ( $this , 'validate_setting_' . $field [ 'type' ] . '_field' ) ) ) {
$value = $this -> { 'validate_setting_' . $field [ 'type' ] . '_field' }( $request [ 'settings' ][ $key ], $field );
} else {
$value = $this -> validate_setting_text_field ( $request [ 'settings' ][ $key ], $field );
}
if ( is_wp_error ( $value ) ) {
$errors_found = true ;
break ;
}
$settings [ $key ] = $value ;
2016-08-31 21:16:52 +00:00
}
}
2016-09-08 22:14:40 +00:00
if ( $errors_found ) {
return new WP_Error ( 'rest_setting_value_invalid' , __ ( 'An invalid setting value was passed.' , 'woocommerce' ), array ( 'status' => 400 ) );
}
2016-08-31 21:16:52 +00:00
}
// Update if this method is enabled or not.
if ( isset ( $request [ 'enabled' ] ) ) {
2018-03-06 18:04:58 +00:00
$settings [ 'enabled' ] = wc_bool_to_string ( $request [ 'enabled' ] );
$gateway -> enabled = $settings [ 'enabled' ];
2016-11-29 19:38:47 +00:00
}
// Update title.
if ( isset ( $request [ 'title' ] ) ) {
2018-03-06 18:04:58 +00:00
$settings [ 'title' ] = $request [ 'title' ];
$gateway -> title = $settings [ 'title' ];
2016-11-29 19:38:47 +00:00
}
// Update description.
if ( isset ( $request [ 'description' ] ) ) {
2018-03-06 18:04:58 +00:00
$settings [ 'description' ] = $request [ 'description' ];
$gateway -> description = $settings [ 'description' ];
2017-03-24 13:22:09 +00:00
}
// Update options.
$gateway -> settings = $settings ;
update_option ( $gateway -> get_option_key (), apply_filters ( 'woocommerce_gateway_' . $gateway -> id . '_settings_values' , $settings , $gateway ) );
2018-03-06 18:04:58 +00:00
// Update order.
2017-03-24 13:22:09 +00:00
if ( isset ( $request [ 'order' ] ) ) {
2018-03-05 20:53:06 +00:00
$order = ( array ) get_option ( 'woocommerce_gateway_order' );
2017-03-24 13:22:09 +00:00
$order [ $gateway -> id ] = $request [ 'order' ];
update_option ( 'woocommerce_gateway_order' , $order );
$gateway -> order = absint ( $request [ 'order' ] );
2016-08-31 21:16:52 +00:00
}
$gateway = $this -> prepare_item_for_response ( $gateway , $request );
return rest_ensure_response ( $gateway );
}
/**
* Get a gateway based on the current request object .
*
2018-03-06 18:04:58 +00:00
* @ param WP_REST_Request $request Request data .
2016-08-31 21:16:52 +00:00
* @ return WP_REST_Response | null
*/
public function get_gateway ( $request ) {
$gateway = null ;
$payment_gateways = WC () -> payment_gateways -> payment_gateways ();
foreach ( $payment_gateways as $payment_gateway_id => $payment_gateway ) {
if ( $request [ 'id' ] !== $payment_gateway_id ) {
continue ;
}
$payment_gateway -> id = $payment_gateway_id ;
$gateway = $payment_gateway ;
}
return $gateway ;
}
/**
* Prepare a payment gateway for response .
*
* @ param WC_Payment_Gateway $gateway Payment gateway object .
* @ param WP_REST_Request $request Request object .
* @ return WP_REST_Response $response Response data .
*/
public function prepare_item_for_response ( $gateway , $request ) {
2018-03-05 20:53:06 +00:00
$order = ( array ) get_option ( 'woocommerce_gateway_order' );
$item = array (
2016-08-31 21:16:52 +00:00
'id' => $gateway -> id ,
'title' => $gateway -> title ,
'description' => $gateway -> description ,
'order' => isset ( $order [ $gateway -> id ] ) ? $order [ $gateway -> id ] : '' ,
'enabled' => ( 'yes' === $gateway -> enabled ),
2016-09-06 17:32:54 +00:00
'method_title' => $gateway -> get_method_title (),
'method_description' => $gateway -> get_method_description (),
2016-08-31 21:16:52 +00:00
'settings' => $this -> get_settings ( $gateway ),
);
$context = ! empty ( $request [ 'context' ] ) ? $request [ 'context' ] : 'view' ;
$data = $this -> add_additional_fields_to_object ( $item , $request );
2016-09-06 20:34:25 +00:00
$data = $this -> filter_response_by_context ( $data , $context );
2016-08-31 21:16:52 +00:00
2016-09-06 20:34:25 +00:00
$response = rest_ensure_response ( $data );
2016-08-31 21:16:52 +00:00
$response -> add_links ( $this -> prepare_links ( $gateway , $request ) );
/**
* Filter payment gateway objects returned from the REST API .
*
* @ param WP_REST_Response $response The response object .
* @ param WC_Payment_Gateway $gateway Payment gateway object .
* @ param WP_REST_Request $request Request object .
*/
return apply_filters ( 'woocommerce_rest_prepare_payment_gateway' , $response , $gateway , $request );
}
/**
* Return settings associated with this payment gateway .
2017-05-15 11:50:52 +00:00
*
2018-03-06 18:04:58 +00:00
* @ param WC_Payment_Gateway $gateway Gateway data .
2017-05-15 11:50:52 +00:00
*
* @ return array
2016-08-31 21:16:52 +00:00
*/
public function get_settings ( $gateway ) {
$settings = array ();
$gateway -> init_form_fields ();
foreach ( $gateway -> form_fields as $id => $field ) {
2018-03-06 18:04:58 +00:00
// Make sure we at least have a title and type.
2016-08-31 21:16:52 +00:00
if ( empty ( $field [ 'title' ] ) || empty ( $field [ 'type' ] ) ) {
continue ;
}
2018-03-06 18:04:58 +00:00
// Ignore 'title' settings/fields -- they are UI only.
2016-08-31 21:16:52 +00:00
if ( 'title' === $field [ 'type' ] ) {
continue ;
}
2017-03-24 13:27:47 +00:00
// Ignore 'enabled' and 'description' which get included elsewhere.
2018-03-06 18:04:58 +00:00
if ( in_array ( $id , array ( 'enabled' , 'description' ), true ) ) {
2017-03-24 13:27:47 +00:00
continue ;
}
2016-08-31 21:16:52 +00:00
$data = array (
'id' => $id ,
'label' => empty ( $field [ 'label' ] ) ? $field [ 'title' ] : $field [ 'label' ],
'description' => empty ( $field [ 'description' ] ) ? '' : $field [ 'description' ],
'type' => $field [ 'type' ],
2017-03-21 20:34:16 +00:00
'value' => empty ( $gateway -> settings [ $id ] ) ? '' : $gateway -> settings [ $id ],
2016-08-31 21:16:52 +00:00
'default' => empty ( $field [ 'default' ] ) ? '' : $field [ 'default' ],
'tip' => empty ( $field [ 'description' ] ) ? '' : $field [ 'description' ],
'placeholder' => empty ( $field [ 'placeholder' ] ) ? '' : $field [ 'placeholder' ],
);
if ( ! empty ( $field [ 'options' ] ) ) {
$data [ 'options' ] = $field [ 'options' ];
}
$settings [ $id ] = $data ;
}
return $settings ;
}
/**
* Prepare links for the request .
*
* @ param WC_Payment_Gateway $gateway Payment gateway object .
* @ param WP_REST_Request $request Request object .
* @ return array
*/
protected function prepare_links ( $gateway , $request ) {
2018-03-05 20:53:06 +00:00
$links = array (
'self' => array (
2016-08-31 21:16:52 +00:00
'href' => rest_url ( sprintf ( '/%s/%s/%s' , $this -> namespace , $this -> rest_base , $gateway -> id ) ),
),
'collection' => array (
'href' => rest_url ( sprintf ( '/%s/%s' , $this -> namespace , $this -> rest_base ) ),
),
);
return $links ;
}
2018-03-05 20:53:06 +00:00
/**
2016-08-31 21:16:52 +00:00
* Get the payment gateway schema , conforming to JSON Schema .
*
* @ return array
*/
public function get_item_schema () {
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'payment_gateway' ,
'type' => 'object' ,
'properties' => array (
2018-03-05 20:53:06 +00:00
'id' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway ID.' , 'woocommerce' ),
'type' => 'string' ,
2016-11-29 19:38:47 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
2016-08-31 21:16:52 +00:00
),
2018-03-05 20:53:06 +00:00
'title' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway title on checkout.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-03-05 20:53:06 +00:00
'description' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway description on checkout.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-03-05 20:53:06 +00:00
'order' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway sort order.' , 'woocommerce' ),
'type' => 'integer' ,
'context' => array ( 'view' , 'edit' ),
'arg_options' => array (
'sanitize_callback' => 'absint' ,
),
),
2018-03-05 20:53:06 +00:00
'enabled' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway enabled status.' , 'woocommerce' ),
'type' => 'boolean' ,
'context' => array ( 'view' , 'edit' ),
),
2018-03-05 20:53:06 +00:00
'method_title' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway method title.' , 'woocommerce' ),
'type' => 'string' ,
2016-11-29 19:38:47 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
2016-08-31 21:16:52 +00:00
),
'method_description' => array (
'description' => __ ( 'Payment gateway method description.' , 'woocommerce' ),
'type' => 'string' ,
2016-11-29 19:38:47 +00:00
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
2016-08-31 21:16:52 +00:00
),
2018-03-05 20:53:06 +00:00
'settings' => array (
2016-08-31 21:16:52 +00:00
'description' => __ ( 'Payment gateway settings.' , 'woocommerce' ),
2016-12-07 20:23:23 +00:00
'type' => 'object' ,
2016-08-31 21:16:52 +00:00
'context' => array ( 'view' , 'edit' ),
2018-03-05 20:53:06 +00:00
'properties' => array (
'id' => array (
2017-03-21 20:47:28 +00:00
'description' => __ ( 'A unique identifier for the setting.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-03-05 20:53:06 +00:00
'label' => array (
2017-03-22 13:51:52 +00:00
'description' => __ ( 'A human readable label for the setting used in interfaces.' , 'woocommerce' ),
2017-03-21 20:47:28 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'description' => array (
2017-03-22 13:51:52 +00:00
'description' => __ ( 'A human readable description for the setting used in interfaces.' , 'woocommerce' ),
2017-03-21 20:47:28 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-03-05 20:53:06 +00:00
'type' => array (
2017-03-21 20:47:28 +00:00
'description' => __ ( 'Type of setting.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'enum' => array ( 'text' , 'email' , 'number' , 'color' , 'password' , 'textarea' , 'select' , 'multiselect' , 'radio' , 'image_width' , 'checkbox' ),
'readonly' => true ,
),
2018-03-05 20:53:06 +00:00
'value' => array (
2017-03-21 20:47:28 +00:00
'description' => __ ( 'Setting value.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
),
2018-03-05 20:53:06 +00:00
'default' => array (
2017-03-21 20:47:28 +00:00
'description' => __ ( 'Default value for the setting.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
2018-03-05 20:53:06 +00:00
'tip' => array (
2017-03-22 13:51:52 +00:00
'description' => __ ( 'Additional help text shown to the user about the setting.' , 'woocommerce' ),
2017-03-21 20:47:28 +00:00
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
'placeholder' => array (
'description' => __ ( 'Placeholder text to be displayed in text inputs.' , 'woocommerce' ),
'type' => 'string' ,
'context' => array ( 'view' , 'edit' ),
'readonly' => true ,
),
),
2016-08-31 21:16:52 +00:00
),
),
);
return $this -> add_additional_fields_schema ( $schema );
}
/**
* Get any query params needed .
*
* @ return array
*/
public function get_collection_params () {
return array (
'context' => $this -> get_context_param ( array ( 'default' => 'view' ) ),
);
}
}