PHP code example of papajo / wxpay

1. Go to this page and download the library: Download papajo/wxpay 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/ */

    

papajo / wxpay example snippets



use WXPay\WXPay;
use WXPay\WXPayConstants;
use WXPay\WXPayUtil;

$wxpay = new WXPay(
        'wx888888888',  // appid
        '22222222',     // mch id
        '123456781234567812345678',  // key
        '/path/to/apiclient_cert.pem',
        '/path/to/apiclient_key.pem',
        6000);  // 超时时间,毫秒
$resp = $wxpay->orderQuery(array(
                  'out_trade_no' => '201610265257070987061763',
                  'total_fee' => 1,
                  'body' => '腾讯充值中心-QQ会员充值',
                  'spbill_create_ip' => '123.12.12.123',
                  'trade_type' => 'NATIVE',
                  'notify_url' => 'https://www.example.com/wxpay/notify'
              ));
var_dump($resp);


use WXPay\WXPay;
use WXPay\WXPayConstants;
use WXPay\WXPayUtil;

$wxpay = new WXPay(
        'wx888888888',  // appid
        '22222222',     // mch id
        '123456781234567812345678',  // key
        '/path/to/apiclient_cert.pem',
        '/path/to/apiclient_key.pem',
        6000);  // 超时时间,毫秒
$resp = $wxpay->orderQuery(array('out_trade_no' => '201610265257070987061763'));
var_dump($resp);

$wxpay = new WXPay(
        'wx888888888',  // appid
        '22222222',     // mch id
        '123456781234567812345678',  // key
        '/path/to/apiclient_cert.pem',
        '/path/to/apiclient_key.pem',
        6000,  // 超时时间,毫秒
        WXPayConstants::SIGN_TYPE_HMACSHA256);  
$resp = $wxpay->orderQuery(array('out_trade_no' => '201610265257070987061763'));
var_dump($resp);

$useSandbox = true;

$wxpay = new WXPay(
       'wx888888888',  // appid
       '22222222',     // mch id
       '123456781234567812345678',  // 沙箱环境key
       '/path/to/apiclient_cert.pem',
       '/path/to/apiclient_key.pem',
       6000,  // 超时时间,毫秒
       WXPayConstants::SIGN_TYPE_MD5,
       $useSandbox);

var_dump( $wxpay->orderQuery(array('out_trade_no' => '201610265257070987061763')) );

$data = array(
    'bill_date' => '20140603',
    'bill_type' => 'ALL',
    'tar_type' => 'GZIP'
);
$wxpay = new WXPay(
        'wx888888888',  // appid
        '22222222',     // mch id
        '123456781234567812345678',  // key
        '/path/to/apiclient_cert.pem',
        '/path/to/apiclient_key.pem',
        6000);  // 超时时间,毫秒
$data = $wxpay->fillRequestData($data); // 会添加appid、mch_id、sign_type、sign、nonce_str
var_dump($data);
echo "\n";
echo \WXPay\WXPayUtil::array2xml($data);

$wxpay = new WXPay(
    'wx888888888',  // appid
    '22222222',     // mch id
    "123456781234567812345678",  // key
    '/path/to/apiclient_cert.pem',
    '/path/to/apiclient_key.pem',
    6000);

$xml = "<xml> 
    ......
    <sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign> 
    </xml>";
$data = WXPayUtil::xml2array($xml);
// $data必须有sign字段,也就是return_code必须为SUCCESS。否则返回false
var_dump($wxpay->isPayResultNotifySignatureValid($data));  // 布尔类型,标识签名是否正确