PHPackages                             itc/weixin-payment - 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. [Payment Processing](/categories/payments)
4. /
5. itc/weixin-payment

ActiveLibrary[Payment Processing](/categories/payments)

itc/weixin-payment
==================

WeChat payment library

1.3.0(9y ago)127095MITPHP

Since Jul 24Pushed 9y ago10 watchersCompare

[ Source](https://github.com/itconsultis/weixin-payment)[ Packagist](https://packagist.org/packages/itc/weixin-payment)[ RSS](/packages/itc-weixin-payment/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (20)Used By (0)

weixin-payment
==============

[](#weixin-payment)

WeChat payment client library for PHP 5.5+

[![Build Status](https://camo.githubusercontent.com/3664e1098ad240f5d175f878ceaf8aacd77c17fb9e9e4f5350483cdc1a6d55c1/68747470733a2f2f7472617669732d63692e6f72672f6974636f6e73756c7469732f77656978696e2d7061796d656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/itconsultis/weixin-payment)

Features
--------

[](#features)

- Simple, intuitive programming interface
- Composer-friendly; just install the package and go!
- [PSR-7](http://www.php-fig.org/psr/psr-7/) compatible
- [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) compatible
- Integrates with [Laravel 5](http://laravel.com)
- Fully tested

What it does (and doesn't)
--------------------------

[](#what-it-does-and-doesnt)

The client exposes a simple programming interface to WeChat's payment-related web service calls. It transparently handles boilerplate stuff like request signing and XML serialization so you can focus on things that matter.

This package does not perform authentication; it will *not* help you get a user's OpenID. Fortunately, there are plenty of other packages that already do this. [overtrue/wechat](https://packagist.org/packages/overtrue/wechat) is a pretty good one.

Usage
-----

[](#usage)

#### Create a Client instance

[](#create-a-client-instance)

```
$client = \ITC\Weixin\Payment\Client::instance([
    'app_id' => 'your appid',
    'secret' => 'your signing secret',
    'mch_id' => 'your merchant id',
    'public_key_path' => '/path/to/public_key',
    'private_key_path' => '/path/to/private_key',
]);
```

#### Start a payment

[](#start-a-payment)

```
// execute the "pay/unifiedorder" command; the result is a Message instance
$result = $client->command('pay/unifiedorder')->execute([
    'openid' => 'wx_9f8a98g9a8geag0',
    'trade_type' => 'JSAPI',
    'out_trade_no' => 'your-order-id',
    'total_fee' => 1000,
]);

// authenticate the result
$authentic = $result->authenticate();

// if a prepay_id is in the Message, the payment is ready to execute
if ($authentic && $prepay_id = $result->get('prepay_id'))
{
    // jsapize() returns a JsonSerializable object
    $jsbridge_params = $client->jsapize(['prepay_id'=>$prepay_id]);
}
```

The `$jsbridge_params` object can then be JSON-serialized and supplied directly to the `WeixinJSBridge` global in the Javascript space. Here's an example:

```
var jsbridge_params = ;

WeixinJSBridge.invoke('getBrandWCPayRequest', jsbridge_params, function(result) {
    // do something with the result
});
```

Messages
--------

[](#messages)

This library represents XML payloads transported between the client and the WeChat web service as *messages*. A [Message](https://github.com/itconsultis/weixin-payment/blob/master/src/ITC/Weixin/Payment/Contracts/Message.php)is an object that:

- can be serialized to XML
- supports hash-based signing and authentication
- provides key/value access to its attributes

#### How to create a Message instance

[](#how-to-create-a-message-instance)

```
// create a Message instance with the "return_code" attribute
$message = $client->message(['return_code'=>'FAIL']);
```

#### How to add message attributes

[](#how-to-add-message-attributes)

```
$message->set('foo', 1);
$message->set('bar', 'two');
```

#### How to convert a message to XML

[](#how-to-convert-a-message-to-xml)

```
$message->serialize();

```

#### How to sign a message

[](#how-to-sign-a-message)

```
// this adds a "sign" attribute to the Mesage instance
$message->sign();

$message->get('sign');
>>> "2C2B2A1D626E750FCFD0ED661E80E3AA"
```

#### How to authenticate a signed message

[](#how-to-authenticate-a-signed-message)

Generally, whenever you execute a `Command` (see below), you will want to authenticate the result:

```
$result = $client->command('pay/unifiedorder')->execute([/* ... */]);
$kosher = $result->authenticate();
```

Commands
--------

[](#commands)

The various web service calls to the payment API are *commands*. A [Command](https://github.com/itconsultis/weixin-payment/blob/master/src/ITC/Weixin/Payment/Contracts/Command.php) is an object that has an `execute` method which returns a `Message`.

- `pay/unifiedorder` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_1)

    ```
    $result = $client->command('pay/unifiedorder')->execute([
        'openid' => 'wx_9f8a98g9a8geag0',
        'trade_type' => 'JSAPI',
        'out_trade_no' => 'domain-order-id',
        'total_fee' => 1000,
    ]);
    ```
- `pay/orderquery` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_2&index=4)

    ```
    // query a payment by wechat transaction id
    $result = $client->command('pay/orderquery')->execute([
        'transaction_id' => '1008450740201411110005820873'
    ]);

    // or by domain order id ("out_trade_no")
    $result = $client->command('pay/orderquery')->execute([
        'out_trade_no' => 'domain-order-id'
    ]);
    ```
- `pay/closeorder` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_3&index=5) (NOT IMPLEMENTED)

    ```
    $result = $client->command('pay/closeorder')->execute([/* ... */]);
    ```
- `secapi/refund` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_4&index=6) (NOT IMPLEMENTED)

    ```
    $result = $client->command('secapi/refund')->execute([/* ... */]);
    ```
- `pay/refundquery` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_5&index=7) (NOT IMPLEMENTED)

    ```
    $result = $client->command('pay/refundquery')->execute([/* ... */]);
    ```
- `pay/downloadbill` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_6&index=8) (NOT IMPLEMENTED)

    ```
    $result = $client->command('pay/downloadbill')->execute([/* ... */]);
    ```
- `payitil/report` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_8&index=9) (NOT IMPLEMENTED)

    ```
    $result = $client->command('payitil/report')->execute([/* ... */]);
    ```
- `tools/shorturl` [spec](https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_9&index=10) (NOT IMPLEMENTED)

    ```
    $result = $client->command('tools/shorturl')->execute([/* ... */]);
    ```
- `mmpaymkttransfers/sendredpack` [spec](https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5)

    ```
    $result = $client->command('mmpaymkttransfers/sendredpack')->execute([
        'mch_billno' => '10000098201411111234567890',
        'send_name' => '天虹百货',
        're_openid' => 'oxTWIuGaIt6gTKsQRLau2M0yL16E',
        'total_amount' => 1000,
        'total_num' => 1,
        'client_ip' => '192.168.0.1',
        'wishing' => '感谢您参加猜灯谜活动，祝您元宵节快乐！',
        'act_name' => '猜灯谜抢红包活动',
        'remark' => '猜越多得越多，快来抢！',
    ]);
    ```
- `mmpaymkttransfers/gethbinfo` [spec](https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_7)

    ```
    $result = $client->command('mmpaymkttransfers/gethbinfo')->execute([
        'mch_billno' => '10000098201411111234567890',
        'bill_type' => 'MCHT',
    ]);
    ```

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require itc/weixin-payment:1.3.0

```

### Laravel

[](#laravel)

The package ships with a [Laravel 5](http://laravel.com) service provider that registers the Client on the application service container. Install the service provider by adding the following line to the `providers`array in `config/app.php`:

```
ITC\Weixin\Payment\ServiceProvider::class

```

Then publish the package configuration via:

```
php artisan vendor:publish

```

Now you can access the client instance via dependency injection or through the service container:

```
$client = App::make('ITC\Weixin\Payment\Contracts\Client');
```

Contributing
------------

[](#contributing)

Feel free to fork this project and create a pull request!

### How to implement a Command

[](#how-to-implement-a-command)

1. Implement a concrete `ITC\Weixin\Payment\Contracts\Command`. Feel free to extend `ITC\Weixin\Payment\Command\Command`.
2. Register the command inside `Client::instance()`:

    ```
    public static function instance()
    {
        ...

        $client->register(Command\YourCommand::class);
        ...
    }

    ```

#### How to run tests

[](#how-to-run-tests)

```
./phpunit

```

License
-------

[](#license)

[MIT](./LICENSE)

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 92.1% 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 ~28 days

Recently: every ~101 days

Total

18

Last Release

3463d ago

Major Versions

0.3.3 → 1.0.02015-07-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/96bd7a3ca2e2960b631718607fe231ed0a9611af7a46bc391339e3faabe2d743?d=identicon)[rich-choy](/maintainers/rich-choy)

---

Top Contributors

[![rich-choy](https://avatars.githubusercontent.com/u/433756?v=4)](https://github.com/rich-choy "rich-choy (163 commits)")[![benjah1](https://avatars.githubusercontent.com/u/1473730?v=4)](https://github.com/benjah1 "benjah1 (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/itc-weixin-payment/health.svg)

```
[![Health](https://phpackages.com/badges/itc-weixin-payment/health.svg)](https://phpackages.com/packages/itc-weixin-payment)
```

###  Alternatives

[dnetix/redirection

Library to connect with PlacetoPay Checkout service

17123.3k2](/packages/dnetix-redirection)[buckaroo/sdk

Buckaroo payment SDK

12189.1k9](/packages/buckaroo-sdk)[amazonpaymentservices/aps-php-sdk

Amazon Payment Services PHP SDK

3114.6k](/packages/amazonpaymentservices-aps-php-sdk)[contica/facturador-electronico-cr

Un facturador de código libre para integrar facturación electrónica en Costa Rica a un proyecto PHP

2128.8k](/packages/contica-facturador-electronico-cr)

PHPackages © 2026

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