thinkPHP6.0 微信付款码支付和查询支付状态

目录

  • 付款码支付
    • post请求
    • 随机数
    • 商户号
    • XML数据转换诚数组
    • 数组转换为XML格式
    • 生成签名
  • 查询订单支付状态

付款码支付

 /**
     * Notes:  付款码支付
     * Author: Seven
     * Method: getScanCodePayment
     * Date: 2023/7/8: 0:20
     * @param $nonce_str [随机字符串]
     * @param $out_trade_no [订单编号唯一]
     * @param $spbill_create_ip[获得用户设备IP]
     * @param $auth_code[付款码]
     * @param $money[付款金额]
     */
   public function getScanCodePayment($nonce_str,$out_trade_no,$spbill_create_ip,$auth_code,$money)
    {
        $baseConfigModel = new \app\admin\model\Baseconfig();
        $app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');
        $mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');
        $payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');

        $data = [
            'appid' => $app_id,
            'mch_id' => $mch_id,
            'nonce_str' => $nonce_str,//随机字符串
            'body' => '云舍-按摩',//商品简单描述
            'out_trade_no' => $out_trade_no,
            'total_fee' => 1,//订单总金额,单位为分,只能为整数
//            'total_fee' => $money * 100,//订单总金额,单位为分,只能为整数
            'spbill_create_ip' => $spbill_create_ip,//获得用户设备IP
            'auth_code' => $auth_code,
            'sign_type' => 'MD5',
        ];
        $data['sign'] = $this->getSign($data, $payment_key);//生成密钥
        $xml = $this->ToXml($data);//转换xml格式
        $url = "https://api.mch.weixin.qq.com/pay/micropay";        //微信传参地址
        return $this->post_url($url,$xml);
    }

post请求

 /**
     * Notes:  post请求
     * Author: Seven
     * Method: post_url
     * Date: 2023/7/8: 14:43
     * @param $url
     * @param $xml
     * @return mixed
     */
    public function post_url($url,$xml){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);                                    //设置超时
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);                                //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);                //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, TRUE);                                        //POST提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $dataxml = curl_exec($ch);                                                            //运行curl
        if (!$dataxml) {
            $error = curl_errno($ch);
            throw new ValidateException('付款失败,错误码:'.$error);
        }
        curl_close($ch);
        return $this->FromXml($dataxml);
    }

随机数

    /**
     * Notes:  随机字符串
     * Author: Seven
     * Method: order_number
     * Date: 2023/6/25: 21:38
     * @param $order_no
     * @return string
     */
    function generate_password($length = 32)
    {
        // 密码字符集,可任意添加你需要的字符
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $password = "";
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }

商户号

    /**
     * Notes:  商户订单号
     * Author: Seven
     * Method: order_number
     * Date: 2023/6/25: 21:42
     * @param $order_no
     * @return string
     */
    function order_number($order_no)
    {
        return $order_no . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 14);
    }

XML数据转换诚数组

    /**
     * Notes   (备注):将xml数据转换为数组
     * Author  (作者):seven
     * Date    (时间):2022/9/14 15:01
     * Name    (名称):FromXml
     * @param $xml
     * @return mixed
     */
    public function FromXml($xml)
    {
        if (!$xml) {
            echo "xml数据异常!";
        }
        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }

数组转换为XML格式

    /**
     * Notes: 转化为XML格式
     * Author: Seven
     * Method: ToXml
     * Date: 2023/6/25: 22:16
     * @param array $data
     * @return string
     */
    public function ToXml($data = array())
    {
        if (!is_array($data) || count($data) <= 0) {
            return '数组异常';
        }
        $xml = "";
        foreach ($data as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . " . $key . ">";
            } else {
                $xml .= "<" . $key . "> . $val . "]]> . $key . ">";
            }
        }
        $xml .= "";
        return $xml;
    }

生成签名

    /**
     * Notes   (备注):生成签名
     * Author  (作者):seven
     * Date    (时间):2022/9/14 14:59
     * Name    (名称):getSign
     * @param $params
     * @return string
     */
    public function getSign($params, $key1)
    {
        ksort($params); //将参数数组按照参数名ASCII码从小到大排序
        foreach ($params as $key => $item) {
            if (!empty($item)) {  //剔除参数值为空的参数
                $newArr[] = $key . '=' . $item; // 整合新的参数数组
            }
        }
        $stringA = implode("&", $newArr);  //使用 & 符号连接参数
        $stringSignTemp = $stringA . "&key=" . $key1; //拼接key
        // key是在商户平台API安全里自己设置的
        $stringSignTemp = MD5($stringSignTemp); //将字符串进行MD5加密
        $sign = strtoupper($stringSignTemp); //将所有字符转换为大写
        return $sign;
    }

查询订单支付状态

 /**
     * Notes:  查询订单支付
     * Author: Seven
     * Method: getViewOrder
     * Date: 2023/7/8: 0:38
     * @param $out_trade_no[订单号]
     * @return mixed
     */
    public function getViewOrder($out_trade_no){
        $baseConfigModel = new \app\admin\model\Baseconfig();
        $app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');
        $mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');
        $payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');
        $data = [
            'appid' => $app_id,
            'mch_id' => $mch_id,
            'out_trade_no' => $out_trade_no,
            'nonce_str' => $this->generate_password(),//随机字符串
            'sign_type' => 'MD5',
        ];
        $data['sign'] = $this->getSign($data, $payment_key);
        $xml = $this->ToXml($data);
        $url = "https://api.mch.weixin.qq.com/pay/orderquery";        //微信传参地址
        return $this->post_url($url,$xml);
    }

你可能感兴趣的:(微信,后端,php)