PHPackages                             ccob/omnipay-wechatpay - 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. ccob/omnipay-wechatpay

ActiveLibrary[Payment Processing](/categories/payments)

ccob/omnipay-wechatpay
======================

Wechatpay gateway for Omnipay payment processing library

v0.1.2(10y ago)355MITPHPPHP &gt;=5.4.0

Since Nov 6Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Ccob-x/omnipay-wechatpay)[ Packagist](https://packagist.org/packages/ccob/omnipay-wechatpay)[ Docs](https://github.com/Ccob-x/omnipay-wechatpay)[ RSS](/packages/ccob-omnipay-wechatpay/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (3)Used By (0)

Omnipay-WechatPay
=================

[](#omnipay-wechatpay)

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

[](#installation)

To install, simply add it to your composer.json file:

```
{
    "require": {
        "ccob/omnipay-wechatpay": "dev-master"
    }
}
```

Usage
-----

[](#usage)

### Create unified order

[](#create-unified-order)

```
use Omnipay\Omnipay;

$gateway = Omnipay::create( 'WechatPay' );
$gateway->setAppId( 'Your appid here.' );
$gateway->setMchId( 'Your mch_id here.' );
$gateway->setKey( 'Your key for WeChat payment here.' );
$gateway->setTradeType( 'JSAPI' );
$gateway->setAttach( 'test' );
$gateway->setBody( 'test' );
$gateway->setGoodsTag( 'test' );
$gateway->setOutTradeNo( 'Unique order id in your site.' );
$gateway->setTotalFee( 1 );
$gateway->setSpbillCreateIP( Request::ip() );
$gateway->setNotifyUrl( 'http://test.com/pay/notify' );

$response = $gateway->createUnifiedOrder()->send(); // Get prepay_id, code_url etc.

$package = $response->createWebPaymentPackage(); // Get payment parameters for web
```

### Validate notification parameters

[](#validate-notification-parameters)

You can validate parameters as follow:

```
use Omnipay\Omnipay;

$request_content = file_get_contents('php://input');
$gateway = Omnipay::create( 'WechatPay' );
$gateway->setKey( 'Your key for WeChat payment here.' );

$complete_request = $gateway->completeOrder( $request_content );  // Auto convert xml string to array

$complete_response = $complete_request->send();
$complete_response->isResultSuccessful();
$complete_response->isResponseSuccessful();

/**
 * Would get xml string followed while function 'isResultSuccessful' return Boolean true:
 *
 * Or string followed while false
 *
 */
$complete_response->getResponseText();
```

Or use it as follow. But example above is recommended.

```
use Omnipay\Omnipay;

$request_content = file_get_contents('php://input');
$request_params = json_decode(json_encode(simplexml_load_string($request->getContent(), 'SimpleXMLElement', LIBXML_NOCDATA)), true);

$gateway = Omnipay::create( 'WechatPay' );
$gateway->setKey( 'Your key for WeChat payment here.' );

$complete_request = $gateway->completeOrder(array(
  'request_params' => $request_params
));

$complete_response = $complete_request->send();
$complete_response->isResultSuccessful();
$complete_response->isResponseSuccessful();
$complete_response->getResponseText();
```

### Query order

[](#query-order)

```
use Omnipay\Omnipay;

$gateway = Omnipay::create( 'WechatPay' );
$gateway->setAppId( 'Your appid here.' );
$gateway->setMchId( 'Your mch_id here.' );
$gateway->setKey( 'Your key for WeChat payment here.' );
$request = $gateway->queryOrder([
    'transaction_id'   => 'Transaction id created by wechat'
]);

$response = $request->send();

$response->getOpenId(),
$response->isSubscribe(),
$response->getTradeType(),
$response->getBankType(),
$response->getTotalFee(),
$response->getFeeType(),
$response->getTransactionId(),
$response->getOutTradeNo(),
$response->getAttach(),
$response->getTimeEnd(),
$response->getTradeState(),
$response->getCashFee(),
$response->getTradeStateDesc()
```

### Refund

[](#refund)

```
use Omnipay\Omnipay;

$gateway = Omnipay::create( 'Your appid here.' );
$gateway->setAppId( 'Your appid here.' );
$gateway->setMchId( 'Your mch_id here.' );
$gateway->setKey( 'Your key for WeChat payment here.' );

$request = $gateway->createRefund([
    'ssl_cert_path'     =>  storage_path() . '/cert/apiclient_cert.pem',
    'ssl_key_path'      =>  storage_path() . '/cert/apiclient_key.pem',
    'transaction_id'    =>  '111111111111',
    'out_trade_no'      =>  '222222222222',
    'out_refund_no'     =>  '333333333333',
    'total_fee'         =>  50,
    'refund_fee'        =>  20,
    'op_user_id'        =>  'Your mch_id here.'
]);

$response = $request->send();
$response->isResponseSuccessful();
$response->isSignatureMatched();
$response->isResultSuccessful();
```

### Query refund

[](#query-refund)

```
use Omnipay\Omnipay;

$gateway = Omnipay::create( 'WechatPay' );
$gateway->setAppId( 'Your appid here.' );
$gateway->setMchId( 'Your mch_id here.' );
$gateway->setKey( 'Your key for WeChat payment here.' );

/**
 * One of 'transaction_id', 'out_trade_no', 'out_refund_no', 'refund_id' is required
 */
$request = $gateway->queryRefund([
    'transaction_id'    => '11111111',
    'out_trade_no'      => '22222222',
    'out_refund_no'     => '33333333',
    'refund_id'         => '44444444'
]);

$response = $request->send();
$response->isResultSuccessful();
$response->isResponseSuccessful();
$response->isSignatureMatched();
$response->getTransactionId();
$response->getOutTradeNo();
$response->getRefundFee();
$response->getRefundCount();
$response->getNthOutRefundNo( N );      // 0 getNthRefundId( N );         // 0 getNthRefundFee( N );        // 0 getNthRefundStatus( N );     // 0 getNthRefundChannel( N );    // 0
