PHP code example of ccob / omnipay-wechatpay

1. Go to this page and download the library: Download ccob/omnipay-wechatpay library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ccob / omnipay-wechatpay example snippets


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

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();

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();

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()

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();

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 essful();
$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()