The purpose of this article is mainly because the SDK provided by the public platform does not provide the SDK implementation of this function. In fact, the final implementation still relies on the public platform development documentation and SDK. Enterprise Payment Application scenario: payment by public accounts to paying users already concerned, for example, the purpose of this article is mainly because the SDK provided by the public platform does not provide the SDK implementation of this function,
In fact, the final implementation is still based on the public platform development documentation and SDK.
Enterprise Payment Application scenario: payment by public accounts to paying users already concerned, such as refund processing and financial settlement
Let's talk about the implementation ideas:
The WxMchPay component is extended based on the self-contained class library in the SDK to implement the enterprise payment function.
To put it bluntly, the code below is a component that inherits the SDK and implements enterprise payment:
$ Parameters parameter reference: Enterprise Payment API documentation
*/Class WxMchPay extends Wxpay_client_pub {/*** API parameter * @ var array * 'mch _ appid '# Public id appid * 'mchid' # merchant ID * 'device _ info' # device number * 'nonce _ str' # random string * 'partner _ trade_no' # merchant order number * 'openid' # beneficiary user openid * 'check _ name' # check User name option for real-name authenticated users, *'re _ user_name '# beneficiary user name * 'amount' # Payment amount * 'desc' # enterprise payment description information * 'spbill _ create_ip' # IP address * 'sign' # Signature */public $ parameters = []; public function _ construct () {$ this-> url =' https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers '; $ This-> curl_timeout = WxPayConf_pub: CURL_TIMEOUT;}/*** generate request xml data * @ return string */public function createXml () {$ this-> parameters ['mch _ appid '] = WxPayConf_pub: APPID; $ this-> parameters ['mchid'] = WxPayConf_pub: mchid; $ this-> parameters ['nonce _ str'] = $ this-> createNoncestr (); $ this-> parameters ['sign'] = $ this-> getSign ($ this-> parameters); return $ this-> arrayToXml ($ this-> parameters );} /*** function: Use the certificate to submit xml to the corresponding interface url in post mode */function postXmlSSLCurl ($ xml, $ url, $ second = 30) {$ ch = curl_init (); // timeout curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second); // Set the proxy here. if yes, // curl_setopt ($ ch, CURLOPT_PROXY, '8. 8.8.8 '); // curl_setopt ($ ch, CURLOPT_PROXYPORT, 8080); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE); // Set the header curl_setopt ($ ch, CURLOPT_HEADER, FALSE); // The result must be a string and be output to curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE); // Set the certificate curl_setopt ($ ch, CURLOPT_CAINFO, WxPayConf_pub: SSLROOTCA_PATH); // use the certificate: cert and key belong to two. pem file // the default format is PEM. you can comment out curl_setopt ($ ch, CURLOPT_SSLCERTTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLCERT, WxPayConf_pub: SSLCERT_PATH ); // the default format is PEM. you can comment out curl_setopt ($ ch, CURLOPT_SSLKEYTYPE, 'pem'); curl_setopt ($ ch, CURLOPT_SSLKEY, WxPayConf_pub: SSLKEY_PATH ); // post submission method: curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml); $ data = curl_exec ($ ch ); // if ($ data) {curl_close ($ ch); return $ data;} else {$ error = curl_errno ($ ch); echo "curl error, error code: $ error "."
"; Echo" error cause query
"; Curl_close ($ ch); return false ;}}}
Controller layer function implementation:
*/Namespace Home \ Controller; class TestController extends CommonController {/*** enterprise payment test */public function rebate () {import ('Common. util. wxmchpay'); $ mchPay = new \ WxMchPay (); // User openid $ mchPay-> setParameter ('openid', 'oy2lbszxkgvlekthrzqezikebzqu '); // merchant Order No. $ mchPay-> setParameter ('partner _ trade_no ', 'Test -'. time (); // check the user name option $ mchPay-> setParameter ('check _ name', 'no _ check'); // enterprise payment amount unit: $ mc HPay-> setParameter ('amount ', 100); // enterprise payment description $ mchPay-> setParameter ('desc', 'Development test '); // custom IP address of the machine that calls the interface $ mchPay-> setParameter ('spbill _ create_ip ', '2017. 0.0.1 '); # getClientIp () // name of the receiving user // $ mchPay-> setParameter ('re _ user_name', 'Max Wen '); // device information // $ mchPay-> setParameter ('device _ info', 'Dev _ server'); $ response = $ mchPay-> postXmlSSL (); if (! Empty ($ response) {$ data = simplexml_load_string ($ response, null, LIBXML_NOCDATA); echo json_encode ($ data );} else {echo json_encode (array ('return _ code' => 'fail ', 'Return _ msg' => 'transfers _ interface error ', 'return _ ext '=> array ()));}}}
After completing the above two sections of code, you can basically successfully call the Enterprise Payment API.
Sample data structure of the returned results:
{ "return_code": "SUCCESS", "return_msg": { }, "mch_appid": "wx519cae424099ed6b", "mchid": "1228636402", "device_info": { }, "nonce_str": "qjupk84q4iqxkb578hb5h2qiatgcwxwg", "result_code": "SUCCESS", "partner_trade_no": "test-1442801966", "payment_no": "1000018301201509210739170397", "payment_time": "2015-09-21 10:19:26"}
Possible problems:
1. CA certificate error
In WxMchPay, we can see that I have rewritten the postXmlSSLCurl () method of Wxpay_client_pub in the SDK.
By default, this method in the SDK does not include a CA certificate in the curl post request.
In comparison, there is more.
Curl_setopt ($ ch, CURLOPT_CAINFO, WxPayConf_pub: SSLROOTCA_PATH); such a line of code.
The function is to attach a CA certificate to the request.
2. the transfer operation for the same user is too frequent. please try again later.
This error is a limitation on the server. the frequency of the error cannot be determined. However, it takes about one minute to test the error.
Therefore, you need to pay more attention during development.
For more development enterprise payment PHP code implementation articles, please follow the PHP Chinese network!