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