支付宝扫码支付

这篇文章主要为大家详细介绍了php实现支付宝当面付,扫码支付功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

网上的很多PHP支付宝支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通过整理后给出一个单文件版的,希望可以给各位想接入支付宝支付的带来些许帮助和借鉴意义。

扫码支付,指用户打开支付宝钱包中的“扫一扫”功能,扫描商家展示在某收银场景下的二维码并进行支付的模式。该模式适用于线下实体店支付、面对面支付等场景。

运行以下php文件代码即可生成一张付款二维码图片,使用支付宝扫一扫即可付款。

一个PHP文件搞定支付宝系列

一个PHP文件搞定微信支付系列

<?php

namespace common;

/**
 * 提供支付宝扫码支付服务管理服务
 * @name AliCodePay.php
 * @package zndx
 * @category model
 * @link http://www.phphui.com
 * @author swoole
 * @version 1.0
 * @copyright SWOOLE INC
 * @since 2023-02-15
 */
class AliCodePay
{
    public $appId;

    public $notifyUrl;

    public $charset = 'utf-8';

    public $rsaPrivateKey;

    public $totalFee;

    public $outTradeNo;

    public $orderName;

    public $returnUrl;

    /**
     * 设置APPID
     */
    public function setAppId($appid)
    {
        $this->appId = $appid;
    }

    /**
     * 设置支付通知URL
     */
    public function setNotifyUrl($notifyUrl)
    {
        $this->notifyUrl = $notifyUrl;
    }

    /**
     * 设置跳转URL
     */
    public function setReturnUrl($returnUrl)
    {
        $this->returnUrl = $returnUrl;
    }

    /**
     * 设置私钥
     */
    public function setRsaPrivateKey($saPrivateKey)
    {
        $this->rsaPrivateKey = $saPrivateKey;
    }

    /**
     * 设置支付金额
     */
    public function setTotalFee($payAmount)
    {
        $this->totalFee = $payAmount;
    }

    /**
     * 设置支付订单号
     */
    public function setOutTradeNo($outTradeNo)
    {
        $this->outTradeNo = $outTradeNo;
    }

    /**
     * 设置订单标题
     */
    public function setOrderName($orderName)
    {
        $this->orderName = $orderName;
    }

    /**
     * 获取签名
     */
    public function generateSign($params, $signType = "RSA")
    {
        return $this->sign($this->getSignContent($params), $signType);
    }

    /**
     * 签名
     */
    protected function sign($data, $signType = "RSA")
    {
        $priKey = $this->rsaPrivateKey;
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
        if ("RSA2" == $signType) {
            openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
        } else {
            openssl_sign($data, $sign, $res);
        }
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * 校验是否非空
     **/
    protected function checkEmpty($value)
    {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;
        return false;
    }

    /**
     * 获取签名内容
     */
    public function getSignContent($params)
    {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 转换成目标字符集
                $v = $this->characet($v, $this->charset);
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset($k, $v);
        return $stringToBeSigned;
    }

    /**
     * 转换字符集编码
     */
    function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            $fileType = $this->charset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
                //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
            }
        }
        return $data;
    }

    /**
     * 发送支付请求
     */
    public function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = false; //是否返回响应头信息
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_HTTPHEADER] = array('content-type: application/x-www-form-urlencoded;charset=' . $this->charset);
        $params[CURLOPT_SSL_VERIFYPEER] = false; //禁用证书校验
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_POSTFIELDS] = $postData;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    /**
     * 下单
     */
    public function unifiedorder($isH5)
    {
        //请求参数
        $requestConfigs = array(
            'out_trade_no' => $this->outTradeNo,
            //单位 元
            'total_amount' => $this->totalFee,
            //订单标题
            'subject' => $this->orderName,
            //该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。 
            'timeout_express' => '2h'
        );
        $method = 'alipay.trade.precreate';
        if ($isH5) {
            $method = 'alipay.trade.wap.pay';
        }

        $commonConfigs = array(
            //公共参数
            'app_id' => $this->appId,
            'method' => $method, //接口名称
            'format' => 'JSON',
            'charset' => $this->charset,
            'sign_type' => 'RSA2',
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '1.0',
            'notify_url' => $this->notifyUrl,
            'return_url' => $this->returnUrl,
            'biz_content' => json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        if ($isH5) {
            $params = http_build_query($commonConfigs);
            return sprintf('https://openapi.alipay.com/gateway.do?%s', $params);
        } else {
            $result = $this->curlPost('https://openapi.alipay.com/gateway.do', $commonConfigs);
            return json_decode($result, true);
        }
    }
}
    /**
      * 生成支付宝支付二维码
     */
    public function getAlipayParams($orderNum, $total, $body, $isH5 = false)
    {
        $libConfig = new LibWechatConfig();
        $config = $libConfig->findConfigKeyValue();
        $config = $config['data'];
        $aliPay = new AliCodePay();
        $aliPay->setAppid($config['alipay_appid']);
        $aliPay->setNotifyUrl($config['alipay_notity_url']);
        $aliPay->setRsaPrivateKey($config['alipay_private_key']);
        $aliPay->setTotalFee($total);
        $aliPay->setOutTradeNo($orderNum);
        $aliPay->setOrderName($body);
        $result = $aliPay->unifiedorder($isH5);
        if ($isH5) {
            return $result;
        }
        $result = $result['alipay_trade_precreate_response'];
        if ($result['code'] && $result['code'] == '10000') {
            //这里是支付宝返回的付款url,需要返回到页面生成二维码
            return $result['qr_code'];
        } else {
            return $result['msg'] . ' : ' . $result['sub_msg'];
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值