Payment JSAPI mode and refund CodeIgniter integration-against water cold Dragon Payment Interface documentation: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php? Chapter = 7_1
First of all, you must know that this jsapi cannot be left for calling and payment. we are ready to understand this truth. the page is displayed on the inside and the payment component is called through jsapi for payment.
Let's take a look at our previous article, mainly Native code scanning payment mode 2.
We continue to use the wechatpay. php payment integration class, which is simple and easy to understand. However, if jsapi is applied, this class has a bug.
There is a timestamp when we construct the parameters required by jsapi. We use time () to generate,A message is displayed, indicating that the parameter timeStamp is missing for calling JSAPI.
Modify as follows:
/*** Get the second parameter used for js payment */public function get_package ($ prepay_id) {$ data = array (); $ data ["appId"] = $ this-> _ config ["appid"]; // change it to a string $ time = time (); $ data ["timeStamp"] = "\"". $ time. "\" "; $ data [" nonceStr "] = $ this-> get_nonce_string (); $ data [" package "] =" prepay_id = $ prepay_id "; $ data ["signType"] = "MD5"; $ data ["paySign"] = $ this-> sign ($ data); return $ data ;}
In fact, this method is used to obtain the jsapi payment parameters.
I. JSAPI payment
You cannot forget to configure the authorization Directory. to call jsapi, I configured this directory at http://xxx.com/index.php/home.
First, we still need to call the unified order interface to obtain the parameters we need (if such configuration is not available, please refer to the previous article). This is the pay method, when calling the unified order interface, we need to know the required parameters.
1. to obtain the openid, I used an API class library for the project, https://github.com/dodgepudding/wechat-php-sdk. the main method is to use this.
Some friends in this project have specially integrated the extended class library of the CodeIgniter framework, which can be used directly and the directory structure. let's go directly to the code.
Public function _ construct () {parent ::__ construct (); $ this-> load-> library ('ci _ Wechat '); // because my projects are tied together at all times, they are directly loaded into the constructor without loading every method. $ This-> load-> library ('pagination ');}
CI_Model: Let's take a look at the above class library source code and how to configure it. let's take a look at how to obtain the openid.
Function oauthurl () {$ oauth_url = $ this-> ci_wechat-> getOauthRedirect (base_url (). 'index. php/home/oauth ', 1); header ('Location :'. $ oauth_url); exit ();} function oauth () {if (! Isset ($ _ GET ['code']) {// trigger return code $ baseUrl = urlencode ('http ://'. $ _ SERVER ['http _ host']. $ _ SERVER ['php _ SELF ']. $ _ SERVER ['query _ string']); $ url = $ this->__ CreateOauthUrlForCode ($ baseUrl); Header ("Location: $ url "); exit ();} else {$ json = $ this-> ci_wechat-> getoauthaccesen en (); $ openid = $ json ['openid']; // register a user, after successful, you can obtain a ticket // return $ this-> _ isRegistered ($ _ SESSION ['user'] ['openid']); return $ openid ;}}
The above two methods are used to obtain the openid. after obtaining the openid, I saved it in the session. on each page, I decided whether to obtain the openid directly.
$this->session->set_userdata('openid', $this->oauth());
This ensures that the openid is always available.
2. construct the parameters required for JSAPI payment (unified parameter construction for single order)
$ This-> load-> model ('publist '); // Obtain the order information $ pub = $ this-> publist-> GetList (array ('id' = >$ _ SESSION ['orderid']); // read the parameter configuration of the payment configuration $ this-> load-> config ('wxpay _ config '); $ wxconfig ['appid '] = $ this-> config-> item ('appid '); $ wxconfig ['mch _ id'] = $ this-> config-> item ('mch _ id '); $ wxconfig ['apikey'] = $ this-> config-> item ('apikey '); $ wxconfig ['appsecret'] = $ this-> config-> item ('appsecret'); $ wxconfig ['sslcertpath'] = $ this-> co Nfig-> item ('sslcertpath'); $ wxconfig ['sslkeypath'] = $ this-> config-> item ('sslkeypath '); $ this-> load-> library ('wechatpa', $ wxconfig); // merchant transaction ticket No. $ out_trade_no = $ pub-> listno; $ total_fee = $ pub-> detail; $ openid = $ _ SESSION ['openid']; $ param ['body'] = "black toothpaste"; $ param ['Attach '] = $ pub-> id; $ param ['detail'] = "black toothpaste -". $ out_trade_no; $ param ['out _ trade_no '] = $ out_trade_no; $ param ['total _ taobao'] = $ total_0000* 100; $ param ["spbill _ Create_ip "] = $ _ SERVER ['remote _ ADDR ']; $ param [" time_start "] = date (" YmdHis "); $ param ["time_expire"] = date ("YmdHis", time () + 600); $ param ["goods_tag"] = "black toothpaste "; $ param ["yy_url"] = base_url (). "index. php/home/notify "; $ param [" trade_type "] =" JSAPI "; $ param [" openid "] = $ openid; // place an order in a unified manner to obtain the result, the result is to construct the required parameter $ result = $ this-> wechatpay-> unifiedOrder ($ param) for jsapi to call the payment component; // if the result is successful, we can construct the required parameter, first, determine the pre-payment id if (iss Et ($ result ["prepay_id"]) &! Empty ($ result ["prepay_id"]) {// call the get_package method in the payment class, the constructed parameter $ data ['parameters '] = json_encode ($ this-> wechatpay-> get_package ($ result ['prepay _ id']) is obtained. $ data ['categoryurl'] = $ param ["category_url"]; $ data ['category'] = $ total_fee; $ data ['pubid'] = $ _ SESSION ['orderid']; $ this-> load-> view ('Home/Header '); // you must have a page to pass the above data and present it to the user $ this-> load-> view ('Home/PA', $ data ); $ this-> load-> view ('Home/footer ');}
3. payment page, views pay. php