人人商城支付流程
在收银台页面,pay.js为每种支付方式.pay-btn都绑定了相应的处理事件,
首先通过order/pay/check接口核对订单https://www.gouwanmei.wang/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.pay.check,如果符合条件,根据按钮的type类型获取支付类型,modal.pay(btn),根据不同的支付类型执行支付处理,比如微信拉起支付。

微信支付相关:
JS API网页支付参数
支付授权目录: https://www.gouwanmei.wang/payment/wechat/ 和 https://www.gouwanmei.wang/app/
支付请求实例: https://www.gouwanmei.wang/payment/wechat/pay.php

https://www.gouwanmei.wang/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.pay&id=310
对应php文件 /addons/ewei_shopv2/core/mobile/order/pay.php
包含的模板文件 /addons/ewei_shopv2/template/mobile/default/order/pay.html
如果开启余额支付:

        {if $credit['success']}
            <div class='fui-list pay-btn' data-type='credit'>
                <div class='fui-list-media'>
                    <!--<i class='icon icon-money credit'></i>-->
                    <img src="{EWEI_SHOPV2_STATIC}images/ye.png" alt="">
                </div>
                <div class='fui-list-inner'>
                    <div class="title">{$_W['shopset']['trade']['moneytext']}支付</div>
                    <div class="subtitle c999 f24">当前{$_W['shopset']['trade']['moneytext']}: <span class='text-danger'>{php echo number_format($member['credit2'],2)}</span>
                    </div>
                </div>
                <div class='fui-list-angle'>
		    <span class="angle">
 
		    </span>
                </div>
            </div>
            {/if}

点击余额支付:
ajax请求地址:https://www.gouwanmei.wang/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.pay.complete
请求post数据:
id:310
ordersn:SH20190228182212379942
type:credit
peerpay:
peerpaymessage:

/addons/ewei_shopv2/core/mobile/order/pay.php页面中的complete方法执行支付处理:

1002行,执行成功返回json数据{"status":1,"result":{"result":true,"url":"https:\/\/www.gouwanmei.wang\/app\/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.pay&id=322"}}
			if( $_W["ispost"] ) 
			{
				show_json(1, array( "result" => $pay_result ));
			}
			else 
			{
				header("location:" . mobileUrl("order/pay/success", array( "id" => $order["id"], "result" => $pay_result )));
                               // 等同于支付成功页面https://www.gouwanmei.wang/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=order.pay.success&id=324&result=true
			}

/addons/ewei_shopv2/static/js/app/biz/order/pay.js绑定了一系列的代码

define(['core', 'tpl'],
function(core, tpl) {
    var modal = {
        params: {}
    };
    modal.init = function(params) {
        var defaults = {
            orderid: 0,
            wechat: {
                success: false
            },
            cash: {
                success: false
            },
            alipay: {
                success: false
            },
        };
        modal.params = $.extend(defaults, params || {});
		//绑定各个支付按钮的事件
        $('.pay-btn').unbind('click').click(function() {
            var btn = $(this);
            core.json('order/pay/check', {
                id: modal.params.orderid
            },
            function(pay_json) {
                if (pay_json.status == 1) {
                    modal.pay(btn)    //调用支付事件
                } else {
                    FoxUI.toast.show(pay_json.result.message)
                }
            },
            false, true)
        });
        if (modal.params.wechat.jie == 1) {
            $('.pay-btn[data-type="wechat"]').click()
        }
    };
	//支付事件
    modal.pay = function(btn) {
        var type = btn.data('type') || '';  //如支付宝div data-type='alipay'
        if (type == '') {
            return
        }
        if (btn.attr('stop')) {
            return
        }
        btn.attr('stop', 1);
        if (type == 'wechat') {  //微信支付
            if (core.ish5app()) {  //如果是h5打包app
                appPay('wechat', null, null, true);
                return
            }
            modal.payWechat(btn)   //微信支付
        } else if (type == 'alipay') {  // 支付宝支付
            if (core.ish5app()) {  
                appPay('alipay', null, null, true);
                return
            }
            modal.payAlipay(btn)
        } else if (type == 'credit') {  //余额支付
            FoxUI.confirm('确认要支付吗?', '提醒',
            function() {
                modal.complete(btn, type)
            },
            function() {
                btn.removeAttr('stop')
            })
        } else if (type == 'peerpay') {   //货到付款  直接跳转到支付成功页面
            location.href = core.getUrl('order/pay/peerpay', {
                id: modal.params.orderid
            });
            return
        } else {
            modal.complete(btn, type)
        }
    };
    modal.payWechat = function(btn) {  //拉起微信支付
        var wechat = modal.params.wechat;
        if (!wechat.success) {
            return
        }
        if (wechat.weixin) {
            function onBridgeReady() {
                WeixinJSBridge.invoke('getBrandWCPayRequest', {
                    'appId': wechat.appid ? wechat.appid: wechat.appId,
                    'timeStamp': wechat.timeStamp,
                    'nonceStr': wechat.nonceStr,
                    'package': wechat.package,
                    'signType': wechat.signType,
                    'paySign': wechat.paySign
                },
                function(res) {
				    //微信成功支付处理
                    if (res.err_msg == 'get_brand_wcpay_request:ok') {
                        modal.complete(btn, 'wechat')   //调用支付验证
                    } else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
					    //取消微信支付处理
                        FoxUI.toast.show('取消支付')
                    } else {
					    //微信支付失败处理
                        FoxUI.toast.show(res.err_msg)
                    }
                    btn.removeAttr('stop')
                })
            }
            if  (typeof WeixinJSBridge  ==  "undefined") {
                if ( document.addEventListener ) {
                    document.addEventListener('WeixinJSBridgeReady',  onBridgeReady,  false)
                } else 
                if  (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady',  onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady',  onBridgeReady)
                }
            } else {
                onBridgeReady()
            }
        }
        if (wechat.weixin_jie || wechat.jie == 1) {
            modal.payWechatJie(btn, wechat)
        }
    };
    modal.payWechatJie = function(btn, wechat) {
        var img = core.getUrl('index/qr', {
            url: wechat.code_url
        });
        $('#qrmoney').text(modal.params.money);
        $('.order-weixinpay-hidden').show();
        $('#btnWeixinJieCancel').unbind('click').click(function() {
            btn.removeAttr('stop');
            clearInterval(settime);
            $('.order-weixinpay-hidden').hide()
        });
        var settime = setInterval(function() {
            $.getJSON(core.getUrl('order/pay/orderstatus'), {
                id: modal.params.orderid
            },
            function(data) {
                if (data.status >= 1) {
                    clearInterval(settime);
                    location.href = core.getUrl('order/pay/success', {
                        id: modal.params.orderid
                    })
                }
            })
        },
        1000);
        $('.verify-pop').find('.close').unbind('click').click(function() {
            $('.order-weixinpay-hidden').hide();
            btn.removeAttr('stop');
            clearInterval(settime)
        });
        $('.verify-pop').find('.qrimg').attr('src', img).show()
    };
    modal.payAlipay = function(btn) {  //支付宝支付处理方法
        var alipay = modal.params.alipay;
        if (!alipay.success) {
            return
        }
        location.href = core.getUrl('order/pay_alipay', {  //点击支付宝跳转的url对应文件 /addons/ewei_shopv2/core/mobile/order/pay_alipay.php
            orderid: modal.params.orderid,
            type: 0,
            url: alipay.url
        })
    };
    modal.complete = function(btn, type) {
        var peerpay = $('#peerpay').text();
        var peerpaymessage = $('#peerpaymessage').val();
        FoxUI.loader.show('mini');
        setTimeout(function() {
            core.json('order/pay/complete', {
                id: modal.params.orderid,
                ordersn: modal.params.ordersn,
                type: type,
                peerpay: peerpay,
                peerpaymessage: peerpaymessage
            },
            function(pay_json) {
                if (pay_json.status == 1) {   //如果支付成功,则跳转到支付成功页面
                    location.href = core.getUrl('order/pay/success', {
                        id: modal.params.orderid,
                        result: pay_json.result.result
                    });
                    return
                }
                FoxUI.loader.hide();
                btn.removeAttr('stop');
                FoxUI.toast.show(pay_json.result.message)
            },
            false, true)
        },
        1000)
    };
    return modal
});

支付宝pay_alipay.php文件中的main方法

class Pay_Alipay_EweiShopV2Page extends MobilePage 
{
	public function main() 
	{
		global $_W;
		global $_GPC;
		$url = urldecode($_GPC["url"]);  //对应上边js中的alipay.url进行url解码
		if( !is_weixin() ) 
		{
                        //非微信环境中直接跳转到支付宝页面
			header("location: " . $url);   //拉起支付宝
			exit();
		}
		include($this->template("order/alipay"));
	}
}

include($this->template(“order/alipay”))对应页面模板内容,定时查询订单任务:

        var settime = setInterval(function () {
 
            //alert(url_return);
           //获取订单状态
            $.getJSON(url, data, function (ret) {
 
                if (ret.status >=1) {
 
                    clearInterval(settime);
 
                    location.href = url_return;  //跳转到支付成功页面
 
                }else{
 
                    //FoxUI.toast.show(JSON.stringify(ret));
 
                }
 
            })
 
        }, 1000);

修改订单状态之类的逻辑,不能同步处理,都放在了/addons/ewei_shopv2/payment文件夹中,包含了商品订单和充值之类的逻辑处理,其中包括分销逻辑。
验证支付
当用户支付完成后,系统会调用模块中的 $this->payResult($params); 方法。

	public function payResult($params) 
	{
		global $_W;
		$fee = intval($params["fee"]);  //收银台中显示需要支付的金额,只能大于 0
		$data = array( "status" => ($params["result"] == "success" ? 1 : 0) );  //支付状态
		$ordersn_tid = $params["tid"];   //订单编号
		$ordersn = rtrim($ordersn_tid, "TR");
		$order = pdo_fetch("select id,uniacid,ordersn, price,openid,dispatchtype,addressid,carrier,status,isverify,deductcredit2,`virtual`,isvirtual,couponid,isvirtualsend,isparent,paytype,merchid,agentid,createtime,buyagainprice,istrade,tradestatus,iscycelbuy from " . tablename("ewei_shop_order") . " where  ordersn=:ordersn and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":ordersn" => $ordersn ));
		$plugincoupon = com("coupon");
		if( $plugincoupon ) 
		{
		    //更新优惠券状态
			$plugincoupon->useConsumeCoupon($order["id"]);
		}
		if( 1 <= $order["status"] ) 
		{
			return true;
		}
		$orderid = $order["id"];
		$ispeerpay = $this->checkpeerpay($orderid);  //是否货到付款
		if( !empty($ispeerpay) )   //使用货到付款并没有添加到数据库ewei_shop_order_peerpay_payinfo中
		{
			$peerpay_info = (double) pdo_fetchcolumn("select SUM(price) price from " . tablename("ewei_shop_order_peerpay_payinfo") . " where pid=:pid limit 1", array( ":pid" => $ispeerpay["id"] ));
			if( $peerpay_info < $ispeerpay["peerpay_realprice"] ) 
			{
				return NULL;
			}
			pdo_update("ewei_shop_order", array( "status" => 0 ), array( "id" => $order["id"] ));
			$order["status"] = 0;
			pdo_update("ewei_shop_order_peerpay", array( "status" => 1 ), array( "id" => $ispeerpay["id"] ));
			$params["type"] = "peerpay";
		}
		if( $params["from"] == "return" )   //如果是同步通知
		{
			$seckill_result = plugin_run("seckill::setOrderPay", $order["id"]);
			if( $seckill_result == "refund" ) 
			{
				return "seckill_refund";
			}
			$address = false;
			//dispatchtype	0 商家配送 1 自提
			if( empty($order["dispatchtype"]) ) 
			{
				$address = pdo_fetch("select realname,mobile,address from " . tablename("ewei_shop_member_address") . " where id=:id limit 1", array( ":id" => $order["addressid"] ));
			}
			$carrier = false;
			if( $order["dispatchtype"] == 1 || $order["isvirtual"] == 1 ) 
			{
				$carrier = unserialize($order["carrier"]);
			}
			m("verifygoods")->createverifygoods($order["id"]);
			if( $params["type"] == "cash" )   //现金支付
			{
				if( $order["isparent"] == 1 ) 
				{
					$change_data = array( );
					$change_data["merchshow"] = 1;
					pdo_update("ewei_shop_order", $change_data, array( "id" => $order["id"] ));
					$this->setChildOrderPayResult($order, 0, 0);
				}
				return true;
			}
			// istrade	v2 0 普通 1 门店预约订单
			if( $order["istrade"] == 0 ) 
			{
				if( $order["status"] == 0 ) 
				{
					if( !empty($order["virtual"]) && com("virtual") ) 
					{
						if (p('lottery') && empty($ispeerpay)) 
							{
								$res = p('lottery')->getLottery($order['openid'], 1, array('money' => $order['price'], 'paytype' => 1));
								if ($res) 
								{
									p('lottery')->getLotteryList($order['openid'], array('lottery_id' => $res));
								}
							}
						return com("virtual")->pay($order, $ispeerpay);
					}
					if( $order["isvirtualsend"] ) 
					{
						if (p('lottery') && empty($ispeerpay)) 
							{
								$res = p('lottery')->getLottery($order['openid'], 1, array('money' => $order['price'], 'paytype' => 1));
								if ($res) 
								{
									p('lottery')->getLotteryList($order['openid'], array('lottery_id' => $res));
								}
							}
						return $this->payVirtualSend($order["id"], $ispeerpay);
					}
					$isonlyverifygoods = $this->checkisonlyverifygoods($order["id"]);
					$time = time();
					$change_data = array( );
					if( $isonlyverifygoods ) 
					{
						$change_data["status"] = 2;
					}
					else 
					{
						$change_data["status"] = 1;
					}
					$change_data["paytime"] = $time;
					if( $order["isparent"] == 1 ) 
					{
						$change_data["merchshow"] = 1;
					}
					pdo_update("ewei_shop_order", $change_data, array( "id" => $order["id"] ));
					if( $order["iscycelbuy"] == 1 && p("cycelbuy") ) 
					{
						p("cycelbuy")->cycelbuy_periodic($order["id"]);
					}
					if( $order["isparent"] == 1 ) 
					{
						$this->setChildOrderPayResult($order, $time, 1);
					}
					$this->setStocksAndCredits($orderid, 1);
					if( com("coupon") ) 
					{
						com("coupon")->sendcouponsbytask($order["id"]);
						com("coupon")->backConsumeCoupon($order["id"]);
					}
					if( $order["isparent"] == 1 ) 
					{
						$child_list = $this->getChildOrder($order["id"]);
						foreach( $child_list as $k => $v ) 
						{
							m("notice")->sendOrderMessage($v["id"]);
						}
					}
					else 
					{
						m("notice")->sendOrderMessage($order["id"]);
					}
					if( $order["isparent"] == 1 ) 
					{
						$merchSql = "SELECT id,merchid FROM " . tablename("ewei_shop_order") . " WHERE uniacid = " . intval($order["uniacid"]) . " AND parentid = " . intval($order["id"]);
						$merchData = pdo_fetchall($merchSql);
						foreach( $merchData as $mk => $mv ) 
						{
							com_run("printer::sendOrderMessage", $mv["id"]);
						}
					}
					else 
					{
						com_run("printer::sendOrderMessage", $order["id"]);
					}
					if( p("commission") )    //分销逻辑处理 \addons\ewei_shopv2\plugin\commission\core\model.php
					{                        //分销模型插件中的checkOrderPay
						p("commission")->checkOrderPay($order["id"]);
					}
					$this->afterPayResult($order, $ispeerpay);
				}
			}
			else 
			{
				$time = time();
				$change_data = array( );
				$count_ordersn = $this->countOrdersn($ordersn_tid);
				if( $order["status"] == 0 && $count_ordersn == 1 ) 
				{
					$change_data["status"] = 1;
					$change_data["tradestatus"] = 1;
					$change_data["paytime"] = $time;
				}
				else 
				{
					if( $order["status"] == 1 && $order["tradestatus"] == 1 && $count_ordersn == 2 ) 
					{
						$change_data["tradestatus"] = 2;
						$change_data["tradepaytime"] = $time;
					}
				}
				pdo_update("ewei_shop_order", $change_data, array( "id" => $order["id"] ));
				if( $order["status"] == 0 && $count_ordersn == 1 ) 
				{
					m("notice")->sendOrderMessage($order["id"]);
				}
			}
			return true;
		}
		else 
		{
			return false;
		}
	}

p(“commission”)->checkOrderPay($order[“id”])
分销逻辑处理,分销升级处理,将订单的三级分销写入订单表中,调用\addons\ewei_shopv2\plugin\commission\core\model.php中的checkOrderPay,

		public function checkOrderPay($orderid = "0") 
		{
			global $_W;
			global $_GPC;
			if( empty($orderid) ) 
			{
				return NULL;
			}
			// 在ims_ewei_shop_sysset获取分销相关信息
			$set = $this->getSet();
 
			if( empty($set["level"]) ) 
			{
				return NULL;
			}
			//获取订单信息
			$order = pdo_fetch("select id,isparent,parentid,openid,ordersn,goodsprice,agentid,paytime,uniacid from " . tablename("ewei_shop_order") . " where id=:id and status>=1 and uniacid=:uniacid limit 1", array( ":id" => $orderid, ":uniacid" => $_W["uniacid"] ));
			if( empty($order) ) 
			{
				return NULL;
			}
			$openid = $order["openid"];
			$member = m("member")->getMember($openid);
			if( empty($member) ) 
			{
				return NULL;
			}
			$become_check = intval($set["become_check"]);
			$become_child = intval($set["become_child"]);
			$parent = false;
			if( empty($become_child) ) 
			{
			    //获取父级相关信息
				$parent = m("member")->getMember($member["agentid"]);
			}
			else 
			{
			    //获取邀请人相关信息
				$parent = m("member")->getMember($member["inviter"]);
			}
			// isagent是否分销商 包括已经提交分销申请的   status	状态 0 没有审核 1 已经审核
			$parent_is_agent = !empty($parent) && $parent["isagent"] == 1 && $parent["status"] == 1;
			$time = time();
			$become_child = intval($set["become_child"]);
			if( $parent_is_agent && $become_child == 2 && empty($member["agentid"]) && $member["id"] != $parent["id"] && empty($member["fixagentid"]) ) 
			{
			    //agentid	上级分销商ID
				$member["agentid"] = $parent["id"];
				$authorid = (empty($parent["isauthor"]) ? $parent["authorid"] : $parent["id"]);
				$author = p("author");
				if( $author ) 
				{
					$author->upgradeLevelByAgent($parent["id"]);
					pdo_update("ewei_shop_member", array( "agentid" => $parent["id"], "childtime" => $time, "authorid" => $authorid ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
				}
				else 
				{
					pdo_update("ewei_shop_member", array( "agentid" => $parent["id"], "childtime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
				}
				if( p("dividend") ) 
				{
					$this->saveRelation($member["id"], $parent["id"], 1);
					p("dividend")->update_headsid($member["id"], $parent["id"]);
				}
				if( $author ) 
				{
					$author_set = $author->getSet();
					if( !empty($author_set["become"]) && ($author_set["become"] == "2" || $author_set["become"] == "5") ) 
					{
						$can_author = false;
						$getAgentsDownNum = $this->getAgentsDownNum($parent["openid"]);
						if( $author_set["become"] == "2" ) 
						{
							if( $author_set["become_down1"] <= $getAgentsDownNum["level1"] ) 
							{
								$can_author = true;
							}
							else 
							{
								if( $author_set["become_down2"] <= $getAgentsDownNum["level2"] ) 
								{
									$can_author = true;
								}
								else 
								{
									if( $author_set["become_down3"] <= $getAgentsDownNum["level3"] ) 
									{
										$can_author = true;
									}
								}
							}
						}
						else 
						{
							if( $author_set["become"] == "5" ) 
							{
								if( $author_set["become_downcount"] <= $getAgentsDownNum["total"] ) 
								{
									$can_author = true;
								}
							}
							else 
							{
								if( $author_set["become"] == "5" ) 
								{
									$temp_parent = $parent["id"];
									do 
									{
										$res = $author->becomeType6($temp_parent);
										$temp_parent = $res["agentid"];
									}
									while( $res["agentid"] != 0 );
								}
							}
						}
						if( $can_author ) 
						{
							$become_check = intval($author_set["become_check"]);
							if( empty($member["authorblack"]) ) 
							{
								pdo_update("ewei_shop_member", array( "authorstatus" => $become_check, "isauthor" => 1, "authortime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $parent["id"] ));
								if( $become_check == 1 ) 
								{
									$this->sendMessage($parent["openid"], array( "nickname" => $parent["nickname"], "authortime" => $time ), TM_AUTHOR_BECOME);
								}
							}
						}
					}
				}
				$this->sendMessage($parent["openid"], array( "nickname" => $member["nickname"], "childtime" => $time ), TM_COMMISSION_AGENT_NEW);
				$this->upgradeLevelByAgent($parent["id"]);
				if( p("globonus") ) 
				{
					p("globonus")->upgradeLevelByAgent($parent["id"]);
				}
				if( p("abonus") ) 
				{
					p("abonus")->upgradeLevelByAgent($parent["id"]);
				}
				if( p("author") ) 
				{
					p("author")->upgradeLevelByAgent($parent["id"]);
				}
				if( empty($order["agentid"]) ) 
				{
					$order["agentid"] = $parent["id"];
					if( $order["isparent"] && $order["parentid"] == 0 ) 
					{
						$merchSql = "SELECT id,merchid FROM " . tablename("ewei_shop_order") . " WHERE uniacid = " . intval($order["uniacid"]) . " AND parentid = " . intval($orderid);
						$merchData = pdo_fetchall($merchSql);
						foreach( $merchData as $mk => $mv ) 
						{
							pdo_update("ewei_shop_order", array( "agentid" => $parent["id"] ), array( "id" => $mv["id"] ));
						}
					}
					pdo_update("ewei_shop_order", array( "agentid" => $parent["id"] ), array( "id" => $orderid ));
					$order_agent_id = (!empty($parent["id"]) ? $parent["id"] : NULL);
					//计算分销收入  写入订单表中
					$this->calculate($orderid, true, $order_agent_id);
				}
			}
			$isagent = $member["isagent"] == 1 && $member["status"] == 1;
			//非分销商
			if( !$isagent ) 
			{
				if( intval($set["become"]) == 4 && !empty($set["become_goodsid"]) ) 
				{
					if( empty($set["become_order"]) ) 
					{
						$order_goods = pdo_fetchall("select goodsid from " . tablename("ewei_shop_order_goods") . " where orderid=:orderid and uniacid=:uniacid  ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ), "goodsid");
						if( in_array($set["become_goodsid"], array_keys($order_goods)) && empty($member["agentblack"]) ) 
						{
							pdo_update("ewei_shop_member", array( "status" => $become_check, "isagent" => 1, "agenttime" => ($become_check == 1 ? $time : 0), "applyagenttime" => 0 ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
							if( $become_check == 1 ) 
							{
								$this->sendMessage($openid, array( "nickname" => $member["nickname"], "agenttime" => $time ), TM_COMMISSION_BECOME);
								if( !empty($parent) ) 
								{
									$this->upgradeLevelByAgent($parent["id"]);
									if( p("globonus") ) 
									{
										p("globonus")->upgradeLevelByAgent($parent["id"]);
									}
									if( p("abonus") ) 
									{
										p("abonus")->upgradeLevelByAgent($parent["id"]);
									}
									if( p("author") ) 
									{
										p("author")->upgradeLevelByAgent($parent["id"]);
									}
								}
							}
						}
					}
				}
				else 
				{  // become成为分销商的条件 1申请   2按消费次数   3消费金额    4购买商品   become_order 0为付款后 1为完成后
					if( ($set["become"] == 2 || $set["become"] == 3) && empty($set["become_order"]) ) 
					{
						$time = time();
						$parentisagent = true;
						if( !empty($member["agentid"]) ) 
						{
							$parent = m("member")->getMember($member["agentid"]);
							if( empty($parent) || $parent["isagent"] != 1 || $parent["status"] != 1 ) 
							{
								$parentisagent = false;
							}
						}
						$can = false;
						// 2按消费次数
						if( $set["become"] == "2" ) 
						{
							$ordercount = pdo_fetchcolumn("select count(*) from " . tablename("ewei_shop_order") . " where openid=:openid and status>=1 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
							$can = intval($set["become_ordercount"]) <= $ordercount;  // 是否达到消费次数
						}
						else 
						{   //3消费金额
							if( $set["become"] == "3" ) 
							{
								$moneycount = pdo_fetchcolumn("select sum(og.realprice) from " . tablename("ewei_shop_order_goods") . " og left join " . tablename("ewei_shop_order") . " o on og.orderid=o.id  where o.openid=:openid and o.status>=1 and o.uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
								$can = floatval($set["become_moneycount"]) <= $moneycount; //是否达到消费总额
							}
						}
						//如果符合条件
						if( $can && empty($member["agentblack"]) ) 
						{
							pdo_update("ewei_shop_member", array( "status" => $become_check, "isagent" => 1, "agenttime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
							if( $become_check == 1 ) 
							{
								$this->sendMessage($openid, array( "nickname" => $member["nickname"], "agenttime" => $time ), TM_COMMISSION_BECOME);
								if( $parentisagent ) 
								{
									$this->upgradeLevelByAgent($parent["id"]);
									if( p("globonus") ) 
									{
										p("globonus")->upgradeLevelByAgent($parent["id"]);
									}
									if( p("abonus") ) 
									{
										p("abonus")->upgradeLevelByAgent($parent["id"]);
									}
									if( p("author") ) 
									{
										p("author")->upgradeLevelByAgent($parent["id"]);
									}
								}
							}
						}
					}
				}
			}
			if( !empty($member["agentid"]) ) 
			{
				$parent = m("member")->getMember($member["agentid"]);
				if( !empty($parent) && $parent["isagent"] == 1 && $parent["status"] == 1 ) 
				{
					$order_goods = pdo_fetchall("select g.id,g.title,og.total,og.price,og.realprice, og.optionname as optiontitle,g.noticeopenid,g.noticetype,og.commission1,og.commissions  from " . tablename("ewei_shop_order_goods") . " og " . " left join " . tablename("ewei_shop_goods") . " g on g.id=og.goodsid " . " where og.uniacid=:uniacid and og.orderid=:orderid ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ));
					$goods = "";
					$commission_total1 = 0;
					$commission_total2 = 0;
					$commission_total3 = 0;
					$pricetotal = 0;
					foreach( $order_goods as $og ) 
					{
						$goods .= "" . $og["title"] . "( ";
						if( !empty($og["optiontitle"]) ) 
						{
							$goods .= " 规格: " . $og["optiontitle"];
						}
						$goods .= " 单价: " . $og["realprice"] / $og["total"] . " 数量: " . $og["total"] . " 总价: " . $og["realprice"] . "); ";
						$commissions = iunserializer($og["commissions"]);
						$commission_total1 += (isset($commissions["level1"]) ? floatval($commissions["level1"]) : 0);
						$commission_total2 += (isset($commissions["level2"]) ? floatval($commissions["level2"]) : 0);
						$commission_total3 += (isset($commissions["level3"]) ? floatval($commissions["level3"]) : 0);
						$pricetotal += $og["realprice"];
					}
					if( $order["agentid"] == $member["id"] ) 
					{
						$this->sendMessage($member["openid"], array( "nickname" => $member["nickname"], "ordersn" => $order["ordersn"], "orderopenid" => $order["openid"], "price" => $pricetotal, "goods" => $goods, "commission1" => $commission_total1, "commission2" => $commission_total2, "commission3" => $commission_total3, "paytime" => $order["paytime"] ), TM_COMMISSION_ORDER_PAY);
					}
					else 
					{
						if( $order["agentid"] == $parent["id"] ) 
						{
							$this->sendMessage($parent["openid"], array( "nickname" => $member["nickname"], "ordersn" => $order["ordersn"], "orderopenid" => $order["openid"], "price" => $pricetotal, "goods" => $goods, "commission1" => $commission_total1, "commission2" => $commission_total2, "commission3" => $commission_total3, "paytime" => $order["paytime"] ), TM_COMMISSION_ORDER_PAY);
						}
					}
					if( p("author") && !empty($member["authorid"]) ) 
					{
						$author = m("member")->getMember($member["authorid"]);
						if( !empty($author["isauthor"]) && $author["authorstatus"] ) 
						{
							p("author")->sendMessage($author["openid"], array( "nickname" => $member["nickname"], "ordersn" => $order["ordersn"], "price" => $pricetotal, "goods" => $goods, "paytime" => $order["paytime"] ), TM_AUTHOR_DOWN_PAY);
						}
					}
				}
			}
			if( $isagent ) 
			{
				$plugin_globonus = p("globonus");
				if( !$plugin_globonus ) 
				{
					return NULL;
				}
				$set = $plugin_globonus->getSet();
				if( empty($set["open"]) ) 
				{
					return NULL;
				}
				if( $member["ispartner"] ) 
				{
					return NULL;
				}
				$become_check = intval($set["become_check"]);
				if( intval($set["become"]) == 4 && !empty($set["become_goodsid"]) ) 
				{
					if( empty($set["become_order"]) ) 
					{
						$order_goods = pdo_fetchall("select goodsid from " . tablename("ewei_shop_order_goods") . " where orderid=:orderid and uniacid=:uniacid  ", array( ":uniacid" => $_W["uniacid"], ":orderid" => $order["id"] ), "goodsid");
						if( in_array($set["become_goodsid"], array_keys($order_goods)) && empty($member["partnerblack"]) ) 
						{
							pdo_update("ewei_shop_member", array( "partnerstatus" => $become_check, "ispartner" => 1, "partnertime" => ($become_check == 1 ? $time : 0) ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
							if( $become_check == 1 ) 
							{
								$plugin_globonus->sendMessage($openid, array( "nickname" => $member["nickname"], "partnertime" => $time ), TM_GLOBONUS_BECOME);
							}
						}
					}
				}
				else 
				{
					if( ($set["become"] == 2 || $set["become"] == 3) && empty($set["become_order"]) ) 
					{
						$time = time();
						$can = false;
						if( $set["become"] == "2" ) 
						{
							$ordercount = pdo_fetchcolumn("select count(*) from " . tablename("ewei_shop_order") . " where openid=:openid and status>=1 and uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
							$can = intval($set["become_ordercount"]) <= $ordercount;
						}
						else 
						{
							if( $set["become"] == "3" ) 
							{
								$moneycount = pdo_fetchcolumn("select sum(og.realprice) from " . tablename("ewei_shop_order_goods") . " og left join " . tablename("ewei_shop_order") . " o on og.orderid=o.id  where o.openid=:openid and o.status>=1 and o.uniacid=:uniacid limit 1", array( ":uniacid" => $_W["uniacid"], ":openid" => $openid ));
								$can = floatval($set["become_moneycount"]) <= $moneycount;
							}
						}
						if( $can && empty($member["partnerblack"]) ) 
						{
							pdo_update("ewei_shop_member", array( "partnerstatus" => $become_check, "ispartner" => 1, "partnertime" => $time ), array( "uniacid" => $_W["uniacid"], "id" => $member["id"] ));
							if( $become_check == 1 ) 
							{
								$plugin_globonus->sendMessage($openid, array( "nickname" => $member["nickname"], "partnertime" => $time ), TM_GLOBONUS_BECOME);
							}
						}
					}
				}
			}
		}

计算分销逻辑

public function calculate($orderid = 0, $update = true, $order_agentid = NULL) 
		{
			global $_W;
			$set = $this->getSet();
			$levels = $this->getLevels();
			$order = pdo_fetch("select agentid,price,goodsprice,deductcredit2,discountprice,isdiscountprice,dispatchprice,changeprice,ispackage,packageid,couponprice from " . tablename("ewei_shop_order") . " where id=:id limit 1", array( ":id" => $orderid ));
			$commission = m("common")->getPluginset("commission");
			if( empty($commission["commissiontype"]) ) 
			{
				$rate = 1;
			}
			else 
			{
				$numm = $order["goodsprice"] - $order["isdiscountprice"] - $order["discountprice"] - $order["couponprice"];
				if( $numm != 0 ) 
				{
					$rate = ($order["price"] - $order["changeprice"] - $order["dispatchprice"] + $order["deductcredit2"]) / $numm;
				}
				else 
				{
					$rate = 1;
				}
			}
			$agentid = (!is_null($order_agentid) ? $order_agentid : $order["agentid"]);
			$hascommission = false;
			if( $order["isparent"] && $order["parentid"] == 0 ) 
			{
				$parent_sql = "select id from " . tablename("ewei_shop_order") . "where parentid=" . $orderid;
				$condition = " WHERE  og.uniacid=:uniacid";
				$param[":uniacid"] = $_W["uniacid"];
				$condition .= " AND og.orderid in(" . $parent_sql . ")";
				$goods_sql = "select og.id,og.realprice,og.total,g.hasoption,og.goodsid,og.optionid,g.hascommission,g.nocommission, g.commission1_rate,g.commission1_pay,g.commission2_rate,g.commission2_pay,\r\n          g.commission3_rate,g.commission3_pay,g.commission,og.commissions,og.seckill,og.seckill_taskid,og.seckill_timeid from " . tablename("ewei_shop_order_goods") . "  og " . " left join " . tablename("ewei_shop_goods") . " g on g.id = og.goodsid " . $condition;
				$goods = pdo_fetchall($goods_sql, $param);
			}
			else 
			{
				$goods = pdo_fetchall("select og.id,og.realprice,og.total,g.hasoption,og.goodsid,og.optionid,g.hascommission,g.nocommission, g.commission1_rate,g.commission1_pay,g.commission2_rate,g.commission2_pay,\r\n                  g.commission3_rate,g.commission3_pay,g.commission,og.commissions,og.seckill,og.seckill_taskid,og.seckill_timeid from " . tablename("ewei_shop_order_goods") . "  og " . " left join " . tablename("ewei_shop_goods") . " g on g.id = og.goodsid" . " where og.orderid=:orderid and og.uniacid=:uniacid", array( ":orderid" => $orderid, ":uniacid" => $_W["uniacid"] ));
			}
			if( 0 < $set["level"] ) 
			{
				foreach( $goods as &$cinfo ) 
				{
					$price = $cinfo["realprice"] * $rate;
					$seckill_goods = false;
					if( $cinfo["seckill"] ) 
					{
						$seckill_goods = pdo_fetch("select commission1,commission2,commission3 from " . tablename("ewei_shop_seckill_task_goods") . "\r\n                                            where  goodsid=:goodsid and optionid =:optionid and taskid=:taskid and timeid=:timeid and uniacid=:uniacid limit 1", array( ":goodsid" => $cinfo["goodsid"], ":optionid" => $cinfo["optionid"], ":taskid" => $cinfo["seckill_taskid"], ":timeid" => $cinfo["seckill_timeid"], ":uniacid" => $_W["uniacid"] ));
					}
					if( !empty($seckill_goods) ) 
					{
						$hascommission = true;
						$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? $seckill_goods["commission1"] * $cinfo["total"] : 0) );
						$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? $seckill_goods["commission2"] * $cinfo["total"] : 0) );
						$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? $seckill_goods["commission3"] * $cinfo["total"] : 0) );
						foreach( $levels as $level ) 
						{
							$cinfo["commission1"]["level" . $level["id"]] = $seckill_goods["commission1"] * $cinfo["total"];
							$cinfo["commission2"]["level" . $level["id"]] = $seckill_goods["commission2"] * $cinfo["total"];
							$cinfo["commission3"]["level" . $level["id"]] = $seckill_goods["commission3"] * $cinfo["total"];
						}
					}
					else 
					{
						$goods_commission = (!empty($cinfo["commission"]) ? json_decode($cinfo["commission"], true) : "");
						if( empty($cinfo["nocommission"]) ) 
						{
							$hascommission = true;
							if( $cinfo["hascommission"] == 1 ) 
							{
								if( empty($goods_commission["type"]) ) 
								{
									$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? (0 < $cinfo["commission1_rate"] ? round(($cinfo["commission1_rate"] * $price) / 100, 2) . "" : round($cinfo["commission1_pay"] * $cinfo["total"], 2)) : 0) );
									$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? (0 < $cinfo["commission2_rate"] ? round(($cinfo["commission2_rate"] * $price) / 100, 2) . "" : round($cinfo["commission2_pay"] * $cinfo["total"], 2)) : 0) );
									$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? (0 < $cinfo["commission3_rate"] ? round(($cinfo["commission3_rate"] * $price) / 100, 2) . "" : round($cinfo["commission3_pay"] * $cinfo["total"], 2)) : 0) );
									foreach( $levels as $level ) 
									{
										$cinfo["commission1"]["level" . $level["id"]] = (0 < $cinfo["commission1_rate"] ? round(($cinfo["commission1_rate"] * $price) / 100, 2) . "" : round($cinfo["commission1_pay"] * $cinfo["total"], 2));
										$cinfo["commission2"]["level" . $level["id"]] = (0 < $cinfo["commission2_rate"] ? round(($cinfo["commission2_rate"] * $price) / 100, 2) . "" : round($cinfo["commission2_pay"] * $cinfo["total"], 2));
										$cinfo["commission3"]["level" . $level["id"]] = (0 < $cinfo["commission3_rate"] ? round(($cinfo["commission3_rate"] * $price) / 100, 2) . "" : round($cinfo["commission3_pay"] * $cinfo["total"], 2));
									}
								}
								else 
								{
									if( empty($cinfo["hasoption"]) ) 
									{
										$temp_price = array( );
										for( $i = 0; $i < $set["level"]; $i++ ) 
										{
											if( !empty($goods_commission["default"]["option0"][$i]) ) 
											{
												if( strexists($goods_commission["default"]["option0"][$i], "%") ) 
												{
													$dd = floatval(str_replace("%", "", $goods_commission["default"]["option0"][$i]));
													if( 0 < $dd && $dd < 100 ) 
													{
														$temp_price[$i] = round($dd / 100 * $price, 2);
													}
													else 
													{
														$temp_price[$i] = 0;
													}
												}
												else 
												{
													$temp_price[$i] = round($goods_commission["default"]["option0"][$i] * $cinfo["total"], 2);
												}
											}
										}
										$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? $temp_price[0] : 0) );
										$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? $temp_price[1] : 0) );
										$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? $temp_price[2] : 0) );
										foreach( $levels as $level ) 
										{
											$temp_price = array( );
											for( $i = 0; $i < $set["level"]; $i++ ) 
											{
												if( !empty($goods_commission["level" . $level["id"]]["option0"][$i]) ) 
												{
													if( strexists($goods_commission["level" . $level["id"]]["option0"][$i], "%") ) 
													{
														$dd = floatval(str_replace("%", "", $goods_commission["level" . $level["id"]]["option0"][$i]));
														if( 0 < $dd && $dd < 100 ) 
														{
															$temp_price[$i] = round($dd / 100 * $price, 2);
														}
														else 
														{
															$temp_price[$i] = 0;
														}
													}
													else 
													{
														$temp_price[$i] = round($goods_commission["level" . $level["id"]]["option0"][$i] * $cinfo["total"], 2);
													}
												}
											}
											list($cinfo["commission1"]["level" . $level["id"]], $cinfo["commission2"]["level" . $level["id"]], $cinfo["commission3"]["level" . $level["id"]]) = $temp_price;
										}
									}
									else 
									{
										$temp_price = array( );
										for( $i = 0; $i < $set["level"]; $i++ ) 
										{
											if( !empty($goods_commission["default"]["option" . $cinfo["optionid"]][$i]) ) 
											{
												if( strexists($goods_commission["default"]["option" . $cinfo["optionid"]][$i], "%") ) 
												{
													$dd = floatval(str_replace("%", "", $goods_commission["default"]["option" . $cinfo["optionid"]][$i]));
													if( 0 < $dd && $dd < 100 ) 
													{
														$temp_price[$i] = round($dd / 100 * $price, 2);
													}
													else 
													{
														$temp_price[$i] = 0;
													}
												}
												else 
												{
													$temp_price[$i] = round($goods_commission["default"]["option" . $cinfo["optionid"]][$i] * $cinfo["total"], 2);
												}
											}
										}
										$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? $temp_price[0] : 0) );
										$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? $temp_price[1] : 0) );
										$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? $temp_price[2] : 0) );
										foreach( $levels as $level ) 
										{
											$temp_price = array( );
											for( $i = 0; $i < $set["level"]; $i++ ) 
											{
												if( !empty($goods_commission["level" . $level["id"]]["option" . $cinfo["optionid"]][$i]) ) 
												{
													if( strexists($goods_commission["level" . $level["id"]]["option" . $cinfo["optionid"]][$i], "%") ) 
													{
														$dd = floatval(str_replace("%", "", $goods_commission["level" . $level["id"]]["option" . $cinfo["optionid"]][$i]));
														if( 0 < $dd && $dd < 100 ) 
														{
															$temp_price[$i] = round($dd / 100 * $price, 2);
														}
														else 
														{
															$temp_price[$i] = 0;
														}
													}
													else 
													{
														$temp_price[$i] = round($goods_commission["level" . $level["id"]]["option" . $cinfo["optionid"]][$i] * $cinfo["total"], 2);
													}
												}
											}
											list($cinfo["commission1"]["level" . $level["id"]], $cinfo["commission2"]["level" . $level["id"]], $cinfo["commission3"]["level" . $level["id"]]) = $temp_price;
										}
									}
								}
							}
							else 
							{
								$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? round(($set["commission1"] * $price) / 100, 2) . "" : 0) );
								$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? round(($set["commission2"] * $price) / 100, 2) . "" : 0) );
								$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? round(($set["commission3"] * $price) / 100, 2) . "" : 0) );
								foreach( $levels as $level ) 
								{
									$cinfo["commission1"]["level" . $level["id"]] = (1 <= $set["level"] ? round(($level["commission1"] * $price) / 100, 2) . "" : 0);
									$cinfo["commission2"]["level" . $level["id"]] = (2 <= $set["level"] ? round(($level["commission2"] * $price) / 100, 2) . "" : 0);
									$cinfo["commission3"]["level" . $level["id"]] = (3 <= $set["level"] ? round(($level["commission3"] * $price) / 100, 2) . "" : 0);
								}
							}
							if( 0 < $order["ispackage"] ) 
							{
								$packoption = array( );
								if( !empty($cinfo["optionid"]) ) 
								{
									$packoption = pdo_fetch("select commission1,commission2,commission3 from " . tablename("ewei_shop_package_goods_option") . "\r\n                                            where uniacid = " . $_W["uniacid"] . " and pid = " . $order["packageid"] . " and optionid = " . $cinfo["optionid"] . " ");
								}
								else 
								{
									$packoption = pdo_fetch("select commission1,commission2,commission3 from " . tablename("ewei_shop_package_goods") . "\r\n                                            where uniacid = " . $_W["uniacid"] . " and pid = " . $order["packageid"] . " and goodsid = " . $cinfo["goodsid"] . " ");
								}
								$cinfo["commission1"] = array( "default" => (1 <= $set["level"] ? $packoption["commission1"] : 0) );
								$cinfo["commission2"] = array( "default" => (2 <= $set["level"] ? $packoption["commission2"] : 0) );
								$cinfo["commission3"] = array( "default" => (3 <= $set["level"] ? $packoption["commission3"] : 0) );
								foreach( $levels as $level ) 
								{
									$cinfo["commission1"]["level" . $level["id"]] = $packoption["commission1"];
									$cinfo["commission2"]["level" . $level["id"]] = $packoption["commission2"];
									$cinfo["commission3"]["level" . $level["id"]] = $packoption["commission3"];
								}
							}
						}
						else 
						{
							$cinfo["commission1"] = array( "default" => 0 );
							$cinfo["commission2"] = array( "default" => 0 );
							$cinfo["commission3"] = array( "default" => 0 );
							foreach( $levels as $level ) 
							{
								$cinfo["commission1"]["level" . $level["id"]] = 0;
								$cinfo["commission2"]["level" . $level["id"]] = 0;
								$cinfo["commission3"]["level" . $level["id"]] = 0;
							}
						}
					}
					if( $update ) 
					{
						$commissions = array( "level1" => 0, "level2" => 0, "level3" => 0 );
						if( !empty($agentid) ) 
						{
							$m1 = m("member")->getMember($agentid);
							if( $m1["isagent"] == 1 && $m1["status"] == 1 ) 
							{
								$l1 = $this->getLevel($m1["openid"]);
								$commissions["level1"] = (empty($l1) ? round($cinfo["commission1"]["default"], 2) : round($cinfo["commission1"]["level" . $l1["id"]], 2));
								if( !empty($m1["agentid"]) ) 
								{
									$m2 = m("member")->getMember($m1["agentid"]);
									$l2 = $this->getLevel($m2["openid"]);
									$commissions["level2"] = (empty($l2) ? round($cinfo["commission2"]["default"], 2) : round($cinfo["commission2"]["level" . $l2["id"]], 2));
									if( !empty($m2["agentid"]) ) 
									{
										$m3 = m("member")->getMember($m2["agentid"]);
										$l3 = $this->getLevel($m3["openid"]);
										$commissions["level3"] = (empty($l3) ? round($cinfo["commission3"]["default"], 2) : round($cinfo["commission3"]["level" . $l3["id"]], 2));
									}
								}
							}
						}
						//将订单分成信息写入订单表中
						pdo_update("ewei_shop_order_goods", array( "commission1" => iserializer($cinfo["commission1"]), "commission2" => iserializer($cinfo["commission2"]), "commission3" => iserializer($cinfo["commission3"]), "commissions" => iserializer($commissions), "nocommission" => $cinfo["nocommission"] ), array( "id" => $cinfo["id"] ));
					}
				}
				unset($cinfo);
			}
			if( !$hascommission ) 
			{
				pdo_update("ewei_shop_order", array( "agentid" => 0 ), array( "id" => $orderid ));
			}
			return $goods;
		}