Php WeChat payment interface development program, php payment Interface

Source: Internet
Author: User
Tags flock openid

Php payment interface development program, php payment Interface

Php payment Interface Development Program:

Prerequisites:
Appid // obtain from the Developer Center of the Public Account (same as in the email)

Mchid // obtain in-Email

Key // set by the merchant's background

Appsecret // obtain the public account from the Developer Center
Two certificate files. Obtain apiclient_cert.pem apiclient_key.pem in the email.
Note:
Public Account background payment-> Development Configuration-> new test directory and test personal number.
Developer Center-webpage authorization to get basic user information-change to your test domain name. Otherwise, the redirect_uri parameter is incorrect.
---------- To be improved in the future -------
The payment ready page has three operations in the background:

1. Obtain the openid

// Use the jsapi interface code to copy the code $ jsApi = new JsApi_pub (); // ========== Step 1: obtain openid through code if (! Isset ($ _ GET ['code']) {// return code $ url = $ jsApi-> createOauthUrlForCode (WxPayConf_pub: JS_API_CALL_URL); // echo $ url; header ("Location: $ url");} else {// GET the code to GET openid $ code =$ _ GET ['code']; $ jsApi-> setCode ($ code); $ openid = $ jsApi-> getOpenid ();}

At the beginning, the first step encountered a problem. The openid cannot be obtained because it is related to some servers. The curl method is used in the demo.
It is strange that my server curl cannot be obtained all the time. After that, you can change it to file_get_contents to get it normally.
But this is not the solution. Because more curl operations are required later.
I can see that there is a place in the development documentation that the certificate writing operation requires libcurl 7.20.1 or later, and then I have been trying to improve the php curl version of linux on the whole server. At last, I changed to another windows server.
Now, debug it the next time you need it.

Step 2:Get and Pay Order Number id
The Code is as follows:

$ UnifiedOrder = new UnifiedOrder_pub (); // var_dump ($ unifiedOrder); // you can specify the parameters for the unified payment interface. // you can specify the required parameters. // appid is set, the merchant does not need to fill in the/mch_id field repeatedly, the merchant does not need to fill in the/noncestr field repeatedly, the merchant does not need to fill in the/spbill_create_ip field repeatedly, and the Merchant does not need to fill in the/sign field repeatedly, the merchant does not need to fill in $ unifiedOrder-> setParameter ("openid", "$ openid"); // product description $ unifiedOrder-> setParameter ("body", ""); // product description // custom order number. For example, $ timeStamp = time (); $ out_trade_no = WxPayConf_pub: APPID. "$ timeStamp"; $ unifiedOrder-> setParameter ("out_trade_no", "$ out_trade_no"); // Merchant Order No. $ unifiedOrder-> setParameter ("total_fee", "1 "); // total amount $ unifiedOrder-> setParameter ("yy_url", WxPayConf_pub: notify_url); // notification address $ unifiedOrder-> setParameter ("trade_type", "JSAPI "); // transaction type // a non-required parameter. The Merchant can select this parameter based on the actual situation. // $ unifiedOrder-> setParameter ("sub_mch_id", "XXXX "); // sub-merchant ID // $ unifiedOrder-> setParameter ("device_info", "XXXX"); // device ID // $ unifiedOrder-> setParameter ("attach ", "XXXX"); // additional data // $ unifiedOrder-> setParameter ("time_start", "XXXX "); // transaction start time // $ unifiedOrder-> setParameter ("time_expire", "XXXX"); // transaction End Time // $ unifiedOrder-> setParameter ("goods_tag ", "XXXX"); // product tag // $ unifiedOrder-> setParameter ("openid", "XXXX "); // user ID // $ unifiedOrder-> setParameter ("product_id", "XXXX"); // item ID $ prepay_id = $ unifiedOrder-> getPrepayId (); // echo 'prepay _ id: '; var_dump ($ prepay_id );

This step also encountered many problems.
First, it is difficult to pay for the test. Only the test can be performed. I used my cell phone to brush it.
Second, var_dump debugging is not good. Print some xml files that only show the length of characters, not the content. Therefore, logs are written to the server for debugging. The log code is as follows:
The Code is as follows:

// Print log function log_d ($ word) {$ log_name = ". /logd. log "; // log file path $ fp = fopen ($ log_name," a "); flock ($ fp, LOCK_EX); fwrite ($ fp," execution date :". strftime ("% Y-% m-% d-% H: % M: % S", time ()). "n ". $ word. "nn"); flock ($ fp, LOCK_UN); fclose ($ fp );}

Use $ this-> log_d (xxx); In WxPayPubHelper. php In the demo.
At the beginning, an error was reported because the mchid and appid do not match .. They gave me the wrong account. I didn't know how to try it at the beginning. Debug this step in the getPrepayId () var_dump ($ this-> result); To see the error code.

Step 3:The js Code for generating the payment front-end is ready on the webpage:
The Code is as follows:

$jsApi->setPrepayId($prepay_id); $jsApiParameters = $jsApi->getParameters();

-------- Click to pay ---------

This part has encountered the following problems:
Android returns "System: Access_denied", ios returns "access_control: not_allowed"
I searched Baidu many times. In fact, I have never noticed this thing!
The page for initiating an authorization request must be in the authorization directory, but not in a subdirectory. Otherwise, an error is returned.
I put the payment file in/Domain Name/pay/demo/
At the beginning, I always thought it was okay at the end of/Domain Name/pay. Sub-directories are supported, and the results cannot be returned !.
--------- Last reading -----

Wxpay1
Wxpay3
Wxpay2

------ Xmljs ------- in the process -------
Generation and payment order id to be submitted:
The Code is as follows:

<Xml> <openid> <! [CDATA [ou9dHt0L8qFLI1foP-kj5x1mDWsM]> </openid> <body> <! [CDATA [Contribution]> </body> <out_trade_no> <! [CDATA [wx888888888888881414411779]> </out_trade_no> <total_fee> 1 </total_fee> <policy_url> <! [CDATA [http://shanmao.me/wxpay/policy_url.php?]> </policy_url> <trade_type> <! [CDATA [JSAPI]> </trade_type> <appid> <! [CDATA [wx88888888888888]> </appid> <mch_id> 10012345 </mch_id> <spbill_create_ip> <! [CDATA [61.50.221.43]> </spbill_create_ip> <nonce_str> <! [CDATA [60uf9sh6nmppr9azveb2bn7arhy79izk]> </nonce_str> <sign> <! [CDATA [2D8A96553672D56BB2908CE4B0A23D0F]> </sign> </xml>

After submission, the returned result is correct, including perpay_id:

<xml> <return_code><![CDATA[SUCCESS]]></return_code>  <return_msg><![CDATA[OK]]></return_msg>  <appid><![CDATA[wx8888888888888888]]></appid>  <mch_id><![CDATA[10012345]]></mch_id>  <nonce_str><![CDATA[Be8YX7gjCdtCT7cr]]></nonce_str>  <sign><![CDATA[885B6D84635AE6C020EF753A00C8EEDB]]></sign>  <result_code><![CDATA[SUCCESS]]></result_code>  <prepay_id><![CDATA[wx201410272009395522657a690389285100]]></prepay_id>  <trade_type><![CDATA[JSAPI]]></trade_type> </xml>

Generate the js for payment:

{  "appId": "wx8888888888888888",  "timeStamp": "1414411784",  "nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75",  "package": "prepay_id=wx201410272009395522657a690389285100",  "signType": "MD5",  "paySign": "9C6747193720F851EB876299D59F6C7D"}

Notification xml returned after successful payment:

<xml><appid><![CDATA[wx8888888888]]></appid><bank_type><![CDATA[CCB_DEBIT]]></bank_type><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[1011111]]></mch_id><nonce_str><![CDATA[38gt0ffgsvfsdfsdfbt1981duv63p7]]></nonce_str><openid><![CDATA[o4p3SjfdsfdsfdsdCE5Y2XHw4]]></openid><out_trade_no><![CDATA[wx4b56d1fsdfdsf416643247]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[356EfsdfdsfsdsfE69509EDA344]]></sign><sub_mch_id><![CDATA[10018826]]></sub_mch_id><time_end><![CDATA[20141122160122]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[100715001020fsdfsd1220006123174]]></transaction_id></xml>

I am free to use the data here. You can refer to the following format. Note the case sensitivity.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.