| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | <?php namespace Pay\Yspay;use Dever;class Cash{	# 千分制	private $num = 1000;	# 入账:待确认    public function add($merchant_id, $amount, $order_num, $source_order_num, $fenzhang = 0)    {    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);    	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, $fenzhang = 0)    {    	if ($amount) {    		$amount = $amount * $this->num;    	}    	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 == 2) {	    			if ($amount && $amount > 0 && $info['ycash'] != $amount) {		        		$this->getCash($amount, $merchant, $update, $fenzhang);	    			}	    			$update['fdate'] = time();		        }	    		$state = Dever::db('pay/yspay_cash')->update($update);	    		if ($state) {	    			if ($status == 2) {	    				$total = Dever::db('pay/yspay_cash')->getTotal(array('status' => 2, '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']));	    				}	    				$info = Dever::db('pay/yspay_cash')->find(array('id' => $info['id'], 'clear' => true));	    				if ($info['fz_cash'] && $info['fz_cash'] > 0) {	    					$state = $this->fenzhang_act($info);		    				if ($state == 1) {		    					$update = array();			    				$update['where_id'] = $info['id'];			    				if ($state == 1) {			    					$update['fenzhang_status'] = 2;			    				} else {			    					$update['fenzhang_status'] = 3;			    				}			    				$update['rdate'] = time();			    				Dever::db('pay/yspay_cash')->update($update);		    				}	    				}	    			}	    			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/$this->num;	    		}			}			    		$per = $yspay['per']/$this->num;    		$cash_jy_per = $yspay['cash_jy_per']/$this->num;        	$data['ycash'] = $amount/$this->num;        	$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);	        $data['fz_cash'] = 0;	        if ($fenzhang && $fenzhang > 0) {	        	$data['fz_cash'] += $fenzhang;	        }	        $data['hf_cash'] = round($data['cash'] - $data['fz_cash'], 2);	        $data['ycash'] *= $this->num;	        $data['yl_cash'] *= $this->num;			$data['pt_cash'] *= $this->num;			$data['cash'] *= $this->num;			$data['fz_cash'] *= $this->num;			$data['hf_cash'] *= $this->num;	    }    }    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')->huafu($config, $merchant, $info);    		}    	}    	return false;    }    private function fenzhang_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')->fenzhang($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_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];    		}    	}    	return false;    }    public function tixian($id, $name, $data)    {    	$merchant_id = Dever::param('merchant_id', $data);    	$cash = Dever::param('cash', $data);    	$merchant = Dever::db('pay/yspay_merchant')->one($merchant_id);    	if ($merchant) {    		$cash = $cash*$this->num;    		$yue = $merchant['hf_cash'] - $merchant['hf_tx_cash'] - $this->num*100;    		if ($yue < $cash) {    			Dever::alert('余额不足');    		}    		$info['merchant_id'] = $merchant_id;    		$info['hf_cash'] = $cash;    		$info['order_num'] = Dever::order('TX');    		$status = $this->huafu_act($info);    		if ($status == 1) {    			Dever::db('pay/yspay_merchant')->updateTxCash(array('where_id' => $merchant_id, 'hf_tx_cash' => $cash));    		}    	}    	return false;    }}
 |