Esempio n. 1
0
function GetSellerTransactions($ebay_starttime, $ebay_endtime, $ebay_account, $type, $id)
{
    global $api_gst, $oa, $user;
    global $dbConn, $mctime, $defaultstoreid;
    $pcount = 1;
    $errors = 1;
    do {
        echo "抓取....\t";
        $responseXml = $api_gst->request($ebay_starttime, $ebay_endtime, $pcount);
        if (empty($responseXml)) {
            echo "Return Empty...Sleep 10 seconds..";
            sleep(10);
            $hasmore = true;
            continue;
        }
        //网络出现代理Proxy error 脚本休眠20秒
        $poxy_error_p = '#Proxy\\s*Error#i';
        if (preg_match($poxy_error_p, $responseXml)) {
            echo "Proxy Error...Sleep 20 seconds..";
            sleep(20);
            $hasmore = true;
            continue;
        }
        echo "\n";
        $data = XML_unserialize($responseXml);
        $responseXml = null;
        unset($responseXml);
        $getorder = $data['GetSellerTransactionsResponse'];
        $data = null;
        unset($data);
        $TotalNumberOfPages = @$getorder['PaginationResult']['TotalNumberOfPages'];
        $TotalNumberOfEntries = @$getorder['PaginationResult']['TotalNumberOfEntries'];
        $hasmore = @$getorder['HasMoreTransactions'];
        $strline = $TotalNumberOfPages . '/' . $TotalNumberOfEntries;
        $Ack = @$getorder['Ack'];
        echo "正在请求:{$pcount}/{$TotalNumberOfPages} \t记录数[ {$TotalNumberOfEntries} ]\t\n\t\t\t\t  同步状态: {$Ack} 还有更多: {$hasmore} \n";
        if ($id == '' && $type == '1') {
            if ($Ack == '') {
                $ss = "insert into errors_ack(ebay_account,starttime,endtime,status,notes) \n\t\t\t\t\t\t\tvalues('{$ebay_account}','{$ebay_starttime}','{$ebay_endtime}','0','Ack False')";
                $dbConn->query($ss);
            }
            if ($hasmore == '' || $Ack == '') {
                $ss = "insert into errors_ack(ebay_account,starttime,endtime,status,notes) \n\t\t\t\t\t\t\tvalues('{$ebay_account}','{$ebay_starttime}','{$ebay_endtime}','0','Ack False')";
                $dbConn->query($ss);
            }
        }
        if ($id > 0) {
            if ($Ack == 'Success') {
                $gg = "update errors_ack set status = 1 where id='{$id}' ";
            } else {
                $gg = "update errors_ack set status = 0 where id='{$id}' ";
            }
            $dbConn->query($gg);
        }
        /**/
        $log_name = '同步订单';
        $log_operationtime = $mctime;
        $log_notes = $ebay_account . ":{$pcount}/{$TotalNumberOfPages} ,Ack={$Ack}";
        addlogs($log_name, $log_operationtime, 0, $log_notes, $user, $ebay_account, $ebay_starttime, $ebay_endtime, $type);
        /**/
        $Trans = @$getorder['TransactionArray']['Transaction'];
        $ReturnedTransactionCountActual = @$getorder['ReturnedTransactionCountActual'];
        if ($ReturnedTransactionCountActual == 1) {
            $Trans = array();
            $Trans[0] = $getorder['TransactionArray']['Transaction'];
        }
        $getorder = null;
        unset($getorder);
        foreach ((array) $Trans as $Transaction) {
            //每笔记录编号
            $tran_recordnumber = $Transaction['ShippingDetails']['SellingManagerSalesRecordNumber'];
            //交易状态
            $LastTimeModified = strtotime($Transaction['Status']['LastTimeModified']);
            $eBayPaymentStatus = $Transaction['Status']['eBayPaymentStatus'];
            $CompleteStatus = $Transaction['Status']['CompleteStatus'];
            $CheckStatus = $Transaction['Status']['CompleteStatus'];
            //其他交易信息比如payapl整合到ebay
            $ptid = @$Transaction['ExternalTransaction']['ExternalTransactionID'];
            $FeeOrCreditAmount = @$Transaction['ExternalTransaction']['FeeOrCreditAmount'];
            $FinalValueFee = $Transaction['FinalValueFee'];
            $tid = $Transaction['TransactionID'];
            //ebay 交易号
            $AmountPaid = @$Transaction['AmountPaid'];
            $Buyer = str_rep(@$Transaction['Buyer']);
            $Email = str_rep(@$Buyer['Email']);
            //email
            $UserID = str_rep(@$Buyer['UserID']);
            //userid
            $BuyerInfo = $Buyer['BuyerInfo']['ShippingAddress'];
            $Name = str_rep($BuyerInfo['Name']);
            $Name = mysql_real_escape_string($Name);
            $Street1 = str_rep($BuyerInfo['Street1']);
            $Street2 = str_rep(@$BuyerInfo['Street2']);
            $CityName = str_rep(@$BuyerInfo['CityName']);
            $StateOrProvince = str_rep(@$BuyerInfo['StateOrProvince']);
            $Country = str_rep(@$BuyerInfo['Country']);
            $CountryName = str_rep(@$BuyerInfo['CountryName']);
            $PostalCode = str_rep(@$BuyerInfo['PostalCode']);
            $Phone = @$BuyerInfo['Phone'];
            //该交易的物品信息
            $Item = $Transaction['Item'];
            $CategoryID = $Item['PrimaryCategory']['CategoryID'];
            //ebay刊登物品的分类ID,备用字段
            $Currency = $Item['Currency'];
            //货币类型
            $ItemID = $Item['ItemID'];
            //ebay物品id
            $ListingType = $Item['ListingType'];
            $Title = str_rep($Item['Title']);
            //ebay物品标题
            $sku = str_rep($Item['SKU']);
            $site = $Item['Site'];
            $CurrentPrice = $Item['SellingStatus']['CurrentPrice'];
            //产品当前价格
            $QuantityPurchased = $Transaction['QuantityPurchased'];
            //购买数量
            $PaidTime = strtotime($Transaction['PaidTime']);
            //付款时间
            $CreatedDate = strtotime($Transaction['CreatedDate']);
            //交易创建时间...********多个产品订单每个产品的创建时间不同判依据
            $ShippedTime = strtotime($Transaction['ShippedTime']);
            $shipingservice = $Transaction['ShippingServiceSelected']['ShippingService'];
            $shipingfee = $Transaction['ShippingServiceSelected']['ShippingServiceCost'];
            $containing_order = @$Transaction['ContainingOrder'];
            $combined_recordnumber = @$containing_order['ShippingDetails']['SellingManagerSalesRecordNumber'];
            //合并后的recordnumber
            $orderid = 0;
            if ($combined_recordnumber != '') {
                $orderid = @$containing_order['OrderID'];
            } else {
                $orderid = $ItemID . '-' . $tid;
            }
            $BuyerCheckoutMessage = str_rep($Transaction['BuyerCheckoutMessage']);
            //顾客购买留言
            $BuyerCheckoutMessage = str_replace('<![CDATA[', '', $BuyerCheckoutMessage);
            $BuyerCheckoutMessage = str_replace(']]>', '', $BuyerCheckoutMessage);
            //店铺收款paypal account
            $PayPalEmailAddress = $Transaction['PayPalEmailAddress'];
            $addrecordnumber = $tran_recordnumber;
            if ($combined_recordnumber != '') {
                $addrecordnumber = $combined_recordnumber;
            }
            if ($CompleteStatus == "Complete" && $eBayPaymentStatus == "NoPaymentFailure" && $PaidTime > 0) {
                $orderstatus = 1;
            }
            if ($ShippedTime > 0) {
                $orderstatus = 2;
            }
            //已经发货
            ################################
            $RefundAmount = 0;
            //表示未垦退款
            if ($orderstatus == 1 && $ShippedTime <= 0 && $PaidTime > 0) {
                echo "销售编号[{$addrecordnumber}]有效";
                //检查汇总表该 recordnumber是否已经存在
                //主要是避免multiple line item 这种情况 造成重复添加 汇总数据
                $check_ordersn = CheckOrderSN($addrecordnumber, $ebay_account);
                $new_ebay_id = true;
                if ($check_ordersn == "0") {
                    //该交易还无汇总数据	添加订单汇总
                    /* 生成一个本地系统订单号 */
                    $our_sys_ordersn = date('Y-m-d-His') . mt_rand(100, 999) . $addrecordnumber;
                    $order_no = '';
                    //已废弃
                    $obj_order = new eBayOrder();
                    $obj_order_data = array('ebay_paystatus' => $CompleteStatus, 'ebay_ordersn' => $our_sys_ordersn, 'ebay_tid' => $tid, 'ebay_ptid' => $ptid, 'ebay_orderid' => $orderid, 'ebay_createdtime' => $CreatedDate, 'ebay_paidtime' => $PaidTime, 'ebay_userid' => $UserID, 'ebay_username' => $Name, 'ebay_usermail' => $Email, 'ebay_street' => $Street1, 'ebay_street1' => $Street2, 'ebay_city' => $CityName, 'ebay_state' => $StateOrProvince, 'ebay_couny' => $Country, 'ebay_countryname' => $CountryName, 'ebay_postcode' => $PostalCode, 'ebay_phone' => $Phone, 'ebay_currency' => $Currency, 'ebay_total' => $AmountPaid, 'ebay_status' => $orderstatus, 'ebay_user' => $user, 'ebay_shipfee' => $shipingfee, 'ebay_account' => $ebay_account, 'recordnumber' => $addrecordnumber, 'ebay_addtime' => $mctime, 'ebay_note' => $BuyerCheckoutMessage, 'ebay_site' => $site, 'eBayPaymentStatus' => $eBayPaymentStatus, 'PayPalEmailAddress' => $PayPalEmailAddress, 'ShippedTime' => $ShippedTime, 'RefundAmount' => $RefundAmount, 'ebay_warehouse' => $defaultstoreid, 'order_no' => $order_no);
                    $obj_order->init($obj_order_data);
                    $obj_order_data = null;
                    unset($obj_order_data);
                    $new_ebay_id = $oa->addOrder($obj_order);
                    $obj_order = null;
                    unset($obj_order);
                    if ($new_ebay_id !== false) {
                        echo "\t订单[{$our_sys_ordersn}] 汇总数据入库成功=>\n\tUserID:{$UserID}" . " AMT:{$AmountPaid} recordNO:{$addrecordnumber} ";
                        echo "付款状态:{$CompleteStatus} 交易ID:{$ptid}\n";
                        $check_ordersn = $our_sys_ordersn;
                        //检验ebay 订单号 是否在订单号汇总表中
                        if (check_ebay_orderid_exists_in_statistic_table($orderid, $ebay_account) === false) {
                            save_ebay_orderid_table($new_ebay_id, $ptid, $orderid, $ebay_account, $CreatedDate);
                        }
                    } else {
                        echo "\t订单[{$our_sys_ordersn}] 入库失败\n";
                    }
                }
                if ($new_ebay_id !== false) {
                    //添加订单明细
                    $sql = "select \tebay_id from ebay_orderdetail \n\t\t\t\t\t\t\t\twhere \tebay_ordersn='{$check_ordersn}' \n\t\t\t\t\t\t\t\tand \trecordnumber='{$tran_recordnumber}'";
                    $sql = $dbConn->query($sql);
                    $sql = $dbConn->fetch_array_all($sql);
                    if (count($sql) == 0) {
                        /* 多属性订单 */
                        $Variation = @$Transaction['Variation']['VariationSpecifics']['NameValueList'];
                        $attribute = '';
                        if (!empty($Variation)) {
                            if (!isset($Variation['Name']) || !isset($Variation['Value'])) {
                                foreach ($Variation as $variate) {
                                    $aname = $variate['Name'];
                                    $avalue = $variate['Value'];
                                    $attribute .= $aname . ":" . $avalue . " ";
                                }
                            } else {
                                $attribute = $Variation['Name'] . ":" . $Variation['Value'];
                            }
                            unset($Variation);
                        }
                        $obj_order_detail = new eBayOrderDetail();
                        $obj_order_detail_data = array('ebay_ordersn' => $check_ordersn, 'ebay_itemid' => $ItemID, 'ebay_itemtitle' => $Title, 'ebay_itemprice' => $CurrentPrice, 'ebay_amount' => $QuantityPurchased, 'ebay_createdtime' => $CreatedDate, 'ebay_shiptype' => $shipingservice, 'ebay_user' => $user, 'sku' => $sku, 'shipingfee' => $shipingfee, 'ebay_account' => $ebay_account, 'addtime' => $mctime, 'ebay_itemurl' => '', 'ebay_site' => $site, 'recordnumber' => $tran_recordnumber, 'storeid' => '', 'ListingType' => $ListingType, 'ebay_tid' => $tid, 'FeeOrCreditAmount' => $FeeOrCreditAmount, 'FinalValueFee' => $FinalValueFee, 'attribute' => $attribute, 'notes' => $BuyerCheckoutMessage, 'goods_location' => @get_good_location($sku, $user));
                        $obj_order_detail->init($obj_order_detail_data);
                        $obj_order_detail_data = null;
                        unset($obj_order_detail_data);
                        if (false !== $oa->addOrderDetail($obj_order_detail)) {
                            echo "\t订单[{$check_ordersn}] 编号[{$tran_recordnumber}]明细入库OK!\n";
                        } else {
                            echo "\t订单[{$check_ordersn}] 编号[{$tran_recordnumber}]明细入库Error!\n";
                        }
                        $obj_order_detail = null;
                        unset($obj_order_detail);
                    }
                    $sql = "select ebay_id from ebay_orderdetail \n\t\t\t\t\t\t\t\twhere ebay_ordersn='{$check_ordersn}' \n\t\t\t\t\t\t\t\tand recordnumber='{$tran_recordnumber}'";
                    $sql = $dbConn->query($sql);
                    $sql = $dbConn->fetch_array_all($sql);
                    if (count($sql) >= 2 && strlen($check_ordersn) >= 5) {
                        $id = $sql[0]['ebay_id'];
                        $ss = "delete from ebay_orderdetail where ebay_id='{$id}'";
                        $dbConn->query($ss);
                    }
                    if ($ShippedTime > 0) {
                        $ss = "update ebay_order set ShippedTime='{$ShippedTime}',\n\t\t\t\t\t\t\t\t\tebay_status='2',ebay_markettime='{$ShippedTime}' \n\t\t\t\t\t\t\t\t\twhere ebay_ordersn='{$check_ordersn}' and ebay_status='1'";
                        $dbConn->query($ss);
                    }
                }
            } else {
                echo "销售编号[{$addrecordnumber}]无效 不入库...\n";
            }
        }
        if ($id == '' && $type == '1') {
            if ($Ack == '' || $Ack == 'Failure') {
                $ss = "insert into errors_ack(ebay_account,starttime,endtime,status,notes,currentpage) \n\t\t\t\t\t\t\tvalues('{$ebay_account}','{$ebay_starttime}','{$ebay_endtime}','0','Ack False','{$pcount}')";
                $dbConn->query($ss);
            }
        }
        if ($pcount >= $TotalNumberOfPages) {
            echo $hasmore . "程序退出了\n";
            break;
        }
        $pcount++;
        $hasmore = strtolower($hasmore) == 'true' ? true : false;
    } while ($hasmore);
}
Esempio n. 2
0
function push_ebay_orderid_queue($ebay_orderid, $ebay_account)
{
    global $dbcon;
    //step 1 check ebay orderid statistic table
    if (check_ebay_orderid_exists_in_statistic_table($ebay_orderid, $ebay_account) === true) {
        echo "ebay orderid[{$ebay_orderid}] already exists in ebay orderid statistic table\n";
        return;
    }
    $table_name = 'ebay_order_id_queue_' . $ebay_account;
    $check_sql = 'select * from ' . $table_name . ' where ebay_orderid="' . $ebay_orderid . '"';
    $check = $dbcon->execute($check_sql);
    $check = $dbcon->getResultArray($check);
    if (count($check) == 0) {
        $sql = 'insert into ' . $table_name . ' (ebay_orderid) value("' . $ebay_orderid . '")';
        $try_insert_count = 0;
        while (1) {
            $try_insert_count++;
            $res = $dbcon->execute($sql);
            if ($res) {
                echo "Push ebay orderid[{$ebay_orderid}]  into queue table successfully!\n";
                break;
            } else {
                if ($try_insert_count == 3) {
                    $lost_orderid_path = EBAY_RAW_DATA_PATH . 'lost_ebay_orderid/' . $ebay_account . '/lost_sql.txt';
                    write_lost_sql($lost_orderid_path, $sql . "\n");
                    echo "oops...failed again,give this order[{$ebay_orderid}] up finally!\n";
                    break;
                }
                echo "fail to push  ebay orderid[{$ebay_orderid}]  into queue table !Sleep 10 sconds then try again\n";
                sleep(10);
            }
        }
    } else {
        echo "ebay orderid[{$ebay_orderid}] already exists in queue\n";
    }
}