getFileContext(); /** * 当且仅当微信异步回调地址发起请求的的时候,放他通过 */ if($this->_inWhiteList){ return true; } $this->_Account = $this->getKey($this->_apiCode); //验证是否具有访问这个接口的权限 if(!isset($this->_Account['Wechat_pay_appId']) || !isset($this->_Account['Wechat_pay_appsecret']) || !isset($this->_Account['Wechat_pay_key']) || !isset($this->_Account['Wechat_pay_mchId']) || !isset($this->_Account['apiclientCert']) || !isset($this->_Account['apiclientKey']) ){ HelperService::returnJson(['code'=>400,'msg'=>'WECHAT pay interface unauthorized access','data'=>$this->_Account]); } $this->_APPID = $this->_Account['Wechat_pay_appId']; $this->_APPSECRET = $this->_Account['Wechat_pay_appsecret']; $this->_apiclientCert = $this->_Account['apiclientCert']; $this->_apiclientKey = $this->_Account['apiclientKey']; $WechatPayTip = isset($this->_Account['Wechat_pay_tip']) ?$this->_Account['Wechat_pay_tip']:""; $WechatPaySubMchId = isset($this->_Account['Wechat_pay_sub_mchId']) ?$this->_Account['Wechat_pay_sub_mchId']:""; Config::set('WECHAT_APPID',$this->_APPID); Config::set('WECHAT_APPSECRET',$this->_APPSECRET); Config::set('WECHAT_CERT',$this->_apiclientCert); Config::set('WECHAT_KEY',$this->_apiclientKey); Config::set('WECHAT_TIP',$WechatPayTip); Config::set('WECHAT_MCHID',$this->_Account['Wechat_pay_mchId']); Config::set('WECHAT_SUB_MCHID',$WechatPaySubMchId); Config::set('WECHAT_PAY_KEY',$this->_Account['Wechat_pay_key']); } /** * 平台版支付接口 */ public function platformWxPay(){ $param = $this->_sysParams; $rule = [ 'pay_notify_url|支付通知跳转'=>'max:1000', 'order_no|订单编号'=>'require', 'total_price|总金额'=>'require' ]; // Log::record("platformWxPay:".$param); $validate = new Validate($rule); if(!$validate->check($param)){ HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]); } session('pay_notify_url',$param['pay_notify_url']); session('order_no',$param['order_no']); session('total_price',$param['total_price']); require_once(APP_PATH.'/common/service/wechat/WxPayPubHelper.php'); //使用jsapi接口 $jsApi = new JsApi_pub(); //通过code获得openid,通知的接口 $httpFix = HelperService::getHttpHeader(); $url = $jsApi->createOauthOpenidForCode("{$httpFix}{$_SERVER['HTTP_HOST']}/v1/getOpenId2Pay/apiCode/".$this->_apiCode, rand(1000,9999)); HelperService::addLog([ 'data'=>$param, 'url'=>$url, ]); header("Location: $url"); exit; } /** * 支付获取openid之后跳转的地址 */ public function getOpenId2Pay(){ //使用jsapi接口 require_once(APP_PATH.'/common/service/wechat/WxPayPubHelper.php'); $jsApi = new JsApi_pub(); $code = $this->_sysParams['code']; $this->_apiCode = $this->_sysParams['apiCode']; $account = $this->getKey($this->_apiCode, false); Config::set('WECHAT_APPID',$account['Wechat_pay_appId']); Config::set('WECHAT_APPSECRET',$account['Wechat_pay_appsecret']); Config::set('WECHAT_MCHID',$account['Wechat_pay_mchId']); Config::set('WECHAT_PAY_KEY',$account['Wechat_pay_key']); $jsApi->setCode($code); $openInfo = $jsApi->getOpenidInfo(); HelperService::addLog([ 'openInfo'=>$openInfo, 'code'=>$code, ]); if(!isset($openInfo['openid'])){ $notify_url = base64_decode(session('pay_notify_url')); header("HTTP/1.1 301 Moved Permanently"); header("Location: $notify_url"); exit; } $openid = $openInfo['openid']; $unifiedOrder = new UnifiedOrder_pub(); $money = intval(session('total_price')); if($money<=0){ header("HTTP/1.1 500 支付调用失败(支付金额为0)"); exit; } //设置统一支付接口参数 $unifiedOrder->setParameter("openid", "$openid"); //商品描述 $unifiedOrder->setParameter("body",Config::get('WECHAT_TIP')."[".date('Y-m-d H:i:s')."]"); //商品描述 //自定义订单号,此处仅作举例 $out_trade_no = $this->_apiCode."_".session('order_no'); $unifiedOrder->setParameter("out_trade_no", $out_trade_no); //商户订单号 $unifiedOrder->setParameter("total_fee", $money); //总金额 $httpFix = HelperService::getHttpHeader(); $unifiedOrder->setParameter("notify_url", "{$httpFix}{$_SERVER['HTTP_HOST']}/v1/platformNotifyUrl.html"); //通知地址 $unifiedOrder->setParameter("trade_type", "JSAPI"); //交易类型 $prepay_id = $unifiedOrder->getPrepayId($out_trade_no."_".time()); $jsApi->setPrepayId($prepay_id); $jsApiParameters = $jsApi->getParameters(); $notify_url = session('pay_notify_url')?base64_decode(session('pay_notify_url')):"/v1/paySuccess"; HelperService::addLog([ 'data'=>$prepay_id, 'url'=>$notify_url, ]); require_once(APP_PATH.'/expand/view/wechat/wxPlatformPay.php'); } public function paySuccess(){ return $this->fetch('wechat/paySuccess'); } /** * 支付异步通知接口 */ public function platformNotifyUrl(){ $this->getInput(); $param = $this->_oldParams; $params = HelperService::object2Arr(simplexml_load_string($param, 'SimpleXMLElement', LIBXML_NOCDATA)); if(empty($params)){ HelperService::returnJson(['code' => 400, 'msg' => "参数错误"]); } try { $arr = ['total_fee', 'out_trade_no']; foreach ($arr as $key=>$value) { if (!isset($params[$value])) { HelperService::returnJson(['code' => 400, 'msg' => "参数错误({$key})",'params'=>$params]); } } $tradeNoArr = explode('_', $params['out_trade_no']); if(count($tradeNoArr)<2){ HelperService::returnJson(['code' => 400, 'msg' => "参数错误(trade)"],$param); } $companyCode = $tradeNoArr[0]; $this->_Account = $this->getKey($companyCode,false); $orderNo = isset($tradeNoArr[1]) ? $tradeNoArr[1] : 0; //开发分发给不同的支付主体 if(!isset($this->_Account['Wechat_pay_notify_url']) || empty($this->_Account['Wechat_pay_notify_url'])){ HelperService::returnJson(['code' => 400, 'msg' => "回调地址不存在"],$param); } if(!is_array($this->_Account['Wechat_pay_notify_url'])){ HelperService::returnJson(['code' => 400, 'msg' => "回调地址不是数组"],$param); } $notifyUrlArr = $this->_Account['Wechat_pay_notify_url']; foreach($notifyUrlArr as $url){ $is_ssl = strpos($url, 'https://')!==false?true:false; $data = [ 'order_no'=>$orderNo, 'total_price'=>"{$params['total_fee']}", 'out_trade_no'=>"{$params['out_trade_no']}" ]; $times = 3; while($times--){ $res = HelperService::httpPost($url,json_encode($data),$is_ssl); HelperService::addLog(['item'=>$url,'data'=>$data,'result'=>$res]); if($res != false){ break; } } if($times ==0){ Log::record("支付推送异常=>url:".$url."=>res:". json_encode($res)."=>data:". json_encode($data)); } } HelperService::addLog(['ret'=>'success']); die('success'); }catch (\Exception $ex){ HelperService::addLog(['msg'=>$ex->getMessage(),"file"=>$ex->getFile(),"line"=>$ex->getLine()]); die('fail'); } } /** * 平台版退款接口 */ public function platformRefund(){ $params = $this->_params; $rule = [ 'out_trade_no'=>'require', 'total_fee'=>'require', 'refund_fee'=>'require', 'op_user_id'=>'require' ]; $validate = new Validate($rule); if(!$validate->check($params)){ HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()],$params); } require_once(APP_PATH.'/index/service/wechat/WxPayPubHelper.php'); $Refund = new Refund_pub(); $Refund->setParameter('out_trade_no',$params['out_trade_no']); $Refund->setParameter('out_refund_no',uniqid()); $Refund->setParameter('total_fee',$params['total_fee']); $Refund->setParameter('refund_fee',$params['refund_fee']); $Refund->setParameter('op_user_id',$params['op_user_id']); $data = $Refund->getResult(); $msg = isset($data['result_code'])?$data['result_code']:'fail'; HelperService::returnJson(['code'=>200,'msg'=>"{$msg}",'data'=>$data]); } }