PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.8.0
WooCommerce Square v4.8.0
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 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Gateway / API / Requests / Orders.php
woocommerce-square / includes / Gateway / API / Requests Last commit date
Card.php 3 years ago Customers.php 3 years ago Gift_Card.php 2 years ago Gift_Card_Activities.php 2 years ago Orders.php 2 years ago Payments.php 2 years ago Refunds.php 3 years ago Transactions.php 3 years ago
Orders.php
519 lines
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 GNU General Public License v3.0 or later
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
20 namespace WooCommerce\Square\Gateway\API\Requests;
21
22 defined( 'ABSPATH' ) || exit;
23
24 use WooCommerce\Square\API;
25 use WooCommerce\Square\Framework\Square_Helper;
26 use WooCommerce\Square\Handlers\Product;
27 use WooCommerce\Square\Utilities\Money_Utility;
28
29 /**
30 * The Orders API request class.
31 *
32 * @since 2.0.0
33 */
34 class Orders extends API\Request {
35
36
37 /**
38 * Initializes a new Catalog request.
39 *
40 * @since 2.0.0
41 *
42 * @param \Square\SquareClient $api_client the API client
43 */
44 public function __construct( $api_client ) {
45 $this->square_api = $api_client->getOrdersApi();
46 }
47
48
49 /**
50 * Sets the data for creating an order.
51 *
52 * @since 2.0.0
53 *
54 * @param string $location_id location ID
55 * @param \WC_Order $order order object
56 */
57 public function set_create_order_data( $location_id, \WC_Order $order ) {
58
59 $this->square_api_method = 'createOrder';
60 $this->square_request = new \Square\Models\CreateOrderRequest();
61
62 $order_model = new \Square\Models\Order( $location_id );
63 if ( ! empty( $order->square_customer_id ) ) {
64 $order_model->setCustomerId( $order->square_customer_id );
65 }
66
67 // Set the data.
68 $this->set_order_data( $order, $order_model );
69 }
70
71 /**
72 * Prepares data to retrieve a Square order.
73 *
74 * @param string $order_id The Square order ID.
75 */
76 public function set_retrieve_order_data( $order_id ) {
77 $this->square_api_method = 'retrieveOrder';
78 $this->square_api_args = array( $order_id );
79 }
80
81 /**
82 * Prepare data to update Square order.
83 *
84 * @param \WC_Order $order WooCommerce order object.
85 * @param \Square\Models\Order $square_order Square order object.
86 */
87 public function set_update_order_data( \WC_Order $order, \Square\Models\Order $square_order ) {
88
89 $this->square_api_method = 'updateOrder';
90 $this->square_request = new \Square\Models\UpdateOrderRequest();
91 $this->square_request->setFieldsToClear(
92 array(
93 'discounts',
94 'line_items',
95 'service_charges',
96 'taxes',
97 )
98 );
99
100 $order_model = new \Square\Models\Order( $square_order->getLocationId() );
101 $order_model->setCustomerId( $square_order->getCustomerId() );
102
103 $order_model->setVersion( $square_order->getVersion() );
104
105 // Set the data.
106 $this->set_order_data( $order, $order_model );
107 $this->square_api_args = array( $order->square_order_id, $this->square_request );
108 }
109
110 /**
111 * Sets the data for an order.
112 *
113 * @since 3.7.0
114 *
115 * @param \WC_Order $order WooCommerce order object.
116 * @param \Square\Models\Order $square_order Square order object.
117 */
118 public function set_order_data( \WC_Order $order, \Square\Models\Order $order_model ) {
119 $order_model->setReferenceId( $order->get_order_number() );
120
121 $taxes = $this->get_order_taxes( $order );
122 $all_line_items = $this->get_api_line_items(
123 $order,
124 array_merge( $this->get_product_line_items( $order ), $this->get_fee_line_items( $order ), $this->get_shipping_line_items( $order ) ),
125 $taxes
126 );
127
128 $square_order_line_items = array_values(
129 array_filter(
130 $all_line_items,
131 function( $line_item ) {
132 return $line_item instanceof \Square\Models\OrderLineItem;
133 }
134 )
135 );
136
137 $square_discount_line_items = array_values(
138 array_filter(
139 $all_line_items,
140 function( $line_item ) {
141 return $line_item instanceof \Square\Models\OrderLineItemDiscount;
142 }
143 )
144 );
145
146 $square_updated_taxes_line_items = array_values(
147 array_filter(
148 $all_line_items,
149 function( $line_item ) {
150 return $line_item instanceof \Square\Models\OrderLineItemTax;
151 }
152 )
153 );
154
155 // Merge existing and new taxes.
156 $taxes = array_merge( $taxes, $square_updated_taxes_line_items );
157
158 $order_model->setLineItems( $square_order_line_items );
159
160 if ( ! empty( $square_discount_line_items ) ) {
161 $order_model->setDiscounts( $square_discount_line_items );
162 }
163
164 $order_model->setTaxes( array_values( $taxes ) );
165
166 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) );
167 $this->square_request->setOrder( $order_model );
168
169 $this->square_api_args = array( $this->square_request );
170 }
171
172 /**
173 * Sets request data when a payment is to be made using multiple payment methods.
174 * For example: Gift Card + Square Credit Card.
175 *
176 * @param array $payment_ids Array of payment IDs.
177 * @param string $order_id Square order ID.
178 * @since 3.9.0
179 */
180 public function set_pay_order_data( $payment_ids, $order_id ) {
181 $this->square_api_method = 'payOrder';
182 $this->square_request = new \Square\Models\PayOrderRequest( uniqid() );
183
184 $this->square_request->setPaymentIds( $payment_ids );
185 $this->square_api_args = array( $order_id, $this->square_request );
186 }
187
188 /**
189 * Gets Square line item objects for an order's product items.
190 *
191 * @since 2.0.0
192 *
193 * @param \WC_Order $order order object
194 * @return \WC_Order_Item_Product[]
195 */
196 protected function get_product_line_items( \WC_Order $order ) {
197
198 $line_items = array();
199
200 foreach ( $order->get_items() as $item ) {
201
202 if ( ! $item instanceof \WC_Order_Item_Product ) {
203 continue;
204 }
205
206 $line_items[] = $item;
207 }
208
209 return $line_items;
210 }
211
212
213 /**
214 * Gets Square line item objects for an order's fee items.
215 *
216 * @since 2.0.0
217 *
218 * @param \WC_Order $order order object
219 * @return \WC_Order_Item_Fee[]
220 */
221 protected function get_fee_line_items( \WC_Order $order ) {
222
223 $line_items = array();
224
225 foreach ( $order->get_fees() as $item ) {
226
227 if ( ! $item instanceof \WC_Order_Item_Fee ) {
228 continue;
229 }
230
231 $line_items[] = $item;
232 }
233
234 return $line_items;
235 }
236
237
238 /**
239 * Gets Square line item objects for an order's shipping items.
240 *
241 * @since 2.0.0
242 *
243 * @param \WC_Order $order order object
244 * @return \WC_Order_Item_Shipping[]
245 */
246 protected function get_shipping_line_items( \WC_Order $order ) {
247
248 $line_items = array();
249
250 foreach ( $order->get_shipping_methods() as $item ) {
251
252 if ( ! $item instanceof \WC_Order_Item_Shipping ) {
253 continue;
254 }
255
256 $line_items[] = $item;
257 }
258
259 return $line_items;
260 }
261
262
263 /**
264 * Gets Square API line item objects.
265 *
266 * @since 2.2.6
267 *
268 * @param \WC_Order $order
269 * @param \WC_Order_Item[] $line_items
270 * @param \Square\Models\OrderLineItemTax[] $taxes
271 * @return \Square\Models\OrderLineItem[]
272 */
273 protected function get_api_line_items( \WC_Order $order, $line_items, $taxes ) {
274 $api_line_items = array();
275 $tax_type = wc_prices_include_tax() ? API::TAX_TYPE_INCLUSIVE : API::TAX_TYPE_ADDITIVE;
276
277 /** @var \WC_Order_Item_Product $item */
278 foreach ( $line_items as $item ) {
279 $is_product = $item instanceof \WC_Order_Item_Product;
280 $line_item = new \Square\Models\OrderLineItem( $is_product ? (string) $item->get_quantity() : (string) 1 );
281
282 if ( $is_product && Product::is_gift_card( $item->get_product() ) ) {
283 $line_item->setItemType( 'GIFT_CARD' );
284 }
285
286 $total_tax = $item->get_total_tax();
287 $total_amount = $item->get_total();
288 $subtotal_amount = $is_product ? $item->get_subtotal() : $total_amount;
289
290 // Inlcude the tax in subtotal when prices are inclusive of taxes.
291 if ( API::TAX_TYPE_INCLUSIVE === $tax_type ) {
292 $subtotal_amount += $total_tax;
293 }
294
295 // Subtotal per quantity.
296 $subtotal_amount = $subtotal_amount / $item->get_quantity();
297
298 $line_item->setQuantity( $is_product ? (string) $item->get_quantity() : (string) 1 );
299 $line_item->setBasePriceMoney( Money_Utility::amount_to_money( $subtotal_amount, $order->get_currency() ) );
300
301 if ( $is_product && $item->get_meta( Product::SQUARE_VARIATION_ID_META_KEY ) ) {
302 $line_item->setCatalogObjectId( $item->get_meta( Product::SQUARE_VARIATION_ID_META_KEY ) );
303 } else {
304 $line_item->setName( $item->get_name() );
305 }
306
307 // CALCULATE DISCOUNT.
308 if ( $item instanceof \WC_Order_Item_Product ) {
309 $discount = $item->get_subtotal() - $item->get_total();
310 $discount_uid = wc_square()->get_idempotency_key( '', false );
311
312 if ( $discount > 0 ) {
313 $line_item->setAppliedDiscounts(
314 array( new \Square\Models\OrderLineItemAppliedDiscount( $discount_uid ) )
315 );
316
317 $order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
318 $order_line_item_discount->setUid( $discount_uid );
319 $order_line_item_discount->setName( __( 'Discount', 'woocommerce-square' ) );
320 $order_line_item_discount->setType( 'FIXED_AMOUNT' );
321 $order_line_item_discount->setScope( 'LINE_ITEM' );
322 $order_line_item_discount->setAmountMoney(
323 Money_Utility::amount_to_money(
324 $discount,
325 $order->get_currency()
326 )
327 );
328
329 $api_line_items[] = $order_line_item_discount;
330 }
331 }
332
333 // CALCULATE TAXES.
334 $applied_taxes = array();
335 $get_taxes = $item->get_taxes();
336 if ( isset( $get_taxes['total'] ) ) {
337 foreach ( $get_taxes['total'] as $key => $tax_amount ) {
338 // CALCULATE TAX.
339
340 if ( empty( $tax_amount ) ) {
341 continue;
342 }
343
344 $item_uid = $item->get_id();
345 $tax_uid = $taxes[ $key ]->getUid();
346 $prev_percentage = $taxes[ $key ]->getPercentage();
347 $adjusted_percentage = $tax_amount * 100 / $total_amount;
348 $adjusted_percentage = number_format( (float) $adjusted_percentage, 2, '.', '' );
349
350 if ( $prev_percentage !== $adjusted_percentage ) {
351 // Create a new tax.
352 $uniqid = uniqid();
353
354 $tax_item = new \Square\Models\OrderLineItemTax();
355 $adjusted_tax_name = $taxes[ $key ]->getName() . __( ' - (Adjusted Tax for) - ', 'woocommerce-square' ) . $item_uid;
356 $tax_item->setUid( $uniqid );
357 $tax_item->setName( $adjusted_tax_name );
358 $tax_item->setType( $tax_type );
359 $tax_item->setScope( 'LINE_ITEM' );
360 $tax_item->setPercentage( $adjusted_percentage );
361
362 $api_line_items[] = $tax_item;
363 } else {
364 $uniqid = $tax_uid;
365 }
366
367 $applied_taxes[] = new \Square\Models\OrderLineItemAppliedTax( $uniqid );
368 }
369 }
370
371 $line_item->setAppliedTaxes( $applied_taxes );
372
373 $api_line_items[] = $line_item;
374 }
375
376 return $api_line_items;
377 }
378
379
380 /**
381 * Gets the tax line items for an order.
382 *
383 * @since 2.0.0
384 *
385 * @param \WC_Order $order
386 * @return \Square\Models\OrderLineItemTax[]
387 */
388 protected function get_order_taxes( \WC_Order $order ) {
389 $taxes = array();
390 $tax_type = wc_prices_include_tax() ? API::TAX_TYPE_INCLUSIVE : API::TAX_TYPE_ADDITIVE;
391
392 foreach ( $order->get_taxes() as $tax ) {
393 $tax_item = new \Square\Models\OrderLineItemTax();
394 $tax_item->setUid( uniqid() );
395 $tax_item->setName( $tax->get_name() );
396 $tax_item->setType( $tax_type );
397 $tax_item->setScope( 'LINE_ITEM' );
398 $tax_item->setPercentage( Square_Helper::number_format( (float) $tax->get_rate_percent() ) );
399 $taxes[ $tax->get_rate_id() ] = $tax_item;
400 }
401
402 return $taxes;
403 }
404
405 /**
406 * Creates applied taxes array for each Square line item.
407 *
408 * @since 2.0.4
409 *
410 * @param \Square\Models\OrderLineItemTax[] $taxes
411 * @param WC_Order_Item $line_item
412 * @return \Square\Models\OrderLineItemAppliedTax[] $taxes
413 */
414 protected function apply_taxes( $taxes, $line_item ) {
415
416 $tax_ids = array();
417
418 $get_taxes = $line_item->get_taxes();
419 if ( isset( $get_taxes['total'] ) ) {
420 foreach ( $get_taxes['total'] as $key => $value ) {
421 $tax_ids[] = $key;
422 }
423 }
424
425 $applied_taxes = array();
426
427 foreach ( $tax_ids as $tax_id ) {
428 if ( empty( $tax_id ) ) {
429 continue;
430 }
431
432 $applied_taxes[] = new \Square\Models\OrderLineItemAppliedTax( $taxes[ $tax_id ]->getUid() );
433 };
434
435 return empty( $applied_taxes ) ? null : $applied_taxes;
436 }
437
438
439 /**
440 * Sets the data for updating an order with a line item adjustment.
441 *
442 * @since 2.0.4
443 *
444 * @param string $location_id location ID
445 * @param \WC_Order $order order object
446 * @param int $version Current 'version' value of Square order
447 * @param int $amount Amount of line item in smallest unit
448 */
449 public function add_line_item_order_data( $location_id, \WC_Order $order, $version, $amount ) {
450
451 $this->square_api_method = 'updateOrder';
452 $this->square_request = new \Square\Models\UpdateOrderRequest();
453
454 $order_model = new \Square\Models\Order( $location_id );
455 $order_model->setVersion( $version );
456
457 $line_item = new \Square\Models\OrderLineItem( (string) 1 );
458 $line_item->setName( __( 'Adjustment', 'woocommerce-square' ) );
459 $line_item->setQuantity( (string) 1 );
460
461 $money_object = new \Square\Models\Money();
462 $money_object->setAmount( $amount );
463 $money_object->setCurrency( $order->get_currency() );
464
465 $line_item->setBasePriceMoney( $money_object );
466 $order_model->setLineItems( array( $line_item ) );
467
468 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version );
469 $this->square_request->setOrder( $order_model );
470
471 $this->square_api_args = array(
472 $order->square_order_id,
473 $this->square_request,
474 );
475 }
476
477
478 /**
479 * Sets the data for updating an order with a discount adjustment.
480 *
481 * @since 2.0.4
482 *
483 * @param string $location_id location ID
484 * @param \WC_Order $order order object
485 * @param int $version Current 'version' value of Square order
486 * @param int $amount Amount of discount in smallest unit
487 */
488 public function add_discount_order_data( $location_id, \WC_Order $order, $version, $amount ) {
489
490 $this->square_api_method = 'updateOrder';
491 $this->square_request = new \Square\Models\UpdateOrderRequest();
492
493 $order_model = new \Square\Models\Order( $location_id );
494 $order_model->setVersion( $version );
495
496 $order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
497 $order_line_item_discount->setName( __( 'Adjustment', 'woocommerce-square' ) );
498 $order_line_item_discount->setType( 'FIXED_AMOUNT' );
499
500 $money_object = new \Square\Models\Money();
501 $money_object->setAmount( $amount );
502 $money_object->setCurrency( $order->get_currency() );
503
504 $order_line_item_discount->setAmountMoney( $money_object );
505 $order_line_item_discount->setScope( 'ORDER' );
506
507 $order_model->setDiscounts( array( $order_line_item_discount ) );
508
509 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version );
510 $this->square_request->setOrder( $order_model );
511
512 $this->square_api_args = array(
513 $order->square_order_id,
514 $this->square_request,
515 );
516 }
517
518 }
519