Home >Backend Development >PHP Tutorial >Share the problems and solutions encountered in the development of WeChat scan code payment - Attached is the Ecshop WeChat payment plug-in, encounter problems ecshop_PHP tutorial

Share the problems and solutions encountered in the development of WeChat scan code payment - Attached is the Ecshop WeChat payment plug-in, encounter problems ecshop_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:151114browse

Share the problems and solutions encountered in the development of WeChat scan code payment - Attached is the Ecshop WeChat payment plug-in. When encountering problems with ecshop

It has been easier recently, and I helped a friend with his ecshop-based The developed mall added the WeChat scan payment function. I thought it would be a simple matter - download the official SDK or development help document and follow the instructions in it. Unexpectedly, it took two or three days to get it done. I also looked for a lot of technical articles on the Internet with questions, but found that they only wrote roughly how they developed the access, and did not solve the problems I encountered... Alas, sometimes I really feel that I just Can rely on oneself'.

The purpose of this article is to write down the problems and solutions I encountered, so that friends who develop in this area can be helpful!

Before development, please check the official [Scan QR code payment] development document. Scan QR code payment is divided into the following two modes:

 △Mode 1:

Issues encountered: Scanning the QR code for the first time can still be scanned normally, but scanning again without payment will prompt: ok or the request for merchant information has timed out and the HttpCode is not 200 - this problem has been tried through various ways to find a solution, but to no avail As a result, I hope someone who knows can give me an answer, thank you!

 △Mode 2:

Note: You cannot directly use the order number of the mall as the transaction order number (out_trade_no), otherwise when you generate the WeChat scan code payment QR code again, the interface will prompt: The merchant order number is duplicated and cannot be regenerated.

So here comes Jiang Zi’s question, how should I set the transaction order number and ensure that the corresponding order information in the WeChat merchant platform can be queried through the interface later or reconciliation can be achieved? The feasible method is: use the unified order interface before scanning the QR code. Instead of passing in the order number of the mall order system for out_trade_no, regenerate a new unique flow; add another order number corresponding to the WeChat payment transaction order number (out_trade_no). Table, as follows:

There is a serial_is_paid field in the above table: used to mark whether this transaction has been completed, that is: in the payment success notification callback processing, the payment status is updated based on the returned out_trade_no; as for how to query the order payment status, I believe you should also Got it...

 

Okay, I haven’t written a technical blog for a long time and I feel a bit confused. The writing may be rough. Friends who are not sure can pay attention and communicate!

WeChat Pay V3 is full of pitfalls - WeChat Pay callback page

According to WeChat Pay V3, which was full of pitfalls last time, many gardeners finally jumped to the friendly WeChat payment interface after being abused thousands of times. However, they did not know what to do after entering the password to pay. Next, make up for it. Post-payment processing procedures.

1. In the html, jump to the relevant page based on the success or failure of the feedback information after the payment at the front desk

if (res.err_msg == "get_brand_wcpay_request:ok") {
   // message: "微信支付成功!",
   window.location.replace("@Url.Content("~/WxPay/Success?ordercode=@(Model.order_no)"));
}else if (res.err_msg == "get_brand_wcpay_request:cancel") { 
  // message: "已取消微信支付!"
 }

2. Download the WeChat payment interface document and demo (public account) from the payment development tutorial (WeChat merchant platform version).zip

For the parameters returned, please refer to the section 4.2. General Notification Interface in the interface document.

The notification URL is the parameter notify_url submitted in Section 4.1. After the payment is completed, WeChat will send relevant and user information to this URL, and the merchant needs to receive the processing information.

//支付回调页面
    public ActionResult notice_url()
    {
      string resultFromWx = getPostStr();
      //设置支付参数
      RequestHandler paySignReqHandler = new RequestHandler(null);
      WriteLog(" 微支付notice resultFromWx=" + resultFromWx);
      var res = XDocument.Parse(resultFromWx);
      //通信成功
      if (res.Element("xml").Element("return_code").Value == "SUCCESS")
      {
        if (res.Element("xml").Element("result_code").Value == "SUCCESS")
        {
          //交易成功
          paySignReqHandler.SetParameter("return_code", "SUCCESS");
          paySignReqHandler.SetParameter("return_msg", "OK");
          string ordecode = res.Element("xml").Element("out_trade_no").Value;
          BLL.orders Bll = new BLL.orders();
          try
          {
            if (Bll.Update(ordecode ))
            {
             WriteLog(" 微支付交易成功=" + ordecode);
            }
            else
            {
             WriteLog(" 微支付交易失败=" + ordecode);
            }
          }
          catch (Exception ex)
          {
            WriteLog(ex," 微支付交易异常=" + ordecode);
          }
        }
        else
        {
          paySignReqHandler.SetParameter("return_code", "FAIL");
          paySignReqHandler.SetParameter("return_msg", "交易失败");
        }
      }
      else
      {
        paySignReqHandler.SetParameter("return_code", "FAIL");
        paySignReqHandler.SetParameter("return_msg", "签名失败");
      }
      string data = paySignReqHandler.ParseXML();
      var result = TenPayV3.Unifiedorder(data);
      WriteLog(" 微支付notice result=" + result);
      return View();
    }

Note:

Therefore, after receiving a successful payment, you need to return the red part of the code to remind the WeChat server that it has been processed, otherwise you will continue to receive feedback from the WeChat server.

getPostStr() method:

//获得Post过来的数据
    public string getPostStr()
    {
      Int32 intLen = Convert.ToInt32(Request.InputStream.Length);
      byte[] b = new byte[intLen];
      Request.InputStream.Read(b, 0, intLen);
      return System.Text.Encoding.UTF8.GetString(b);
    }

Now that the front and backend are connected, the main thing is to be patient in debugging. I wish you good luck~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1049133.htmlTechArticleShare the problems and solutions encountered in the development of WeChat scan code payment - Attached is the Ecshop WeChat payment plug-in. Encountered problems with ecshop recently Comparing work is relatively easy. I helped a friend’s business developed based on ecshop...
Statement:
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