UnionTrans.php 14.6 KB
<?php

/**
 * 联盟数据转换
 */

namespace Plugin;

use Hood\Core\Security\AuthCode;
use Api\Yohobuy;
use LibModels\Wap\Home\OrderData;

class UnionTrans
{

    /**
     * 第三方联盟名称
     * 
     * @var array
     */
    private static $unionNames = array(
        1001 => '亿起发',
        1010 => '领科特',
        1009 => '成果网',
        2995 => '亿起发wap',
        2997 => '领科特wap',
        3001 => '51fanli',
        3017 => '多麦web',
        3019 => '多麦wap',
        3057 => '多麦优质',
    );

    /**
     * 过滤人民币
     * 
     * @param string $price
     * @return array
     */
    private static function filterPrice($price)
    {
        return strtr($price, array('-' => '', '+' => '', '¥' => ''));
    }

    /**
     * 添加成交记录
     *
     * @param String $order_code
     * @param Integer $pay_time
     * @param Integer $ip
     * @return bool
     */
    public static function set($uid, $order_code, $order_amount)
    {
        do {
            /* 判断是否是友盟过来的用户 */
            if (empty($_COOKIE['_QYH_UNION'])) {
                break;
            }

            /* 判断参数是否有效 */
            if (empty($uid) || empty($order_code) || empty($order_amount)) {
                break;
            }

            /* 解密客户端联盟信息 */
            $unionKey = AuthCode::decode($_COOKIE['_QYH_UNION'], 'q_union_yohobuy');
            if (empty($unionKey)) {
                break;
            }

            /* 检查联盟参数是否有效 */
            $unionInfo = json_decode($unionKey, true);
            if (empty($unionInfo['client_id'])) {
                break;
            }

            /* 通过用户下单量判断新老客户 (有下单1, 没有0) */
            $is_old_user = 0;
            try {
                $orderList = OrderData::getOrderData(1, 1, 10, '1,2,3', 1, $uid);
                if (!empty($orderList['data']['total']) && intval($orderList['data']['total']) > 1) {
                    $is_old_user = 1;
                }
            } catch (Exception $e) {
                // do nothing
            }

            $clientId = intval($unionInfo['client_id']);
            $append = isset($unionInfo['append']) ? $unionInfo['append'] : '';
            $channel_code = isset($unionInfo['channel_code']) ? $unionInfo['channel_code'] : '';
            $mbr_name = isset($unionInfo['mbr_name']) ? $unionInfo['mbr_name'] : '';
            $u_id = isset($unionInfo['u_id']) ? $unionInfo['u_id'] : '';
            $create_times = time();

            /* 加入联盟订单库 */
            $keyStr = 'client_id=' . $unionInfo['client_id'] . '&order_code=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&mbr_name=' . $mbr_name . '&u_id=' . $u_id;
            Yohobuy::get('http://union.yohobuy.com/order', array(
                'client_id' => $unionInfo['client_id'],
                'order_code' => $order_code,
                'orders_price' => $order_amount,
                'buy_time' => $create_times,
                'channel_code' => $channel_code,
                'append' => $append,
                'mbr_name' => $mbr_name,
                'u_id' => $u_id,
                'is_old_user' => $is_old_user,
                'verify_code' => md5($keyStr)
            ));

            /* 通知新ERP (老的ERP http://192.168.200.102/CreateOrderUnion.aspx) */
            Yohobuy::post('http://portal.admin.yohobuy.com/api/orderunion/updateunion', array(
                'data' => json_encode(array(
                    'order_code' => $order_code,
                    'unionid' => $unionInfo['client_id'],
                    'union_name' => isset(self::$unionNames[$clientId]) ? self::$unionNames[$clientId] : '',
                ))
            ));

            /* 对接第三方联盟 */

            // 亿起发
            if ($unionInfo['client_id'] == 1001) {
                $keyStr = 'client_id=' . $unionInfo['client_id'] . '&orders_id=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&is_old_user=' . $is_old_user;
                Yohobuy::get('http://o.yiqifa.com/adv/yoho.jsp', array(
                    'client_id' => $unionInfo['client_id'],
                    'orders_id' => $order_code,
                    'orders_price' => $order_amount,
                    'create_time' => $create_times,
                    'channel_code' => $channel_code,
                    'append' => $append,
                    'is_old_user' => $is_old_user,
                    'verify_code' => md5($keyStr)
                ));
                break;
            }

            // 亿起发wap
            if ($unionInfo['client_id'] == 2995) {
                $keyStr = 'client_id=' . $unionInfo['client_id'] . '&orders_id=' . $order_code . '&orders_price=' . $order_amount . '&create_time=' . $create_times . '&channel_code=' . $channel_code . '&append=' . $append . '&is_old_user=' . $is_old_user;
                Yohobuy::get('http://o.yiqifa.com/adv/yoho.jsp', array(
                    'client_id' => $unionInfo['client_id'],
                    'orders_id' => $order_code,
                    'orders_price' => $order_amount,
                    'create_time' => $create_times,
                    'channel_code' => $channel_code,
                    'append' => $append,
                    'is_old_user' => $is_old_user,
                    'dt' => 'm',
                    'verify_code' => md5($keyStr)
                ));
                break;
            }

            // 多麦
            $clientUrls = array(
                3017 => 'http://www.duomai.com/api/push/yohobuy.php',
                3019 => 'http://www.duomai.com/api/push/myohobuy.php',
                3057 => 'http://www.duomai.com/api/push/yohobuyroi.php',
            );
            if (array_key_exists($clientId, $clientUrls)) {
                $urldata = self::getMaiDuoSplitData($order_code, $unionInfo['client_id'], $mbr_name);
                Yohobuy::post($clientUrls[$clientId], array(
                    'content' => $urldata,
                ));
                break;
            }

            // 51fanli
            if ($unionInfo['client_id'] == 3001) {
                $xmldata = self::getOrderXmlData($order_code, $uid, $unionInfo['client_id'], $u_id, $mbr_name, $channel_code);
                $keyStr = 'content=' . $xmldata . '&shopid=690';
                Yohobuy::post('http://union.fanli.com/dingdan/push?shopid=690', array(
                    'content' => $xmldata,
                    'shopid' => 690,
                    'verify_code' => md5($keyStr)
                ));
                break;
            }

            // 领科特wap 成果 51fanli
            $clientUrls = array(
                1010 => 'http://service.linktech.cn/purchase_cps.php',
                1009 => 'http://count.chanet.com.cn/add_action_ec.cgi',
                2997 => 'http://service.linktech.cn/purchase_cps.php',
                3001 => 'http://union.fanli.com/dingdan/push?shopid=690',
            );
            if (array_key_exists($clientId, $clientUrls)) {
                Yohobuy::post($clientUrls[$clientId], array(
                    'client_id' => $unionInfo['client_id'],
                    'order_id' => $order_code,
                    'create_time' => $create_times,
                    'channel_code' => $channel_code,
                    'append' => $append,
                    'mbr_name' => $mbr_name,
                    'UID' => $u_id,
                    'is_old_user' => $is_old_user,
                ));
                break;
            }
        } while (false);
    }

    /**
     * 根据用户id和订单号, 返回xml数据
     */
    public static function getOrderXmlData($order_id, $uid, $client_id, $u_id, $mbr_name, $channel_code, $client_type = 'web')
    {
        $xmlData = '';

        do {
            $orderDetail = OrderData::viewOrderData($order_id, $uid, '');
            if (empty($orderDetail['data'])) {
                break;
            }

            $orderDetail = $orderDetail['data'];
            $amount = self::filterPrice($orderDetail['amount']) - self::filterPrice($orderDetail['shipping_cost']);
            if (!empty($orderDetail['order_goods'])) {
                break;
            }

            $goodsAmount = $orderDetail['goods_total_amount'];
            $goodsData = array();

            foreach ($orderDetail['order_goods'] as $key => $val) {
                $goodsData['goods' . $key] = array(
                    'pid' => $val['product_skn'],
                    'title' => $val['product_name'],
                    'category' => $val['color_name'],
                    'category_title' => '',
                    'url' => 'http://item.m.yohobuy.com/product/show_' . $val['product_skn'] . '.html',
                    'num' => $val['buy_number'],
                    'price' => $val['goods_price'],
                    'real_pay_fee' => round($amount * ($val['goods_amount'] / $goodsAmount), 2),
                    'refund_num' => '',
                    'commission' => round($amount * ($val['goods_amount'] / $goodsAmount) * 0.05, 2),
                    'comm_type' => 'A',
                );
            }

            $orderData[] = array(
                's_id' => 690,
                'order_id_parent' => $orderDetail['order_code'],
                'order_id' => $orderDetail['order_code'],
                'order_time' => date('Y-m-d H:i:s', $orderDetail['create_time']),
                'uid' => $u_id,
                'uname' => $mbr_name,
                'tc' => $channel_code,
                'pay_time' => '',
                'status' => $orderDetail['status'],
                'locked' => '',
                'lastmod' => '',
                'is_newbuyer' => '',
                'platform' => $client_type == 'h5' ? 2 : 1,
                'code' => '',
                'remark' => '',
                'products' => $goodsData,
            );

            $xmlData = strtr(self::toXml($orderData), array('<![CDATA[", "]]>' => ''));
            
        } while (false);

        return $xmlData;
    }

    /**
     * 麦多联盟
     */
    public static function getMaiDuoSplitData($order_code, $uid, $client_id, $mbr_name, $is_old_user)
    {
        $result = '';

        do {
            $orderDetail = OrderData::viewOrderData($order_code, $uid, '');
            if (empty($orderDetail['data']['order_goods'])) {
                break;
            }
            $orderDetail = $orderDetail['data'];

            if ($client_id == '3017') {
                $hash = '96613bf38393aa3d16451218f22344a8';
                $channel = 0;
            } elseif ($client_id == '3019') {
                $hash = 'd54be2dbc75753eb863ba6139950656b';
                $channel = 1;
            } elseif ($client_id == '3057') {
                $hash = 'bbf70bcaf5c52947ad26853f7cc1176d';
                $channel = 0;
            } else {
                break;
            }

            $goodsName = '';
            $goodsIds = '';
            $goodsPrice = '';
            $goodsTotalPrice = '';
            $goodsNum = '';

            foreach ($orderDetail['order_goods'] as $val) {
                $goodsName .= str_replace(array("&", "<", ">", "'", '"', " ", "|"), array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "%20", "%20"), $val['product_name']) . '|';
                $goodsIds = $val['product_sku'] . '|';
                $goodsPrice = $val['goods_price'] . '|';
                $goodsTotalPrice = $val['goods_amount'] . '|';
                $goodsNum = $val['buy_number'] . '|';
            }

            $orderStatus = $orderDetail['status_str'];

            if ($orderDetail['is_cancel'] == 'Y') {
                $orderStatus = -1;
            } elseif ($orderDetail['payment_status'] == 'N' && $orderDetail['status'] == 0) {
                $orderStatus = 0;
            }

            $orderData = array(
                'hash' => $hash,
                'euid' => $mbr_name,
                'order_sn' => $order_code,
                'order_time' => date('Y-m-d H:i:s', $orderDetail['create_time']),
                'orders_price' => sprintf('%.2f', $orderDetail['goods_total_amount']),
                'promotion_code' => 0,
                'is_new_custom' => $is_old_user ? 0 : 1,
                'channel' => $channel,
                'status' => $orderStatus,
                'goods_id' => rtrim($goodsIds, '|'), //商品id,
                'goods_name' => rtrim($goodsName, '|'), //商品名称,
                'goods_price' => rtrim($goodsPrice, '|'), //商品单价,
                'goods_ta' => rtrim($goodsNum, '|'), //商品数量,
                'goods_cate' => '', //商品分类
                'goods_cate_name' => 0,
                'totalPrice' => rtrim($goodsTotalPrice, '|'), //商品总净金额,
                'rate' => 0,
                'commission' => '', //佣金,
                'commission_type' => 0,
                'coupon' => sprintf('%.2f', self::filterPrice($orderDetail['coupons_amount']))
            );
            $result = http_build_query($orderData);
            
        } while (false);

        return $result;
    }

    /**
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be - defaultsto data.
     * @param SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     */
    public static function toXml($data, $rootNodeName = 'data', $xml = null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1) {
            ini_set('zend.ze1_compatibility_mode', 0);
        }

        if ($xml == null) {
            $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
        }

        // loop through the data passed in.
        foreach ($data as $key => $value) {
            // no numeric keys in our xml please!
            if (is_numeric($key)) {
                // make string key...
                $key = "unknownNode_" . (string) $key;
            }

            // if there is another array found recrusively call this function
            if (is_array($value)) {
                $node = $xml->addChild($key);
                // recrusive call.
                self::toXml($value, $rootNodeName, $node);
            } else {
                // add single node.
                $value = htmlentities($value);
                $xml->addChild($key, $value);
            }
        }
        // pass back as string. or simple xml object if you want!
        return $xml->asXML();
    }

}