| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 | <?php namespace Pay\Lib;use Dever;Dever::apply('sdk/wechat', 'pay');class Wechat extends Core{	public function __construct($config)	{		$project = Dever::project('pay');		$this->config = new \WxPayConfig();		# 通知接口		$config['notify'] = $this->url($config['type']);		# 证书		$config['ssl'] = array		(			'cert' => $config['file_cert'],			'key' => $config['file_key'],		);		$this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']);	}	/**	 * 通知	 */	public function notify()	{		$callback = new Callback();		$result = $callback->Handle($this->config, false);	}	/**	 * 获取统一下单的基本信息	 */	public function order($account_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1)	{		$trade_type = $this->getType($type);		$order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->GetType());		$tools = new \JsApiPay($this->config);		$openid = $openid ? $openid : $tools->GetOpenid();		$input = new \WxPayUnifiedOrder();		$input->SetBody($name);		$input->SetAttach($name);		$input->SetOut_trade_no($order_id);		$input->SetTotal_fee($cash);		$input->SetTime_start(date("YmdHis"));		$input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));		//$input->SetGoods_tag($name);		$input->SetNotify_url($this->config->GetNotifyUrl());		$input->SetTrade_type($trade_type);		$input->SetProduct_id($product_id);		$input->SetOpenid($openid);		if ($type == 1) {			$order = \WxPayApi::unifiedOrder($this->config, $input);			print_r($order);die;			# 下单信息			$this->updateOrderParam($order_id, $order);			return array($order_id, $order);		} else {			# 下单信息			$this->updateOrderParam($order_id, $input);			return array($order_id, $input);		}	}	/**	 * 获取二维码支付	 */	public function qrcode($order, $refer)	{		$notify = new \NativePay();		$result = $notify->GetPayUrl($order[1]);		$url = $result['code_url'];		return $url;	}	/**	 * 获取页面支付	 */	public function page($order, $refer)	{		$refer = urldecode($refer);		$tools = new \JsApiPay($this->config);		$info = $tools->GetJsApiParameters($order[1]);		$html = '<script type="text/javascript">		function jsApiCall()		{			WeixinJSBridge.invoke(				"getBrandWCPayRequest",				'.$info.',				function(res){					//WeixinJSBridge.log(res.err_msg);					if(res.err_msg == "get_brand_wcpay_request:ok")					{						location.href = "'.$refer.'";					} else {						alert(res.err_code+res.err_desc+res.err_msg);					}				}			);		}		function callpay()		{			if (typeof WeixinJSBridge == "undefined"){			    if( document.addEventListener ){			        document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);			    }else if (document.attachEvent){			        document.attachEvent("WeixinJSBridgeReady", jsApiCall); 			        document.attachEvent("onWeixinJSBridgeReady", jsApiCall);			    }			}else{			    jsApiCall();			}		}		callpay();		</script>';		return $html;	}	private function getType($type)	{		switch ($type) {			case 1:				$type = 'JSAPI';				break;						case 2:				$type = 'NATIVE';				break;		}		return $type;	}}class Callback extends \WxPayNotify{	public function NotifyProcess($objData, $config, &$msg)	{		$data = $objData->GetValues();		$obj = Dever::load('pay/lib/wechat');		$callback = function($msg = '') use ($obj, $data) {			if ($msg) {				$msg = $data['transaction_id'] . ':' . $msg;			}			$obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);		};		# 参数校验		if(!array_key_exists("return_code", $data) 			||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {			$msg = '异常';			return false;		}		if(!array_key_exists("transaction_id", $data)){			$msg = '输入参数不正确';			return false;		}		# 进行签名验证		try {			$checkResult = $objData->CheckSign($config);			if($checkResult == false){				$msg = '签名错误';				$callback($msg);				return false;			}		} catch(Exception $e) {			$msg = '签名异常';			$callback($msg);			return false;		}		# 查询订单,判断订单真实性		if(!$this->Queryorder($data["transaction_id"])){			$msg = '订单查询失败';			$callback($msg);			return false;		}		# 处理业务逻辑		$callback();		return true;	}}
 |