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