精易论坛

标题: 5个文件搞定zfb手机网站支fu接口调式 [打印本页]

作者: 李仁炜    时间: 2025-4-19 00:30
标题: 5个文件搞定zfb手机网站支fu接口调式
5个文件搞定支付宝手机网站支付接口调式
<?php
/**
* 支付宝手机网站支付服务类
* 封装支付宝支付相关功能
*/
class AlipayService {
    protected $config;

    /**
     * 构造函数
     * @param array $config 支付宝配置数组
     */
    public function __construct($config) {
        $this->config = $config;
        // 确保日志目录存在
        if (!empty($this->config['log_path']) && !is_dir($this->config['log_path'])) {
            mkdir($this->config['log_path'], 0777, true);
        }
    }

    /**
     * 手机网站支付接口
     * @param string $out_trade_no 商户订单号
     * @param float $total_amount 订单总金额
     * @param string $subject 订单标题
     * @param string $product_code 销售产品码
     * @Return string 支付表单HTML
     */
    public function wapPay($out_trade_no, $total_amount, $subject, $product_code) {
        // 构造业务参数
        $requestConfigs = array(
            'out_trade_no' => $out_trade_no,
            'product_code' => $product_code,
            'total_amount' => $total_amount,
            'subject' => $subject,
        );

        // 构造公共参数
        $commonConfigs = array(
            'app_id' => $this->config['app_id'],
            'method' => 'alipay.trade.wap.pay', // 接口名称
            'format' => 'JSON',
            'charset' => $this->config['charset'],
            'sign_type' => $this->config['sign_type'],
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '1.0',
            'notify_url' => $this->config['notify_url'],
            'return_url' => $this->config['return_url'],
            'biz_content' => json_encode($requestConfigs, JSON_UNESCAPED_UNICODE),
        );

        // 生成签名
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $this->config['sign_type']);

        // 生成支付表单
        $form = '<form id="alipaysubmit" name="alipaysubmit" action="'.$this->config['gatewayUrl'].'?charset='.$this->config['charset'].'" method="POST">';
        foreach ($commonConfigs as $key => $value) {
            $form .= '<input type="hidden" name="'.$key.'" value="'.htmlspecialchars($value, ENT_QUOTES).'"/>';
        }
        $form .= '<input type="submit" value="立即支付" style="display:none;"></form>';
        $form .= '<script>document.forms["alipaysubmit"].submit();</script>';

        return $form;
    }

    /**
     * RSA验签
     * @param array $data 待验签数据
     * @param string $signType 签名类型 RSA/RSA2
     * @return bool 验签结果
     */
    public function rsaCheck($data, $signType = 'RSA2') {
        // 提取签名
        $sign = $data['sign'];

        // 去除签名和签名类型参数
        $data['sign'] = null;
        $data['sign_type'] = null;

        // 获取待签名字符串
        $stringToBeSigned = $this->getSignContent($data);

        // 加载支付宝公钥
        $pubKey = $this->config['alipay_public_key'];
        $res = "-----BEGIN PUBLIC KEY-----\n" .
            wordwrap($pubKey, 64, "\n", true) .
            "\n-----END PUBLIC KEY-----";

        // 检查公钥是否正确
        ($res) or die('支付宝RSA公钥错误。请检查公钥文件格式是否正确');

        // 调用openssl内置方法验签
        if ("RSA2" == $signType) {
            $result = (bool)openssl_verify($stringToBeSigned, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
        } else {
            $result = (bool)openssl_verify($stringToBeSigned, base64_decode($sign), $res);
        }

        return $result;
    }

    /**
     * 生成签名
     * @param array $params 待签名数据
     * @param string $signType 签名类型
     * @return string 签名结果
     */
    protected function generateSign($params, $signType = "RSA2") {
        return $this->sign($this->getSignContent($params), $signType);
    }

    /**
     * 获取待签名字符串
     * @param array $params 待签名数据
     * @return string 待签名字符串
     */
    protected function getSignContent($params) {
        // 参数按key排序
        ksort($params);

        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            // 排除空值和签名参数
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 参数拼接
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }

        unset ($k, $v);
        return $stringToBeSigned;
    }

    /**
     * 签名
     * @param string $data 待签名数据
     * @param string $signType 签名类型
     * @return string 签名结果
     */
    protected function sign($data, $signType = "RSA2") {
        // 加载商户私钥
        $priKey = $this->config['merchant_private_key'];
        $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);
        } else {
            openssl_sign($data, $sign, $res);
        }

        // base64编码签名结果
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * 检查值是否为空
     * @param mixed $value 待检查值
     * @return bool
     */
    protected function checkEmpty($value) {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }

    /**
     * 记录日志
     * @param string $text 日志内容
     */
    public function writeLog($text) {
        if (!empty($this->config['log_path'])) {
            file_put_contents($this->config['log_path']."alipay_".date('Y-m-d').".log", date('Y-m-d H:i:s')."  ".$text."\r\n", FILE_APPEND);
        }
    }
}
有需要的可以下载看看





作者: 李仁炜    时间: 2025-4-19 00:32
我自己也看不到吗
作者: 夜雨y    时间: 2025-4-20 17:12
6666666666666666
作者: 心梦ゞ颜熙    时间: 2025-4-22 14:01
111111111111
作者: 涤尘    时间: 2025-4-25 16:40
感谢分享




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4