HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/cs.wgegeghx.com/application/pay/controller/Api.php
<?php
// | 支付API接口控制器

namespace app\pay\controller;

use app\common\controller\Homebase;
use app\pay\library\Service;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;
use think\facade\Session;
use think\Response;
use Yansongda\Pay\Pay;
use think\Db;

class Api extends HomeBase
{
    /**
     * 微信支付
     * @return string
     */
    public function wechat()
    {
        $config    = Service::getConfig('wechat');
        $isWechat  = stripos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
        $orderData = Session::get("wechatorderdata");
                    // print_r($orderData);die;
        if ($isWechat) {
            $type = 'jsapi';
            $this->assign("orderData", $orderData);
        } else {
            
            // print_r(Session::get("wechatorderdata"));die;
            
            $return_url="https://".$_SERVER['HTTP_HOST']."";
            
            // echo $return_url;die;
            
            $notify_url="https://".$_SERVER['HTTP_HOST']."/pay/index/epay/type/notify/pay_type/wechat";

// echo $notify_url;die;

// print_r(Session::get(""));die;

                
            
            //新增 微信H5支付
            if($this->isMobiles()){
                
                $appid=$config['app_id'];
                $mch_id=$config['mch_id'];
                $key=$config['key'];
                
                $uid=Session::get("user_auth")['uid'];
                
                $order_sn=date("Ymdhis") . sprintf("%08d", $uid) . mt_rand(1000, 9999);
                
                $orderinfo= Db::name('pay_account')->where('trade_sn',$orderData['out_trade_no'])->find();
                
                
                // print_r($orderinfo);die;
                
                
                    Db::name('pay_account')->where('id', $orderinfo['id'])->update(['trade_sn' => $order_sn]);
                
                    //  Session::set("wechatorderdata", [
                    //     'out_trade_no' => $orderData['out_trade_no'], //你的订单号
                    //     'body'         => $orderData['body'],
                    //     'total_fee'    => $orderData['total_fee'], //单位分
                    //     // "code_url"     => $html->code_url,
                    //     //"code_url" => "weixin://wxpay/bizpayurl?pr=4aXtmGv",
                    // ]);
                
                $this-> wxPayh5($appid,$mch_id,$key,$order_sn,$orderData['total_fee'],$orderData['body'],$notify_url,$return_url);
              
             
             exit;
            }
            
       
            
     
                //发起PC支付(Native支付)
            $data = [
                'body'         => $orderData['body'],
                'code_url'     => $orderData['code_url'],
                'out_trade_no' => $orderData['out_trade_no'],
                'return_url'   => $orderData['return_url'],
                'total_fee'    => $orderData['total_fee'],
            ];
            //检测订单状态
            if ($this->request->isPost()) {
                $pay    = Pay::wechat($config);
                $result = $pay->find($orderData['out_trade_no']);
                if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                    $this->success('', url('pay/index/pay_list'), ['trade_state' => $result['trade_state']]);
                } else {
                    $this->error("查询失败");
                }
            }
            $type = 'pc';
            $this->assign("data", $data);
        }
        $this->assign("type", $type);
        return $this->fetch('/wechat');
             
             
         
            
         
    }

    /**
     * 支付宝支付
     * @return string
     */
    public function alipay()
    {
      
        //   print_r($data);die;
        $config    = Service::getConfig('alipay');
        $orderData = Session::get("alipayorderdata");
        $data      = [
            'body'         => $orderData['body'],
            'qr_code'      => $orderData['qr_code'],
            'out_trade_no' => $orderData['out_trade_no'],
            'return_url'   => $orderData['return_url'],
            'total_fee'    => $orderData['total_fee'],
        ];
        //检测订单状态
        if ($this->request->isPost()) {
            $pay    = Pay::alipay($config);
            $result = $pay->find($orderData['out_trade_no']);
            if (in_array($result['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
                $this->success('', url('pay/index/pay_list'), ['trade_status' => $result['trade_status']]);
            } else {
                $this->error("查询失败");
            }
        }
        
      
        
        $this->assign("data", $data);
        return $this->fetch('/alipay');
    }

    /**
     * 生成二维码
     * @return Response
     */
    public function qrcode()
    {
        $text   = $this->request->get('text', 'hello world');
        $qrCode = new QrCode($text);
        $rs     = $qrCode
            ->setWriterByName('png')
            ->setMargin(10)
            ->setEncoding('UTF-8')
            ->setSize(250)
            /*->setLogoPath(__DIR__ . '/../assets/symfony.png')
        ->setLogoWidth(150)*/
            ->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH) //纠错级别
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255])
            ->setValidateResult(false);
        return new Response($qrCode->writeString(), 200, ['Content-Type' => $qrCode->getContentType()]);
    }
     public  function isMobiles()
    {
           if(isset($_SERVER['HTTP_USER_AGENT'])) {
            $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $clientkeywords = array(
                'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-'
            ,'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
                'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini',
                'operamobi', 'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
            );
            if(preg_match("/(".implode('|',$clientkeywords).")/i",$userAgent)&&strpos($userAgent,'ipad') === false)
            {
                return true;
            }
        }
        return false;
    }
    
public function wxPayh5($appid,$mch_id,$key,$out_trade_no,$money,$body,$notify_url,$return_url){
    //   Session::set("wechatorderdata", []);

    $userip = $_SERVER["REMOTE_ADDR"]; //获得用户设备IP
    $nonce_str=$out_trade_no;//随机字符串
    $total_fee = $money; //金额*100
    //$total_fee = 1;
    $spbill_create_ip = $userip; //IP
    $notify_url = $notify_url; //回调地址 jishu.whwlhd.com/index.php/Home/Pay/wx/id/
    $trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
    $body=$body;
    $out_trade_no=$out_trade_no;
//
    $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://ke.zzyun.com/","wap_name":"支付"}}';//场景信息 必要参数
    $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";

    $strSignTmp = $signA."&key=$key"; //拼接字符串  注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面XML  是否正确
    $sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写

    $post_data="<xml><appid>$appid</appid><body>$body</body><mch_id>$mch_id</mch_id><nonce_str>$nonce_str</nonce_str><notify_url>$notify_url</notify_url><out_trade_no>$out_trade_no</out_trade_no><scene_info>$scene_info</scene_info><spbill_create_ip>$spbill_create_ip</spbill_create_ip><total_fee>$total_fee</total_fee><trade_type>$trade_type</trade_type><sign>$sign</sign>
        </xml>";//拼接成XML 格式

    // print_r($post_data);die;
    // echo $out_trade_no;
    

    $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址

    $dataxml =$this->http_post($url,$post_data);
    
    
    $objectxml = (array)simplexml_load_string($dataxml,'SimpleXMLElement',LIBXML_NOCDATA); //将微信返回的XML 转换成数组

    // print_r($objectxml);die;

  
 
    if($objectxml['return_code'] == 'SUCCESS'){
        $redirect_url = urlencode($return_url);
        
        $url = $objectxml['mweb_url'].'&redirect_url='.$redirect_url;
        echo "<script> window.location.href='$url'</script>";
        exit;

    }else{
        
        
        echo $objectxml['err_code_des'];
        
        
        
    }

}


public function http_post($url='',$post_data=array(),$header=array(),$timeout=30) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
}

}