PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
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 3 years ago Orders.php 3 years ago Payments.php 3 years ago Refunds.php 3 years ago Transactions.php 3 years ago
Orders.php
498 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 * Gets Square line item objects for an order's product items.
174 *
175 * @since 2.0.0
176 *
177 * @param \WC_Order $order order object
178 * @return \WC_Order_Item_Product[]
179 */
180 protected function get_product_line_items( \WC_Order $order ) {
181
182 $line_items = array();
183
184 foreach ( $order->get_items() as $item ) {
185
186 if ( ! $item instanceof \WC_Order_Item_Product ) {
187 continue;
188 }
189
190 $line_items[] = $item;
191 }
192
193 return $line_items;
194 }
195
196
197 /**
198 * Gets Square line item objects for an order's fee items.
199 *
200 * @since 2.0.0
201 *
202 * @param \WC_Order $order order object
203 * @return \WC_Order_Item_Fee[]
204 */
205 protected function get_fee_line_items( \WC_Order $order ) {
206
207 $line_items = array();
208
209 foreach ( $order->get_fees() as $item ) {
210
211 if ( ! $item instanceof \WC_Order_Item_Fee ) {
212 continue;
213 }
214
215 $line_items[] = $item;
216 }
217
218 return $line_items;
219 }
220
221
222 /**
223 * Gets Square line item objects for an order's shipping items.
224 *
225 * @since 2.0.0
226 *
227 * @param \WC_Order $order order object
228 * @return \WC_Order_Item_Shipping[]
229 */
230 protected function get_shipping_line_items( \WC_Order $order ) {
231
232 $line_items = array();
233
234 foreach ( $order->get_shipping_methods() as $item ) {
235
236 if ( ! $item instanceof \WC_Order_Item_Shipping ) {
237 continue;
238 }
239
240 $line_items[] = $item;
241 }
242
243 return $line_items;
244 }
245
246
247 /**
248 * Gets Square API line item objects.
249 *
250 * @since 2.2.6
251 *
252 * @param \WC_Order $order
253 * @param \WC_Order_Item[] $line_items
254 * @param \Square\Models\OrderLineItemTax[] $taxes
255 * @return \Square\Models\OrderLineItem[]
256 */
257 protected function get_api_line_items( \WC_Order $order, $line_items, $taxes ) {
258 $api_line_items = array();
259 $tax_type = wc_prices_include_tax() ? API::TAX_TYPE_INCLUSIVE : API::TAX_TYPE_ADDITIVE;
260
261 foreach ( $line_items as $item ) {
262 $is_product = $item instanceof \WC_Order_Item_Product;
263 $line_item = new \Square\Models\OrderLineItem( $is_product ? (string) $item->get_quantity() : (string) 1 );
264
265 $total_tax = $item->get_total_tax();
266 $total_amount = $item->get_total();
267 $subtotal_amount = $is_product ? $item->get_subtotal() : $total_amount;
268
269 // Inlcude the tax in subtotal when prices are inclusive of taxes.
270 if ( API::TAX_TYPE_INCLUSIVE === $tax_type ) {
271 $subtotal_amount += $total_tax;
272 }
273
274 // Subtotal per quantity.
275 $subtotal_amount = $subtotal_amount / $item->get_quantity();
276
277 $line_item->setQuantity( $is_product ? (string) $item->get_quantity() : (string) 1 );
278 $line_item->setBasePriceMoney( Money_Utility::amount_to_money( $subtotal_amount, $order->get_currency() ) );
279
280 if ( $is_product && $item->get_meta( Product::SQUARE_VARIATION_ID_META_KEY ) ) {
281 $line_item->setCatalogObjectId( $item->get_meta( Product::SQUARE_VARIATION_ID_META_KEY ) );
282 } else {
283 $line_item->setName( $item->get_name() );
284 }
285
286 // CALCULATE DISCOUNT.
287 if ( $item instanceof \WC_Order_Item_Product ) {
288 $discount = $item->get_subtotal() - $item->get_total();
289 $discount_uid = wc_square()->get_idempotency_key( '', false );
290
291 if ( $discount > 0 ) {
292 $line_item->setAppliedDiscounts(
293 array( new \Square\Models\OrderLineItemAppliedDiscount( $discount_uid ) )
294 );
295
296 $order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
297 $order_line_item_discount->setUid( $discount_uid );
298 $order_line_item_discount->setName( __( 'Discount', 'woocommerce-square' ) );
299 $order_line_item_discount->setType( 'FIXED_AMOUNT' );
300 $order_line_item_discount->setScope( 'LINE_ITEM' );
301 $order_line_item_discount->setAmountMoney(
302 Money_Utility::amount_to_money(
303 $discount,
304 $order->get_currency()
305 )
306 );
307
308 $api_line_items[] = $order_line_item_discount;
309 }
310 }
311
312 // CALCULATE TAXES.
313 $applied_taxes = array();
314 $get_taxes = $item->get_taxes();
315 if ( isset( $get_taxes['total'] ) ) {
316 foreach ( $get_taxes['total'] as $key => $tax_amount ) {
317 // CALCULATE TAX.
318
319 if ( empty( $tax_amount ) ) {
320 continue;
321 }
322
323 $item_uid = $item->get_id();
324 $tax_uid = $taxes[ $key ]->getUid();
325 $prev_percentage = $taxes[ $key ]->getPercentage();
326 $adjusted_percentage = $tax_amount * 100 / $total_amount;
327 $adjusted_percentage = number_format( (float) $adjusted_percentage, 2, '.', '' );
328
329 if ( $prev_percentage !== $adjusted_percentage ) {
330 // Create a new tax.
331 $uniqid = uniqid();
332
333 $tax_item = new \Square\Models\OrderLineItemTax();
334 $adjusted_tax_name = $taxes[ $key ]->getName() . __( ' - (Adjusted Tax for) - ', 'woocommerce-square' ) . $item_uid;
335 $tax_item->setUid( $uniqid );
336 $tax_item->setName( $adjusted_tax_name );
337 $tax_item->setType( $tax_type );
338 $tax_item->setScope( 'LINE_ITEM' );
339 $tax_item->setPercentage( $adjusted_percentage );
340
341 $api_line_items[] = $tax_item;
342 } else {
343 $uniqid = $tax_uid;
344 }
345
346 $applied_taxes[] = new \Square\Models\OrderLineItemAppliedTax( $uniqid );
347 }
348 }
349
350 $line_item->setAppliedTaxes( $applied_taxes );
351
352 $api_line_items[] = $line_item;
353 }
354
355 return $api_line_items;
356 }
357
358
359 /**
360 * Gets the tax line items for an order.
361 *
362 * @since 2.0.0
363 *
364 * @param \WC_Order $order
365 * @return \Square\Models\OrderLineItemTax[]
366 */
367 protected function get_order_taxes( \WC_Order $order ) {
368 $taxes = array();
369 $tax_type = wc_prices_include_tax() ? API::TAX_TYPE_INCLUSIVE : API::TAX_TYPE_ADDITIVE;
370
371 foreach ( $order->get_taxes() as $tax ) {
372 $tax_item = new \Square\Models\OrderLineItemTax();
373 $tax_item->setUid( uniqid() );
374 $tax_item->setName( $tax->get_name() );
375 $tax_item->setType( $tax_type );
376 $tax_item->setScope( 'LINE_ITEM' );
377 $tax_item->setPercentage( Square_Helper::number_format( (float) $tax->get_rate_percent() ) );
378 $taxes[ $tax->get_rate_id() ] = $tax_item;
379 }
380
381 return $taxes;
382 }
383
384 /**
385 * Creates applied taxes array for each Square line item.
386 *
387 * @since 2.0.4
388 *
389 * @param \Square\Models\OrderLineItemTax[] $taxes
390 * @param WC_Order_Item $line_item
391 * @return \Square\Models\OrderLineItemAppliedTax[] $taxes
392 */
393 protected function apply_taxes( $taxes, $line_item ) {
394
395 $tax_ids = array();
396
397 $get_taxes = $line_item->get_taxes();
398 if ( isset( $get_taxes['total'] ) ) {
399 foreach ( $get_taxes['total'] as $key => $value ) {
400 $tax_ids[] = $key;
401 }
402 }
403
404 $applied_taxes = array();
405
406 foreach ( $tax_ids as $tax_id ) {
407 if ( empty( $tax_id ) ) {
408 continue;
409 }
410
411 $applied_taxes[] = new \Square\Models\OrderLineItemAppliedTax( $taxes[ $tax_id ]->getUid() );
412 };
413
414 return empty( $applied_taxes ) ? null : $applied_taxes;
415 }
416
417
418 /**
419 * Sets the data for updating an order with a line item adjustment.
420 *
421 * @since 2.0.4
422 *
423 * @param string $location_id location ID
424 * @param \WC_Order $order order object
425 * @param int $version Current 'version' value of Square order
426 * @param int $amount Amount of line item in smallest unit
427 */
428 public function add_line_item_order_data( $location_id, \WC_Order $order, $version, $amount ) {
429
430 $this->square_api_method = 'updateOrder';
431 $this->square_request = new \Square\Models\UpdateOrderRequest();
432
433 $order_model = new \Square\Models\Order( $location_id );
434 $order_model->setVersion( $version );
435
436 $line_item = new \Square\Models\OrderLineItem( (string) 1 );
437 $line_item->setName( __( 'Adjustment', 'woocommerce-square' ) );
438 $line_item->setQuantity( (string) 1 );
439
440 $money_object = new \Square\Models\Money();
441 $money_object->setAmount( $amount );
442 $money_object->setCurrency( $order->get_currency() );
443
444 $line_item->setBasePriceMoney( $money_object );
445 $order_model->setLineItems( array( $line_item ) );
446
447 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version );
448 $this->square_request->setOrder( $order_model );
449
450 $this->square_api_args = array(
451 $order->square_order_id,
452 $this->square_request,
453 );
454 }
455
456
457 /**
458 * Sets the data for updating an order with a discount adjustment.
459 *
460 * @since 2.0.4
461 *
462 * @param string $location_id location ID
463 * @param \WC_Order $order order object
464 * @param int $version Current 'version' value of Square order
465 * @param int $amount Amount of discount in smallest unit
466 */
467 public function add_discount_order_data( $location_id, \WC_Order $order, $version, $amount ) {
468
469 $this->square_api_method = 'updateOrder';
470 $this->square_request = new \Square\Models\UpdateOrderRequest();
471
472 $order_model = new \Square\Models\Order( $location_id );
473 $order_model->setVersion( $version );
474
475 $order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
476 $order_line_item_discount->setName( __( 'Adjustment', 'woocommerce-square' ) );
477 $order_line_item_discount->setType( 'FIXED_AMOUNT' );
478
479 $money_object = new \Square\Models\Money();
480 $money_object->setAmount( $amount );
481 $money_object->setCurrency( $order->get_currency() );
482
483 $order_line_item_discount->setAmountMoney( $money_object );
484 $order_line_item_discount->setScope( 'ORDER' );
485
486 $order_model->setDiscounts( array( $order_line_item_discount ) );
487
488 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version );
489 $this->square_request->setOrder( $order_model );
490
491 $this->square_api_args = array(
492 $order->square_order_id,
493 $this->square_request,
494 );
495 }
496
497 }
498