Customers.php
5 years ago
Orders.php
5 years ago
Payments.php
6 years ago
Refunds.php
6 years ago
Transactions.php
6 years ago
Orders.php
368 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 |
| 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\API\Requests; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | use SquareConnect\Api\OrdersApi; |
| 30 | use SquareConnect\Model as SquareModel; |
| 31 | use WooCommerce\Square\API; |
| 32 | use WooCommerce\Square\Handlers\Product; |
| 33 | use WooCommerce\Square\Utilities\Money_Utility; |
| 34 | |
| 35 | /** |
| 36 | * The Orders API request class. |
| 37 | * |
| 38 | * @since 2.0.0 |
| 39 | */ |
| 40 | class Orders extends API\Request { |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Initializes a new Catalog request. |
| 45 | * |
| 46 | * @since 2.0.0 |
| 47 | * |
| 48 | * @param \SquareConnect\ApiClient $api_client the API client |
| 49 | */ |
| 50 | public function __construct( $api_client ) { |
| 51 | |
| 52 | $this->square_api = new OrdersApi( $api_client ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Sets the data for creating an order. |
| 58 | * |
| 59 | * @since 2.0.0 |
| 60 | * |
| 61 | * @param string $location_id location ID |
| 62 | * @param \WC_Order $order order object |
| 63 | */ |
| 64 | public function set_create_order_data( $location_id, \WC_Order $order ) { |
| 65 | |
| 66 | $this->square_api_method = 'createOrder'; |
| 67 | $this->square_request = new SquareModel\CreateOrderRequest(); |
| 68 | |
| 69 | $order_model = new SquareModel\Order(); |
| 70 | $order_model->setReferenceId( $order->get_order_number() ); |
| 71 | if ( ! empty( $order->square_customer_id ) ) { |
| 72 | $order_model->setCustomerId( $order->square_customer_id ); |
| 73 | } |
| 74 | |
| 75 | $line_items = array_merge( $this->get_product_line_items( $order ), $this->get_fee_line_items( $order ), $this->get_shipping_line_items( $order ) ); |
| 76 | $taxes = $this->get_order_taxes( $order ); |
| 77 | |
| 78 | $this->apply_taxes( $taxes, $line_items ); |
| 79 | $order_model->setLineItems( $line_items ); |
| 80 | $order_model->setTaxes( $taxes ); |
| 81 | |
| 82 | if ( $order->get_discount_total() ) { |
| 83 | |
| 84 | $order_model->setDiscounts( |
| 85 | array( |
| 86 | new SquareModel\OrderLineItemDiscount( |
| 87 | array( |
| 88 | 'name' => __( 'Discount', 'woocommerce-square' ), |
| 89 | 'type' => 'FIXED_AMOUNT', |
| 90 | 'amount_money' => Money_Utility::amount_to_money( $order->get_discount_total(), $order->get_currency() ), |
| 91 | 'scope' => 'ORDER', |
| 92 | ) |
| 93 | ), |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) ); |
| 99 | $this->square_request->setOrder( $order_model ); |
| 100 | |
| 101 | $this->square_api_args = array( |
| 102 | $location_id, |
| 103 | $this->square_request, |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Gets Square line item objects for an order's product items. |
| 110 | * |
| 111 | * @since 2.0.0 |
| 112 | * |
| 113 | * @param \WC_Order $order order object |
| 114 | * @return SquareModel\OrderLineItem[] |
| 115 | */ |
| 116 | protected function get_product_line_items( \WC_Order $order ) { |
| 117 | |
| 118 | $line_items = array(); |
| 119 | |
| 120 | foreach ( $order->get_items() as $item ) { |
| 121 | |
| 122 | if ( ! $item instanceof \WC_Order_Item_Product ) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $line_item = new SquareModel\OrderLineItem(); |
| 127 | |
| 128 | $line_item->setQuantity( (string) $item->get_quantity() ); |
| 129 | $line_item->setBasePriceMoney( Money_Utility::amount_to_money( $order->get_item_subtotal( $item ), $order->get_currency() ) ); |
| 130 | |
| 131 | $square_id = $item->get_meta( Product::SQUARE_VARIATION_ID_META_KEY ); |
| 132 | |
| 133 | if ( $square_id ) { |
| 134 | $line_item->setCatalogObjectId( $square_id ); |
| 135 | } else { |
| 136 | $line_item->setName( $item->get_name() ); |
| 137 | } |
| 138 | |
| 139 | $line_items[] = $line_item; |
| 140 | } |
| 141 | |
| 142 | return $line_items; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * Gets Square line item objects for an order's fee items. |
| 148 | * |
| 149 | * @since 2.0.0 |
| 150 | * |
| 151 | * @param \WC_Order $order order object |
| 152 | * @return SquareModel\OrderLineItem[] |
| 153 | */ |
| 154 | protected function get_fee_line_items( \WC_Order $order ) { |
| 155 | |
| 156 | $line_items = array(); |
| 157 | |
| 158 | foreach ( $order->get_fees() as $item ) { |
| 159 | |
| 160 | if ( ! $item instanceof \WC_Order_Item_Fee ) { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | $line_item = new SquareModel\OrderLineItem(); |
| 165 | |
| 166 | $line_item->setQuantity( (string) 1 ); |
| 167 | |
| 168 | $line_item->setName( $item->get_name() ); |
| 169 | $line_item->setBasePriceMoney( Money_Utility::amount_to_money( $item->get_total(), $order->get_currency() ) ); |
| 170 | |
| 171 | $line_items[] = $line_item; |
| 172 | } |
| 173 | |
| 174 | return $line_items; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Gets Square line item objects for an order's shipping items. |
| 180 | * |
| 181 | * @since 2.0.0 |
| 182 | * |
| 183 | * @param \WC_Order $order order object |
| 184 | * @return SquareModel\OrderLineItem[] |
| 185 | */ |
| 186 | protected function get_shipping_line_items( \WC_Order $order ) { |
| 187 | |
| 188 | $line_items = array(); |
| 189 | |
| 190 | foreach ( $order->get_shipping_methods() as $item ) { |
| 191 | |
| 192 | if ( ! $item instanceof \WC_Order_Item_Shipping ) { |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | $line_item = new SquareModel\OrderLineItem(); |
| 197 | |
| 198 | $line_item->setQuantity( (string) 1 ); |
| 199 | |
| 200 | $line_item->setName( $item->get_name() ); |
| 201 | $line_item->setBasePriceMoney( Money_Utility::amount_to_money( $item->get_total(), $order->get_currency() ) ); |
| 202 | |
| 203 | $line_items[] = $line_item; |
| 204 | } |
| 205 | |
| 206 | return $line_items; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Gets the tax line items for an order. |
| 212 | * |
| 213 | * @since 2.0.0 |
| 214 | * |
| 215 | * @param \WC_Order $order |
| 216 | * @return SquareModel\OrderLineItemTax[] |
| 217 | */ |
| 218 | protected function get_order_taxes( \WC_Order $order ) { |
| 219 | |
| 220 | $taxes = array(); |
| 221 | |
| 222 | foreach ( $order->get_taxes() as $tax ) { |
| 223 | |
| 224 | $tax_item = new SquareModel\OrderLineItemTax( |
| 225 | array( |
| 226 | 'uid' => uniqid(), |
| 227 | 'name' => $tax->get_name(), |
| 228 | 'type' => 'ADDITIVE', |
| 229 | 'scope' => 'LINE_ITEM', |
| 230 | ) |
| 231 | ); |
| 232 | |
| 233 | $pre_tax_total = (float) $order->get_total() - (float) $order->get_total_tax(); |
| 234 | $total_tax = (float) $tax->get_tax_total() + (float) $tax->get_shipping_tax_total(); |
| 235 | |
| 236 | $percentage = ( $total_tax / $pre_tax_total ) * 100; |
| 237 | |
| 238 | $tax_item->setPercentage( Framework\SV_WC_Helper::number_format( $percentage ) ); |
| 239 | |
| 240 | $taxes[] = $tax_item; |
| 241 | } |
| 242 | |
| 243 | return $taxes; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Applies taxes on each Square line item. |
| 249 | * |
| 250 | * @since 2.0.4 |
| 251 | * |
| 252 | * @param SquareModel\OrderLineItemTax[] $taxes |
| 253 | * @param SquareModel\OrderLineItem[] $line_items |
| 254 | */ |
| 255 | protected function apply_taxes( $taxes, $line_items ) { |
| 256 | |
| 257 | foreach ( $line_items as $line_item ) { |
| 258 | |
| 259 | $applied_taxes = array(); |
| 260 | |
| 261 | foreach ( $taxes as $tax ) { |
| 262 | |
| 263 | $applied_taxes[] = new SquareModel\OrderLineItemAppliedTax( |
| 264 | array( |
| 265 | 'tax_uid' => $tax->getUid(), |
| 266 | ) |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | $line_item->setAppliedTaxes( $applied_taxes ); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * Sets the data for updating an order with a line item adjustment. |
| 277 | * |
| 278 | * @since 2.0.4 |
| 279 | * |
| 280 | * @param string $location_id location ID |
| 281 | * @param \WC_Order $order order object |
| 282 | * @param int $version Current 'version' value of Square order |
| 283 | * @param int $amount Amount of line item in smallest unit |
| 284 | */ |
| 285 | public function add_line_item_order_data( $location_id, \WC_Order $order, $version, $amount ) { |
| 286 | |
| 287 | $this->square_api_method = 'updateOrder'; |
| 288 | $this->square_request = new SquareModel\UpdateOrderRequest(); |
| 289 | |
| 290 | $order_model = new SquareModel\Order(); |
| 291 | $order_model->setVersion( $version ); |
| 292 | |
| 293 | $order_model->setLineItems( |
| 294 | array( |
| 295 | new SquareModel\OrderLineItem( |
| 296 | array( |
| 297 | 'name' => __( 'Adjustment', 'woocommerce-square' ), |
| 298 | 'quantity' => (string) 1, |
| 299 | 'base_price_money' => new SquareModel\Money( |
| 300 | array( |
| 301 | 'amount' => $amount, |
| 302 | 'currency' => $order->get_currency(), |
| 303 | ) |
| 304 | ), |
| 305 | ) |
| 306 | ), |
| 307 | ) |
| 308 | ); |
| 309 | |
| 310 | $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version ); |
| 311 | $this->square_request->setOrder( $order_model ); |
| 312 | |
| 313 | $this->square_api_args = array( |
| 314 | $location_id, |
| 315 | $order->square_order_id, |
| 316 | $this->square_request, |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /** |
| 322 | * Sets the data for updating an order with a discount adjustment. |
| 323 | * |
| 324 | * @since 2.0.4 |
| 325 | * |
| 326 | * @param string $location_id location ID |
| 327 | * @param \WC_Order $order order object |
| 328 | * @param int $version Current 'version' value of Square order |
| 329 | * @param int $amount Amount of discount in smallest unit |
| 330 | */ |
| 331 | public function add_discount_order_data( $location_id, \WC_Order $order, $version, $amount ) { |
| 332 | |
| 333 | $this->square_api_method = 'updateOrder'; |
| 334 | $this->square_request = new SquareModel\UpdateOrderRequest(); |
| 335 | |
| 336 | $order_model = new SquareModel\Order(); |
| 337 | $order_model->setVersion( $version ); |
| 338 | |
| 339 | $order_model->setDiscounts( |
| 340 | array( |
| 341 | new SquareModel\OrderLineItemDiscount( |
| 342 | array( |
| 343 | 'name' => __( 'Adjustment', 'woocommerce-square' ), |
| 344 | 'type' => 'FIXED_AMOUNT', |
| 345 | 'amount_money' => new SquareModel\Money( |
| 346 | array( |
| 347 | 'amount' => $amount, |
| 348 | 'currency' => $order->get_currency(), |
| 349 | ) |
| 350 | ), |
| 351 | 'scope' => 'ORDER', |
| 352 | ) |
| 353 | ), |
| 354 | ) |
| 355 | ); |
| 356 | |
| 357 | $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) . $version ); |
| 358 | $this->square_request->setOrder( $order_model ); |
| 359 | |
| 360 | $this->square_api_args = array( |
| 361 | $location_id, |
| 362 | $order->square_order_id, |
| 363 | $this->square_request, |
| 364 | ); |
| 365 | } |
| 366 | |
| 367 | } |
| 368 |