亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet WeChat Development Detailed introduction to php WeChat payment interface development program

Detailed introduction to php WeChat payment interface development program

Mar 16, 2017 pm 03:23 PM

WeChat paymentInterface Now slowly, like Alipay, you can use the api interface to realize payment on third-party websites or applications. The following is a php WeChat payment interface development The program has been tested. Interested friends can refer to

php WeChat payment interface development program explanation:

Required conditions:
appid //Obtained by the official account backend developer center (same as the one in the email)

mchid//Obtained in the email

key//Set by the merchant backend

appsecret //The official account developer center obtained
two certificate files, and obtained apiclient_cert.pem in the email apiclient_key.pem
Notes:
WeChat payment in the background of the official account-》Development configuration-》Add test directory and test personal WeChat ID.
Developer Center-》Web page authorization to obtain basic user information-》Change to your test domain name. Otherwise, redirect_uri parameter error will occur
——————————Follow-up to be improved——————-
The WeChat payment ready page has performed three operations on its own in the background:

1. Get openid


//使用jsapi接口
 
 代碼如下復(fù)制代碼
  $jsApi = new JsApi_pub();
 
  //=========步驟1:網(wǎng)頁授權(quán)獲取用戶openid============
  //通過code獲得openid
  if (!isset($_GET['code']))
  {
    //觸發(fā)微信返回code碼
    $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
    //echo $url;
    Header("Location: $url");
  }else
  {
    //獲取code碼,以獲取openid
    $code = $_GET['code'];
    $jsApi->setCode($code);
    $openid = $jsApi->getOpenid();
  }

I also encountered problems in the first step when I first started. The inability to obtain openid is related to some servers. The demo uses curl to obtain it.
It’s strange that my server curl has been unable to be obtained. Later, it was changed to file_get_contents and it can be obtained normally.
But this is not the solution. Because more curl operations will be needed later.
I saw a place in the development documentation where the certificate operation requires libcurl 7.20.1 or above. Then I have been working on the server to improve the php curl version of linux. In the end, I just switched to another windows server.
Let's do this for now, and debug when we need to use it next time.

Second step: Get and pay order number id
The code is as follows


$unifiedOrder = new UnifiedOrder_pub();
   
  //var_dump($unifiedOrder);
  //設(shè)置統(tǒng)一支付接口參數(shù)
  //設(shè)置必填參數(shù)
  //appid已填,商戶無需重復(fù)填寫
  //mch_id已填,商戶無需重復(fù)填寫
  //noncestr已填,商戶無需重復(fù)填寫
  //spbill_create_ip已填,商戶無需重復(fù)填寫
  //sign已填,商戶無需重復(fù)填寫
  $unifiedOrder->setParameter("openid","$openid");//商品描述
  $unifiedOrder->setParameter("body","貢獻一分錢");//商品描述
  //自定義訂單號,此處僅作舉例
  $timeStamp = time();
  $out_trade_no = WxPayConf_pub::APPID."$timeStamp";
  $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商戶訂單號 
  $unifiedOrder->setParameter("total_fee","1");//總金額
  $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
  $unifiedOrder->setParameter("trade_type","JSAPI");//交易類型
  //非必填參數(shù),商戶可根據(jù)實際情況選填
  //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商戶號 
  //$unifiedOrder->setParameter("device_info","XXXX");//設(shè)備號 
  //$unifiedOrder->setParameter("attach","XXXX");//附加數(shù)據(jù) 
  //$unifiedOrder->setParameter("time_start","XXXX");//交易起始時間
  //$unifiedOrder->setParameter("time_expire","XXXX");//交易結(jié)束時間 
  //$unifiedOrder->setParameter("goods_tag","XXXX");//商品標記 
  //$unifiedOrder->setParameter("openid","XXXX");//用戶標識
  //$unifiedOrder->setParameter("product_id","XXXX");//商品ID
 
 
  $prepay_id = $unifiedOrder->getPrepayId();
   
  //echo 'prepay_id:';
  var_dump($prepay_id);

There are many problems encountered in this step The problem.
First of all, it is difficult to test WeChat payment, and it can only be tested within WeChat. I just use my phone to swipe around.
Secondly, it is not easy to use var_dump for debugging. Print some files in xml format and only display the character length, not the content. So I wrote it in the form of log for debugging on the server. The log code:
The code is as follows


// 打印log
  function log_d($word) 
  {
    $log_name="./logd.log";//log文件路徑
    $fp = fopen($log_name,"a");
    flock($fp, LOCK_EX) ;
    fwrite($fp,"執(zhí)行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."n".$word."nn");
    flock($fp, LOCK_UN);
    fclose($fp);
  }

Use $this->log_d in WxPayPubHelper.php in the demo (xxx); call.
At the beginning, I kept getting errors because the mchid and appid given to me did not match. . They gave me the wrong account number. At the beginning, I didn’t know how to try randomly. For this step of debugging, you can see the error code using var_dump($this->result); in getPrepayId().

The third step: Generate the payment front-end js code and put it on the web page:
The code is as follows


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

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

Another problem encountered in this part:
android Returns "System: Access_denied", ios returns "access_control:not_allowed"
I searched a lot on Baidu. In fact, I have seen this thing for a long time and never noticed it!
The page that initiates the authorization request must be in the authorization directory and cannot exist in a subdirectory. Otherwise, an error will be returned
I placed the payment file in /domain name/pay/demo/
At the beginning, I always reached the end of /domain name/pay/ and thought that was enough. Support subdirectories, the result is not possible! .
————————Finally look at the picture below————

wxpay1
wxpay3
wxpay2

—— ————xmljs in the process——————–
Generate and pay order id to be submitted:
The code is as follows


<xml>
 <openid><![CDATA[ou9dHt0L8qFLI1foP-kj5x1mDWsM]]></openid>
 <body><![CDATA[貢獻一下]]></body>
 <out_trade_no><![CDATA[wx88888888888888881414411779]]></out_trade_no>
 <total_fee>1</total_fee>
 <notify_url><![CDATA[http://shanmao.me/wxpay/notify_url.php]]></notify_url>
 <trade_type><![CDATA[JSAPI]]></trade_type>
 <appid><![CDATA[wx8888888888888888]]></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>

Return after submission Correct, it contains 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 js for payment:


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

Payment successful The notification xml returned after that is:


<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’m random about the data in this, so please refer to the format below. Also note that it is case sensitive.

The above is the detailed content of Detailed introduction to php WeChat payment interface development program. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72