2020年8月

获取异步通知数据

$weixin = file_get_contents('php://input');

解析数据 xml转数组

$obj = simplexml_load_string($weixin,"SimpleXMLElement", LIBXML_NOCDATA);
$data = json_decode(json_encode($obj),true);

notify返回的数据

A、支付成功的异步通知

Array (
[appid] => appid
[bank_type] => CCB_DEBIT
[cash_fee] => 1
[fee_type] => CNY
[is_subscribe] => N
[mch_id] => mch_id
[nonce_str] => 5f48558fa41a7
[openid] => o6Yya08PrSfOFS8DsCmBJ_RoURwc
[out_trade_no] => 20200828085335873434
[result_code] => SUCCESS
[return_code] => SUCCESS
[sign] => 2A23DE00DD138E553288F3DBCC49E912
[time_end] => 20200828085403
[total_fee] => 1
[trade_type] => NATIVE
[transaction_id] => 4200000721202008289823097889
)

查询订单的支付状态

A、扫描未支付

Array (
[return_code] => SUCCESS
[return_msg] => OK
[appid] => appid
[mch_id] => mch_id
[device_info] =>
[nonce_str] => GR8mjKXt8tuNsu81
[sign] => 4E348373A478E752400F3593E27C9DAB
[result_code] => SUCCESS
[total_fee] => 1
[out_trade_no] => 20200828150554884809
[trade_state] => NOTPAY
[trade_state_desc] => 订单未支付
)

B、扫描支付成功的

Array (
[return_code] => SUCCESS
[return_msg] => OK
[appid] => appid
[mch_id] => mch_id
[nonce_str] => t4pCO8Nvmr7uqOx0
[sign] => 3876BA2223A99B066787F1D9A551028E
[result_code] => SUCCESS
[openid] => o6Yya076u3kY9ExdgIpi-SDXZOsc
[is_subscribe] => N
[trade_type] => NATIVE
[bank_type] => OTHERS
[total_fee] => 1
[fee_type] => CNY
[transaction_id] => 4200000719202008288009765782
[out_trade_no] => 20200828093538226231
[attach] =>
[time_end] => 20200828095549
[trade_state] => SUCCESS
[cash_fee] => 1
[trade_state_desc] => 支付成功
[cash_fee_type] => CNY
)

 //回调校验
public  function  notify(){
        //微信来的通知
        $weixin = file_get_contents('php://input');
        //$weixin ="<xml><appid><![CDATA[APPID]]></appid>\n<bank_type><![CDATA[OTHERS]]></bank_type>\n<cash_fee><![CDATA[1]]></cash_fee>\n<fee_type><![CDATA[CNY]]></fee_type>\n<is_subscribe><![CDATA[N]]></is_subscribe>\n<mch_id><![CDATA[MCHID]]></mch_id>\n<nonce_str><![CDATA[5f485f6a36efc]]></nonce_str>\n<openid><![CDATA[o6Yya076u3kY9ExdgIpi-SDXZOsc]]></openid>\n<out_trade_no><![CDATA[20200828093538226231]]></out_trade_no>\n<result_code><![CDATA[SUCCESS]]></result_code>\n<return_code><![CDATA[SUCCESS]]></return_code>\n<sign><![CDATA[1808F30039789D9B7A47E84C390AF304]]></sign>\n<time_end><![CDATA[20200828095549]]></time_end>\n<total_fee>1</total_fee>\n<trade_type><![CDATA[NATIVE]]></trade_type>\n<transaction_id><![CDATA[4200000719202008288009765782]]></transaction_id>\n</xml>";
       //解析微信传来的参数
       $obj = simplexml_load_string($weixin,"SimpleXMLElement", LIBXML_NOCDATA);
       $data = json_decode(json_encode($obj),true);
       //我收到通知了
       $paydata=array();
       if($data['return_code']=='SUCCESS'){
              //我检查我的库有没有
              $out_trade_no=$data['out_trade_no'];
              $pay_count=D('pay_lists')->where(array('order_id'=>$out_trade_no))->find();
              //原来我有此订单 
              if(count($pay_count)>0){
                    //我还要查询下订单 校验是否一致
                    $sign_check=$this->app->order->queryByOutTradeNumber($out_trade_no);
                    if($sign_check['result_code']=='SUCCESS'&&$data['result_code']=='SUCCESS'){
                        //金额校对结果
                        $amount =$pay_count['amount']*100; //订单的单位是元 而微信这里是分
                        if($data['total_fee']=$sign_check['total_fee']=$amount){
                          //三方校对都合法了 是时候改变业务数据的支付状态了
                          $paydata['pay_status'] = 1;
                          $paydata['pay_time'] = time();
                          $paydata['remark']=$sign_check['trade_state_desc'];
                        }else{  
                          $paydata['pay_status'] = 2;
                          $paydata['remark']='支付失败:金额不一致';
                        }
                    }else{
                           $paydata['pay_status'] = 2;
                           $paydata['remark']='支付失败:返回失败状态';
                    }
                    //处理完业务告诉微信
                    $result=D('pay_lists')->where(array('order_id'=>$out_trade_no))->save($paydata);
                    if($result){
                       return true;
                    }else{
                       return false;
                    }
             }else{
               //没有这个订单了 不再发消息来了
               return true;
             }
       }else{
         return false; //没接收到 下次继续通知我
       }
}

使用thinkphp3.2做图片流数据返回的时候要特别注意别用json,json的返回有长度限制,会将(data:image/png;base64,...)里面的data rn换行,从而导致图片无法解析。
错误做法:
$this->Ajaxreturn($img,'json')
正确做法:
$this->Ajaxreturn($img)

git仓库地址

https://github.com/endroid/qr-code

composer安装

$ composer require endroid/qr-code

实例:

<?php
use Endroid\QrCode\QrCode;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\WriterRegistryInterface;
//代码片段
public function erweima(){
    $text='hello';
    $qrCode = new QrCode($text);
    $qrCode->setSize(300);
    // Set advanced options
    $logo=APP_ROOT.'/logo.jpg';
    $qrCode->setWriterByName('png');
    $qrCode->setMargin(10);
    $qrCode->setEncoding('UTF-8');
    $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
    $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 10]);
    //加logo
    $qrCode->setLogoPath($logo);
    $qrCode->setLogoSize(80, 80);
    $qrCode->setRoundBlockSize(true);
    $qrCode->setValidateResult(false);
    $qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
    //返回二进制流
    $img=$qrCode->writeString();
    $imginfo = 'data:png;base64,' . chunk_split(base64_encode($img));//转base64
    //echo "<img src='$imginfo'>";
    return  $imginfo;
}

?>

public function delete(){
        // 启动事务
        Db::startTrans();
        try{
            $res=Db::table('user')->delete(1039);
            if(!$res){
                throw new \Exception('删除失败');
            }else{
                throw new \Exception('删除成功');
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
             dump($e->getMessage());
            // 回滚事务
            Db::rollback();
            //注意:我们做了回滚处理,所以id为1039的数据还在
        }
    }

composer config -g repos.packagist composer https://mirrors.tencent.com/composer/  //腾讯云
composer config repo.packagist composer https://mirrors.aliyun.com/composer/   //阿里云

//合并两个一维数组,去掉重复元素
function arraymerge($arr1=array(),$arr2=array(),$returnarr=array()){
        if(!empty($arr1) && is_array($arr1)){
            foreach($arr1 as $key=>$val){
                $returnarr[] = $val;
            }
        }
        if(!empty($arr2) && is_array($arr2)){
            if(!empty($returnarr)){
                foreach($arr2 as $k=>$v){
                    $flag = false;
                    foreach($returnarr as $ke=>$va){
                        if($va == $v){
                            $flag = true;
                        }
                    }
                    if(!$flag){
                        $returnarr[] = $v;
                    }
                }
                
            }else{
                if(!empty($arr2) && is_array($arr2)){
                    foreach($arr2 as $key2=>$val2){
                        $returnarr[] = $val2;
                    }
                }
            }
        }
        return $returnarr;
}