微信APP支付回调 处理方法 PHP 记录日志方式 ,笔记。2020/5/26

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/23 0023
 * Time: 16:06
 */
function wxQueryPay_1($order){
    $app_id      = 'app_id '; //应用的appid
    $mch_id     = 'mch_id '; // 您的商户账号
    $nonce_str =  nonce_str_1(); //随机字符串
    $out_trade_no = $order['orderid']; //商户订单号
    //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
    $post['appid'] = $app_id;
    $post['mch_id'] = $mch_id;
    $post['nonce_str'] = $nonce_str; //随机字符串
    $post['out_trade_no'] = $out_trade_no;
    $sign = wx_sign_1($post); //签名
    $post_xml = "<xml><appid><![CDATA[$app_id]]></appid><mch_id><![CDATA[$mch_id]]></mch_id><nonce_str><![CDATA[$nonce_str]]></nonce_str><out_trade_no><![CDATA[$out_trade_no]]></out_trade_no><sign><![CDATA[$sign]]></sign></xml>";
    //统一接口prepay_id
    $url = 'https://api.mch.weixin.qq.com/pay/orderquery';
    $xml =wx_http_request_1($url, $post_xml);
    // return $xml;
    $array = wx_xml_1($xml); //全要大写
    // return $array;
    if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
        $data =$array;
    }else {
        $data['status'] = 0;
        $data['text'] = "错误";
        $data['RETURN_CODE'] = $array['RETURN_CODE'];
        $data['RETURN_MSG'] = $array['RETURN_MSG'];
    }
    return $data;
}
///生成签名
function wx_sign_1($data){
    //签名 $data要先排好顺序
    $stringA = '';
    ksort($data);
    foreach($data as $key => $value) {
        if (!$value) continue;
        if ($stringA)
            $stringA.= '&'.$key."=".$value;
        else
            $stringA = $key."=".$value;
    }

    $stringSignTemp = $stringA.'&key='.'key'; //申请支付后有给予一个商户账号和密码,登陆后自己设置key
    return strtoupper(md5($stringSignTemp));
}
function nonce_str_1() {
    $result = '';
    $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
    for ($i = 0; $i < 32; $i++) {
        $result.= $str[rand(0, 48)];
    }
    return $result;
}
function wx_xml_1($xml) {
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xml, $vals, $index);
    xml_parser_free($p);
    $data = "";
    foreach($index as $key => $value) {
        if ($key == 'xml' || $key == 'XML') continue;
        $tag = $vals[$value[0]]['tag'];
        $value = $vals[$value[0]]['value'];
        $data[$tag] = $value;
    }
    return $data;
}

function wx_http_request_1($url, $data = null, $headers = array()) {
    $curl = curl_init();
    if (count($headers) >= 1) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

$payXml = file_get_contents("php://input");
//将xml格式转化为json格式
$jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
//将json格式转成数组格式
$result = json_decode($jsonXml, true);
$myfile     =   "order_log.txt";
$paytype    =   "paytype.txt";
file_put_contents($myfile, $jsonXml.'</br>',FILE_APPEND);
if($result){
    if($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS'){
        $order['orderid'] =$result['out_trade_no'];
        $rs = wxQueryPay_1($order);
        file_put_contents($paytype, json_encode($rs).'</br>',FILE_APPEND);
        if($rs['TRADE_STATE_DESC'] =='支付成功'){
            $servername = "0.0.0.0";///数据库ip地址
            $username = "username";    ///数据库用户名
            $password = "password"; 数据库密码
            $tablename ="tablename";  数据库表名
            // 创建连接
            $link = mysqli_connect($servername,$username,$password,$tablename) or die(mysqli_errno());
            //处理数据库
            $order_sql ='select * from cmf_users_charge where orderno = "'.$rs['out_trade_no'].'";';
            $order_type =mysqli_query($link,$order_sql);
            $order_result = mysqli_fetch_array($order_type);
            $user_sql  ='select * from cmf_users where id = "'.$order_result['touid'].'";';
            $user_type =mysqli_query($link,$user_sql);
            $user_result = mysqli_fetch_array($user_type);
            if($order_result['power']==4){
                $order_status_sql ='UPDATE cmf_users_charge SET status = 1 where orderno =' .$rs['out_trade_no'].'; ';
                mysqli_query($link,$sql);
                $coin = $user_result['coin']+$order_result['coin'];
                $coin_sql = 'UPDATE cmf_users SET coin =.'.$coin.' where id =' .$order_result['touid'].'and banlc_type = 0; ';
                mysqli_query($link,$coin_sql);
                $banle_type = 'UPDATE cmf_users_charge SET banlc_type = 1 where orderno =' .$rs['out_trade_no'].'; ';
                mysqli_query($link,$banle_type);
            }
            mysqli_close($link);
        }
///处理微信回调重复
        $return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
        $xml_t = '<xml>';
        foreach($return as $k=>$v){
            $xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
        }
        $xml_t.='</xml>';
        echo $xml_t;
    }else{
        $return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
        $xml_t = '<xml>';
        foreach($return as $k=>$v){
            $xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
        }
        $xml_t.='</xml>';
        echo $xml_t;

    }
}else{
    $return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
    $xml_t = '<xml>';
    foreach($return as $k=>$v){
        $xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
    }
    $xml_t.='</xml>';
    echo $xml_t;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值