Payment development (1) js api payment, jsapi
Keywords: Payment v3 jsapi payment unified payment Native payment prepay_id
Author: Fang Times studio
Original article: http://blog.csdn.net/pondbay/article/details/40536677
This article introduces the implementation process of jsapi under payment.
Preface
Payment is now divided into v2 and v3. The application for payment is v2 before January 1, September 10, 2014, and the application for payment is v3. The paySignKey parameter is not provided for payment in V3. For more information about v2, see other articles by Fang Times studio. This article introduces payment v3.
Process Implementation 1. OAuth2.0 authorization
Before JSAPI payment, you must call the logon authorization interface to obtain the user's Openid. Therefore, an authorization is required. The confirmation box is not displayed for this authorization.
The essence is that
http://www.fangbei.org/wxpay/js_api_call.php
Jump
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8888888888888888&redirect_uri=http://www.fangbei.org/wxpay/js_api_call.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
To obtain the code parameter, and obtain the authorized access_token and openid according to the code.
For details about the implementation process, refer to OAuth2.0 webpage authorization on the public platform development (71 ).
In the payment Demo, the code is
1 // use jsapi interface 2 $ jsApi = new JsApi_pub (); 3 4 // =========== Step 1: openid 6 if (! Isset ($ _ GET ['code']) 7 {8 // trigger return code 9 $ url = $ jsApi-> createOauthUrlForCode (WxPayConf_pub: JS_API_CALL_URL ); 10 Header ("Location: $ url"); 11} else12 {13 // GET the code to GET openid14 $ code = $ _ GET ['code']; 15 $ jsApi-> setCode ($ code); 16 $ openid = $ jsApi-> getOpenId (); 17}
The final result of this step is to obtain the openid of the current user.
ou9dHt0L8qFLI1foP-kj5x1mDWsM
2. Unified payment
Unified payment is the interface for generating payment orders and returning pre-payment order numbers in various payment scenarios of JSAPI/NATIVE/APP. Currently, this interface is used in all payment scenarios.
In unified payment, the following parameters are obtained from the configuration or automatically generated by the class.
$ This-> parameters ["appid"] = WxPayConf_pub: APPID; // public account ID $ this-> parameters ["mch_id"] = WxPayConf_pub: MCHID; // merchant ID $ this-> parameters ["spbill_create_ip"] = $ _ SERVER ['remote _ ADDR ']; // Terminal ip $ this-> parameters ["nonce_str"] = $ this-> createNoncestr (); // random string $ this-> parameters ["sign"] = $ this-> getSign ($ this-> parameters); // Signature
In JSAPI payment, enter the following parameters
// In the unified payment interface, when trade_type is JSAPI, openid is a required parameter! $ 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
Others are optional parameters
// This parameter is not required. The Merchant can select the parameter/$ unifiedOrder-> setParameter ("sub_mch_id", "XXXX") based on the actual situation "); // 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
These parameters form such xml data,
<Xml> <openid> <! [CDATA [ou9dHt0L8qFLI1foP-kj5x1mDWsM]> </openid> <body> <! [CDATA [Fang Times studio]> </body> <out_trade_no> <! [CDATA [wx888888888888881414411779]> </out_trade_no> <total_fee> 1 </total_fee> <policy_url> <! [CDATA [http://www.fangbei.org/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>
Submit the data to the unified payment Interface
https://api.mch.weixin.qq.com/pay/unifiedorder
The following data will be returned:
<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>
It contains the most important pre-payment ID parameter, prepay_id, and the value is
wx201410272009395522657a690389285100
3. js api payment
After the preparations are completed, js api generates jsapi payment parameters based on prepay_id.
The generated code is as follows:
// ========== Step 3: use jsapi to initiate a payment =============$ jsApi-> setPrepayId ($ prepay_id); $ jsApiParameters = $ jsApi-> getParameters ();
The generated json data is as follows:
{ "appId": "wx8888888888888888", "timeStamp": "1414411784", "nonceStr": "gbwr71b5no6q6ne18c8up1u7l7he2y75", "package": "prepay_id=wx201410272009395522657a690389285100", "signType": "MD5", "paySign": "9C6747193720F851EB876299D59F6C7D"}
Debug the js interface in the browser. The Code is as follows:
<Html>
When you click "Contribute", the payment plug-in will pop up and you can start to pay.
4. Payment notification
After the payment is successful, the notification interface will also receive an xml notification of successful payment.
<xml> <appid><![CDATA[wx8888888888888888]]></appid> <bank_type><![CDATA[CFT]]></bank_type> <fee_type><![CDATA[CNY]]></fee_type> <is_subscribe><![CDATA[Y]]></is_subscribe> <mch_id><![CDATA[10012345]]></mch_id> <nonce_str><![CDATA[60uf9sh6nmppr9azveb2bn7arhy79izk]]></nonce_str> <openid><![CDATA[ou9dHt0L8qFLI1foP-kj5x1mDWsM]]></openid> <out_trade_no><![CDATA[wx88888888888888881414411779]]></out_trade_no> <result_code><![CDATA[SUCCESS]]></result_code> <return_code><![CDATA[SUCCESS]]></return_code> <sign><![CDATA[0C1D7F2534F1473247550A5A138F0CEB]]></sign> <sub_mch_id><![CDATA[10012345]]></sub_mch_id> <time_end><![CDATA[20141027200958]]></time_end> <total_fee>1</total_fee> <trade_type><![CDATA[JSAPI]]></trade_type> <transaction_id><![CDATA[1002750185201410270005514026]]></transaction_id> </xml>
Authentication Service No. Payment js api webpage payment Native payment How to fill in these URLs
If you have cooperation with a third party, you will provide these directory paths. Otherwise, you are a developer and provide the directory paths. There are no other options. If you have any questions, for more details, please pay attention to my "golden companion". My service number is also applied for payment, and then we work with a third party, so we have some teaching materials here.
Must the developer mode or third-party cooperation be required for the opening and payment of public platforms?
This usually requires a third party to write the payment interface, which is difficult for you