No Description

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * REST API WC Payment gateways controller
  4. *
  5. * Handles requests to the /payment_gateways endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Paymenga gateways controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Payment_Gateways_V2_Controller
  16. */
  17. class WC_REST_Payment_Gateways_Controller extends WC_REST_Payment_Gateways_V2_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v3';
  24. /**
  25. * Prepare a payment gateway for response.
  26. *
  27. * @param WC_Payment_Gateway $gateway Payment gateway object.
  28. * @param WP_REST_Request $request Request object.
  29. * @return WP_REST_Response $response Response data.
  30. */
  31. public function prepare_item_for_response( $gateway, $request ) {
  32. $order = (array) get_option( 'woocommerce_gateway_order' );
  33. $item = array(
  34. 'id' => $gateway->id,
  35. 'title' => $gateway->title,
  36. 'description' => $gateway->description,
  37. 'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '',
  38. 'enabled' => ( 'yes' === $gateway->enabled ),
  39. 'method_title' => $gateway->get_method_title(),
  40. 'method_description' => $gateway->get_method_description(),
  41. 'method_supports' => $gateway->supports,
  42. 'settings' => $this->get_settings( $gateway ),
  43. );
  44. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  45. $data = $this->add_additional_fields_to_object( $item, $request );
  46. $data = $this->filter_response_by_context( $data, $context );
  47. $response = rest_ensure_response( $data );
  48. $response->add_links( $this->prepare_links( $gateway, $request ) );
  49. /**
  50. * Filter payment gateway objects returned from the REST API.
  51. *
  52. * @param WP_REST_Response $response The response object.
  53. * @param WC_Payment_Gateway $gateway Payment gateway object.
  54. * @param WP_REST_Request $request Request object.
  55. */
  56. return apply_filters( 'woocommerce_rest_prepare_payment_gateway', $response, $gateway, $request );
  57. }
  58. /**
  59. * Return settings associated with this payment gateway.
  60. *
  61. * @param WC_Payment_Gateway $gateway Gateway instance.
  62. *
  63. * @return array
  64. */
  65. public function get_settings( $gateway ) {
  66. $settings = array();
  67. $gateway->init_form_fields();
  68. foreach ( $gateway->form_fields as $id => $field ) {
  69. // Make sure we at least have a title and type.
  70. if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
  71. continue;
  72. }
  73. // Ignore 'enabled' and 'description' which get included elsewhere.
  74. if ( in_array( $id, array( 'enabled', 'description' ), true ) ) {
  75. continue;
  76. }
  77. $data = array(
  78. 'id' => $id,
  79. 'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],
  80. 'description' => empty( $field['description'] ) ? '' : $field['description'],
  81. 'type' => $field['type'],
  82. 'value' => empty( $gateway->settings[ $id ] ) ? '' : $gateway->settings[ $id ],
  83. 'default' => empty( $field['default'] ) ? '' : $field['default'],
  84. 'tip' => empty( $field['description'] ) ? '' : $field['description'],
  85. 'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
  86. );
  87. if ( ! empty( $field['options'] ) ) {
  88. $data['options'] = $field['options'];
  89. }
  90. $settings[ $id ] = $data;
  91. }
  92. return $settings;
  93. }
  94. /**
  95. * Get the payment gateway schema, conforming to JSON Schema.
  96. *
  97. * @return array
  98. */
  99. public function get_item_schema() {
  100. $schema = array(
  101. '$schema' => 'http://json-schema.org/draft-04/schema#',
  102. 'title' => 'payment_gateway',
  103. 'type' => 'object',
  104. 'properties' => array(
  105. 'id' => array(
  106. 'description' => __( 'Payment gateway ID.', 'woocommerce' ),
  107. 'type' => 'string',
  108. 'context' => array( 'view', 'edit' ),
  109. 'readonly' => true,
  110. ),
  111. 'title' => array(
  112. 'description' => __( 'Payment gateway title on checkout.', 'woocommerce' ),
  113. 'type' => 'string',
  114. 'context' => array( 'view', 'edit' ),
  115. ),
  116. 'description' => array(
  117. 'description' => __( 'Payment gateway description on checkout.', 'woocommerce' ),
  118. 'type' => 'string',
  119. 'context' => array( 'view', 'edit' ),
  120. ),
  121. 'order' => array(
  122. 'description' => __( 'Payment gateway sort order.', 'woocommerce' ),
  123. 'type' => 'integer',
  124. 'context' => array( 'view', 'edit' ),
  125. 'arg_options' => array(
  126. 'sanitize_callback' => 'absint',
  127. ),
  128. ),
  129. 'enabled' => array(
  130. 'description' => __( 'Payment gateway enabled status.', 'woocommerce' ),
  131. 'type' => 'boolean',
  132. 'context' => array( 'view', 'edit' ),
  133. ),
  134. 'method_title' => array(
  135. 'description' => __( 'Payment gateway method title.', 'woocommerce' ),
  136. 'type' => 'string',
  137. 'context' => array( 'view', 'edit' ),
  138. 'readonly' => true,
  139. ),
  140. 'method_description' => array(
  141. 'description' => __( 'Payment gateway method description.', 'woocommerce' ),
  142. 'type' => 'string',
  143. 'context' => array( 'view', 'edit' ),
  144. 'readonly' => true,
  145. ),
  146. 'method_supports' => array(
  147. 'description' => __( 'Supported features for this payment gateway.', 'woocommerce' ),
  148. 'type' => 'array',
  149. 'context' => array( 'view', 'edit' ),
  150. 'readonly' => true,
  151. 'items' => array(
  152. 'type' => 'string',
  153. ),
  154. ),
  155. 'settings' => array(
  156. 'description' => __( 'Payment gateway settings.', 'woocommerce' ),
  157. 'type' => 'object',
  158. 'context' => array( 'view', 'edit' ),
  159. 'properties' => array(
  160. 'id' => array(
  161. 'description' => __( 'A unique identifier for the setting.', 'woocommerce' ),
  162. 'type' => 'string',
  163. 'context' => array( 'view', 'edit' ),
  164. 'readonly' => true,
  165. ),
  166. 'label' => array(
  167. 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),
  168. 'type' => 'string',
  169. 'context' => array( 'view', 'edit' ),
  170. 'readonly' => true,
  171. ),
  172. 'description' => array(
  173. 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),
  174. 'type' => 'string',
  175. 'context' => array( 'view', 'edit' ),
  176. 'readonly' => true,
  177. ),
  178. 'type' => array(
  179. 'description' => __( 'Type of setting.', 'woocommerce' ),
  180. 'type' => 'string',
  181. 'context' => array( 'view', 'edit' ),
  182. 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ),
  183. 'readonly' => true,
  184. ),
  185. 'value' => array(
  186. 'description' => __( 'Setting value.', 'woocommerce' ),
  187. 'type' => 'string',
  188. 'context' => array( 'view', 'edit' ),
  189. ),
  190. 'default' => array(
  191. 'description' => __( 'Default value for the setting.', 'woocommerce' ),
  192. 'type' => 'string',
  193. 'context' => array( 'view', 'edit' ),
  194. 'readonly' => true,
  195. ),
  196. 'tip' => array(
  197. 'description' => __( 'Additional help text shown to the user about the setting.', 'woocommerce' ),
  198. 'type' => 'string',
  199. 'context' => array( 'view', 'edit' ),
  200. 'readonly' => true,
  201. ),
  202. 'placeholder' => array(
  203. 'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ),
  204. 'type' => 'string',
  205. 'context' => array( 'view', 'edit' ),
  206. 'readonly' => true,
  207. ),
  208. ),
  209. ),
  210. ),
  211. );
  212. return $this->add_additional_fields_schema( $schema );
  213. }
  214. }