支付宝支付类

功能说明:

集成了当面付、H5、PC、查询订单、退款订单、异步通知校验等功能

部署说明:

将类文件解压至phpgrace/tools文件夹下即可。  

https://sharewh1.xuexi365.com/share/download/5e918484da2c4e5a50d36a6f82e45f73  

使用说明:

1.当面付qrcodePay($outTradeNo,$orderName,$payAmount,$notifyUrl)
  参数:
      $outTradeNo=商户订单号,$orderName=订单名称,
      $payAmount=付款金额,$notifyUrl=异步通知地址
  📢注意:当面付 建议用轮询方式,不太建议用异步,$notifyUrl 参数可以为""空
  
2.PC支付pcPay($outTradeNo,$orderName,$payAmount,$notifyUrl,$returnUrl)
  参数:同当面付,$returnUrl=支付成功后跳转地址
3.H5支付h5Pay($outTradeNo,$orderName,$payAmount,$notifyUrl,$returnUrl)参数:同PC支付
4.异步校验rsaCheck($_POST,$_POST['sign_type'])参数:POST数据

5.交易查询PayQuery($orderNo,$tradeNo=false)
  参数:
      $orderNo=订单号(商户订单号或支付宝交易号),
      $tradeNo=如果是支付宝交易号设置true,默认false商户订单
  
6.交易退款PayRefund($refundAmount,$orderNo,$tradeNo=false)
  参数:$refundAmount=退款金额,
       $orderNo=订单号(商户订单号或支付宝交易号),
       $tradeNo=如果是支付宝交易号设置true,默认false商户订单
       
7.交易关闭PayClose($orderNo,$tradeNo=false)
  参数:
      $orderNo=订单号(商户订单号或支付宝交易号),
      $tradeNo=如果是支付宝交易号设置true,默认false商户订单
      
8.设置APPID 调用方法:setAppid($appid)
9.设置商户私钥  调用方法:setPrivateKey($PrivateKey)
10.设置支付宝公钥 调用方法:setPublicKey($alipayPublicKey)

使用演示:

class indexController extends grace{
	
	public $appid = '';//应用ID
	public $PrivateKey = '';//商户私钥
	public $alipayPublicKey = '';//支付宝公钥
	
	public function index(){//当面付(手机扫码付款)
	        // t('AliPay') 为实例化类 ,也可以 $pay = new phpgrace/tools/Alipay();
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$outTradeNo = date("YmdHis",time()).rand(1000,9999);//生成订单号
		$orderName = '一瓶矿泉水';
		$payAmount = 2;//支付金额
		$notifyUrl = 'http://xxxx.xxx.xx/xx/xx' //  当面付不推荐使用异步的方式进行判断 ,可以考虑:客户端轮询支付结果或者服务端使用 websocket 通知客户端
		$this->res = $pay->qrcodePay($outTradeNo,$orderName,$payAmount,$notifyUrl);
		p($this->res);
	}
	
	public function pcpay(){//PC支付
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$outTradeNo = date("YmdHis",time()).rand(1000,9999);//生成订单号
		$orderName = '一瓶矿泉水';
		$payAmount = 2;//支付金额
		$notifyUrl = 'http://www.xxx.com/index/notify';//用户支付成功后异步通知地址
		$returnUrl = 'http://www.xxx.com';//用户支付成功后跳转的地址
		$this->res = $pay->pcPay($outTradeNo,$orderName,$payAmount,$notifyUrl,$returnUrl);
		p($this->res);
	}
	
	public function h5Pay(){//H5支付
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$outTradeNo = date("YmdHis",time()).rand(1000,9999);//生成订单号
		$orderName = '一瓶矿泉水';
		$payAmount = 2;//支付金额
		$notifyUrl = 'http://www.xxx.com/index/notify';//用户支付成功后异步通知地址
		$returnUrl = 'http://www.xxx.com';//用户支付成功后跳转的地址
		$res = $pay->h5Pay($outTradeNo,$orderName,$payAmount,$notifyUrl,$returnUrl);
		p($res);
	}
	
	public function notify(){//异步通知
		$pay = t('AliPay')->setPublicKey($alipayPublicKey);
		$res = $pay->rsaCheck($_POST,$_POST['sign_type']);
		if($res===true && $_POST['trade_status'] == 'TRADE_SUCCESS'){
            //处理你的逻辑,例如获取订单号$_POST['out_trade_no'],订单金额$_POST['total_amount']等
            //程序执行完后必须打印输出“success”(不包含引号)。如果商户反馈给支付宝的字符不是success这7个字符,支付宝服务器会不断重发通知,直到超过24小时22分钟。一般情况下,25小时以内完成8次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h);
            
            //这里写用户付款成功后需执行的业务代码
            echo 'success';exit();
        } 
	}
	
	public function Query(){//查询支付结果
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$res = $pay->PayQuery('2022122322001458841438390852',true);
        p($res);
	}
	
	public function Close(){//关闭交易
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$res = $pay->PayClose('202212232258376155');
        p($res);
	}
	
	public function Refund(){//订单退款
		$pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
		$res = $pay->PayRefund(2,'202212232242121239');
        p($res);
	}}

当面付 完整演示

举例 显示一个扫码二维码,使用轮询方法扫描支付状态

下面例如代码前端 doc_test.php:

<?php include $this->templateDir.'header.php';?>  
<div class="layui-container gui-bg-white padding-5 layui-font-12">
    <div class="layui-row padding-top-5"> 
        <div class="layui-col-md4 " id="qrcode" style="height: 200px;"></div>
    </div>
<script src="/statics/js/jquery.qrcode.min.js"></script> 
<?php
    p($this->res);
?>
<script>
    layui.use(['form', 'layer'],  function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        $('#qrcode').qrcode("<?php echo $this->res['alipay_trade_precreate_response']['qr_code'];?>");
    });
    // 定义一个函数,用于执行定时任务
    function myFunction() {
      var url = "<?php
    $order = '202403120025472725'; //支付成功的测试订单号
    $order_1 =$this->res['alipay_trade_precreate_response']['out_trade_no'];
    echo u('doc','checkpay',array($order));?>"; 
    $.getJSON(url, function(res ) {  
        if(res.alipay_trade_query_response.trade_status == 'TRADE_SUCCESS'){
            console.log('支付成功');
            // 在需要停止定时器时,调用 clearInterval() 并传入 intervalID
                    clearInterval(intervalID);
        }
    });  
    }
    // 使用 setInterval() 创建一个定时器 轮询查看支付情况,每隔5000毫秒执行一次 myFunction()
    var intervalID = setInterval(myFunction, 5000);
</script>
<?php include $this->templateDir.'footer.php';?>


后端 doc类下的方法:

// 生成待支付二维码
public function test(){ 
        $pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
        $outTradeNo = date("YmdHis",time()).rand(1000,9999);//生成订单号
        $orderName = '测试';
        $payAmount = 0.01;//支付金额
        $notifyUrl = "http://www.666443.xyz/doc-notify"  ; //异步通知地址
        $returnUrl = 'http://www.uephp.com';//用户支付成功后跳转的地址
        $this->res = $pay->qrcodePay($outTradeNo,$orderName,$payAmount,$notifyUrl);
}
// 查询支付结果
public function checkpay(){
       $pay = t('AliPay')->setAppid($this->appid)->setPrivateKey($this->PrivateKey);
       $order_id = $this->gets[0];
       $res = $pay->PayQuery($order_id,false); // 查询结果 可在前端判断也可在后端判断,看自己习惯
       echo  json_encode ( $res);
}