WeChat payment-scan the QR code to pay

Source: Internet
Author: User
: This article mainly introduces payment-scan payment. if you are interested in the PHP Tutorial, refer to it. I personally think that code scanning payment is much easier to develop and use than Jsapi payment. Scan the QR code to make payment without worrying about access from PCs, mobile browsers, or clients. generate a QR code and scan the payment.

For some configuration and code SDK and SDK errors, refer to the previous article on payment.

Payment-explanation of public account payment code


Note: The following content is really simple--# If you pay through Jsapi, there is nothing special to pay by scanning the code.

The file for initiating payment by scanning the code is in the native. php file of example SDK.

There are two payment methods for scan-code payment. you must configure the payment callback URL before using Scan-code payment. For more information, see

Developer Documentation http://pay.weixin.qq.com/wiki/doc/api/native.php? Chapter = 6_3

Two payment methods

Mode 1: http://pay.weixin.qq.com/wiki/doc/api/native.php? Chapter = 6_4

Mode 2: http://pay.weixin.qq.com/wiki/doc/api/native.php? Chapter = 6_5


In actual Mode 2, you do not need to set the callback URL. However, once you modify the payment configuration and use scan code to pay, you must check Native payment. in this case, the callback URL is required.

However, I only want to use Mode 2, Mode 2 does not use the callback URL, so I have to randomly write a URL that may be used later in Mode 1.

Code analysis:

Scan code payment mode 1


$notify = new NativePay();$url1 = $notify->GetPrePayUrl("123456789");
Let's take a look at the scanning payment mode 2.



$input = new WxPayUnifiedOrder();$input->SetBody("test");$input->SetAttach("test");$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));$input->SetTotal_fee("1");$input->SetTime_start(date("YmdHis"));$input->SetTime_expire(date("YmdHis", time() + 600));$input->SetGoods_tag("test");$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");$input->SetTrade_type("NATIVE");$input->SetProduct_id("123456789");$result = $notify->GetPayUrl($input);$url2 = $result["code_url"];

QR code:



" style="width:150px;height:150px;"/>
First, instantiate the WxPayUnifiedOrder class, set some parameters required for payment, and pass the required parameters to the function GetPayUrl ()


The function is defined in the example/Wxpay. NativePay. php file.


public function GetPayUrl($input){if($input->GetTrade_type() == "NATIVE"){$result = WxPayApi::unifiedOrder($input);return $result;}}

$result = WxPayApi::unifiedOrder($input);
The code here is to call the unified order interface. the code is located in part of the code in the file lib/WxPay. Api. php.



If ($ inputObj-> GetTrade_type () = "JSAPI "&&! $ InputObj-> IsOpenidSet () {throw new WxPayException ("The required parameter openid is missing in the unified payment interface! When trade_type is JSAPI, openid is a required parameter! ");} If ($ inputObj-> GetTrade_type () =" NATIVE "&&! $ InputObj-> IsProduct_idSet () {throw new WxPayException ("in the unified payment interface, the required parameter product_id is missing! When trade_type is JSAPI, product_id is a required parameter! ");}

Determine the payment method. if the JsApi method requires Openid


The Native method must require product_id. By the way, let's try again to determine whether it is a Native payment method. if the product_id prompt is missing, then the JSAPI must be set to product_id,

Alas, I can't be so careless anymore. The SDKs can be so sloppy to write and there is no sei.

After that, the unified order interface is called.


After the function is executed, a link starting with weixin: // is returned, and the phpqrcode program is called to generate a QR code.

The payment result processing page still uses the processing logic in the notify. php file.


Additional reading:

Payment by QR code (native payments for java)
Payment development process
Payment JS-SDK latest version, starting from 0
IOS-about payment

The above introduces the payment-scan code payment, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.