PHPackages                             syxwxpay/wxpay-common-fun - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. syxwxpay/wxpay-common-fun

ActiveLibrary

syxwxpay/wxpay-common-fun
=========================

基于微信支付封装的方法，继承了：回调签名验证丶证书的获取丶签名的生成丶报文的解密等

0.01(4y ago)045Apache-2.0PHPPHP &gt;= 7.1

Since Jul 28Pushed 4y ago1 watchersCompare

[ Source](https://github.com/863401844/wx-pay-fun)[ Packagist](https://packagist.org/packages/syxwxpay/wxpay-common-fun)[ RSS](/packages/syxwxpay-wxpay-common-fun/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

syxwxpay/wxpay-common-fun
=========================

[](#syxwxpaywxpay-common-fun)

概览
--

[](#概览)

基于[微信支付API v3](https://wechatpay-api.gitbook.io/wechatpay-api-v3/)再次封装。封装了签名生成丶回调验签丶证书中提取公钥丶报文解密等功能

项目状态
----

[](#项目状态)

当前版本为`0.0.1`测试版本。

环境要求
----

[](#环境要求)

我们开发和测试使用的环境如下：

- PHP 7.0+
- guzzlehttp/guzzle 6.0+
- wechatpay/wechatpay-guzzle-middleware ^0.2.0

安装
--

[](#安装)

可以使用PHP包管理工具composer引入SDK到项目中：

#### Composer

[](#composer)

方式一：在项目目录中，通过composer命令行添加：

```
composer require syxwxpay/wxpay-common-fun
```

#### 第一步使用先引入（如用框架：例如 laravel 则不需要这一步，直接开始下面的步骤即可）

[](#第一步使用先引入如用框架例如-laravel-则不需要这一步直接开始下面的步骤即可)

```
require "vendor/autoload.php";

```

开始 直接调用微信支付已APP支付为例
-------------------

[](#开始-直接调用微信支付已app支付为例)

```
//先加载配置文件
$data = [
    'wx_mch_id'=>'1',//商户号
    'serial_no'=>'1',//api证书号(设置v3证书获得的)
    'wx_APIv3'=>'1',//APIv3密钥
    'wx_appid'=>'1', //微信appid
    'apiclient_key'=>__DIR__.'/wxpay_key/apiclient_key.pem', //API安全->申请证书 完成步骤获得的 apiclient_key.pem文件路径
    'cert'=>__DIR__.'/wxpay_key/cert.pem' //获取平台证书接口中获取的 平台证书文件路径 获取方法可查看下方的【## 获取平台证书】
];
\WxPayFun\WxPayConfig::initConfig($data); //必须初始化
$test = new \WxPayFun\WxPayFun();

//接下来，正常使用Guzzle发起API请求  可以参照微信支付API列表进行传参
$client  = $test->relatedConfig();
try{
    $resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/pay/transactions/app', [
                'json' => [ // JSON请求体
                    'appid' => 1, //你的appid
                    'mchid' => 1,   //你的商户号
                    'description'=>$description, //商品描述
                    'out_trade_no'=>$out_trade_no, //订单号
                    'amount'=>[
                        'total'=>$amount //金额分 int类型 传字符串会报错
                    ],
                    'notify_url'=>$notify_url, //回调地址
                ],
                'headers' => [ 'Accept' => 'application/json']
            ]);
            //以上传参 均按照API列表下的App下单接口传参 其他支付可按照对应的接口进行传参
            $body = $resp->getBody();
            $resdata = json_decode($body->getContents(),true);

           //A生成App端唤起支付的数据
             $data = [
                        'appid'=>'',
                        'timestamp'=>time(),
                        'noncestr'=>$test->makeRandom(32), //生成32位的随机字符串
                        'prepayid'=>$resdata['prepay_id']
                     ];

            $data['sign']= $test->dqsign($data);
            $data['partnerid'] = 1;//商户号
            $data['package'] = 'Sign=WXPay'; //固定值

            return ['error'=>0,'data'=>$data]; //0代表成功 data里的数据 就是App端要的数据
        }catch (RequestException $e) {
            // 进行错误处理
            if ($e->hasResponse()) {
                return  ['error'=>1,'msg'=>$e->getResponse()->getBody()];
            }
            return  ['error'=>2,'msg'=>$e->getMessage()];
        }
//以上按照App下单为例，其他的支付方式可参照微信支付对应的文档接口
```

获取平台证书
------

[](#获取平台证书)

```
$data = [
    'wx_mch_id'=>'1',//商户号
    'serial_no'=>'1',//api证书号(设置v3证书获得的)
    'wx_APIv3'=>'1',//APIv3密钥
    'wx_appid'=>'1', //微信appid
    'apiclient_key'=>__DIR__.'/wxpay_key/apiclient_key.pem', //API安全->申请证书 完成步骤获得的 apiclient_key.pem文件路径
];
\WxPayFun\WxPayConfig::initConfig($data); //必须初始化
$test = new \WxPayFun\WxPayFun();

/**
 * 获取/更新 证书和编号
 * @param string $filename 生成后证书文件保存路径 例如：www/wwwroot/wxce/wx_pay/   绝对路径,生成的证书文件名为cert.pem
 * @return mixed 返回值为serial_no号
 */

 try{
     // 此方法建议放入定时任务种，执行时间不 大于 24 小时
     $serial_no = $test->filePutWxCert($filename);
 }catch (\Exception $e){
     //获取失败 错误均为data中数据不正确或者权限问题
 }
```

回调验证签名
------

[](#回调验证签名)

```
$data = [
    'cert'=>__DIR__.'/wxpay_key/cert.pem' //获取平台证书接口中获取的 平台证书文件路径
];
\WxPayFun\WxPayConfig::initConfig($data); //必须初始化
$test = new \WxPayFun\WxPayFun();
 /**
 * 验证回调主方法  verifySigns
 * @param $http_data header头的信息（微信回调发送的header头）可用getallheaders()获取
 * @param $body 应答主体信息 (微信发送的数据)(未解密的数据!!!) 一个json字符串 如果出现中文乱码，必须转义
 * @return int 验证成功返回 1  失败返回 0
 */
 $http_data = getallheaders();
 $info = file_get_contents("php://input"); //如果里面有中文乱码 必须转义
$red = $test->verifySigns($http_data,$body); //验证{$red}返回 1和0即可
if($red == 1){
 //你的微信逻辑
}
```

报文解密
----

[](#报文解密)

```
$wx_APIv3 = '1';//APIv3密钥 微信商户平台 -API安全里设置
$jm = new \WxPayFun\AesUtil($wx_APIv3);
//参考例子：报文
$res = '{
  "data": [
      {
          "serial_no": "5157F09EFDC096DE15EBE81A47057A7232F1B8E1",
          "effective_time ": "2018-06-08T10:34:56+08:00",
          "expire_time ": "2018-12-08T10:34:56+08:00",
          "encrypt_certificate": {
              "algorithm": "AEAD_AES_256_GCM",
              "nonce": "61f9c719728a",
              "associated_data": "certificate",
              "ciphertext": "sRvt… "
          }
      },
      {
          "serial_no": "50062CE505775F070CAB06E697F1BBD1AD4F4D87",
          "effective_time ": "2018-12-07T10:34:56+08:00",
          "expire_time ": "2020-12-07T10:34:56+08:00",
          "encrypt_certificate": {
              "algorithm": "AEAD_AES_256_GCM",
              "nonce": "35f9c719727b",
              "associated_data": "certificate",
              "ciphertext": "aBvt… "
          }
      }
  ]
}';
$res = json_decode($res,true);
$enc = $res['data'][0]['encrypt_certificate'];
$str = $jm->decryptToString($enc['associated_data'],$enc['nonce'],$enc['ciphertext']); //解密之后的数据
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

1749d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a2c2a66c0b6a7c1c8d312868b0fbd121088c8764df67fde251eaa210a4188a0a?d=identicon)[863401844](/maintainers/863401844)

---

Top Contributors

[![863401844](https://avatars.githubusercontent.com/u/34825196?v=4)](https://github.com/863401844 "863401844 (7 commits)")

### Embed Badge

![Health badge](/badges/syxwxpay-wxpay-common-fun/health.svg)

```
[![Health](https://phpackages.com/badges/syxwxpay-wxpay-common-fun/health.svg)](https://phpackages.com/packages/syxwxpay-wxpay-common-fun)
```

###  Alternatives

[neuron-core/neuron-ai

The PHP Agentic Framework.

1.8k245.3k21](/packages/neuron-core-neuron-ai)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3731.2M42](/packages/tencentcloud-tencentcloud-sdk-php)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
