Erlo

php对微信支付回调处理的方法(合集)

2019-07-25 17:08:39 发布   850 浏览  
页面报错/反馈
收藏 点赞

微信无论是微信内置JSAPI支付、H5外部浏览器支付、扫码支付,都需要通过异步回调接收支付结果。

本文简介如何获取微信支付通知。

仅需要一个在之前设置好的回调地址的方法里写上如下:

//处理微信支付回调
    public function notify(){
        $testxml  = file_get_contents("php://input");
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组,
        if($result){
            //如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
            //执行业务逻辑
          //查询创建订单表 where("out_trade_no='".$out_trade_no."' and status=1") status为1表示待支付状态 1 待支付
          //查询出来有该订单 就改变支付状态 status=2 2表示支付成功
 
            }
        }
    }

对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 
(通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒)    
    
结束微信重新通知用代码:echo 'SUCCESS';

附上:
微信支付返回的xml转化为json格式如下: 

{
  "appid": "12345",
  "attach": "pay", 
  "bank_type": "CFT",
  "cash_fee": "1",
  "fee_type": "CNY",
  "is_subscribe": "Y",
  "mch_id": "12345",
  "nonce_str": "dZYFpaDYRpF5rwhv",
  "openid": "onhwF1hiutUySKCsrV21A6MCtT5Q",
  "out_trade_no": "SH201808222055598628",
  "result_code": "SUCCESS",
  "return_code": "SUCCESS",
  "sign": "5A019F52BEF1C3A98AE0F1FF29D01574",
  "time_end": "20180822205606",
  "total_fee": "1",
  "trade_type": "MWEB",
  "transaction_id": "4200000171201808221550954201" 
}

 

//详细流程如下

public function wycz(){//我要充值
        if(session('uid') ==NULL || session('uid') == "" || session('uid') == false){
            //没有登录 跳转到登录页
            $this->redirect('/Login/login');exit;
       }else {
            //接受参数
            if($_POST){
                $Total_fee = trim(I("post.fotal_fee"));
                if(!empty($Total_fee)){
                    if($Total_fee){
                        //提示输入正整数金额
                        $arr['code'] = "301";
                        $arr['msg']  = "输入正整数";
                        echo json_encode($arr);exit();
                    }else{

                        session("total_fee",$Total_fee);
                        $arr['code'] = "200";
                        $arr['msg'] = "确认充值吗?";
                        echo json_encode($arr);exit();
                    }
                }else{
                    //提示输入正整数金额
                    $arr['code'] = "302";
                    $arr['msg']  = "输入充值金额";
                    echo json_encode($arr);exit();
                }
            }
            $this->display();
       }
    }

    //充值确认页面
    public function confirmPay(){
        if(session('uid') ==NULL || session('uid') == "" || session('uid') == false){
            //没有登录 跳转到登录页
            $this->redirect('/Login/login');exit;
        }else {
            $result = $this->wapPay();
            $this->assign('jsApiParameters',$result['jsApiParameters']);
            $this->assign('editAddress', $result['editAddress']);
            $this->display();
        }
    }
    //操作充值
    public function wapPAy($Total_fee)
    {
        /**
         *
         * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
         * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
         * 请勿直接直接使用样例对外提供服务
         *
         **/
        Vendor('Wxpaysdk.lib.WxPay#Api');
        Vendor('Wxpaysdk.example.WxPay#JsApiPay');
        // Vendor('Wxpaysdk.example.WxPay#Config');
        //①、获取用户openid
        $tools = new JsApiPay();
        $openId = $tools->GetOpenid();
        $Total_fee = session("total_fee")*100;
        //$Total_fee = session("total_fee");
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody("vip充值");
        $input->SetAttach("vip充值");
        $input->SetOut_trade_no("klkq".date("YmdHis"));
        $input->SetTotal_fee($Total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("vip充值");
        //$input->SetNotify_url("http://paysdk.weixin.qq.com/notify.php");
        $input->SetNotify_url("http://wap.yuming.com/Member/notify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new WxPayConfig();
        $order = WxPayApi::unifiedOrder($config, $input);
        //echo '统一下单支付单信息
'; //printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order); //获取共享收货地址js函数参数 $editAddress = $tools->GetEditAddressParameters(); $data['jsApiParameters'] = $jsApiParameters; $data['editAddress'] = $editAddress; //写入订单表对应数据 $arr = array( 'w_mId'=>session('uid'), 'w_openid'=>$openId, 'w_title'=>'vip充值', 'w_time_create'=>date("YmdHis"), 'w_times_create'=>date("Y-m-d H:i:s"), 'w_out_trade_no'=>"klkq".date("YmdHis"), ); M('wxpay_order')->add($arr); return $data; } //处理微信支付回调 public function notify(){ $testxml = file_get_contents("php://input"); $jsonxml = json_encode(simplexml_load_string($testxml,'SimpleXMLElement',LIBXML_NOCDATA)); $result = json_decode($jsonxml,true); if($result){ $out_trade_no = $result['out_trade_no']; if($result['return_code'] == 'SUCCESS' && $result['result_code'] =='SUCCESS'){ //先判断订单状态是否已经改变 $orderData = M("wxpay_order")->where("w_out_trade_no='".$result['out_trade_no']."' and w_status=1")->find(); if(!empty($orderData)){ //支付成功改变支付状态 $arr = array( 'w_id'=>$orderData['w_id'], 'w_openid'=>$result['openid'], 'w_transaction_id'=>$result['transaction_id'], 'w_time_end'=>$result['time_end'], 'w_times_end'=>$result['time_end'], 'w_total_fee'=>$result['total_fee']/100, 'w_status'=>2,//支付成功 1 待支付 2支付成功 3 支付失败 ); M('wxpay_order')->save($arr); //操作积分 $uid = $orderData['w_mId']; //对该人增加$result['total_fee']积分 $zz_grade = M('grade'); $g_Integral = $zz_grade->where("g_mId=".$uid)->getField('g_Integral'); $zz_grade->g_Integral = $g_Integral + intval($result['total_fee']/100); //更新积分 $giddd = $zz_grade->where("g_mId=".$uid)->save(); //操作积分日志表 $zz_grade_log = M('grade_log'); $zz_grade_log->g_gContent = '充值'.intval($result['total_fee']/100).'积分'; $zz_grade_log->g_mId = $uid;//邀请者会员id $zz_grade_log->g_gNum = intval($result['total_fee']/100);//充值积分 $zz_grade_log->g_gDate = date('Y-m-d');//注册日期 $zz_grade_log->g_gTime = date('Y-m-d H:i:s');//注册时间 //写入数据库 $liddd = $zz_grade_log->add(); } } } }

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认