'ip', 'user_agent' => 'userAgent', 'fbc' => 'fbc', 'fbp' => 'fbp', 'ttclid' => 'ttclid', 'ttp' => 'ttp', 'scclid' => 'sc_click_id', 'gclid' => 'gclid', ); public static function for_job( array $job, WC_Order $order ): ?array { $type = isset( $job['type'] ) ? (string) $job['type'] : ''; $status = isset( $job['status'] ) ? (string) $job['status'] : ''; $timestamp = isset( $job['occurred_at'] ) ? (int) $job['occurred_at'] : time(); if ( 'order.refunded' === $type ) { return self::refunded( $order, isset( $job['refund_id'] ) ? (int) $job['refund_id'] : 0, $timestamp ); } if ( 'order.status_changed' === $type && 'cancelled' === $status ) { $previous = isset( $job['previous_status'] ) ? (string) $job['previous_status'] : ''; return self::envelope( 'Order Cancelled', 'order:' . $order->get_id() . ':cancelled', Payloads::order_cancelled( $order, $previous ), $order, $timestamp ); } if ( ! in_array( $status, wc_get_is_paid_statuses(), true ) ) { return null; } $properties = Payloads::order_completed( $order ); return null === $properties ? null : self::envelope( 'Order Completed', 'order:' . $order->get_id() . ':completed', $properties, $order, $timestamp ); } public static function envelope( string $event, string $message_suffix, array $properties, WC_Order $order, int $timestamp ): array { $checkout = self::checkout_context( $order ); $envelope = array( 'type' => 'track', 'event' => $event, 'channel' => 'server', 'messageId' => 'wc:' . Connection::site_id() . ':' . $message_suffix, 'anonymousId' => isset( $checkout['anonymous_id'] ) ? (string) $checkout['anonymous_id'] : 'wc-order-' . $order->get_id(), 'timestamp' => gmdate( 'Y-m-d\TH:i:s\Z', $timestamp ), 'properties' => $properties, 'context' => self::context( $order, $checkout ), ); if ( $order->get_customer_id() > 0 ) { $envelope['userId'] = (string) $order->get_customer_id(); } return $envelope; } private static function refunded( WC_Order $order, int $refund_id, int $timestamp ): ?array { $refund = wc_get_order( $refund_id ); if ( ! $refund instanceof WC_Order_Refund ) { return null; } return self::envelope( 'Order Refunded', 'refund:' . $refund->get_id(), Payloads::order_refunded( $order, $refund ), $order, $timestamp ); } private static function checkout_context( WC_Order $order ): array { $stored = $order->get_meta( CheckoutContext::META ); $stored = is_array( $stored ) ? $stored : array(); $stored = array_filter( $stored, 'is_string' ); $stored = array_filter( $stored, 'strlen' ); return $stored + array_filter( array( 'ip' => $order->get_customer_ip_address(), 'user_agent' => $order->get_customer_user_agent(), ) ); } private static function page_url( WC_Order $order ): string { return remove_query_arg( 'key', wc_get_endpoint_url( 'order-received', (string) $order->get_id(), wc_get_checkout_url() ) ); } private static function context( WC_Order $order, array $checkout ): array { $context = array(); foreach ( self::CONTEXT_KEYS as $stored_key => $context_key ) { if ( ! empty( $checkout[ $stored_key ] ) ) { $context[ $context_key ] = (string) $checkout[ $stored_key ]; } } $context['page'] = array( 'url' => self::page_url( $order ) ); $traits = array_filter( array( 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone(), 'firstName' => $order->get_billing_first_name(), 'lastName' => $order->get_billing_last_name(), ) ); if ( array() !== $traits ) { $context['traits'] = $traits; } $context['library'] = array( 'name' => 'flowfy-for-woocommerce', 'version' => FLOWFY_VERSION, ); return $context; } }