swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * Class WC_REST_Stripe_Locations_Controller |
| 4 | */ |
| 5 | |
| 6 | defined( 'ABSPATH' ) || exit; |
| 7 | |
| 8 | /** |
| 9 | * REST controller for terminal locations. |
| 10 | */ |
| 11 | class WC_REST_Stripe_Locations_Controller extends WC_Stripe_REST_Base_Controller { |
| 12 | |
| 13 | /** |
| 14 | * Endpoint path. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $rest_base = 'wc_stripe/terminal/locations'; |
| 19 | |
| 20 | /** |
| 21 | * Configure REST API routes. |
| 22 | */ |
| 23 | public function register_routes() { |
| 24 | register_rest_route( |
| 25 | $this->namespace, |
| 26 | '/' . $this->rest_base, |
| 27 | [ |
| 28 | 'methods' => WP_REST_Server::CREATABLE, |
| 29 | 'callback' => [ $this, 'create_location' ], |
| 30 | 'permission_callback' => [ $this, 'check_permission' ], |
| 31 | 'args' => [ |
| 32 | 'display_name' => [ |
| 33 | 'type' => 'string', |
| 34 | 'required' => true, |
| 35 | ], |
| 36 | 'address' => [ |
| 37 | 'type' => 'object', |
| 38 | 'required' => true, |
| 39 | ], |
| 40 | ], |
| 41 | ] |
| 42 | ); |
| 43 | register_rest_route( |
| 44 | $this->namespace, |
| 45 | '/' . $this->rest_base, |
| 46 | [ |
| 47 | 'methods' => WP_REST_Server::READABLE, |
| 48 | 'callback' => [ $this, 'get_all_locations' ], |
| 49 | 'permission_callback' => [ $this, 'check_permission' ], |
| 50 | 'args' => [ |
| 51 | 'ending_before' => [ |
| 52 | 'type' => 'string', |
| 53 | 'required' => false, |
| 54 | ], |
| 55 | 'limit' => [ |
| 56 | 'type' => 'integer', |
| 57 | 'required' => false, |
| 58 | ], |
| 59 | 'starting_after' => [ |
| 60 | 'type' => 'string', |
| 61 | 'required' => false, |
| 62 | ], |
| 63 | ], |
| 64 | ] |
| 65 | ); |
| 66 | register_rest_route( |
| 67 | $this->namespace, |
| 68 | '/' . $this->rest_base . '/store', |
| 69 | [ |
| 70 | 'methods' => WP_REST_Server::READABLE, |
| 71 | 'callback' => [ $this, 'get_store_location' ], |
| 72 | 'permission_callback' => [ $this, 'check_permission' ], |
| 73 | ] |
| 74 | ); |
| 75 | register_rest_route( |
| 76 | $this->namespace, |
| 77 | '/' . $this->rest_base . '/(?P<location_id>\w+)', |
| 78 | [ |
| 79 | 'methods' => WP_REST_Server::DELETABLE, |
| 80 | 'callback' => [ $this, 'delete_location' ], |
| 81 | 'permission_callback' => [ $this, 'check_permission' ], |
| 82 | ] |
| 83 | ); |
| 84 | register_rest_route( |
| 85 | $this->namespace, |
| 86 | '/' . $this->rest_base . '/(?P<location_id>\w+)', |
| 87 | [ |
| 88 | 'methods' => WP_REST_Server::READABLE, |
| 89 | 'callback' => [ $this, 'get_location' ], |
| 90 | 'permission_callback' => [ $this, 'check_permission' ], |
| 91 | ] |
| 92 | ); |
| 93 | register_rest_route( |
| 94 | $this->namespace, |
| 95 | '/' . $this->rest_base . '/(?P<location_id>\w+)', |
| 96 | [ |
| 97 | 'methods' => WP_REST_Server::CREATABLE, |
| 98 | 'callback' => [ $this, 'update_location' ], |
| 99 | 'permission_callback' => [ $this, 'check_permission' ], |
| 100 | 'args' => [ |
| 101 | 'display_name' => [ |
| 102 | 'type' => 'string', |
| 103 | 'required' => false, |
| 104 | ], |
| 105 | 'address' => [ |
| 106 | 'type' => 'object', |
| 107 | 'required' => false, |
| 108 | ], |
| 109 | ], |
| 110 | ] |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Create a terminal location via Stripe API. |
| 116 | * |
| 117 | * @param WP_REST_Request $request Full data about the request. |
| 118 | */ |
| 119 | public function create_location( $request ) { |
| 120 | try { |
| 121 | $response = WC_Stripe_API::request( |
| 122 | [ |
| 123 | 'display_name' => $request['display_name'], |
| 124 | 'address' => $request['address'], |
| 125 | ], |
| 126 | 'terminal/locations' |
| 127 | ); |
| 128 | return rest_ensure_response( $response ); |
| 129 | } catch ( WC_Stripe_Exception $e ) { |
| 130 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get all terminal locations via Stripe API. |
| 136 | * |
| 137 | * @param WP_REST_Request $request Full data about the request. |
| 138 | */ |
| 139 | public function get_all_locations( $request ) { |
| 140 | try { |
| 141 | return rest_ensure_response( $this->fetch_locations() ); |
| 142 | } catch ( WC_Stripe_Exception $e ) { |
| 143 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Delete a terminal location via Stripe API. |
| 149 | * |
| 150 | * @param WP_REST_Request $request Full data about the request. |
| 151 | */ |
| 152 | public function delete_location( $request ) { |
| 153 | try { |
| 154 | $response = WC_Stripe_API::request( [], 'terminal/locations/' . urlencode( $request['location_id'] ), 'DELETE' ); |
| 155 | return rest_ensure_response( $response ); |
| 156 | } catch ( WC_Stripe_Exception $e ) { |
| 157 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get a terminal location via Stripe API. |
| 163 | * |
| 164 | * @param WP_REST_Request $request Full data about the request. |
| 165 | */ |
| 166 | public function get_location( $request ) { |
| 167 | try { |
| 168 | $response = WC_Stripe_API::request( [], 'terminal/locations/' . urlencode( $request['location_id'] ), 'GET' ); |
| 169 | return rest_ensure_response( $response ); |
| 170 | } catch ( WC_Stripe_Exception $e ) { |
| 171 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get default terminal location. |
| 177 | * |
| 178 | * @param WP_REST_Request $request Full data about the request. |
| 179 | */ |
| 180 | public function get_store_location( $request ) { |
| 181 | // Originally `get_bloginfo` was used for location name, later switched to `site_url` as the former may be blank. |
| 182 | $store_hostname = str_replace( [ 'https://', 'http://' ], '', get_site_url() ); |
| 183 | $possible_names = [ get_bloginfo(), $store_hostname ]; |
| 184 | $store_address = WC()->countries; |
| 185 | $address = array_filter( |
| 186 | [ |
| 187 | 'city' => $store_address->get_base_city(), |
| 188 | 'country' => $store_address->get_base_country(), |
| 189 | 'line1' => $store_address->get_base_address(), |
| 190 | 'line2' => $store_address->get_base_address_2(), |
| 191 | 'postal_code' => $store_address->get_base_postcode(), |
| 192 | 'state' => $store_address->get_base_state(), |
| 193 | ] |
| 194 | ); |
| 195 | |
| 196 | // Return an error if store doesn't have a location. |
| 197 | $is_address_populated = isset( $address['country'], $address['city'], $address['postal_code'], $address['line1'] ); |
| 198 | if ( ! $is_address_populated ) { |
| 199 | return rest_ensure_response( |
| 200 | new WP_Error( |
| 201 | 'store_address_is_incomplete', |
| 202 | admin_url( |
| 203 | add_query_arg( |
| 204 | [ |
| 205 | 'page' => 'wc-settings', |
| 206 | 'tab' => 'general', |
| 207 | ], |
| 208 | 'admin.php' |
| 209 | ) |
| 210 | ) |
| 211 | ) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | try { |
| 216 | foreach ( $this->fetch_locations() as $location ) { |
| 217 | if ( |
| 218 | in_array( $location->display_name, $possible_names, true ) |
| 219 | && count( array_intersect( (array) $location->address, $address ) ) === count( $address ) |
| 220 | ) { |
| 221 | return rest_ensure_response( $location ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Create new location if no location matches display name and address. |
| 226 | $response = WC_Stripe_API::request( |
| 227 | [ |
| 228 | 'display_name' => $store_hostname, |
| 229 | 'address' => $address, |
| 230 | ], |
| 231 | 'terminal/locations' |
| 232 | ); |
| 233 | return rest_ensure_response( $response ); |
| 234 | } catch ( WC_Stripe_Exception $e ) { |
| 235 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Update a terminal location via Stripe API. |
| 241 | * |
| 242 | * @param WP_REST_Request $request Full data about the request. |
| 243 | */ |
| 244 | public function update_location( $request ) { |
| 245 | $body = []; |
| 246 | if ( isset( $request['display_name'] ) ) { |
| 247 | $body['display_name'] = $request['display_name']; |
| 248 | } |
| 249 | if ( isset( $request['address'] ) ) { |
| 250 | $body['address'] = $request['address']; |
| 251 | } |
| 252 | try { |
| 253 | $response = WC_Stripe_API::request( $body, 'terminal/locations/' . urlencode( $request['location_id'] ), 'POST' ); |
| 254 | return rest_ensure_response( $response ); |
| 255 | } catch ( WC_Stripe_Exception $e ) { |
| 256 | return rest_ensure_response( new WP_Error( 'stripe_error', $e->getMessage() ) ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Fetch terminal locations from Stripe API. |
| 262 | */ |
| 263 | private function fetch_locations() { |
| 264 | $response = (array) WC_Stripe_API::request( [], 'terminal/locations', 'GET' ); |
| 265 | return $response['data']; |
| 266 | } |
| 267 | } |