微信公众号支付

一、设置支付目录

请确保实际支付时的请求目录与后台配置的目录一致(现在已经支持配置根目录,配置后有一定的生效时间,一般5分钟内生效),否则将无法成功唤起微信支付。

在微信商户平台(pay.weixin.qq.com)设置您的JSAPI支付支付目录,设置路径:商户平台-->产品中心-->开发配置,如图7.7所示。JSAPI支付在请求支付的时候会校验请求来源是否有在商户平台做了配置,所以必须确保支付目录已经正确的被配置,否则将验证失败,请求支付不成功。

微信公众号支付_第1张图片

图7.7 微信JSAPI支付-支付目录配置

二、设置授权域名

开发JSAPI支付时,在统一下单接口中要求必传用户openid,而获取openid则需要您在公众平台设置获取openid的域名,只有被设置过的域名才是一个有效的获取openid的域名,否则将获取失败。具体界面如图7.8所示:

微信公众号支付_第2张图片微信公众号支付_第3张图片

图7.8 微信网页授权域名设置
 
 

三、代码

 $appid,    //appid T
            'mch_id'           => $mchid,    //商户号T
            'nonce_str'        => $nonce_str,
            'body'             => '可待商城系统-商品购买',   //商品信息
            'out_trade_no'     => $orsid,     //订单号
            'total_fee'        => $price,   //价格,微信支付价格会处以一百
            'spbill_create_ip' => self::getip(),
            'notify_url'       => config('api_url') . '/wxnotify',
            'trade_type'       => 'APP',    //小程序,app,之类的选项不一样
        ];


        $data['sign'] = self::makeSign($data, $oid);    //制作签名

        $xmldata = self::array2xml($data);    //传入参数转换成xml格式
        $url     = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $res     = self::curl_post_ssl($url, $xmldata);   //curl发出请求

        if (!$res) {
            return ['status' => 1, 'msg' => "服务器连接失败"];
        }


        $content = self::xml2array($res);   //签名失败时,打印该数据

        //
        if (strval($content['return_code']) == 'FAIL') {
            return json(['status' => 1, 'msg' => strval($content['return_msg'])]);
        }

        if (strval($content['result_code']) == 'FAIL') {
            return json(['status' => 1, 'msg' => strval($content['err_code']), ':' . strval($content['err_code_des'])]);
        }

        //第二次签名,把第一次签名成功微信传过来的数据参数加入第二次签名,prepayid
        $timest = (string)time();

        $resdata = [
            'appid'     => $appid,
            'partnerid' => $mchid,
            'prepayid'  => strval($content['prepay_id']),
            'package'   => 'Sign=WXPay',
            'noncestr'  => self::getNonceStr(),
            'timestamp' => $timest,
        ];

        $resdata['sign'] = self::makeSign($resdata, $oid);   //T同第一次,使用当前时间和随机字符串获取签名
        return json(['data' => $resdata, 'code' => 1]); //把数据传给前端,调起支付

    }

    //回调地址(逻辑处理)
    public function wxnotify()
    {
        $notify     = file_get_contents("php://input");
        $array      = $this->xml2array($notify); //微信回调数据是xml格式,转化成数组
        $order_num  = $array['out_trade_no'];
        $paynumber  = $array["transaction_id"]; //交易流水号
        $price      = $array["total_amount"]; //支付金额
        //$price      = 0.01; //支付金额
  
    }
    //制作字符串
    public function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    //制作签名,需要支付密钥
    public function makeSign($data, $oid = 0)
    {
        //获取微信支付秘钥
        $key = PayConfig::get_wx_config_info('wxkey', $oid);;
        // 去空
        $data = array_filter($data);
        //签名步骤一:按字典序排序参数
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        //签名步骤二:在string后加入KEY
        //$config=$this->config;
        $string_sign_temp = $string_a . "&key=" . $key;
        //签名步骤三:MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四:所有字符转为大写
        $result = strtoupper($sign);
        return $result;
    }

    //获得用户IP
    public function getip()
    {
        static $ip = '';
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CDN_SRC_IP'])) {
            $ip = $_SERVER['HTTP_CDN_SRC_IP'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] as $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }

    /**
     * 将一个数组转换为 XML 结构的字符串
     * @param array $arr 要转换的数组
     * @param int $level 节点层级, 1 为 Root.
     * @return string XML 结构的字符串
     */
    public function array2xml($arr, $level = 1)
    {
        $s = $level == 1 ? "" : '';
        foreach ($arr as $tagname => $value) {
            if (is_numeric($tagname)) {
                $tagname = $value['TagName'];
                unset($value['TagName']);
            }
            if (!is_array($value)) {
                $s .= "<{$tagname}>" . (!is_numeric($value) ? '' : '') . "";
            } else {
                $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1) . "";
            }
        }
        $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
        return $level == 1 ? $s . "" : $s;
    }

    /**
     * 将xml转为array
     * @param  string   $xml xml字符串
     * @return array    转换得到的数组
     */
    public function xml2array($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(false);
        $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }

    //用于支付
    public function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array())
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
        $data = curl_exec($ch);
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }

    //数组转xml
    function arrayToXml($arr)
    {
        $xml = "";
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $xml .= "<" . $key . ">" . arrayToXml($val) . "";
            } else {
                $xml .= "<" . $key . ">" . $val . "";
            }
        }
        $xml .= "";
        return $xml;
    }
}

  

你可能感兴趣的:(微信公众号支付)