PHP code example of ouranoshong / wx-pay-api

1. Go to this page and download the library: Download ouranoshong/wx-pay-api 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/ */

    

ouranoshong / wx-pay-api example snippets


$client = new \WXPay\WXPayClient();

$config = new \WXPay\WXPayConfig();

$config->appId = '';
$config->mchId = '';
$config->key = '';
$config->notifyUrl = '';

$request = new \WXPay\Request\UnifiedOrderRequest();

$request->setBody('');
$request->setOutTradeNo('');
$request->setTotalFee();
$request->setSpBillCreateIp('');
$request->setOpenid('');

// or set other api parameter
$request->setXXX();

$response = new \WXPay\Response\UnifiedOrderResponse();

$client->setConfiguration($config);

$client->setHandlerName(\WXPay\Handler\UnifiedOrderHandler::class);

$client->handle($request, $response);

// print all response default
var_dump(json_encode($response->getResult(), JSON_PRETTY_PRINT));

// get specify reponse message (in camlcase)
$response->getXXX();


// don't check signature
$notify = \WXPay\WXPayNotify::createFromXml('<xml>...</xml>');

// check signature
$notify = \WXPay\WXPayNotify::createFromXml('<xml>...</xml>', 'key');

// get signature from wei xin server push
$notify->sign;

// get something else
$nofiy->getXXX();


// reply success message to wx server
echo \WXPay\WXPayReply::createSuccessReply();

// reply fail messge to wx server
echo \WXPay\WXPayReply::createFailReply('fail message');


// sign `$data` with `$key` use md5
\WXPay\signature($data, $key);

// generate nonce string
\WXPay\generate_nonce_str($len);

// convert asocc array to xml
\WXPay\convert_arr_to_xml($arr);

// convert xml to asocc array
\WXPay\convert_xml_to_arr($xml);