123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use app\common\service\wechat\JsApi_pub;
- use app\common\service\wechat\UnifiedOrder_pub;
- use app\common\service\wechat\Refund_pub;
- use think\Config;
- use think\Validate;
- use think\Log;
- /**
- * 微信支付接口
- * Class WeChat
- * @package app\expand\controller
- */
- class WechatPay extends BaseAuth
- {
- private $_Account = null;
- private $_APPID = null;
- private $_APPSECRET = null;
- public function __construct(){
- parent::__construct();
-
- $this->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']) ){
- 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'];
- $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_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]);
- }
-
- }
|