class-wc-rest-payment-gateways-controller.php 15 KB

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