Home  >  Article  >  Backend Development  >  WeChat Pay——Scan QR code to pay

WeChat Pay——Scan QR code to pay

WBOY
WBOYOriginal
2016-08-08 09:25:141208browse

Personally, I think scanning code payment is much smoother in terms of development and use than Jsapi payment. When you scan the QR code to pay, you don’t have to worry about accessing via PC, mobile browser or WeChat client. Just generate a QR code and scan it to pay.

For some configuration and code SDK and errors in the SDK, please refer to the previous article WeChat Pay Articles

WeChat Payment - Detailed explanation of official account payment code


Friendly reminder that the following content is very simple - -# If you have run through Jsapi payment, then there is nothing special about scanning the QR code to pay.

The file for scanning the QR code to initiate payment is in the native.php file in the example SDK.

There are two payment methods for scan code payment. Before using scan code payment, you must configure the specific configuration of payment callback URL. Reference

WeChat developer documentation http://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_3

Introduction to 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 fact, mode 2 does not need to set a callback URL. However, once you modify the payment configuration and use scan code payment, you must check Native payment. At this time, the callback URL is required.

But I only want to use mode 2. There is no callback URL in mode 2, so I have to randomly write a URL that may be used in mode 1 in the future.

Code analysis:

Scan code payment mode 1


$notify = new NativePay();
$url1 = $notify->GetPrePayUrl("123456789");
Mainly look at the scan code 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:



<img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data=<?php echo urlencode($url2);?>" style="width:150px;height:150px;"/>
First instantiate the class WxPayUnifiedOrder, then 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 ordering interface. The code is located in the file lib/WxPay.Api.php and part of the code is



if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
			throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
		}
		if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
			throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
		}

Determine the payment method, if it is JsApi method, Openid is required


The Native method must require product_id. By the way, let’s make another complaint to determine whether it is a Native payment method. The lack of product_id prompt turns out to be JSAPI required product_id.

Alas, I really can’t be sloppy anymore. The SDK can be written in such a sloppy way.

After that, the unified ordering interface process is called.


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

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


Further reading:

WeChat payment scan code payment (java version native payment)
WeChat payment development process
The latest version of WeChat Payment JS-SDK, starting from 0
iOS-About WeChat Pay

The above introduces WeChat payment - scan code payment, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:Simple use of php pdo (1)Next article:Simple use of php pdo (1)