PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.18
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.18
1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 1.9.13 All 162 releases
woocommerce-pos / includes / API / V1 / Payment_Gateways.php

Payment_Gateways.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.18, at includes/API/V1/Payment_Gateways.php

225 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * POS payment gateways controller.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\API\V1;
9
10 \defined( 'ABSPATH' ) || die;
11
12 use WC_Payment_Gateway;
13 use WC_REST_Controller;
14 use WCPOS\WooCommercePOS\Payments\Gateway_Contract;
15 use WP_REST_Request;
16 use WP_REST_Response;
17 use WP_REST_Server;
18
19 /**
20 * POS payment gateways controller.
21 *
22 * Exposes a lean POS-facing catalog of payment gateways. Unlike WooCommerce's
23 * core payment-gateways controller this deliberately does NOT serialize each
24 * gateway's admin settings schema: the POS only needs the capability contract
25 * (id/title/enabled/provider/pos_type/capabilities/provider_data), and calling
26 * WC_Settings_API::get_settings() forces every gateway's init_form_fields() to
27 * run in a non-admin REST context — which fatals on gateways that gate their
28 * settings code behind is_admin() (e.g. ToyyibPay).
29 */
30 class Payment_Gateways extends WC_REST_Controller {
31 /**
32 * REST namespace.
33 *
34 * @var string
35 */
36 protected $namespace = 'wcpos/v1';
37
38 /**
39 * REST base.
40 *
41 * @var string
42 */
43 protected $rest_base = 'payment-gateways';
44
45 /**
46 * Shared gateway contract helper.
47 *
48 * @var Gateway_Contract
49 */
50 private $gateway_contract;
51
52 /**
53 * Constructor.
54 */
55 public function __construct() {
56 $this->gateway_contract = new Gateway_Contract();
57 }
58
59 /**
60 * Register routes.
61 *
62 * Only the collection (catalog) route is exposed. Reading or updating an
63 * individual gateway's settings is intentionally not supported here.
64 */
65 public function register_routes(): void {
66 register_rest_route(
67 $this->namespace,
68 '/' . $this->rest_base,
69 array(
70 array(
71 'methods' => WP_REST_Server::READABLE,
72 'callback' => array( $this, 'get_items' ),
73 'permission_callback' => array( $this, 'get_items_permissions_check' ),
74 'args' => array(
75 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
76 ),
77 ),
78 'schema' => array( $this, 'get_public_item_schema' ),
79 )
80 );
81 }
82
83 /**
84 * Read permissions for payment gateway catalog.
85 *
86 * @param WP_REST_Request $request Request object.
87 *
88 * @return bool|\WP_Error
89 */
90 public function get_items_permissions_check( $request ) {
91 return current_user_can( 'publish_shop_orders' );
92 }
93
94 /**
95 * Return the POS payment gateway catalog.
96 *
97 * @param WP_REST_Request $request Request object.
98 *
99 * @return WP_REST_Response
100 */
101 public function get_items( $request ) {
102 WC()->payment_gateways();
103 $gateways = WC()->payment_gateways->payment_gateways();
104 $data = array();
105
106 foreach ( $gateways as $gateway ) {
107 $response = $this->prepare_item_for_response( $gateway, $request );
108 $data[] = $this->prepare_response_for_collection( $response );
109 }
110
111 return rest_ensure_response( $data );
112 }
113
114 /**
115 * Prepare a gateway for the POS response.
116 *
117 * Builds the payload from the gateway's already-populated public properties
118 * plus the POS contract helper. It never calls get_settings()/init_form_fields().
119 *
120 * @param WC_Payment_Gateway $item Gateway object.
121 * @param WP_REST_Request $request Request object.
122 */
123 public function prepare_item_for_response( $item, $request ): WP_REST_Response {
124 $data = array(
125 'id' => $item->id,
126 'title' => $item->get_title(),
127 'description' => $item->get_description(),
128 'enabled' => $this->gateway_contract->is_pos_enabled( $item ),
129 'provider' => $this->gateway_contract->get_provider( $item, $request ),
130 'pos_type' => $this->gateway_contract->infer_pos_type( $item, $request ),
131 'capabilities' => $this->gateway_contract->get_capabilities( $item, $request ),
132 'provider_data' => $this->gateway_contract->get_provider_data( $item, $request ),
133 );
134
135 $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
136 $data = $this->add_additional_fields_to_object( $data, $request );
137 $data = $this->filter_response_by_context( $data, $context );
138
139 return rest_ensure_response( $data );
140 }
141
142 /**
143 * Get the POS payment gateway schema.
144 *
145 * @return array
146 */
147 public function get_item_schema(): array {
148 $schema = array(
149 '$schema' => 'http://json-schema.org/draft-04/schema#',
150 'title' => 'pos_payment_gateway',
151 'type' => 'object',
152 'properties' => array(
153 'id' => array(
154 'description' => __( 'Payment gateway ID.', 'woocommerce-pos' ),
155 'type' => 'string',
156 'context' => array( 'view' ),
157 'readonly' => true,
158 ),
159 'title' => array(
160 'description' => __( 'Payment gateway title shown at the POS.', 'woocommerce-pos' ),
161 'type' => 'string',
162 'context' => array( 'view' ),
163 'readonly' => true,
164 ),
165 'description' => array(
166 'description' => __( 'Payment gateway description.', 'woocommerce-pos' ),
167 'type' => 'string',
168 'context' => array( 'view' ),
169 'readonly' => true,
170 ),
171 'enabled' => array(
172 'description' => __( 'Whether the gateway is enabled for the POS.', 'woocommerce-pos' ),
173 'type' => 'boolean',
174 'context' => array( 'view' ),
175 'readonly' => true,
176 ),
177 'provider' => array(
178 'description' => __( 'Provider family identifier.', 'woocommerce-pos' ),
179 'type' => 'string',
180 'context' => array( 'view' ),
181 'readonly' => true,
182 ),
183 'pos_type' => array(
184 'description' => __( 'POS handling type for the gateway.', 'woocommerce-pos' ),
185 'type' => 'string',
186 'context' => array( 'view' ),
187 'readonly' => true,
188 ),
189 'capabilities' => array(
190 'description' => __( 'POS capabilities exposed by the gateway.', 'woocommerce-pos' ),
191 'type' => 'object',
192 'context' => array( 'view' ),
193 'readonly' => true,
194 'properties' => array(
195 'supports_checkout' => array(
196 'type' => 'boolean',
197 'context' => array( 'view' ),
198 ),
199 'supports_automatic_refunds' => array(
200 'type' => 'boolean',
201 'context' => array( 'view' ),
202 ),
203 'supports_provider_refunds' => array(
204 'type' => 'boolean',
205 'context' => array( 'view' ),
206 ),
207 'requires_hardware' => array(
208 'type' => 'boolean',
209 'context' => array( 'view' ),
210 ),
211 ),
212 ),
213 'provider_data' => array(
214 'description' => __( 'Provider-specific public metadata.', 'woocommerce-pos' ),
215 'type' => 'object',
216 'context' => array( 'view' ),
217 'readonly' => true,
218 ),
219 ),
220 );
221
222 return $this->add_additional_fields_schema( $schema );
223 }
224 }
225