Details about JSAPI payment for WeChat development

Source: Internet
Author: User
Tags openid
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. Keywords: payment v3 jsapi payment unified payment Native payment prepay_id

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,

   
  ou9dHt0L8qFLI1foP-kj5x1mDWsM
    方倍工作室  
  wx88888888888888881414411779
    
  
   
1
    
  http://www.fangbei.org/wxpay/notify_url.php
    
  JSAPI
    wx8888888888888888  
  
   
10012345
    
  61.50.221.43
    
  60uf9sh6nmppr9azveb2bn7arhy79izk
    
  2D8A96553672D56BB2908CE4B0A23D0F
  
 

Submit the data to the unified payment interface

https://api.mch.weixin.qq.com/pay/unifiedorder

The following data will be returned:

   
  SUCCESS
      
  OK
      wx8888888888888888    
  10012345
      
  Be8YX7gjCdtCT7cr
      
  885B6D84635AE6C020EF753A00C8EEDB
      
  SUCCESS
      
  wx201410272009395522657a690389285100
      
  JSAPI
   
 

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:

    
     Secure Payment    

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.