| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- class OrderMapper {
- /**
- * Convert a Reverb order object into an OpenCart addOrder() data array.
- *
- * @param array $reverb_order Order object from GET /my/orders/{number}.
- * @param array $oc_product Matching OC product row (product_id, name, model, price).
- * @param array $store_info ['store_id', 'store_name', 'store_url', 'language_id',
- * 'currency_id', 'currency_code', 'currency_value'].
- * @param int $order_status_id Default order status ID (typically 1 = Pending).
- * @return array Ready to pass to model_checkout_order->addOrder().
- */
- public static function toOpenCart(array $reverb_order, array $oc_product, array $store_info, $order_status_id = 1) {
- // --- Buyer name split ---
- $buyer_name = isset($reverb_order['buyer_name']) ? $reverb_order['buyer_name'] : 'Reverb Buyer';
- $name_parts = explode(' ', trim($buyer_name), 2);
- $first_name = $name_parts[0];
- $last_name = isset($name_parts[1]) ? $name_parts[1] : '';
- $buyer_email = isset($reverb_order['buyer_email']) ? $reverb_order['buyer_email'] : '';
- // --- Shipping address ---
- $ship = isset($reverb_order['shipping_address']) ? $reverb_order['shipping_address'] : [];
- $ship_name = isset($ship['name']) ? $ship['name'] : $buyer_name;
- $ship_parts = explode(' ', trim($ship_name), 2);
- $ship_first = $ship_parts[0];
- $ship_last = isset($ship_parts[1]) ? $ship_parts[1] : '';
- $ship_addr1 = isset($ship['street_address']) ? $ship['street_address'] : '';
- $ship_city = isset($ship['locality']) ? $ship['locality'] : '';
- $ship_state = isset($ship['region']) ? $ship['region'] : '';
- $ship_post = isset($ship['postal_code']) ? $ship['postal_code'] : '';
- $ship_country = isset($ship['country_code']) ? strtoupper($ship['country_code']) : 'AU';
- // --- Totals ---
- $total_amount = isset($reverb_order['total']['amount'])
- ? (float)$reverb_order['total']['amount']
- : (float)$oc_product['price'] * (isset($reverb_order['quantity']) ? (int)$reverb_order['quantity'] : 1);
- $quantity = isset($reverb_order['quantity']) ? (int)$reverb_order['quantity'] : 1;
- $order_data = [
- // Store
- 'invoice_prefix' => 'INV-',
- 'invoice_no' => 0,
- 'store_id' => $store_info['store_id'],
- 'store_name' => $store_info['store_name'],
- 'store_url' => $store_info['store_url'],
- // Customer (guest)
- 'customer_id' => 0,
- 'customer_group_id' => 1,
- 'firstname' => $first_name,
- 'lastname' => $last_name,
- 'email' => $buyer_email,
- 'telephone' => '',
- 'fax' => '',
- 'custom_field' => [],
- // Payment (use shipping address for Reverb orders)
- 'payment_firstname' => $ship_first,
- 'payment_lastname' => $ship_last,
- 'payment_company' => '',
- 'payment_address_1' => $ship_addr1,
- 'payment_address_2' => '',
- 'payment_city' => $ship_city,
- 'payment_postcode' => $ship_post,
- 'payment_country' => $ship_country,
- 'payment_country_id' => 0,
- 'payment_zone' => $ship_state,
- 'payment_zone_id' => 0,
- 'payment_address_format'=> '',
- 'payment_custom_field' => [],
- 'payment_method' => 'Reverb',
- 'payment_code' => 'reverb',
- // Shipping
- 'shipping_firstname' => $ship_first,
- 'shipping_lastname' => $ship_last,
- 'shipping_company' => '',
- 'shipping_address_1' => $ship_addr1,
- 'shipping_address_2' => '',
- 'shipping_city' => $ship_city,
- 'shipping_postcode' => $ship_post,
- 'shipping_country' => $ship_country,
- 'shipping_country_id' => 0,
- 'shipping_zone' => $ship_state,
- 'shipping_zone_id' => 0,
- 'shipping_address_format' => '',
- 'shipping_custom_field' => [],
- 'shipping_method' => 'Reverb Shipping',
- 'shipping_code' => 'reverb.reverb',
- // Misc
- 'comment' => 'Reverb Order #' . ($reverb_order['order_number'] ?? ''),
- 'total' => $total_amount,
- 'affiliate_id' => 0,
- 'commission' => 0,
- 'language_id' => $store_info['language_id'],
- 'currency_id' => $store_info['currency_id'],
- 'currency_code' => $store_info['currency_code'],
- 'currency_value' => $store_info['currency_value'],
- 'ip' => '',
- 'forwarded_ip' => '',
- 'user_agent' => '',
- 'accept_language' => '',
- 'order_status_id' => $order_status_id,
- // Products
- 'products' => [
- [
- 'product_id' => (int)$oc_product['product_id'],
- 'name' => $oc_product['name'],
- 'model' => $oc_product['model'],
- 'quantity' => $quantity,
- 'price' => (float)$oc_product['price'],
- 'total' => (float)$oc_product['price'] * $quantity,
- 'tax' => 0.00,
- 'reward' => 0,
- 'option' => [],
- 'download' => [],
- 'subscription' => '',
- 'tax_class_id' => 0,
- ],
- ],
- // Totals
- 'totals' => [
- [
- 'code' => 'sub_total',
- 'title' => 'Sub-Total',
- 'value' => $total_amount,
- 'sort_order' => 1,
- ],
- [
- 'code' => 'total',
- 'title' => 'Total',
- 'value' => $total_amount,
- 'sort_order' => 9,
- ],
- ],
- 'vouchers' => [],
- ];
- return $order_data;
- }
- }
|