(int) self::pick( $settings, 'program_id', 0 ), 'external_order_id' => (string) self::pick( $order, 'order_id', '' ), 'event_id' => Affiliatant_Events::order_event( (string) self::pick( $settings, 'store_ref', '' ), (int) self::pick( $settings, 'program_id', 0 ), (string) self::pick( $order, 'order_id', '' ), $kind ), 'kind' => $kind, 'amount_minor' => AffiliatantClient::toMinor( self::pick( $order, 'total', 0 ), $currency ), 'tax_minor' => AffiliatantClient::toMinor( self::pick( $order, 'total_tax', 0 ), $currency ), 'shipping_minor' => $shipping_minor, 'discount_minor' => AffiliatantClient::toMinor( self::pick( $order, 'discount_total', 0 ), $currency ), 'currency' => $currency, 'channel' => 'woocommerce', 'occurred_at' => (string) self::pick( $order, 'date_created', '' ), 'is_test' => ! empty( $settings['is_test'] ) ? 1 : 0, 'items' => self::line_items( self::pick( $order, 'items', array() ), $currency ), ); // The merchant-facing order number, which a sequential-order-number // plugin can make different from the id. The id stays the key because // it is the only value guaranteed unique and stable; the number is // sent alongside so a merchant searching Affiliatant for what they see // on their own screen finds it. $number = (string) self::pick( $order, 'order_number', '' ); if ( '' !== $number && $number !== (string) self::pick( $order, 'order_id', '' ) ) { $payload['external_order_number'] = $number; } $customer_id = (int) self::pick( $order, 'customer_id', 0 ); $payload['external_customer_id'] = $customer_id > 0 ? (string) $customer_id : null; $store_ref = (string) self::pick( $settings, 'store_ref', '' ); if ( '' !== $store_ref && ctype_digit( $store_ref ) ) { $payload['store_id'] = (int) $store_ref; } // Coupon passthrough. Affiliatant can attribute on a code when there // is no click, which is the only thing that saves an order placed by // someone who heard the code on a podcast. The first code is the one // the attribution model reads; all of them are sent so the merchant // can see what was actually used. $coupons = self::pick( $order, 'coupons', array() ); if ( is_array( $coupons ) && array() !== $coupons ) { $clean = array(); foreach ( $coupons as $code ) { $code = substr( trim( (string) $code ), 0, 64 ); if ( '' !== $code ) { $clean[] = $code; } } if ( array() !== $clean ) { $payload['coupon_code'] = $clean[0]; $payload['coupon_codes'] = $clean; } } $payment_method = substr( (string) self::pick( $order, 'payment_method', '' ), 0, 64 ); if ( '' !== $payment_method ) { $payload['payment_method'] = $payment_method; } // A renewal carries the id of the order that started the subscription, // so a human reading the conversion can get back to the first sale. $parent = (int) self::pick( $order, 'parent_order_id', 0 ); if ( $parent > 0 ) { $payload['parent_external_order_id'] = (string) $parent; } $click_id = substr( (string) self::pick( $context, 'click_id', '' ), 0, 32 ); if ( '' !== $click_id ) { $payload['click_id'] = $click_id; } for ( $i = 1; $i <= 5; $i++ ) { $sub = substr( (string) self::pick( $context, 'sub' . $i, '' ), 0, 190 ); if ( '' !== $sub ) { $payload[ 'sub' . $i ] = $sub; } } $landing = substr( (string) self::pick( $context, 'landing_url', '' ), 0, 512 ); if ( '' !== $landing ) { $payload['landing_url'] = $landing; } return $payload; } /** * Line items. * * line_minor is the authoritative figure: it is WooCommerce's own line * total, after any discount and excluding tax. unit_minor is DERIVED, by * integer division of the pre-discount subtotal by the quantity, because * a "unit price" on a discounted line of three is not a number * WooCommerce stores. Quantity times unit_minor can therefore be a penny * or two away from line_minor, and that is correct rather than a bug: the * two answer different questions. * * @param array $items Snapshot items. * @param string $currency ISO code. * @return array */ public static function line_items( $items, $currency ) { $out = array(); if ( ! is_array( $items ) ) { return $out; } foreach ( $items as $item ) { if ( ! is_array( $item ) ) { continue; } $quantity = (int) self::pick( $item, 'quantity', 1 ); if ( $quantity < 1 ) { $quantity = 1; } $line_minor = AffiliatantClient::toMinor( self::pick( $item, 'total', 0 ), $currency ); $subtotal_minor = AffiliatantClient::toMinor( self::pick( $item, 'subtotal', self::pick( $item, 'total', 0 ) ), $currency ); $out[] = array( // Product names are merchant data. They are carried as data, // truncated and never interpreted. Escaping belongs where the // value is printed, not here. 'name' => substr( (string) self::pick( $item, 'name', '' ), 0, 255 ), 'sku' => substr( (string) self::pick( $item, 'sku', '' ), 0, 120 ), 'product_ref' => (string) (int) self::pick( $item, 'product_id', 0 ), 'variant_ref' => (string) (int) self::pick( $item, 'variation_id', 0 ), 'quantity' => $quantity, 'unit_minor' => intdiv( $subtotal_minor, $quantity ), 'line_minor' => $line_minor, 'tax_minor' => AffiliatantClient::toMinor( self::pick( $item, 'total_tax', 0 ), $currency ), ); } return $out; } /** * The payload for a connection test. * * Always is_test, whatever the test-mode setting says, so pressing a * button on a settings page can never create a real commission on a live * store. * * @param int $program_id Programme id. * @param string $store_ref Store reference. * @param string $currency Shop currency. * @param string $reference The AFFTEST- reference. * @param string $occurred ISO 8601 timestamp. * @return array */ public static function connection_test( $program_id, $store_ref, $currency, $reference, $occurred ) { return array( 'program_id' => (int) $program_id, 'external_order_id' => (string) $reference, 'event_id' => Affiliatant_Events::order_event( $store_ref, $program_id, $reference, 'sale' ), 'kind' => 'sale', 'amount_minor' => 1000, 'tax_minor' => 0, 'shipping_minor' => 0, 'currency' => strtoupper( (string) $currency ), 'channel' => 'woocommerce', 'occurred_at' => (string) $occurred, 'is_test' => 1, ); } /** * Array read with a default, because a snapshot built by an older version * of this plugin can be sitting in the queue when a newer one drains it. * * @param array $source Array. * @param string $key Key. * @param mixed $default Fallback. * @return mixed */ private static function pick( array $source, $key, $default ) { return array_key_exists( $key, $source ) && null !== $source[ $key ] ? $source[ $key ] : $default; } }