| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 | <?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['id']);		# 证书		$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()	{		$this->log('支付回调-初始化', file_get_contents("php://input"));		$callback = new Callback();		$result = $callback->Handle($this->config, false);	}	/**	 * 获取统一下单的基本信息	 */	public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)	{		$trade_type = $this->getType($type);		$order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);		$tools = new \JsApiPay($this->config);		if ($openid != -1) {			$openid = $openid ? $openid : $tools->GetOpenid();		}		$input = new \WxPayUnifiedOrder();		$input->SetBody($name);		$input->SetAttach($name);		$input->SetOut_trade_no($order_id);		$input->SetTotal_fee($cash * 100);		$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);		if ($openid != -1) {			$input->SetOpenid($openid);		}				if ($type == 1 || $type == 3) {			$order = \WxPayApi::unifiedOrder($this->config, $input);			# 下单信息			$order['time'] = '' . time() . '';			$order['order_id'] = $order_id;			$order['sign_type'] = $this->config->GetSignType();			unset($order['mch_id']);			$this->updateOrderParam($order_id, $order);			return $order;		} else {			# 下单信息			$this->updateOrderParam($order_id, $input);			return $input;		}	}	/**	 * 获取二维码支付	 */	public function qrcode($order, $refer)	{		$notify = new \NativePay();		$result = $notify->GetPayUrl($order);		$url = $result['code_url'];		return $url;	}	/**	 * 获取小程序支付	 */	public function applet($order)	{		if (isset($order['prepay_id'])) {			$string = 'appId='.$this->config->GetAppId().'&nonceStr='.$order['nonce_str'].'&package=prepay_id='.$order['prepay_id'].'&signType='.$order['sign_type'].'&timeStamp='.$order['time'].'&key='.$this->config->GetKey();			if($order['sign_type'] == "MD5"){				$string = md5($string);			} else {				$string = hash_hmac("sha256", $string, $this->config->GetKey());			}			$order['sign'] = $string;		}		return $order;	}	/**	 * 获取app支付	 */	public function app($order)	{		if (isset($order['prepay_id'])) {			$order['partnerid'] = $this->config->GetMerchantId();			$order['package_string'] = 'Sign=WXPay';			$string = 'appid='.$this->config->GetAppId().'&partnerid='.$order['partnerid'].'&prepayid='.$order['prepay_id'].'&package='.$order['package_string'].'&noncestr='.$order['nonce_str'].'×tamp='.$order['time'];			if($order['sign_type'] == "MD5"){				$string = md5($string);			} else {				$string = hash_hmac("sha256", $string, $this->config->GetKey());			}			$order['sign'] = $string;		}		return $order;	}	/**	 * 获取页面支付	 */	public function page($order, $refer)	{		$refer = urldecode($refer);		$tools = new \JsApiPay($this->config);		$info = $tools->GetJsApiParameters($order);		$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;			case 3:				$type = 'APP';				break;		}		return $type;	}}class Callback extends \WxPayNotify{	public function NotifyProcess($objData, $config, &$msg)	{		$data = $objData->GetValues();		$obj = Dever::load('pay/lib/core');		$obj->log('支付回调-获取数据', $data);		$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("transaction_id", $data)){			$msg = '输入参数不正确';			$callback($msg);			return false;		}		# 参数校验		if(!array_key_exists("return_code", $data) 			||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {			$msg = $data['return_code'] . '(异常)';			$callback($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;	}}
 |