UnionTrans.php 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
<?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 => '多麦优质',
        
        1014 => '百度',
        2744 => '360搜索',
        2891 => 'M百度CPC',
        3003 => 'M神马搜索',
        3079 => 'm360cpc',
        3187 => '360品专',
        3191 => 'm搜狗品专',
        3189 => '搜狗品专',
        2949 => 'm搜狗cpc',
        1026 => '搜狗',
        2858 => '虎扑CPS',
        2890 => '零售微信手机端',
        1346 => 'qugeremarketing',
        3330 => '猎豹浏览器wap-ios设备',
        2886 => 'EDM (亿业)',
        3251 => '猎豹浏览器',
    );

    /**
     * 过滤人民币
     * 
     * @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)
    {
        $debug = false;

        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;
            }
            if ($debug) {
                var_dump('union info: ', $unionInfo);
            }

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

            $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;
            $result = 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)
            ));
            if ($debug) {
                var_dump('union:', $result, "++");
            }

            /* 通知新ERP (老的ERP http://192.168.200.102/CreateOrderUnion.aspx) */
            $result = 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] : $clientId,
                    ))
            ));
            if ($debug) {
                var_dump('erp:', $result, '++');
            }

            /* 对接第三方联盟 */

            // 亿起发
            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;
                $result = 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)
                ));
                if ($debug) {
                    var_dump('yiqifa:', $result, '++');
                }
                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;
                $result = 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)
                ));
                if ($debug) {
                    var_dump('yiqifa wap:', $result, '++');
                }
                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, $uid, $unionInfo['client_id'], $mbr_name, $is_old_user);
                $result = Yohobuy::post($clientUrls[$clientId], array(
                        'content' => $urldata,
                ));
                if ($debug) {
                    var_dump('duomai: ', $urldata, '++', $result);
                }
                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';
                $result = Yohobuy::post('http://union.fanli.com/dingdan/push?shopid=690', array(
                        'content' => $xmldata,
                        'shopid' => 690,
                        'verify_code' => md5($keyStr)
                ));
                if ($debug) {
                    var_dump('fanli: ', $xmldata, '++', $result);
                }
                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)) {
                $result = 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,
                ));
                if ($debug) {
                    var_dump('other: ', $result);
                }
                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 = self::filterPrice($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', self::filterPrice($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();
    }

}