Top

thinkphp封装支付宝扫码支付当面付

2021-07-30

附件的sdk已全部封装

去官方下载sdk都可以

我们解压到extend目录下

封装函数命名Alipay.php

<?php
namespace f2fpay;
use think\Validate;
/**
 * 支付宝扫码支付
 * @qq50361804
 */
class Alipay
{
    public function qrpay($data = [])
    {
        //验证必传的参数
        $validate = Validate::make([
            'out_trade_no' => 'require|max:30',
            'total_fee' => 'require',
            'subject' => 'require',
            'body' => 'require',
            'notify_url' => 'require',
        ]);
        if (!$validate->check($data)) {
            return ['code' => 0, 'msg' => $validate->getError()];
        }
        $timeExpress = "5m";//超时时间
        require 'extend/f2fpay/f2fpay/model/builder/AlipayTradePrecreateContentBuilder.php';
        require 'extend/f2fpay/f2fpay/service/AlipayTradeService.php';
        // 创建请求builder,设置请求参数
        $qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
        $qrPayRequestBuilder->setOutTradeNo($data['out_trade_no']);
        $qrPayRequestBuilder->setTotalAmount($data['total_fee']);
        $qrPayRequestBuilder->setTimeExpress($timeExpress);
        $qrPayRequestBuilder->setSubject($data['subject']);
        $qrPayRequestBuilder->setBody($data['body']);
        // 调用qrPay方法获取当面付应答
        $config = $data;
        //$config = config('other.alipay_config');
        if (!empty($data['notify_url'])) {
            $config['notify_url'] = $data['notify_url'];
        }
        $qrPay = new \AlipayTradeService($config);
        $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);
        //  根据状态值进行业务处理
        switch ($qrPayResult->getTradeStatus()) {
            case "SUCCESS":
                //     echo "支付宝创建订单二维码成功:"."<br>---------------------------------------<br>";
                $response = $qrPayResult->getResponse();
                return json_encode($response);die;
                break;
            case "FAILED":
                // echo "支付宝创建订单二维码失败!!!"."<br>--------------------------<br>";
 
                return ['code' => 0, 'msg' => '支付宝创建订单二维码失败!!!'];
                break;
            case "UNKNOWN":
                // echo "系统异常,状态未知!!!"."<br>--------------------------<br>";
                return ['code' => 0, 'msg' => '系统异常,状态未知!!!'];
                break;
            default:
                return ['code' => 0, 'msg' => '不支持的返回状态,创建订单二维码返回异常!!!'];
                break;
        }
        return;
    }
}

然后我们在控制器代码

<?php
/**
 *  作者:梦雨
 *  @ QQ50361804
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use f2fpay\Alipay;
class Alipayma extends Controller
{
 
 //支付宝扫码付
    public function order()
    {
        $out_trade_no=generate_password(8) . time();
        $total_fee=input('jiage');
        $_var_20 = [
            'notify_url' => $_SERVER['REQUEST_SCHEME'] . '://' . "{$_SERVER['HTTP_HOST']}/Alipayma/notify.html",
            'out_trade_no' => $out_trade_no,
            'subject' => config('point.ADD_MC'),
            'total_fee' =>$total_fee,
            'body' => '积分充值',
            'sign_type' => 'RSA2',//签名算法,目前新创建的应用仅支持RSA2 不支持RSA
            'alipay_public_key' => '',//公钥
            'merchant_private_key' =>'',//私钥
            'app_id' => '',//应用ID
            'charset' => 'UTF-8',//编码
            'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',//接口
            'MaxQueryRetry' => '10',
            'QueryDuration' => '3',
            'return_url' => '',
            'log_file' => '',
        ];
        $data['uid'] = session('userid');
        $data['id'] = $out_trade_no;
        $data['trade_no'] = 0;
        $data['status'] = 0;
        $data['jiage'] = $total_fee;
        $data['add_time'] = time();
        $data['errorcode'] = 0;
        $data['score'] = $total_fee * config('point.ADD_BL');
        if (warning() == shul()) {
            Db::name('dingdan')->insert($data);
        }
        $_var_21 = new Alipay();
        $_var_22 = $_var_21->qrpay($_var_20);
        $de_json = json_decode($_var_22, true);
        // 生成二维码开始
        require_once 'extend/f2fpay/phpqrcode.php';
        $url=$de_json['qr_code'];
        $errorCorrectionLevel = '8';
        $matrixPointSize = intval(8);
        $object = new \QRcode();
        ob_end_clean();
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }

    public function notify()
    {
        // 异步通知
        // 自己写逻辑
    }
 
}


附 件 下 载

加入我们

如果您有建站需求需要详谈可以留言给我们,我们将会第一时间给您取得联系,还可以添加QQ 1340828870 或者微信 mixinwangluo 与我们实时交谈