get_id()); } } public static function queue_by_id($order_id) { self::queue((int) $order_id); } /** A completed order's lines carry shipped_at: its update is reported too. */ public static function queue_shipped($order_id) { $order = wc_get_order($order_id); if ($order && $order->get_status() === 'completed') { self::queue((int) $order_id); } } public static function queue($order_id, $try = 0, $delay = 0) { if (!$order_id || !Adly_Shop_Settings::connected()) { return; } $args = array('order_id' => (int) $order_id, 'try' => (int) $try); if (function_exists('as_schedule_single_action')) { if ($try === 0 && as_has_scheduled_action(self::HOOK, $args, self::GROUP)) { return; } as_schedule_single_action(time() + $delay, self::HOOK, $args, self::GROUP); } else { wp_schedule_single_event(time() + max(1, $delay), self::HOOK, $args); } } /** The order as ADLY takes it (POST /orders). Null for an order from before the connection. */ public static function payload($order) { $s = Adly_Shop_Settings::get(); $created = $order->get_date_created(); if (!$created) { return null; } if (!empty($s['connected_at']) && $created->getTimestamp() < (int) $s['connected_at'] && !$order->get_meta('_adly_check_id')) { return null; } $status = $order->get_status(); $paid = in_array($status, wc_get_is_paid_statuses(), true) || $status === 'refunded'; $state = in_array($status, array('cancelled', 'failed'), true) ? 'cancelled' : ($paid ? 'paid' : 'pending'); $shipped = $status === 'completed' ? Adly_Shop_Api::iso($order->get_date_completed() ? $order->get_date_completed() : $order->get_date_modified()) : null; $lines = array(); foreach ($order->get_items() as $item_id => $item) { $lines[] = array( 'id' => (string) $item_id, 'product_id' => (string) ($item->get_product_id() ? $item->get_product_id() : $item_id), 'title' => wp_strip_all_tags($item->get_name()), 'quantity' => (int) $item->get_quantity(), 'total' => Adly_Shop_Api::money((float) $item->get_total() + (float) $item->get_total_tax()), 'shipped_at' => $shipped, ); } $refunds = array(); foreach ($order->get_refunds() as $refund) { $reason = trim(wp_strip_all_tags((string) $refund->get_reason())); $refunds[] = array( 'id' => (string) $refund->get_id(), 'amount' => Adly_Shop_Api::money($refund->get_amount()), // ADLY asks why; a refund made without a note says so in plain words 'reason' => $reason !== '' ? mb_substr($reason, 0, 200) : 'Refund in WooCommerce (no reason given)', 'created_at' => Adly_Shop_Api::iso($refund->get_date_created()), ); } $codes = array_values(array_unique(array_map('strtoupper', $order->get_coupon_codes()))); $body = array( 'order_id' => (string) $order->get_order_number(), 'created_at' => Adly_Shop_Api::iso($created), 'updated_at' => Adly_Shop_Api::iso($order->get_date_modified() ? $order->get_date_modified() : $created), 'status' => $state, 'currency' => strtoupper($order->get_currency()), 'total' => Adly_Shop_Api::money($order->get_total()), 'discount_total' => Adly_Shop_Api::money((float) $order->get_discount_total() + (float) $order->get_discount_tax()), 'codes' => $codes, 'lines' => $lines, 'refunds' => $refunds, ); $check = (string) $order->get_meta('_adly_check_id'); if ($check !== '') { $body['check_id'] = $check; } return $body; } public static function report($order_id, $try = 0) { $order = wc_get_order($order_id); if (!$order || $order instanceof WC_Order_Refund) { return; } $body = self::payload($order); if (!$body) { return; } $res = Adly_Shop_Api::post('/orders', $body); if ($res['ok']) { $order->update_meta_data('_adly_reported_at', time()); $order->save_meta_data(); return; } // ADLY did not answer or was busy: again later, a little longer each time if ($res['retry'] && $try < self::MAX_TRIES) { self::queue($order_id, $try + 1, min(3600, 60 * pow(2, $try))); return; } $order->update_meta_data('_adly_report_error', isset($res['body']['error']['message']) ? $res['body']['error']['message'] : ('HTTP ' . $res['status'])); $order->save_meta_data(); } /** The thank-you page: ADLY hears of the order from the buyer's browser too, once. */ public static function beacon($order_id) { $order = wc_get_order($order_id); $s = Adly_Shop_Settings::get(); if (!$order || empty($s['pk']) || $order->get_meta('_adly_beacon')) { return; } $order->update_meta_data('_adly_beacon', time()); $order->save_meta_data(); $body = array('order_id' => (string) $order->get_order_number(), 'total' => Adly_Shop_Api::money($order->get_total())); $code = (string) $order->get_meta('_adly_code'); $check = (string) $order->get_meta('_adly_check_id'); if ($code !== '' && $check !== '') { $body['code'] = $code; $body['check_id'] = $check; } $base = !empty($s['api_base']) ? $s['api_base'] : ADLY_SHOP_API_BASE; wp_print_inline_script_tag(sprintf( "try{fetch(%s,{method:'POST',keepalive:true,headers:{'Authorization':%s,'Content-Type':'application/json'},body:%s});}catch(e){}", wp_json_encode(esc_url_raw(rtrim($base, '/') . '/beacon')), wp_json_encode('Bearer ' . sanitize_text_field($s['pk'])), wp_json_encode(wp_json_encode($body)) )); } }