WC_REST_Payment_Gateways_Controller

The WooCommerce WC REST Payment Gateways Controller class.

Defined (1)

The class is defined in the following location(s).

/includes/api/class-wc-rest-payment-gateways-controller.php  
  1. class WC_REST_Payment_Gateways_Controller extends WC_REST_Controller { 
  2.  
  3. /** 
  4. * Endpoint namespace. 
  5. * @var string 
  6. */ 
  7. protected $namespace = 'wc/v2'; 
  8.  
  9. /** 
  10. * Route base. 
  11. * @var string 
  12. */ 
  13. protected $rest_base = 'payment_gateways'; 
  14.  
  15. /** 
  16. * Register the route for /payment_gateways and /payment_gateways/<id> 
  17. */ 
  18. public function register_routes() { 
  19. register_rest_route( $this->namespace, '/' . $this->rest_base, array( 
  20. array( 
  21. 'methods' => WP_REST_Server::READABLE,  
  22. 'callback' => array( $this, 'get_items' ),  
  23. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  24. 'args' => $this->get_collection_params(),  
  25. ),  
  26. 'schema' => array( $this, 'get_public_item_schema' ),  
  27. ) ); 
  28. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w-]+)', array( 
  29. 'args' => array( 
  30. 'id' => array( 
  31. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),  
  32. 'type' => 'string',  
  33. ),  
  34. ),  
  35. array( 
  36. 'methods' => WP_REST_Server::READABLE,  
  37. 'callback' => array( $this, 'get_item' ),  
  38. 'permission_callback' => array( $this, 'get_item_permissions_check' ),  
  39. 'args' => array( 
  40. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  41. ),  
  42. ),  
  43. array( 
  44. 'methods' => WP_REST_Server::EDITABLE,  
  45. 'callback' => array( $this, 'update_item' ),  
  46. 'permission_callback' => array( $this, 'update_items_permissions_check' ),  
  47. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  48. ),  
  49. 'schema' => array( $this, 'get_public_item_schema' ),  
  50. ) ); 
  51.  
  52. /** 
  53. * Check whether a given request has permission to view payment gateways. 
  54. * @param WP_REST_Request $request Full details about the request. 
  55. * @return WP_Error|boolean 
  56. */ 
  57. public function get_items_permissions_check( $request ) { 
  58. if ( ! wc_rest_check_manager_permissions( 'payment_gateways', 'read' ) ) { 
  59. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  60. return true; 
  61.  
  62. /** 
  63. * Check if a given request has access to read a payment gateway. 
  64. * @param WP_REST_Request $request Full details about the request. 
  65. * @return WP_Error|boolean 
  66. */ 
  67. public function get_item_permissions_check( $request ) { 
  68. if ( ! wc_rest_check_manager_permissions( 'payment_gateways', 'read' ) ) { 
  69. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  70. return true; 
  71.  
  72. /** 
  73. * Check whether a given request has permission to edit payment gateways. 
  74. * @param WP_REST_Request $request Full details about the request. 
  75. * @return WP_Error|boolean 
  76. */ 
  77. public function update_items_permissions_check( $request ) { 
  78. if ( ! wc_rest_check_manager_permissions( 'payment_gateways', 'edit' ) ) { 
  79. return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); 
  80. return true; 
  81.  
  82. /** 
  83. * Get payment gateways. 
  84. * @param WP_REST_Request $request Full details about the request. 
  85. * @return WP_Error|WP_REST_Response 
  86. */ 
  87. public function get_items( $request ) { 
  88. $payment_gateways = WC()->payment_gateways->payment_gateways(); 
  89. $response = array(); 
  90. foreach ( $payment_gateways as $payment_gateway_id => $payment_gateway ) { 
  91. $payment_gateway->id = $payment_gateway_id; 
  92. $gateway = $this->prepare_item_for_response( $payment_gateway, $request ); 
  93. $gateway = $this->prepare_response_for_collection( $gateway ); 
  94. $response[] = $gateway; 
  95. return rest_ensure_response( $response ); 
  96.  
  97. /** 
  98. * Get a single payment gateway. 
  99. * @param WP_REST_Request $request 
  100. * @return WP_REST_Response|WP_Error 
  101. */ 
  102. public function get_item( $request ) { 
  103. $gateway = $this->get_gateway( $request ); 
  104.  
  105. if ( is_null( $gateway ) ) { 
  106. return new WP_Error( 'woocommerce_rest_payment_gateway_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); 
  107.  
  108. $gateway = $this->prepare_item_for_response( $gateway, $request ); 
  109. return rest_ensure_response( $gateway ); 
  110.  
  111. /** 
  112. * Update A Single Shipping Zone Method. 
  113. * @param WP_REST_Request $request 
  114. * @return WP_REST_Response|WP_Error 
  115. */ 
  116. public function update_item( $request ) { 
  117. $gateway = $this->get_gateway( $request ); 
  118.  
  119. if ( is_null( $gateway ) ) { 
  120. return new WP_Error( 'woocommerce_rest_payment_gateway_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); 
  121.  
  122. // Get settings. 
  123. $gateway->init_form_fields(); 
  124. $settings = $gateway->settings; 
  125.  
  126. // Update settings. 
  127. if ( isset( $request['settings'] ) ) { 
  128. $errors_found = false; 
  129. foreach ( $gateway->form_fields as $key => $field ) { 
  130. if ( isset( $request['settings'][ $key ] ) ) { 
  131. if ( is_callable( array( $this, 'validate_setting_' . $field['type'] . '_field' ) ) ) { 
  132. $value = $this->{'validate_setting_' . $field['type'] . '_field'}( $request['settings'][ $key ], $field ); 
  133. } else { 
  134. $value = $this->validate_setting_text_field( $request['settings'][ $key ], $field ); 
  135. if ( is_wp_error( $value ) ) { 
  136. $errors_found = true; 
  137. break; 
  138. $settings[ $key ] = $value; 
  139.  
  140. if ( $errors_found ) { 
  141. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  142.  
  143. // Update if this method is enabled or not. 
  144. if ( isset( $request['enabled'] ) ) { 
  145. $gateway->enabled = $settings['enabled'] = wc_bool_to_string( $request['enabled'] ); 
  146.  
  147. // Update title. 
  148. if ( isset( $request['title'] ) ) { 
  149. $gateway->title = $settings['title'] = $request['title']; 
  150.  
  151. // Update description. 
  152. if ( isset( $request['description'] ) ) { 
  153. $gateway->description = $settings['description'] = $request['description']; 
  154.  
  155. // Update options. 
  156. $gateway->settings = $settings; 
  157. update_option( $gateway->get_option_key(), apply_filters( 'woocommerce_gateway_' . $gateway->id . '_settings_values', $settings, $gateway ) ); 
  158.  
  159. // Update order 
  160. if ( isset( $request['order'] ) ) { 
  161. $order = (array) get_option( 'woocommerce_gateway_order' ); 
  162. $order[ $gateway->id ] = $request['order']; 
  163. update_option( 'woocommerce_gateway_order', $order ); 
  164. $gateway->order = absint( $request['order'] ); 
  165.  
  166. $gateway = $this->prepare_item_for_response( $gateway, $request ); 
  167. return rest_ensure_response( $gateway ); 
  168.  
  169. /** 
  170. * Get a gateway based on the current request object. 
  171. * @param WP_REST_Request $request 
  172. * @return WP_REST_Response|null 
  173. */ 
  174. public function get_gateway( $request ) { 
  175. $gateway = null; 
  176. $payment_gateways = WC()->payment_gateways->payment_gateways(); 
  177. foreach ( $payment_gateways as $payment_gateway_id => $payment_gateway ) { 
  178. if ( $request['id'] !== $payment_gateway_id ) { 
  179. continue; 
  180. $payment_gateway->id = $payment_gateway_id; 
  181. $gateway = $payment_gateway; 
  182. return $gateway; 
  183.  
  184. /** 
  185. * Prepare a payment gateway for response. 
  186. * @param WC_Payment_Gateway $gateway Payment gateway object. 
  187. * @param WP_REST_Request $request Request object. 
  188. * @return WP_REST_Response $response Response data. 
  189. */ 
  190. public function prepare_item_for_response( $gateway, $request ) { 
  191. $order = (array) get_option( 'woocommerce_gateway_order' ); 
  192. $item = array( 
  193. 'id' => $gateway->id,  
  194. 'title' => $gateway->title,  
  195. 'description' => $gateway->description,  
  196. 'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '',  
  197. 'enabled' => ( 'yes' === $gateway->enabled ),  
  198. 'method_title' => $gateway->get_method_title(),  
  199. 'method_description' => $gateway->get_method_description(),  
  200. 'settings' => $this->get_settings( $gateway ),  
  201. ); 
  202.  
  203. $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 
  204. $data = $this->add_additional_fields_to_object( $item, $request ); 
  205. $data = $this->filter_response_by_context( $data, $context ); 
  206.  
  207. $response = rest_ensure_response( $data ); 
  208. $response->add_links( $this->prepare_links( $gateway, $request ) ); 
  209.  
  210. /** 
  211. * Filter payment gateway objects returned from the REST API. 
  212. * @param WP_REST_Response $response The response object. 
  213. * @param WC_Payment_Gateway $gateway Payment gateway object. 
  214. * @param WP_REST_Request $request Request object. 
  215. */ 
  216. return apply_filters( 'woocommerce_rest_prepare_payment_gateway', $response, $gateway, $request ); 
  217.  
  218. /** 
  219. * Return settings associated with this payment gateway. 
  220. */ 
  221. public function get_settings( $gateway ) { 
  222. $settings = array(); 
  223. $gateway->init_form_fields(); 
  224. foreach ( $gateway->form_fields as $id => $field ) { 
  225. // Make sure we at least have a title and type 
  226. if ( empty( $field['title'] ) || empty( $field['type'] ) ) { 
  227. continue; 
  228. // Ignore 'title' settings/fields -- they are UI only 
  229. if ( 'title' === $field['type'] ) { 
  230. continue; 
  231. // Ignore 'enabled' and 'description' which get included elsewhere. 
  232. if ( in_array( $id, array( 'enabled', 'description' ) ) ) { 
  233. continue; 
  234. $data = array( 
  235. 'id' => $id,  
  236. 'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],  
  237. 'description' => empty( $field['description'] ) ? '' : $field['description'],  
  238. 'type' => $field['type'],  
  239. 'value' => empty( $gateway->settings[ $id ] ) ? '' : $gateway->settings[ $id ],  
  240. 'default' => empty( $field['default'] ) ? '' : $field['default'],  
  241. 'tip' => empty( $field['description'] ) ? '' : $field['description'],  
  242. 'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],  
  243. ); 
  244. if ( ! empty( $field['options'] ) ) { 
  245. $data['options'] = $field['options']; 
  246. $settings[ $id ] = $data; 
  247. return $settings; 
  248.  
  249. /** 
  250. * Prepare links for the request. 
  251. * @param WC_Payment_Gateway $gateway Payment gateway object. 
  252. * @param WP_REST_Request $request Request object. 
  253. * @return array 
  254. */ 
  255. protected function prepare_links( $gateway, $request ) { 
  256. $links = array( 
  257. 'self' => array( 
  258. 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $gateway->id ) ),  
  259. ),  
  260. 'collection' => array( 
  261. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),  
  262. ),  
  263. ); 
  264.  
  265. return $links; 
  266.  
  267. /** 
  268. * Get the payment gateway schema, conforming to JSON Schema. 
  269. * @return array 
  270. */ 
  271. public function get_item_schema() { 
  272. $schema = array( 
  273. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  274. 'title' => 'payment_gateway',  
  275. 'type' => 'object',  
  276. 'properties' => array( 
  277. 'id' => array( 
  278. 'description' => __( 'Payment gateway ID.', 'woocommerce' ),  
  279. 'type' => 'string',  
  280. 'context' => array( 'view', 'edit' ),  
  281. 'readonly' => true,  
  282. ),  
  283. 'title' => array( 
  284. 'description' => __( 'Payment gateway title on checkout.', 'woocommerce' ),  
  285. 'type' => 'string',  
  286. 'context' => array( 'view', 'edit' ),  
  287. ),  
  288. 'description' => array( 
  289. 'description' => __( 'Payment gateway description on checkout.', 'woocommerce' ),  
  290. 'type' => 'string',  
  291. 'context' => array( 'view', 'edit' ),  
  292. ),  
  293. 'order' => array( 
  294. 'description' => __( 'Payment gateway sort order.', 'woocommerce' ),  
  295. 'type' => 'integer',  
  296. 'context' => array( 'view', 'edit' ),  
  297. 'arg_options' => array( 
  298. 'sanitize_callback' => 'absint',  
  299. ),  
  300. ),  
  301. 'enabled' => array( 
  302. 'description' => __( 'Payment gateway enabled status.', 'woocommerce' ),  
  303. 'type' => 'boolean',  
  304. 'context' => array( 'view', 'edit' ),  
  305. ),  
  306. 'method_title' => array( 
  307. 'description' => __( 'Payment gateway method title.', 'woocommerce' ),  
  308. 'type' => 'string',  
  309. 'context' => array( 'view', 'edit' ),  
  310. 'readonly' => true,  
  311. ),  
  312. 'method_description' => array( 
  313. 'description' => __( 'Payment gateway method description.', 'woocommerce' ),  
  314. 'type' => 'string',  
  315. 'context' => array( 'view', 'edit' ),  
  316. 'readonly' => true,  
  317. ),  
  318. 'settings' => array( 
  319. 'description' => __( 'Payment gateway settings.', 'woocommerce' ),  
  320. 'type' => 'object',  
  321. 'context' => array( 'view', 'edit' ),  
  322. 'properties' => array( 
  323. 'id' => array( 
  324. 'description' => __( 'A unique identifier for the setting.', 'woocommerce' ),  
  325. 'type' => 'string',  
  326. 'context' => array( 'view', 'edit' ),  
  327. 'readonly' => true,  
  328. ),  
  329. 'label' => array( 
  330. 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),  
  331. 'type' => 'string',  
  332. 'context' => array( 'view', 'edit' ),  
  333. 'readonly' => true,  
  334. ),  
  335. 'description' => array( 
  336. 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),  
  337. 'type' => 'string',  
  338. 'context' => array( 'view', 'edit' ),  
  339. 'readonly' => true,  
  340. ),  
  341. 'type' => array( 
  342. 'description' => __( 'Type of setting.', 'woocommerce' ),  
  343. 'type' => 'string',  
  344. 'context' => array( 'view', 'edit' ),  
  345. 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ),  
  346. 'readonly' => true,  
  347. ),  
  348. 'value' => array( 
  349. 'description' => __( 'Setting value.', 'woocommerce' ),  
  350. 'type' => 'string',  
  351. 'context' => array( 'view', 'edit' ),  
  352. ),  
  353. 'default' => array( 
  354. 'description' => __( 'Default value for the setting.', 'woocommerce' ),  
  355. 'type' => 'string',  
  356. 'context' => array( 'view', 'edit' ),  
  357. 'readonly' => true,  
  358. ),  
  359. 'tip' => array( 
  360. 'description' => __( 'Additional help text shown to the user about the setting.', 'woocommerce' ),  
  361. 'type' => 'string',  
  362. 'context' => array( 'view', 'edit' ),  
  363. 'readonly' => true,  
  364. ),  
  365. 'placeholder' => array( 
  366. 'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ),  
  367. 'type' => 'string',  
  368. 'context' => array( 'view', 'edit' ),  
  369. 'readonly' => true,  
  370. ),  
  371. ),  
  372. ),  
  373. ),  
  374. ); 
  375.  
  376. return $this->add_additional_fields_schema( $schema ); 
  377.  
  378. /** 
  379. * Get any query params needed. 
  380. * @return array 
  381. */ 
  382. public function get_collection_params() { 
  383. return array( 
  384. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),  
  385. ); 
  386.