PluginProbe
WooCommerce Square / 3.4.2
WooCommerce Square v3.4.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / includes / Gateway / API.php
API.php
546 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\Gateway;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use Square\Models\Order;
29
30 /**
31 * The base Square gateway API class.
32 *
33 * @since 2.0.0
34 */
35 class API extends \WooCommerce\Square\API {
36
37
38 /** @var string location ID to use for requests */
39 protected $location_id;
40
41 /** @var \WC_Order order object associated with a request, if any */
42 protected $order;
43
44
45 /**
46 * Constructs the class.
47 *
48 * @since 2.0.0
49 *
50 * @param string $access_token the API access token
51 * @param string $location_id location ID to use for requests
52 */
53 public function __construct( $access_token, $location_id, $is_sandbox = null ) {
54
55 parent::__construct( $access_token, $is_sandbox );
56
57 $this->location_id = $location_id;
58 }
59
60
61 /** Transaction methods *******************************************************************************************/
62
63
64 /**
65 * Performs a credit card authorization for the given order.
66 *
67 * @since 2.0.0
68 *
69 * @param \WC_Order $order order object
70 * @return \WooCommerce\Square\API\Response
71 * @throws \Exception
72 */
73 public function credit_card_authorization( \WC_Order $order ) {
74
75 $request = new API\Requests\Payments( $this->get_location_id(), $this->client );
76
77 $request->set_authorization_data( $order );
78
79 $this->set_response_handler( API\Responses\Create_Payment::class );
80
81 return $this->perform_request( $request );
82 }
83
84
85 /**
86 * Performs a credit card charge for the given order.
87 *
88 * @since 2.0.0
89 *
90 * @param \WC_Order $order order object
91 * @return \WooCommerce\Square\API\Response
92 * @throws \Exception
93 */
94 public function credit_card_charge( \WC_Order $order ) {
95
96 $request = new API\Requests\Payments( $this->get_location_id(), $this->client );
97
98 $request->set_charge_data( $order );
99
100 $this->set_response_handler( API\Responses\Create_Payment::class );
101
102 return $this->perform_request( $request );
103 }
104
105
106 /**
107 * Performs a credit card capture for a given authorized order.
108 *
109 * @since 2.0.0
110 *
111 * @param \WC_Order $order order object
112 * @return \WooCommerce\Square\API\Response
113 * @throws \Exception
114 */
115 public function credit_card_capture( \WC_Order $order ) {
116
117 $location_id = ! empty( $order->capture->location_id ) ? $order->capture->location_id : $this->get_location_id();
118
119 // use the Payments API to capture orders that were processed with Square v2.2+
120 if ( ! empty( $order->square_version ) && version_compare( $order->square_version, '2.2', '>=' ) ) {
121 $request = new API\Requests\Payments( $location_id, $this->client );
122 } else {
123 $request = new API\Requests\Transactions( $location_id, $this->client );
124 }
125
126 $request->set_capture_data( $order );
127
128 $this->set_response_handler( API\Response::class );
129
130 return $this->perform_request( $request );
131 }
132
133
134 /**
135 * Performs a refund for the given order.
136 *
137 * @since 2.0.0
138 *
139 * @param \WC_Order $order order object
140 * @return \WooCommerce\Square\API\Response
141 * @throws \Exception
142 */
143 public function refund( \WC_Order $order ) {
144
145 $location_id = ! empty( $order->refund->location_id ) ? $order->refund->location_id : $this->get_location_id();
146
147 // only use the Refunds API to refund orders that took payment after Square v2.2
148 if ( ! empty( $order->square_version ) && version_compare( $order->square_version, '2.2', '>=' ) ) {
149 $request = new API\Requests\Refunds( $this->client );
150 } else {
151 $request = new API\Requests\Transactions( $location_id, $this->client );
152 }
153
154 $request->set_refund_data( $order );
155
156 $this->set_response_handler( API\Responses\Refund::class );
157
158 return $this->perform_request( $request );
159 }
160
161
162 /**
163 * Performs a void for the given order.
164 *
165 * @since 2.0.0
166 *
167 * @param \WC_Order $order order object
168 * @return \WooCommerce\Square\API\Response
169 * @throws \Exception
170 */
171 public function void( \WC_Order $order ) {
172
173 $location_id = ! empty( $order->refund->location_id ) ? $order->refund->location_id : $this->get_location_id();
174
175 // use the Payments API to void/cancel orders that were processed after Square v2.2
176 if ( ! empty( $order->square_version ) && version_compare( $order->square_version, '2.2', '>=' ) ) {
177 $request = new API\Requests\Payments( $location_id, $this->client );
178 } else {
179 $request = new API\Requests\Transactions( $location_id, $this->client );
180 }
181
182 $request->set_void_data( $order );
183
184 $this->set_response_handler( API\Response::class );
185
186 return $this->perform_request( $request );
187 }
188
189
190 /**
191 * Creates a payment token for the given order.
192 *
193 * @since 2.0.0
194 *
195 * @param \WC_Order $order the order object
196 * @return API\Responses\Create_Customer_Card|API\Responses\Create_Customer
197 * @throws \Exception
198 */
199 public function tokenize_payment_method( \WC_Order $order ) {
200
201 // a customer ID should've already been created, but there may be cases where the customer id is deleted/corrupted at Square
202 if ( ! empty( $order->customer_id ) ) {
203
204 $response = $this->create_customer_card( $order );
205
206 if ( $response->has_error_code( 'NOT_FOUND' ) ) {
207 $order->customer_id = '';
208 } else {
209 return $response;
210 }
211 }
212
213 $response = $this->create_customer( $order );
214
215 if ( ! $response->transaction_approved() ) {
216 return $response;
217 }
218
219 // Update the user meta with the new customer id created for further API requests
220 update_user_meta( $order->get_user_id(), 'wc_square_customer_id', $response->get_customer_id(), $order->customer_id );
221
222 // Update the customer id on the order as well
223 $order->square_customer_id = $order->customer_id = $response->get_customer_id();
224
225 return $this->create_customer_card( $order );
226 }
227
228
229 /**
230 * Creates a payment token for the given order.
231 *
232 * @since 2.0.0
233 *
234 * @param \WC_Order $order the order object
235 * @return API\Responses\Create_Customer_Card
236 * @throws \Exception
237 */
238 public function create_customer_card( \WC_Order $order ) {
239
240 $request = new API\Requests\Card( $this->client );
241
242 $request->set_create_card_data( $order );
243
244 $this->set_response_handler( API\Responses\Create_Customer_Card::class );
245
246 return $this->perform_request( $request );
247 }
248
249
250 /**
251 * Creates a new customer based on the given order.
252 *
253 * @since 2.0.0
254 *
255 * @param \WC_Order $order order object
256 * @return API\Responses\Create_Customer
257 * @throws \Exception
258 */
259 public function create_customer( \WC_Order $order ) {
260
261 $request = new API\Requests\Customers( $this->client );
262
263 $request->set_create_customer_data( $order );
264
265 $this->set_response_handler( API\Responses\Create_Customer::class );
266
267 return $this->perform_request( $request );
268 }
269
270
271 /**
272 * Gets all tokenized payment methods for the customer.
273 *
274 * @since 2.0.0
275 *
276 * @param string $customer_id unique customer id
277 * @return API\Responses\Get_Customer
278 * @throws \Exception
279 */
280 public function get_tokenized_payment_methods( $customer_id ) {
281
282 $request = new API\Requests\Customers( $this->client );
283
284 $request->set_get_customer_data( $customer_id );
285
286 $this->set_response_handler( API\Responses\Get_Customer::class );
287
288 return $this->perform_request( $request );
289 }
290
291
292 /**
293 * Removes the tokenized payment method.
294 *
295 * @since 2.0.0
296 *
297 * @param string $token the payment method token
298 * @param string $customer_id unique customer id
299 * @return API\Response
300 * @throws \Exception
301 */
302 public function remove_tokenized_payment_method( $token, $customer_id ) {
303
304 $request = new API\Requests\Card( $this->client );
305
306 $request->set_delete_card_data( $token );
307
308 $this->set_response_handler( API\Response::class );
309
310 return $this->perform_request( $request );
311 }
312
313
314 /**
315 * Creates a new Square order from a WooCommerce order.
316 *
317 * @since 2.0.0
318 *
319 * @param string $location_id location ID
320 * @param \WC_Order $order
321 * @return Order
322 * @throws \Exception
323 */
324 public function create_order( $location_id, \WC_Order $order ) {
325
326 $request = new API\Requests\Orders( $this->client );
327
328 $request->set_create_order_data( $location_id, $order );
329
330 $this->set_response_handler( \WooCommerce\Square\API\Response::class );
331
332 $response = $this->perform_request( $request );
333
334 return $response->get_data()->getOrder();
335 }
336
337
338 /**
339 * Adjusts an existing Square order by amount.
340 *
341 * @since 2.0.4
342 *
343 * @param string $location_id location ID
344 * @param \WC_Order $order
345 * @param int $version Current 'version' value of Square order
346 * @param int $amount Amount of adjustment in smallest unit
347 * @return Order
348 * @throws \Exception
349 */
350 public function adjust_order( $location_id, \WC_Order $order, $version, $amount ) {
351
352 $request = new API\Requests\Orders( $this->client );
353
354 if ( $amount > 0 ) {
355 $request->add_line_item_order_data( $location_id, $order, $version, $amount );
356 } else {
357 $request->add_discount_order_data( $location_id, $order, $version, -1 * $amount );
358 }
359 $this->set_response_handler( \WooCommerce\Square\API\Response::class );
360
361 $response = $this->perform_request( $request );
362
363 return $response->get_data()->getOrder();
364 }
365
366
367 /**
368 * Gets an existing transaction.
369 *
370 * @since 2.0.0
371 *
372 * @param string $transaction_id transaction ID
373 * @param string $location_id location ID
374 * @return API\Responses\Charge
375 * @throws \Exception
376 */
377 public function get_transaction( $transaction_id, $location_id = '' ) {
378
379 if ( ! $location_id ) {
380 $location_id = $this->get_location_id();
381 }
382
383 $request = new API\Requests\Transactions( $location_id, $this->client );
384
385 $request->set_get_transaction_data( $transaction_id );
386
387 $this->set_response_handler( API\Responses\Charge::class );
388
389 return $this->perform_request( $request );
390 }
391
392
393 /**
394 * Gets an existing payment.
395 *
396 * @since 2.2.0
397 *
398 * @param string $payment_id transaction ID
399 * @return API\Responses\Create_Payment
400 * @throws \Exception
401 */
402 public function get_payment( $payment_id ) {
403
404 $request = new API\Requests\Payments( $this->get_location_id(), $this->client );
405
406 $request->set_get_payment_data( $payment_id );
407
408 $this->set_response_handler( API\Responses\Create_Payment::class );
409
410 return $this->perform_request( $request );
411 }
412
413
414 /**
415 * Validates the parsed response.
416 *
417 * @since 2.0.0
418 *
419 * @return bool
420 * @throws \Exception
421 */
422 protected function do_post_parse_response_validation() {
423
424 // gateway responses need to get through to check API\Response::transaction_approved()
425 if ( $this->get_response() instanceof API\Response ) {
426 return true;
427 }
428
429 return parent::do_post_parse_response_validation();
430 }
431
432
433 /** Conditional methods *******************************************************************************************/
434
435
436 /**
437 * Determines if this API supports getting a customer's tokenized payment methods.
438 *
439 * @since 2.0.0
440 *
441 * @return bool
442 */
443 public function supports_get_tokenized_payment_methods() {
444
445 return true;
446 }
447
448
449 /**
450 * Determines if this API supports updating tokenized payment methods.
451 *
452 * @see Payment_Gateway_API::update_tokenized_payment_method()
453 *
454 * @since 2.0.0
455 *
456 * @return bool
457 */
458 public function supports_update_tokenized_payment_method() {
459
460 return false;
461 }
462
463
464 /**
465 * Determines if this API supports removing a tokenized payment method.
466 *
467 * @since 2.0.0
468 *
469 * @return bool
470 */
471 public function supports_remove_tokenized_payment_method() {
472
473 return true;
474 }
475
476
477 /** Getter methods ************************************************************************************************/
478
479
480 /**
481 * Gets the location ID to be used for requests.
482 *
483 * @since 2.0.0
484 *
485 * @return string
486 */
487 protected function get_location_id() {
488
489 return $this->location_id;
490 }
491
492
493 /**
494 * Gets the object associated with the request, if any.
495 *
496 * @since 2.0.0
497 *
498 * @return \WC_Order
499 */
500 public function get_order() {
501
502 return $this->order;
503 }
504
505
506 /**
507 * Gets the API ID.
508 *
509 * @since 2.0.0
510 *
511 * @return string
512 */
513 protected function get_api_id() {
514
515 return $this->get_plugin()->get_gateway()->get_id();
516 }
517
518
519 /** No-op methods *************************************************************************************************/
520
521
522 /**
523 * The gateway API does not support check debits.
524 *
525 * @since 2.0.0
526 *
527 * @param \WC_Order $order order object
528 */
529 public function check_debit( \WC_Order $order ) {}
530
531
532 /**
533 * Updates a tokenized payment method.
534 *
535 * Square API does not allow updating a stored card's address, and instead recommends deleting and re-adding a new
536 * card. This isn't an option for us since subscriptions would break any time an address is updated.
537 *
538 * @since 2.0.0
539 *
540 * @param \WC_Order $order order object
541 */
542 public function update_tokenized_payment_method( \WC_Order $order ) {}
543
544
545 }
546