'pending', 'created_via' => self::CREATED_VIA, ) ); if ( is_wp_error( $order ) || ! $order ) { return self::failure( is_wp_error( $order ) ? $order->get_error_message() : 'wc_create_order returned nothing' ); } Aha_Is_Woocommerce_Echo_Guard::mark_order( $order->get_id() ); // Older WooCommerce ignores created_via in the arguments array. if ( method_exists( $order, 'set_created_via' ) && self::CREATED_VIA !== $order->get_created_via() ) { $order->set_created_via( self::CREATED_VIA ); } $items_total = self::add_line_items( $order, $lines ); $shipping_total = self::add_shipping_lines( $order, $payload ); self::apply_order_fields( $order, $payload ); // Totals are set by hand. calculate_totals() would replace the Aha // prices with the store prices, which is exactly what must not happen. $order->set_discount_total( 0 ); $order->set_cart_tax( 0 ); $order->set_shipping_tax( 0 ); $order->set_shipping_total( self::money( $shipping_total ) ); $order->set_total( self::money( $items_total + $shipping_total ) ); $order->save(); // A paid Aha order goes straight to processing. payment_complete() // also reduces stock through the standard WooCommerce hooks. if ( ! empty( $payload['set_paid'] ) ) { $order->payment_complete(); } return array( 'ok' => true, 'order_id' => (int) $order->get_id(), 'number' => (string) $order->get_order_number(), 'error' => '', ); } catch ( Throwable $e ) { return self::failure( $e->getMessage() ); } finally { Aha_Is_Woocommerce_Echo_Guard::release(); } } /** * Add the line items with the Aha prices. * * @param WC_Order $order Order. * @param array $lines Resolved lines. * @return float Sum of the line totals. */ private static function add_line_items( $order, array $lines ) { $sum = 0.0; foreach ( $lines as $line ) { $item = new WC_Order_Item_Product(); $item->set_product_id( (int) $line['product_id'] ); if ( ! empty( $line['variation_id'] ) ) { $item->set_variation_id( (int) $line['variation_id'] ); } $item->set_quantity( (int) $line['quantity'] ); $item->set_name( wc_clean( '' !== $line['sent_name'] ? $line['sent_name'] : $line['name'] ) ); // Aha prices, written as they arrived. $item->set_subtotal( self::money( $line['subtotal'] ) ); $item->set_total( self::money( $line['total'] ) ); $item->set_taxes( array() ); $order->add_item( $item ); $sum += (float) $line['total']; } return $sum; } /** * Add the shipping line. * * The contract allows at most one line. Free shipping is one line with the * total "0.00", not an empty array. * * @param WC_Order $order Order. * @param array $payload Payload. * @return float Shipping total. */ private static function add_shipping_lines( $order, array $payload ) { if ( empty( $payload['shipping_lines'] ) || ! is_array( $payload['shipping_lines'] ) ) { return 0.0; } $line = $payload['shipping_lines'][0]; $total = isset( $line['total'] ) ? (float) $line['total'] : 0.0; $item = new WC_Order_Item_Shipping(); $item->set_method_id( isset( $line['method_id'] ) ? (string) $line['method_id'] : 'aha_is_woocommerce_delivery' ); $item->set_method_title( isset( $line['method_title'] ) ? wc_clean( (string) $line['method_title'] ) : 'Aha delivery' ); $item->set_total( self::money( $total ) ); $item->set_taxes( array() ); $order->add_item( $item ); return $total; } /** * Set the addresses, the payment fields, the note and the Aha meta. * * The addresses arrive masked. Aha is the middle seller and owns the * delivery, so the store never receives the real customer contact. Do not * "repair" this. * * @param WC_Order $order Order. * @param array $payload Payload. * @return void */ private static function apply_order_fields( $order, array $payload ) { $order->set_currency( isset( $payload['currency'] ) ? (string) $payload['currency'] : 'ISK' ); $order->set_payment_method( isset( $payload['payment_method'] ) ? (string) $payload['payment_method'] : 'aha' ); $order->set_payment_method_title( isset( $payload['payment_method_title'] ) ? wc_clean( (string) $payload['payment_method_title'] ) : 'Aha' ); if ( ! empty( $payload['billing'] ) && is_array( $payload['billing'] ) ) { $order->set_address( self::address_fields( $payload['billing'] ), 'billing' ); } if ( ! empty( $payload['shipping'] ) && is_array( $payload['shipping'] ) ) { $order->set_address( self::address_fields( $payload['shipping'] ), 'shipping' ); } if ( isset( $payload['customer_note'] ) ) { $order->set_customer_note( wc_sanitize_textarea( (string) $payload['customer_note'] ) ); } $order->update_meta_data( self::META_ORDER_ID, (string) $payload['aha_order_id'] ); if ( isset( $payload['aha_order_increment_id'] ) ) { $order->update_meta_data( self::META_INCREMENT_ID, (string) $payload['aha_order_increment_id'] ); } // Extra meta the caller sent. The two Aha keys above always win. if ( ! empty( $payload['meta_data'] ) && is_array( $payload['meta_data'] ) ) { foreach ( $payload['meta_data'] as $meta ) { if ( ! isset( $meta['key'] ) ) { continue; } $key = (string) $meta['key']; if ( in_array( $key, array( self::META_ORDER_ID, self::META_INCREMENT_ID, 'aha_order_id', 'aha_order_increment_id' ), true ) ) { // The backend also sends the two wire names in meta_data. continue; } $order->update_meta_data( $key, isset( $meta['value'] ) ? $meta['value'] : '' ); } } } /** * Map the contract address fields to the WooCommerce address fields. * * @param array $address Address from the payload. * @return array */ private static function address_fields( array $address ) { $allowed = array( 'first_name', 'last_name', 'address_1', 'city', 'postcode', 'country', 'email', 'phone' ); $fields = array(); foreach ( $allowed as $name ) { if ( isset( $address[ $name ] ) ) { $fields[ $name ] = wc_clean( (string) $address[ $name ] ); } } return $fields; } /** * Format a money value the way WooCommerce stores it. * * @param string|float $value Value. * @return string */ private static function money( $value ) { return wc_format_decimal( $value, wc_get_price_decimals() ); } /** * Build a failure answer. * * @param string $message Message. * @return array */ private static function failure( $message ) { return array( 'ok' => false, 'order_id' => 0, 'number' => '', 'error' => $message, ); } }