ccob/omnipay-wechatpay

Wechatpay gateway for Omnipay payment processing library

v0.1.2 2015-11-24 08:12 UTC

This package is not auto-updated.

Last update: 2024-12-19 01:45:00 UTC


README

Installation

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

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

Usage

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

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:
 *    <xml><return_code><![CDATA[SUCCESS]]></return_code></xml>
 * Or string followed while false
 *    <xml><return_code><![CDATA[FAIL]]></return_code></xml>
 */
$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

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

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

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 <= N < getRefundCount()
$response->getNthRefundId( N );         // 0 <= N < getRefundCount()
$response->getNthRefundFee( N );        // 0 <= N < getRefundCount()
$response->getNthRefundStatus( N );     // 0 <= N < getRefundCount()
$response->getNthRefundChannel( N );    // 0 <= N < getRefundCount()