搜尋
首頁後端開發php教程隨著微信支付的升級,PHP微信支付類V3介面也來了

不知不覺微信支付也更新了,介面版本也升級到了V3,跟著微信的升級,將個人使用微信支付類也進行了升級,V3微信支付文檔:https://pay.weixin .qq.com/wiki/doc/apiv3/index.shtml。

使用方法還和之前的一樣(V2微信支付),直接傳遞參數就可使用:

新版新增了composer安裝,方便整合框架使用(Github位址):

composer require fengkui/pay

先把設定檔填入完整(細心不要填錯,否則會導致簽章錯誤):

# 微信支付配置
$wechatConfig = [
    'xcxid'         => '', // 小程序 appid
    'appid'         => '', // 微信支付 appid
    'mchid'         => '', // 微信支付 mch_id 商户收款账号
    'key'           => '', // 微信支付 apiV3key(尽量包含大小写字母,否则验签不通过)
    'appsecret'     => '', // 公众帐号 secert (公众号支付获取 code 和 openid 使用)

    'notify_url'    => '', // 接收支付状态的连接  改成自己的回调地址
    'redirect_url'  => '', // 公众号支付,调起支付页面

    'serial_no'     => '', // 证书序列号
    'cert_client'   => './cert/apiclient_cert.pem', // 证书(退款,红包时使用)
    'cert_key'      => './cert/apiclient_key.pem', // 商户私钥(Api安全中下载)
    'public_key'    => './cert/public_key.pem', // 平台公钥(调动证书列表,自动生成)
];

支付類別封裝相關方法:

##query查詢訂單close關閉訂單refundnotify#使用方法(這裡已小程式支付為範例):
method 描述
#js JSAPI下單
app APP付款
#h5 H5付款
scan Navicat付款
xcx 小程式付款
##申請退款
支付結果通知
<?php
require_once(&#39;./vendor/autoload.php&#39;);

$config = []; // 支付配置
$order = [
    &#39;order_sn&#39; => time(), // 订单编号
    &#39;total_amount&#39; => 1, // 订单金额(分)
    &#39;body&#39; => &#39;测试商品&#39;, // 商品名称
    &#39;openid&#39; => &#39;&#39;, // 用户openid
    // &#39;type&#39; => &#39;Wap&#39;,
];

$wechat = new fengkui\Pay\Wechat($config);
$re = $wechat->xcx($order);
die(json_encode($re)); // JSON化直接返回小程序客户端

如下程式碼是封裝好的完整支付類別文件(Wechat.php),

可以根據自己需求隨意修改,詳細的使用方法後期會有文件:

<?php
/**
 * @Author: [FENG] <1161634940@qq.com>
 * @Date:   2019-09-06 09:50:30
 * @Last Modified by:   [FENG] <1161634940@qq.com>
 * @Last Modified time: 2021-07-12 18:24:18
 */
namespace fengkui\Pay;

use Exception;
use RuntimeException;
use fengkui\Supports\Http;

/**
 * Wechat 微信支付
 * 新版(V3)接口(更新中)
 */
class Wechat
{
    const AUTH_TAG_LENGTH_BYTE = 16;

    // 新版相关接口
    // GET 获取平台证书列表
    private static $certificatesUrl = &#39;https://api.mch.weixin.qq.com/v3/certificates&#39;;
    // 统一下订单管理
    private static $transactionsUrl = &#39;https://api.mch.weixin.qq.com/v3/pay/transactions/&#39;;
    // 申请退款
    private static $refundUrl = &#39;https://api.mch.weixin.qq.com/v3/refund/domestic/refunds&#39;;
    // 静默授权,获取code
    private static $authorizeUrl = &#39;https://open.weixin.qq.com/connect/oauth2/authorize&#39;;
    // 通过code获取access_token以及openid
    private static $accessTokenUrl = &#39;https://api.weixin.qq.com/sns/oauth2/access_token&#39;;

    // 支付完整配置
    private static $config = array(
        &#39;xcxid&#39;         => &#39;&#39;, // 小程序appid
        &#39;appid&#39;         => &#39;&#39;, // 微信支付appid
        &#39;mchid&#39;         => &#39;&#39;, // 微信支付 mch_id 商户收款账号
        &#39;key&#39;           => &#39;&#39;, // 微信支付 apiV3key(尽量包含大小写字母,否则验签不通过)
        &#39;appsecret&#39;     => &#39;&#39;, // 公众帐号 secert (公众号支付获取code 和 openid使用)

        &#39;notify_url&#39;    => &#39;&#39;, // 接收支付状态的连接  改成自己的回调地址
        &#39;redirect_url&#39;  => &#39;&#39;, // 公众号支付,调起支付页面

        &#39;serial_no&#39;     => &#39;&#39;, // 证书序列号
        &#39;cert_client&#39;   => &#39;./cert/apiclient_cert.pem&#39;, // 证书(退款,红包时使用)
        &#39;cert_key&#39;      => &#39;./cert/apiclient_key.pem&#39;, // 商户私钥(Api安全中下载)
        &#39;public_key&#39;    => &#39;./cert/public_key.pem&#39;, // 平台公钥(调动证书列表,自动生成)
    );

    /**
     * [__construct 构造函数]
     * @param [type] $config [传递微信支付相关配置]
     */
    public function __construct($config=NULL, $referer=NULL){
        $config && self::$config = array_merge(self::$config, $config);
    }

    /**
     * [unifiedOrder 统一下单]
     * @param  [type]  $order [订单信息(必须包含支付所需要的参数)]
     * @param  boolean $type  [区分是否是小程序,是则传 true]
     * @return [type]         [description]
     * $order = array(
     *      &#39;body&#39;         => &#39;&#39;, // 产品描述
     *      &#39;order_sn&#39;     => &#39;&#39;, // 订单编号
     *      &#39;total_amount&#39; => &#39;&#39;, // 订单金额(分)
     * );
     */
    public static function unifiedOrder($order, $type=false)
    {
        $config = array_filter(self::$config);

        // 获取配置项
        $params = array(
            &#39;appid&#39;         => $type ? $config[&#39;xcxid&#39;] : $config[&#39;appid&#39;], // 由微信生成的应用ID
            &#39;mchid&#39;         => $config[&#39;mchid&#39;], // 直连商户的商户号
            &#39;description&#39;   => $order[&#39;body&#39;], // 商品描述
            &#39;out_trade_no&#39;  => (string)$order[&#39;order_sn&#39;], // 商户系统内部订单号
            &#39;notify_url&#39;    => $config[&#39;notify_url&#39;], // 通知URL必须为直接可访问的URL
            &#39;amount&#39;        => [&#39;total&#39; => (int)$order[&#39;total_amount&#39;], &#39;currency&#39; => &#39;CNY&#39;], // 订单金额信息
        );

        !empty($order[&#39;attach&#39;]) && $params[&#39;attach&#39;] = $order[&#39;attach&#39;]; // 附加数据
        if (!empty($order[&#39;time_expire&#39;])) { // 订单失效时间
            preg_match(&#39;/[年\/-]/&#39;, $order[&#39;time_expire&#39;]) && $order[&#39;time_expire&#39;] = strtotime($order[&#39;time_expire&#39;]);
            $time = $order[&#39;time_expire&#39;] > time() ? $order[&#39;time_expire&#39;] : $order[&#39;time_expire&#39;] + time();
            $params[&#39;time_expire&#39;] = date(DATE_ATOM, $time);
        }

        if (!in_array($order[&#39;type&#39;], [&#39;native&#39;])) {
            !empty($order[&#39;openid&#39;]) && $params[&#39;payer&#39;] = [&#39;openid&#39; => $order[&#39;openid&#39;]];
            $params[&#39;scene_info&#39;] = [&#39;payer_client_ip&#39; => self::get_ip()];
        }

        if (in_array($order[&#39;type&#39;], [&#39;iOS&#39;, &#39;Android&#39;, &#39;Wap&#39;])) {
            $params[&#39;scene_info&#39;][&#39;h5_info&#39;] = [&#39;type&#39; => $order[&#39;type&#39;]];
            $url = self::$transactionsUrl . &#39;h5&#39;; // 拼接请求地址
        } else {
            $url = self::$transactionsUrl . strtolower($order[&#39;type&#39;]); // 拼接请求地址
        }

        $header = self::createAuthorization($url, $params, &#39;POST&#39;);
        $response = Http::post($url, json_encode($params, JSON_UNESCAPED_UNICODE), $header);
        $result = json_decode($response, true);
        if (isset($result[&#39;code&#39;]) && isset($result[&#39;message&#39;])) {
            throw new \Exception("[" . $result[&#39;code&#39;] . "] " . $result[&#39;message&#39;]);
        }

        return $result;
    }

    /**
     * [query 查询订单]
     * @param  [type]  $orderSn [订单编号]
     * @param  boolean $type    [微信支付订单编号,是否是微信支付订单号]
     * @return [type]           [description]
     */
    public static function query($orderSn, $type = false)
    {
        $config = self::$config;
        $url = self::$transactionsUrl . ($type ? &#39;id/&#39; : &#39;out-trade-no/&#39;) . $orderSn . &#39;?mchid=&#39; . $config[&#39;mchid&#39;];
        $params = &#39;&#39;;

        $header = self::createAuthorization($url, $params, &#39;GET&#39;);
        $response = Http::get($url, $params, $header);
        $result = json_decode($response, true);

        return $result;
    }

    /**
     * [close 关闭订单]
     * @param  [type] $orderSn [微信支付订单编号]
     * @return [type]          [description]
     */
    public static function close($orderSn)
    {
        $config = self::$config;
        $url = self::$transactionsUrl . &#39;out-trade-no/&#39; . $orderSn . &#39;/close&#39;;
        $params[&#39;mchid&#39;] = $config[&#39;mchid&#39;];

        $header = self::createAuthorization($url, $params, &#39;POST&#39;);
        $response = Http::post($url, json_encode($params, JSON_UNESCAPED_UNICODE), $header);
        $result = json_decode($response, true);

        return true;
    }

    /**
     * [js 获取jssdk需要用到的数据]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     */
    public static function js($order=[]){
        $config = self::$config;
        if (!is_array($order) || count($order) < 3)
            die("订单数组信息缺失!");
        if (count($order) == 4 && !empty($order[&#39;openid&#39;])) {
            $data = self::xcx($order, false, false); // 获取支付相关信息(获取非小程序信息)
            return $data;
        }
        $code = !empty($order[&#39;code&#39;]) ? $order[&#39;code&#39;] : ($_GET[&#39;code&#39;] ?? &#39;&#39;);
        $redirectUri = $_SERVER[&#39;REQUEST_SCHEME&#39;] . &#39;://&#39; . $_SERVER[&#39;HTTP_HOST&#39;] . rtrim($_SERVER[&#39;REQUEST_URI&#39;], &#39;/&#39;) . &#39;/&#39;; // 重定向地址

        $params = [&#39;appid&#39; => $config[&#39;appid&#39;]];
        // 如果没有get参数没有code;则重定向去获取code;
        if (empty($code)) {
            $params[&#39;redirect_uri&#39;] = $redirectUri; // 返回的url
            $params[&#39;response_type&#39;] = &#39;code&#39;;
            $params[&#39;scope&#39;] = &#39;snsapi_base&#39;;
            $params[&#39;state&#39;] = $order[&#39;order_sn&#39;]; // 获取订单号

            $url = self::$authorizeUrl . &#39;?&#39;. http_build_query($params) .&#39;#wechat_redirect&#39;;
        } else {
            $params[&#39;secret&#39;] = $config[&#39;appsecret&#39;];
            $params[&#39;code&#39;] = $code;
            $params[&#39;grant_type&#39;] = &#39;authorization_code&#39;;

            $response = Http::get(self::$accessTokenUrl, $params); // 进行GET请求
            $result = json_decode($response, true);

            $order[&#39;openid&#39;] = $result[&#39;openid&#39;]; // 获取到的openid
            $data = self::xcx($order, false, false); // 获取支付相关信息(获取非小程序信息)

            if (!empty($order[&#39;code&#39;])) {
                return $data;
            }

            $url = $config[&#39;redirect_url&#39;] ?? $redirectUri;
            $url .= &#39;?data=&#39; . json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        header(&#39;Location: &#39;. $url);
        die;
    }

    /**
     * [app 获取APP支付需要用到的数据]
     * @param  [type]  $order [订单信息数组]
     * @return [type]         [description]
     */
    public static function app($order=[], $log=false)
    {
        if(empty($order[&#39;order_sn&#39;]) || empty($order[&#39;total_amount&#39;]) || empty($order[&#39;body&#39;])){
            die("订单数组信息缺失!");
        }

        $order[&#39;type&#39;] = &#39;app&#39;; // 获取订单类型,用户拼接请求地址
        $result = self::unifiedOrder($order, true);
        if (!empty($result[&#39;prepay_id&#39;])) {
            $data = array (
                &#39;appId&#39;     => self::$config[&#39;appid&#39;], // 微信开放平台审核通过的移动应用appid
                &#39;timeStamp&#39; => (string)time(),
                &#39;nonceStr&#39;  => self::get_rand_str(32, 0, 1), // 随机32位字符串
                &#39;prepayid&#39;  => $result[&#39;prepay_id&#39;],
            );
            $data[&#39;paySign&#39;] = self::makeSign($data);
            $data[&#39;partnerid&#39;] = $config[&#39;mchid&#39;];
            $data[&#39;package&#39;] = &#39;Sign=WXPay&#39;;
            return $data; // 数据小程序客户端
        } else {
            return $log ? $result : false;
        }
    }

    /**
     * [h5 微信H5支付]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     */
    public static function h5($order=[], $log=false)
    {
        if(empty($order[&#39;order_sn&#39;]) || empty($order[&#39;total_amount&#39;]) || empty($order[&#39;body&#39;]) || empty($order[&#39;type&#39;]) || !in_array(strtolower($order[&#39;type&#39;]), [&#39;ios&#39;, &#39;android&#39;, &#39;wap&#39;])){
            die("订单数组信息缺失!");
        }
        $result = self::unifiedOrder($order);
        if (!empty($result[&#39;h5_url&#39;])) {
            return $result[&#39;h5_url&#39;]; // 返回链接让用户点击跳转
        } else {
            return $log ? $result : false;
        }
    }

    /**
     * [xcx 获取jssdk需要用到的数据]
     * @param  [type]  $order [订单信息数组]
     * @param  boolean $log   [description]
     * @param  boolean $type  [区分是否是小程序,默认 true]
     * @return [type]         [description]
     */
    public static function xcx($order=[], $log=false, $type=true)
    {
        if(empty($order[&#39;order_sn&#39;]) || empty($order[&#39;total_amount&#39;]) || empty($order[&#39;body&#39;]) || empty($order[&#39;openid&#39;])){
            die("订单数组信息缺失!");
        }

        $order[&#39;type&#39;] = &#39;jsapi&#39;; // 获取订单类型,用户拼接请求地址
        $config = self::$config;
        $result = self::unifiedOrder($order, $type);
        if (!empty($result[&#39;prepay_id&#39;])) {
            $data = array (
                &#39;appId&#39;     => $type ? $config[&#39;xcxid&#39;] : $config[&#39;appid&#39;], // 由微信生成的应用ID
                &#39;timeStamp&#39; => (string)time(),
                &#39;nonceStr&#39;  => self::get_rand_str(32, 0, 1), // 随机32位字符串
                &#39;package&#39;   => &#39;prepay_id=&#39;.$result[&#39;prepay_id&#39;],
            );
            $data[&#39;paySign&#39;] = self::makeSign($data);
            $data[&#39;signType&#39;] = &#39;RSA&#39;;
            return $data; // 数据小程序客户端
        } else {
            return $log ? $result : false;
        }
    }

    /**
     * [scan 微信扫码支付]
     * @param  [type] $order [订单信息数组]
     * @return [type]        [description]
     */
    public static function scan($order=[], $log=false)
    {
        if(empty($order[&#39;order_sn&#39;]) || empty($order[&#39;total_amount&#39;]) || empty($order[&#39;body&#39;])){
            die("订单数组信息缺失!");
        }
        $order[&#39;type&#39;] = &#39;native&#39;; // Native支付
        $result = self::unifiedOrder($order);

        if (!empty($result[&#39;code_url&#39;])) {
            return urldecode($result[&#39;code_url&#39;]); // 返回链接让用户点击跳转
        } else {
            return $log ? $result : false;
        }
    }

    /**
     * [notify 回调验证]
     * @return [array] [返回数组格式的notify数据]
     */
    public static function notify($server = [], $response = [])
    {
        $config = self::$config;
        $server = $server ?? $_SERVER;
        $response = $response ?? file_get_contents(&#39;php://input&#39;, &#39;r&#39;);
        if (empty($response) || empty($server[&#39;HTTP_WECHATPAY_SIGNATURE&#39;])) {
            return false;
        }
        $body = [
            &#39;timestamp&#39; => $server[&#39;HTTP_WECHATPAY_TIMESTAMP&#39;],
            &#39;nonce&#39; => $server[&#39;HTTP_WECHATPAY_NONCE&#39;],
            &#39;data&#39; => $response,
        ];
        // 验证应答签名
        $verifySign = self::verifySign($body, trim($server[&#39;HTTP_WECHATPAY_SIGNATURE&#39;]), trim($server[&#39;HTTP_WECHATPAY_SERIAL&#39;]));
        if (!$verifySign) {
            die("签名验证失败!");
        }
        $result = json_decode($response, true);
        if (empty($result) || $result[&#39;event_type&#39;] != &#39;TRANSACTION.SUCCESS&#39; || $result[&#39;summary&#39;] != &#39;支付成功&#39;) {
            return false;
        }
        // 加密信息
        $associatedData = $result[&#39;resource&#39;][&#39;associated_data&#39;];
        $nonceStr = $result[&#39;resource&#39;][&#39;nonce&#39;];
        $ciphertext = $result[&#39;resource&#39;][&#39;ciphertext&#39;];
        $data = $result[&#39;resource&#39;][&#39;ciphertext&#39;] = self::decryptToString($associatedData, $nonceStr, $ciphertext);

        return json_decode($data, true);
    }

    /**
     * [refund 微信支付退款]
     * @param  [type] $order [订单信息]
     * @param  [type] $type  [是否是小程序]
     */
    public static function refund($order)
    {
        $config = self::$config;
        if(empty($order[&#39;refund_sn&#39;]) || empty($order[&#39;refund_amount&#39;]) || (empty($order[&#39;order_sn&#39;]) && empty($order[&#39;transaction_id&#39;]))){
            die("订单数组信息缺失!");
        }

        $params = array(
            &#39;out_refund_no&#39; => (string)$order[&#39;refund_sn&#39;], // 商户退款单号
            &#39;funds_account&#39; => &#39;AVAILABLE&#39;, // 退款资金来源
            &#39;amount&#39; => [
                    &#39;refund&#39; => $order[&#39;refund_amount&#39;],
                    &#39;currency&#39; => &#39;CNY&#39;,
                ]
        );

        if (!empty($order[&#39;transaction_id&#39;])) {
            $params[&#39;transaction_id&#39;] = $order[&#39;transaction_id&#39;];
            $orderDetail = self::query($order[&#39;transaction_id&#39;], true);
        } else {
            $params[&#39;out_trade_no&#39;] = $order[&#39;order_sn&#39;];
            $orderDetail = self::query($order[&#39;order_sn&#39;]);
        }
        $params[&#39;amount&#39;][&#39;total&#39;] = $orderDetail[&#39;amount&#39;][&#39;total&#39;];
        !empty($order[&#39;reason&#39;]) && $params[&#39;reason&#39;] = $order[&#39;reason&#39;];

        $url = self::$refundUrl;
        $header = self::createAuthorization($url, $params, &#39;POST&#39;);
        $response = Http::post($url, json_encode($params, JSON_UNESCAPED_UNICODE), $header);
        $result = json_decode($response, true);

        return $result;
    }

    /**
     * [queryRefund 查询退款]
     * @param  [type] $refundSn [退款单号]
     * @return [type]           [description]
     */
    public static function queryRefund($refundSn, $type = false)
    {
        $url = self::$refundUrl . &#39;/&#39; . $refundSn;
        $params = &#39;&#39;;

        $header = self::createAuthorization($url, $params, &#39;GET&#39;);
        $response = Http::get($url, $params, $header);
        $result = json_decode($response, true);

        return $result;
    }

    /**
     * [success 通知支付状态]
     */
    public static function success()
    {
        $str = [&#39;code&#39;=>&#39;SUCCESS&#39;, &#39;message&#39;=>&#39;成功&#39;];
        die(json_encode($str, JSON_UNESCAPED_UNICODE));
    }

    /**
     * [createAuthorization 获取接口授权header头信息]
     * @param  [type] $url    [请求地址]
     * @param  array  $data   [请求参数]
     * @param  string $method [请求方式]
     * @return [type]         [description]
     */
    //生成v3 Authorization
    protected static function createAuthorization($url, $data=[], $method=&#39;POST&#39;){
        $config = self::$config;
        //商户号
        $mchid = $config[&#39;mchid&#39;];
        // 证书序列号
        if (empty($config[&#39;serial_no&#39;])) {
            $certFile = @file_get_contents($config[&#39;cert_client&#39;]);
            $certArr = openssl_x509_parse($publicStr);
            $serial_no = $certArr[&#39;serialNumberHex&#39;];
        } else {
            $serial_no = $config[&#39;serial_no&#39;];
        }

        // 解析url地址
        $url_parts = parse_url($url);
        //生成签名
        $body = [
            &#39;method&#39; => $method,
            &#39;url&#39;   => ($url_parts[&#39;path&#39;] . (!empty($url_parts[&#39;query&#39;]) ? "?${url_parts[&#39;query&#39;]}" : "")),
            &#39;time&#39;  => time(), // 当前时间戳
            &#39;nonce&#39; => self::get_rand_str(32, 0, 1), // 随机32位字符串
            &#39;data&#39;  => (strtolower($method) == &#39;post&#39; ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data), // POST请求时 需要 转JSON字符串
        ];
        $sign = self::makeSign($body);
        //Authorization 类型
        $schema = &#39;WECHATPAY2-SHA256-RSA2048&#39;;
        //生成token
        $token = sprintf(&#39;mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"&#39;, $mchid, $body[&#39;nonce&#39;], $body[&#39;time&#39;], $serial_no, $sign);

        $header = [
            &#39;Content-Type:application/json&#39;,
            &#39;Accept:application/json&#39;,
            &#39;User-Agent:*/*&#39;,
            &#39;Authorization: &#39;.  $schema . &#39; &#39; . $token
        ];
        return $header;
    }

    /**
     * [makeSign 生成签名]
     * @param  [type] $data [加密数据]
     * @return [type]       [description]
     */
    public static function makeSign($data)
    {
        $config = self::$config;
        if (!in_array(&#39;sha256WithRSAEncryption&#39;, \openssl_get_md_methods(true))) {
            throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
        }
        // 拼接生成签名所需的字符串
        $message = &#39;&#39;;
        foreach ($data as $value) {
            $message .= $value . "\n";
        }
        // 商户私钥
        $private_key = self::getPrivateKey($config[&#39;cert_key&#39;]);
        // 生成签名
        openssl_sign($message, $sign, $private_key, &#39;sha256WithRSAEncryption&#39;);
        $sign = base64_encode($sign);
        return $sign;
    }

    /**
     * [verifySign 验证签名]
     * @param  [type] $data   [description]
     * @param  [type] $sign   [description]
     * @param  [type] $serial [description]
     * @return [type]         [description]
     */
    public static function verifySign($data, $sign, $serial)
    {
        $config = self::$config;
        if (!in_array(&#39;sha256WithRSAEncryption&#39;, \openssl_get_md_methods(true))) {
            throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");
        }
        $sign = \base64_decode($sign);
        // 拼接生成签名所需的字符串
        $message = &#39;&#39;;
        foreach ($data as $value) {
            $message .= $value . "\n";
        }
        // 获取证书相关信息
        self::certificates($serial);
        // 平台公钥
        $public_key = self::getPublicKey($config[&#39;public_key&#39;]); //平台公钥
        // 验证签名
        $recode = \openssl_verify($message, $sign, $public_key, &#39;sha256WithRSAEncryption&#39;);
        return $recode == 1 ? true : false;
    }

    //获取私钥
    public static function getPrivateKey($filepath)
    {
        return openssl_pkey_get_private(file_get_contents($filepath));
    }

    //获取公钥
    public static function getPublicKey($filepath)
    {
        return openssl_pkey_get_public(file_get_contents($filepath));
    }

    /**
     * [certificates 获取证书]
     * @return [type] [description]
     */
    public static function certificates($serial)
    {
        $config = self::$config;

        $publicStr = @file_get_contents($config[&#39;public_key&#39;]);
        if ($publicStr) { // 判断证书是否存在
            $openssl = openssl_x509_parse($publicStr);
            if ($openssl[&#39;serialNumberHex&#39;] == $serial) { // 是否是所需证书
                // return self::getPublicKey($config[&#39;public_key&#39;]); //平台公钥
                return &#39;&#39;;
            }
        }

        $url = self::$certificatesUrl;
        $params = &#39;&#39;;

        $header = self::createAuthorization($url, $params, &#39;GET&#39;);
        $response = Http::get($url, $params, $header);
        $result = json_decode($response, true);
        if (empty($result[&#39;data&#39;])) {
            throw new RuntimeException("[" . $result[&#39;code&#39;] . "] " . $result[&#39;message&#39;]);
        }
        foreach ($result[&#39;data&#39;] as $key => $certificate) {
            if ($certificate[&#39;serial_no&#39;] == $serial) {
                $publicKey = self::decryptToString(
                    $certificate[&#39;encrypt_certificate&#39;][&#39;associated_data&#39;],
                    $certificate[&#39;encrypt_certificate&#39;][&#39;nonce&#39;],
                    $certificate[&#39;encrypt_certificate&#39;][&#39;ciphertext&#39;]
                );
                file_put_contents($config[&#39;public_key&#39;], $publicKey);
                break; // 终止循环
            }
            // self::$publicKey[$certificate[&#39;serial_no&#39;]] = $publicKey;
        }
        // return self::getPublicKey($config[&#39;public_key&#39;]); //平台公钥
    }

    /**
     * [decryptToString 证书和回调报文解密]
     * @param  [type] $associatedData [附加数据包(可能为空)]
     * @param  [type] $nonceStr       [加密使用的随机串初始化向量]
     * @param  [type] $ciphertext     [Base64编码后的密文]
     * @return [type]                 [description]
     */
    public static function decryptToString($associatedData, $nonceStr, $ciphertext)
    {
        $config = self::$config;
        $ciphertext = base64_decode($ciphertext);
        if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {
            return false;
        }

        // ext-sodium (default installed on >= PHP 7.2)
        if (function_exists(&#39;\sodium_crypto_aead_aes256gcm_is_available&#39;) &&
            \sodium_crypto_aead_aes256gcm_is_available()) {
            return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $config[&#39;key&#39;]);
        }

        // ext-libsodium (need install libsodium-php 1.x via pecl)
        if (function_exists(&#39;\Sodium\crypto_aead_aes256gcm_is_available&#39;) &&
            \Sodium\crypto_aead_aes256gcm_is_available()) {
            return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $config[&#39;key&#39;]);
        }

        // openssl (PHP >= 7.1 support AEAD)
        if (PHP_VERSION_ID >= 70100 && in_array(&#39;aes-256-gcm&#39;, \openssl_get_cipher_methods())) {
            $ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
            $authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);

            return \openssl_decrypt($ctext, &#39;aes-256-gcm&#39;, $config[&#39;key&#39;], \OPENSSL_RAW_DATA, $nonceStr,
                $authTag, $associatedData);
        }

        throw new \RuntimeException(&#39;AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php&#39;);
    }

    /** fengkui.net
     * [get_rand_str 获取随机字符串]
     * @param  integer $randLength    [长度]
     * @param  integer $addtime       [是否加入当前时间戳]
     * @param  integer $includenumber [是否包含数字]
     * @return [type]                 [description]
     */
    public static function get_rand_str($randLength=6, $addtime=0, $includenumber=1)
    {
        if ($includenumber)
            $chars=&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789&#39;;
        $chars=&#39;abcdefghijklmnopqrstuvwxyz&#39;;

        $len = strlen($chars);
        $randStr = &#39;&#39;;
        for ($i=0; $i<$randLength; $i++){
            $randStr .= $chars[rand(0, $len-1)];
        }
        $tokenvalue = $randStr;
        $addtime && $tokenvalue = $randStr . time();
        return $tokenvalue;
    }

    /** fengkui.net
     * [get_ip 定义一个函数get_ip() 客户端IP]
     * @return [type] [description]
     */
    public static function get_ip()
    {
        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else $ip = "Unknow";

        if(preg_match(&#39;/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/&#39;, $ip))
            return $ip;
        else
            return &#39;&#39;;
    }
}

本文參考文檔:

1、

微信支付小程式(v​​3)- PHP 完整後端程式碼
#2、PHP 微信小程式微信支付v3
3、微信支付V3版本小程式支付php簽章,驗簽,資料解密程式碼分享(完整方法主參考)
#4、微信支付API v3 回呼通知簽章驗證PHPdemo有嘛?
推薦:《

PHP影片教學

以上是隨著微信支付的升級,PHP微信支付類V3介面也來了的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:learnku。如有侵權,請聯絡admin@php.cn刪除
使用數據庫存儲會話的優點是什麼?使用數據庫存儲會話的優點是什麼?Apr 24, 2025 am 12:16 AM

使用數據庫存儲會話的主要優勢包括持久性、可擴展性和安全性。 1.持久性:即使服務器重啟,會話數據也能保持不變。 2.可擴展性:適用於分佈式系統,確保會話數據在多服務器間同步。 3.安全性:數據庫提供加密存儲,保護敏感信息。

您如何在PHP中實現自定義會話處理?您如何在PHP中實現自定義會話處理?Apr 24, 2025 am 12:16 AM

在PHP中實現自定義會話處理可以通過實現SessionHandlerInterface接口來完成。具體步驟包括:1)創建實現SessionHandlerInterface的類,如CustomSessionHandler;2)重寫接口中的方法(如open,close,read,write,destroy,gc)來定義會話數據的生命週期和存儲方式;3)在PHP腳本中註冊自定義會話處理器並啟動會話。這樣可以將數據存儲在MySQL、Redis等介質中,提升性能、安全性和可擴展性。

什麼是會話ID?什麼是會話ID?Apr 24, 2025 am 12:13 AM

SessionID是網絡應用程序中用來跟踪用戶會話狀態的機制。 1.它是一個隨機生成的字符串,用於在用戶與服務器之間的多次交互中保持用戶的身份信息。 2.服務器生成並通過cookie或URL參數發送給客戶端,幫助在用戶的多次請求中識別和關聯這些請求。 3.生成通常使用隨機算法保證唯一性和不可預測性。 4.在實際開發中,可以使用內存數據庫如Redis來存儲session數據,提升性能和安全性。

您如何在無狀態環境(例如API)中處理會議?您如何在無狀態環境(例如API)中處理會議?Apr 24, 2025 am 12:12 AM

在無狀態環境如API中管理會話可以通過使用JWT或cookies來實現。 1.JWT適合無狀態和可擴展性,但大數據時體積大。 2.Cookies更傳統且易實現,但需謹慎配置以確保安全性。

您如何防止與會議有關的跨站點腳本(XSS)攻擊?您如何防止與會議有關的跨站點腳本(XSS)攻擊?Apr 23, 2025 am 12:16 AM

要保護應用免受與會話相關的XSS攻擊,需採取以下措施:1.設置HttpOnly和Secure標誌保護會話cookie。 2.對所有用戶輸入進行輸出編碼。 3.實施內容安全策略(CSP)限制腳本來源。通過這些策略,可以有效防護會話相關的XSS攻擊,確保用戶數據安全。

您如何優化PHP會話性能?您如何優化PHP會話性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显著提升应用在高并发环境下的效率。

什麼是session.gc_maxlifetime配置設置?什麼是session.gc_maxlifetime配置設置?Apr 23, 2025 am 12:10 AM

theSession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceisesneededeededeedeedeededto toavoidperformance andunununununexpectedLogOgouts.3)

您如何在PHP中配置會話名?您如何在PHP中配置會話名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函數配置會話名稱。具體步驟如下:1.使用session_name()函數設置會話名稱,例如session_name("my_session")。 2.在設置會話名稱後,調用session_start()啟動會話。配置會話名稱可以避免多應用間的會話數據衝突,並增強安全性,但需注意會話名稱的唯一性、安全性、長度和設置時機。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。