| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | <?php namespace Pay\Yspay;use Dever;class Cash{	# 入账:待确认    public function add($mid, $amount, $order_num, $source_order_num, $fenzhang = 0)    {    	$merchant = Dever::db('pay/yspay_merchant')->one(array('mid' => $mid));    	if ($merchant) {    		$data = array();    		$data['account_id'] = $merchant['account_id'];			$data['merchant_id'] = $merchant['id'];    		$data['order_num'] = $order_num;	        $data['source_order_num'] = $source_order_num;	        $info = Dever::db('pay/yspay_cash')->find($data);	        if ($amount && $amount > 0) {	        	$this->getCash($amount, $merchant, $data, $fenzhang);	        }	        	        $data['status'] = 1;	        if ($info) {	        	$data['where_id'] = $id = $info['id'];	        	Dever::db('pay/yspay_cash')->update($data);	        } else {	        	$id = Dever::db('pay/yspay_cash')->insert($data);	        }	        	        return $id;    	}    	return false;    }    # 修改状态    public function up($id, $status = 2, $amount = false)    {    	if ($amount) {    		$amount = $amount * 100;    	}    	if (is_numeric($id)) {    		$where['id'] = $id;    	} else {    		$where['source_order_num'] = $id;    	}    	    	$info = Dever::db('pay/yspay_cash')->find($where);    	if ($info) {    		$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);    		if ($merchant) {    			$update = array('status' => $status, 'where_id' => $info['id']);	    		if ($status == 3) {	    			# 开始划付	    			$state = $this->huafu_act($info);	    			if ($state != 'ok') {	    				return false;	    			}	    			$update['rdate'] = time();	    		} elseif ($status == 4) {	    			# 开始提现	    			$state = $this->tixian_act($info);	    			if ($state != 'ok') {	    				return false;	    			}	    			$update['tdate'] = time();	    		} elseif ($status == 2 && $amount && $amount > 0 && $info['ycash'] != $amount) {	    			# 待入账 可以修改金额		        	$this->getCash($amount, $merchant, $update);		        }	    		$state = Dever::db('pay/yspay_cash')->update($update);	    		if ($state) {	    			if ($status == 3) {	    				$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 3, 'merchant_id' => $info['merchant_id']));	    				if ($total) {	    					Dever::db('pay/yspay_merchant')->update(array('where_id' => $info['merchant_id'], 'cash' => $total['cash'], 'hf_cash' => $total['hf_cash'], 'fz_cash' => $total['fz_cash']));	    				}	    			}	    			return $state;	    		}    		}    	}    	return false;    }    private function getCash($amount, $merchant, &$data, $fenzhang = 0)    {    	$yspay = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));		if ($yspay) {			$cash_per = 0;			if ($merchant['type'] == 1 && $yspay['type'] == 2) {				$cash_per = $merchant['cash_per'];	    		if (!$cash_per) {	    			if ($yspay && $yspay['cash_per']) {	    				$cash_per = $yspay['cash_per'];	    			}	    		}	    		if (!$cash_per || $cash_per <= 0) {	    			$cash_per = 0;	    		} else {	    			$cash_per = $cash_per/100;	    		}			}			    		$per = $yspay['per']/100;    		$cash_jy_per = $yspay['cash_jy_per']/100;        	$data['ycash'] = $amount/100;        	$data['yl_cash'] = round($data['ycash'] * $per, 2);        	$data['pt_cash'] = round($data['ycash'] * $cash_jy_per, 2);	        $data['cash'] = round($data['ycash'] - $data['yl_cash'] - $data['pt_cash'], 2);	        if ($yspay['cash_type'] == 1) {	        	$fz_cash = $data['ycash'];	        } else {	        	$fz_cash = $data['cash'];	        }	        $data['fz_cash'] = round($fz_cash*$cash_per, 2);	        if ($fenzhang && $fenzhang > 0) {	        	$data['fz_cash'] += $fenzhang;	        }	        $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);	        $data['yl_cash'] *= 100;			$data['pt_cash'] *= 100;			$data['cash'] *= 100;			$data['fz_cash'] *= 100;			$data['hf_cash'] *= 100;	    }    }    private function huafu_act($info)    {    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);    	if ($merchant) {    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));    		if ($config) {    			return Dever::load('pay/yspay/multi')->act($config, $merchant, $info);    		}    	}    	return false;    }    private function tixian_act($info)    {    	$merchant = Dever::db('pay/yspay_merchant')->one($info['merchant_id']);    	if ($merchant) {    		$config = Dever::db('pay/yspay')->one(array('account_id' => $merchant['account_id']));    		if ($config) {    			return Dever::load('pay/yspay/account')->act($config, $merchant, $info);    		}    	}    	return false;    }    # 定时获取总额    # 测试数据    public function test_add_api()    {    	$info['mid'] = 'test';    	$info['merOrderId'] = 'test123';    	$info['amount'] = 10000;    	$this->add('test123', array(), $info);    	return 'ok';    }    # 将测试数据改成待入账    public function test_edit_api()    {    	$data = Dever::db('pay/yspay_cash')->select(array('order_num' => 'test123', 'status' => 1));    	if ($data) {    		foreach ($data as $k => $v) {    			$this->up($v['id'], 2);    		}    	}     	return 'ok';    }    # 资金划付    public function huafu_commit_api()    {    	$where = array('status' => 2);    	$account_id = Dever::input('account_id');    	if ($account_id) {    		$where['account_id'] = $account_id;    	}    	$merchant_id = Dever::input('merchant_id');    	if ($merchant_id) {    		$where['merchant_id'] = $merchant_id;    	}    	$data = Dever::db('pay/yspay_cash')->select($where);    	if ($data) {    		foreach ($data as $k => $v) {    			$this->up($v['id'], 3);    		}    	}    	return 'ok';    }    # 随机获取平台商户    public function getMid($account_id)    {    	$merchant = Dever::db('pay/yspay_merchant')->select(array('type' => 2, 'account_id' => $account_id, 'status' => 1));    	if ($merchant) {    		$key = array_rand($merchant);    		if (isset($merchant[$key])) {    			return $merchant[$key]['mid'];    		}    	}    	return false;    }}
 |