WeChat payment client library for PHP 5.5+
- Simple, intuitive programming interface
- Composer-friendly; just install the package and go!
- PSR-7 compatible
- PSR-3 compatible
- Integrates with Laravel 5
- Fully tested
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 is a pretty good one.
$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',
]);
// 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 = <?php echo json_encode($jsbridge_params) ?>;
WeixinJSBridge.invoke('getBrandWCPayRequest', jsbridge_params, function(result) {
// do something with the result
});
This library represents XML payloads transported between the client and the WeChat web service as messages. A Message is an object that:
- can be serialized to XML
- supports hash-based signing and authentication
- provides key/value access to its attributes
// create a Message instance with the "return_code" attribute
$message = $client->message(['return_code'=>'FAIL']);
$message->set('foo', 1);
$message->set('bar', 'two');
$message->serialize();
// this adds a "sign" attribute to the Mesage instance
$message->sign();
$message->get('sign');
>>> "2C2B2A1D626E750FCFD0ED661E80E3AA"
Generally, whenever you execute a Command
(see below), you will want to
authenticate the result:
$result = $client->command('pay/unifiedorder')->execute([/* ... */]);
$kosher = $result->authenticate();
The various web service calls to the payment API are commands. A
Command is an
object that has an execute
method which returns a Message
.
-
pay/unifiedorder
spec$result = $client->command('pay/unifiedorder')->execute([ 'openid' => 'wx_9f8a98g9a8geag0', 'trade_type' => 'JSAPI', 'out_trade_no' => 'domain-order-id', 'total_fee' => 1000, ]);
-
pay/orderquery
spec$result = $client->command('pay/orderquery')->execute([ 'transaction_id' => '1008450740201411110005820873' ]); // or supply out_trade_no $result = $client->command('pay/orderquery')->execute([ 'out_trade_no' => 'domain-order-id' ]);
-
pay/closeorder
spec (NOT IMPLEMENTED)$result = $client->command('pay/closeorder')->execute([/* ... */]);
-
secapi/refund
spec (NOT IMPLEMENTED)$result = $client->command('secapi/refund')->execute([/* ... */]);
-
pay/refundquery
spec (NOT IMPLEMENTED)$result = $client->command('pay/refundquery')->execute([/* ... */]);
-
pay/downloadbill
spec (NOT IMPLEMENTED)$result = $client->command('pay/downloadbill')->execute([/* ... */]);
-
payitil/report
spec (NOT IMPLEMENTED)$result = $client->command('payitil/report')->execute([/* ... */]);
-
tools/shorturl
spec (NOT IMPLEMENTED)$result = $client->command('tools/shorturl')->execute([/* ... */]);
composer require itc/weixin-payment:1.1.1
The package ships with a Laravel 5 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');
Feel free to fork this project and create a pull request!
./phpunit
The MIT License (MIT)
Copyright (c) 2015 by IT Consultis Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.