国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. Open WeChat payment and configure
2. Introduction to WeChat Payment API
The request parameters seem to be many, and they are roughly divided into two parts. One part is a fixed parameter that is necessary for the system. , part of which is the parameters required by the business.
Based on the above description, we have a general understanding of the WeChat Payment API Explain that based on this information, we can encapsulate it with C# code. For the encapsulation of the code, our key point is the first one. If the first interface is successfully encapsulated, then the subsequent ones will be very easy according to the general method. It's easy to continue working on these interfaces.
4、在頁(yè)面上進(jìn)行掃碼處理
Home WeChat Applet WeChat Development C# develops WeChat portal and application WeChat payment access and API encapsulation use

C# develops WeChat portal and application WeChat payment access and API encapsulation use

Mar 07, 2017 am 10:09 AM

In the application of WeChat, WeChat payment is a more useful part, but it is also a relatively complicated technical point. In the era when WeChat business is very popular, it seems unreasonable that your own store does not add WeChat payment. WeChat payment is designed to serve the majority of people. WeChat users and merchants are provided with better payment services. WeChat’s payment and security system is supported by Tencent Tenpay. This article mainly introduces how to implement WeChat payment access, WeChat payment API encapsulation, and API calls on WeChat official accounts to realize some of our common business calls.

1. Open WeChat payment and configure

WeChat payment is the basis for authentication of WeChat public accounts, that is, it is only open to certified public accounts. WeChat authentication requires signing of relevant information and conducting For reconciliation and verification, you will usually be contacted by phone to confirm the relevant information.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

Before we start using the WeChat payment API, we generally need to perform certain configurations in the background. For example, we need to configure the authorization directory for official account payment, test whitelist and other information. And the callback processing address supported by QR code scanning (this implementation will be discussed later), as shown below.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

Before using the API, we need to know that some key operations on WeChat, such as refunds, order cancellations, etc., require certificates, and for regular payment operations, we also We need information such as merchant number, merchant payment key, etc. These certificates and key information are obtained from the merchant platform of WeChat Pay. After WeChat Pay is activated and approved, we can log in to the merchant platform to perform related operations. .

First we need to install the certificate on the development computer.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

Then you need to set the API secret key

C# develops WeChat portal and application WeChat payment access and API encapsulation use

Finally download the certificate on the [API Security] project for our development environmental use.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

2. Introduction to WeChat Payment API

WeChat payment configuration related parameters, and obtain certificates, API keys, merchants After receiving the number and other information, we can start to understand the specific use of the WeChat payment API. We need to first encapsulate the API into a C# class library for use, so that it can be easily called in various applications.

WeChat payment is divided into many methods, such as scan code payment, official account payment, JSAPI payment, APP payment, etc. However, the core APIs are similar and basically cover the APIs in the screenshot below. , there are only some interface differences.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

We can start to understand from the scan code payment. This is the scenario of scanning the QR code to pay, which is divided into two methods: mode one and mode two.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

# Scan code payment can be divided into two modes. Merchants can choose the corresponding mode according to the payment scenario.

[Mode 1]: The merchant's backend system generates a QR code based on the WeChat payment rule link, with a fixed parameter productid (can be defined as a product ID or order number) in the link. After the user scans the code, the WeChat payment system calls the productid and user's unique identifier (openid) back to the merchant's backend system (the payment callback URL needs to be set). The merchant's backend system generates a payment transaction based on the productid, and finally the WeChat payment system initiates the user payment process.

[Mode 2]: The merchant's backend system calls WeChat Pay [Unified Order API] to generate a prepaid transaction, and generates a QR code from the link returned by the interface. The user scans the code and enters the password to complete the payment transaction. Note: The prepaid order in this mode is valid for 2 hours, and payment cannot be made after expiration.

According to the API description of scan code payment, we can encapsulate these interfaces (such as unified order placement, order inquiry, order closing, refund application, refund inquiry, statement download, etc.) one by one to It is convenient for our development and use.

Both mode one and mode two need to use the unified ordering interface, and then generate the corresponding QR code for customers to scan and pay.

Then we. Let’s first take a look at the unified ordering interface description to understand its specific use


##1) Application scenarios

#.


##Except for the scanned payment scenario, the merchant system first calls this interface to generate a prepayment transaction order in the WeChat payment service background, returns the correct prepayment transaction reply ID, and then scans Code, JSAPI, APP and other different scenarios generate transaction strings to initiate payment.


2) Interface link


URL address: https ://api.mch.weixin.qq.com/pay/unifiedorder


3) Do you need a certificate


Not required

4) Request parameters

The request parameters seem to be many, and they are roughly divided into two parts. One part is a fixed parameter that is necessary for the system. , part of which is the parameters required by the business.

The fixed parameters required by the system are as follows.

C# develops WeChat portal and application WeChat payment access and API encapsulation usePart of it is the business parameters. The business parameters are as follows, which mainly record the relevant product ID, description, cost, etc. of the order

C# develops WeChat portal and application WeChat payment access and API encapsulation useThe invocation of the WeChat payment interface is different from the invocation of other interfaces of the public account. All exchanges here are done using XML, which does not feel as convenient and flexible as JSON. The following is the unified order interface to submit data.

C# develops WeChat portal and application WeChat payment access and API encapsulation useThe returned data is also in XML, as shown in the example code below, and the field contents are not yet certain, so as recommended by the official website, use a dictionary collection. Store the returned data object.

C# develops WeChat portal and application WeChat payment access and API encapsulation use

3. WeChat Payment APIC# Encapsulation and Calling

Based on the above description, we have a general understanding of the WeChat Payment API Explain that based on this information, we can encapsulate it with C# code. For the encapsulation of the code, our key point is the first one. If the first interface is successfully encapsulated, then the subsequent ones will be very easy according to the general method. It's easy to continue working on these interfaces.

For example, we can define the API interface definition of WeChat payment as shown below.

///?<summary>
????///?微信支付接口
????///?</summary>
????public?interface?ITenPayApi
????{??????
????????///?<summary>
????????///?生成掃描支付模式一URL
????????///?</summary>
????????///?<param>商品ID
????????///?<returns></returns>
????????string?GetPrePayUrl(string?productId);

????????///?<summary>
????????///?生成直接支付url,支付url有效期為2小時(shí),模式二
????????///?</summary>
????????///?<param>商品訂單數(shù)據(jù)
????????///?<returns></returns>
????????string?GetPayUrl(WxPayOrderData?info);

????????///?<summary>
????????///?統(tǒng)一下單。(不需要證書(shū),默認(rèn)不需要)
????????///?除被掃支付場(chǎng)景以外,商戶(hù)系統(tǒng)先調(diào)用該接口在微信支付服務(wù)后臺(tái)生成預(yù)支付交易單,
????????///?返回正確的預(yù)支付交易回話標(biāo)識(shí)后再按掃碼、JSAPI、APP等不同場(chǎng)景生成交易串調(diào)起支付。
????????///?</summary>
????????///?<param>商品訂單數(shù)據(jù)
????????WxPayData?UnifiedOrder(WxPayOrderData?info);

????????.............

For the input parameters of the interface method, we define an entity class

WxPayOrderData to store some business parameters, These parameters are defined according to the interface description in point 2. The code is as follows

///?<summary>
????///?統(tǒng)一下單的商品訂單信息
????///?</summary>
????public?class?WxPayOrderData
????{
????????///?<summary>
????????///?商品ID,?trade_type=NATIVE,此參數(shù)必傳
????????///?</summary>
????????public?string?product_id?{?get;?set;?}
????????///?<summary>
????????///?商品或支付單簡(jiǎn)要描述
????????///?</summary>
????????public?string?body?{?get;?set;?}
????????///?<summary>
????????///?訂單總金額,單位為分
????????///?</summary>
????????public?int?total_fee?{?get;?set;?}
????????///?<summary>
????????///?商品標(biāo)記,代金券或立減優(yōu)惠功能的參數(shù),說(shuō)明詳見(jiàn)代金券或立減優(yōu)惠
????????///?</summary>
????????public?string?goods_tag?{?get;?set;?}

????????///?<summary>
????????///?交易類(lèi)型,默認(rèn)為:NATIVE。
????????///?JSAPI--公眾號(hào)支付、NATIVE--原生掃碼支付、APP--app支付
????????///?</summary>
????????public?string?trade_type?{?get;?set;?}

????????///?<summary>
????????///?商品名稱(chēng)明細(xì)列表
????????///?</summary>
????????public?string?detail?{?get;?set;?}
????????///?<summary>
????????///?附加數(shù)據(jù)
????????///?在查詢(xún)API和支付通知中原樣返回,該字段主要用于商戶(hù)攜帶訂單的自定義數(shù)據(jù)
????????///?</summary>
????????public?string?attach?{?get;?set;?}
????????///?<summary>
????????///?用戶(hù)標(biāo)識(shí)
????????///?trade_type=JSAPI,此參數(shù)必傳,用戶(hù)在商戶(hù)appid下的唯一標(biāo)識(shí)。
????????///?</summary>
????????public?string?openid?{?get;?set;?}

????????public?WxPayOrderData()
????????{
????????????this.trade_type?=?"NATIVE";
????????}
????}

Then we define a class WxPayData returned by the interface, which is used to store the returned For object information, this class is explained in the official website example. It has a sorted dictionary object built in to store data. Part of the code is as follows. I have made relevant modifications to it to facilitate initialization in the constructor. Required parameters (fixed parameters).

public?class?WxPayData
????{
????????//采用排序的Dictionary的好處是方便對(duì)數(shù)據(jù)包進(jìn)行簽名,不用再簽名之前再做一次排序
????????private?SortedDictionary<string>?m_values?=?new?SortedDictionary<string>();
????????
????????///?<summary>
????????///?默認(rèn)構(gòu)造函數(shù)
????????///?如果initDefault為true,則自動(dòng)填入字段(appid,mch_id,time_stamp,nonce_str,out_trade_no,)
????????///?</summary>
????????public?WxPayData(bool?initDefault?=?false)
????????{
????????????if(initDefault)
????????????{
????????????????Init();
????????????}
????????}

????????///?<summary>
????????///?對(duì)象初始化后,自動(dòng)填入字段(appid,mch_id,time_stamp,nonce_str,out_trade_no,)
????????///?</summary>
????????public?void?Init()
????????{
????????????//初始化幾個(gè)參數(shù)
????????????this.SetValue("appid",?WxPayConfig.APPID);//公眾帳號(hào)id
????????????this.SetValue("mch_id",?WxPayConfig.MCHID);//商戶(hù)號(hào)
????????????this.SetValue("nonce_str",?GenerateNonceStr());//隨機(jī)字符串
????????????this.SetValue("out_trade_no",?GenerateOutTradeNo(WxPayConfig.MCHID));//隨機(jī)字符串

????????}</string></string>

Then based on the above data definition, we can realize the unified order function content, mainly converting the input parameters into the dictionary parameter set we need, As shown in the following code.

///?<summary>
????????///?統(tǒng)一下單。(不需要證書(shū),默認(rèn)不需要)
????????///?除被掃支付場(chǎng)景以外,商戶(hù)系統(tǒng)先調(diào)用該接口在微信支付服務(wù)后臺(tái)生成預(yù)支付交易單,
????????///?返回正確的預(yù)支付交易回話標(biāo)識(shí)后再按掃碼、JSAPI、APP等不同場(chǎng)景生成交易串調(diào)起支付。
????????///?</summary>
????????///?<param>商品訂單數(shù)據(jù)
????????public?WxPayData?UnifiedOrder(WxPayOrderData?info)
????????{
????????????WxPayData?data?=?new?WxPayData(true);
????????????data.SetValue("product_id",?info.product_id);//商品ID
????????????data.SetValue("openid",?info.openid);//商品ID

????????????//其他信息
????????????data.SetValue("body",?info.body);//商品描述
????????????data.SetValue("attach",?info.attach);//附加數(shù)據(jù)
????????????data.SetValue("total_fee",?info.total_fee);//總金額
????????????data.SetValue("goods_tag",?info.goods_tag);//商品標(biāo)記
????????????data.SetValue("trade_type",?info.trade_type);//交易類(lèi)型

????????????//默認(rèn)構(gòu)建
????????????data.SetValue("time_start",?DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始時(shí)間
????????????data.SetValue("time_expire",?DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易結(jié)束時(shí)間

??????????..............

For the final data exchange logic, we can submit XML data to it by POSTing the URL to get the return result, as shown below.

????????????string?url?=?"https://api.mch.weixin.qq.com/pay/unifiedorder";
????????????return?GetPostResult(data,?url);

其中上面的函數(shù)的代碼邏輯如下所示,主要是把返回的結(jié)果再還原為XML對(duì)象類(lèi)WxPayData。

///?<summary>
????????///?通用的獲取結(jié)果函數(shù)
????????///?</summary>
????????private?WxPayData?GetPostResult(WxPayData?data,?string?url)
????????{
????????????string?xml?=?data.ToXml();
????????????string?response?=?helper.GetHtml(url,?xml,?true);

????????????WxPayData?result?=?new?WxPayData();
????????????result.FromXml(response);
????????????return?result;
????????}

對(duì)于掃碼操作的模式二,直接生成一種二維碼,不需要后臺(tái)進(jìn)行回調(diào)的,那么它的實(shí)現(xiàn)邏輯只需要對(duì)上面代碼進(jìn)行封裝就可以了,如先構(gòu)建二維碼的函數(shù)代碼如下所示。

///?<summary>
????????///?生成直接支付url,支付url有效期為2小時(shí),模式二
????????///?</summary>
????????///?<param>商品訂單數(shù)據(jù)
????????///?<returns></returns>
????????public?string?GetPayUrl(WxPayOrderData?info)
????????{
????????????WxPayData?result?=?UnifiedOrder(info);//調(diào)用統(tǒng)一下單接口
????????????return?result.GetString("code_url");//獲得統(tǒng)一下單接口返回的二維碼鏈接
????????}

如在Winform界面里面,調(diào)用生成二維碼的代碼如下所示,主要邏輯就是構(gòu)建好二維碼,然后顯示在界面上。

private?void?btnGetPayUrl_Click(object?sender,?EventArgs?e)
????????{
????????????//測(cè)試掃碼模式二的生成二維碼方式
????????????WxPayOrderData?data?=?new?WxPayOrderData()
????????????{
????????????????product_id?=?"123456789",
????????????????body?=?"測(cè)試支付-模式二",
????????????????attach?=?"愛(ài)奇迪技術(shù)支持",
????????????????detail?=?"測(cè)試掃碼支付-模式二",
????????????????total_fee?=?1,
????????????????goods_tag?=?"test1"
????????????};

????????????var?url?=?api.GetPayUrl(data);
????????????var?image?=?api.GenerateQRImage(url);

????????????this.imgGetPayUrl.Image?=?image;
????????????this.imgGetPayUrl.SizeMode?=?PictureBoxSizeMode.StretchImage;
????????}

C# develops WeChat portal and application WeChat payment access and API encapsulation use

另外對(duì)于模式一,它在前端傳入一個(gè)簡(jiǎn)單的產(chǎn)品ID,生成二維碼,當(dāng)用戶(hù)掃碼的時(shí)候,微信后臺(tái)會(huì)調(diào)用商戶(hù)平臺(tái)(我們服務(wù)器)的回調(diào)處理方法,這個(gè)回調(diào)方法會(huì)調(diào)用統(tǒng)一下單的API進(jìn)行生成支付交易,過(guò)程有點(diǎn)復(fù)雜,我們來(lái)看看,我們的實(shí)現(xiàn)代碼如下所示。

///?<summary>
????????///?生成掃描支付模式一URL
????????///?</summary>
????????///?<param>商品ID
????????///?<returns></returns>
????????public?string?GetPrePayUrl(string?productId)
????????{
????????????WxPayData?data?=?new?WxPayData(true);
????????????data.SetValue("product_id",?productId);//商品ID?????
????????????data.SetValue("time_stamp",?data.GenerateTimeStamp());//隨機(jī)字符串?????????
????????????data.SetValue("sign",?data.MakeSign());//簽名

????????????string?str?=?data.ToUrlParams();//轉(zhuǎn)換為URL串
????????????string?url?=?"weixin://wxpay/bizpayurl?"?+?str;

????????????return?url;
????????}

它的調(diào)用代碼生成二維碼操作如下所示。

private?void?btnGetPrePayUrl_Click(object?sender,?EventArgs?e)
????????{
????????????var?productId?=?"12345678";
????????????var?url?=?api.GetPrePayUrl(productId);
????????????var?image?=?api.GenerateQRImage(url);

????????????this.imgGetPrePayUrl.Image?=?image;
????????????this.imgGetPayUrl.SizeMode?=?PictureBoxSizeMode.StretchImage;
????????}

我們?cè)诘谝恍」?jié)里面介紹了,需要在微信后臺(tái)配置掃碼的回調(diào)函數(shù),如下所示。

C# develops WeChat portal and application WeChat payment access and API encapsulation use

這樣我們還需要添加一個(gè)頁(yè)面aspx、或者一般處理程序ashx的方式來(lái)實(shí)現(xiàn)掃碼的回調(diào)過(guò)程。具體的邏輯也就是在這個(gè)頁(yè)面里面獲取到提交過(guò)來(lái)的參數(shù),然后調(diào)用統(tǒng)一下單處理后,進(jìn)行數(shù)據(jù)返回即可,代碼邏輯如下所示。

C# develops WeChat portal and application WeChat payment access and API encapsulation use

?

4、在頁(yè)面上進(jìn)行掃碼處理

前面的例子,我介紹了Winfrom的掃碼例子,很多時(shí)候,我們的應(yīng)用可能是基于Web的,那么它的實(shí)現(xiàn)是如何的呢,下面我繼續(xù)介紹一下。

首先我們?cè)谧约旱臉I(yè)務(wù)Web后臺(tái)系統(tǒng)里面,添加兩個(gè)頁(yè)面,主要是用來(lái)生成二維碼在頁(yè)面上進(jìn)行展示的,如下所示。

C# develops WeChat portal and application WeChat payment access and API encapsulation use

最終我們?cè)贜ativePayPage.aspx頁(yè)面上展示我們的二維碼,方便用戶(hù)進(jìn)行掃碼支付處理,頁(yè)面的代碼很簡(jiǎn)單,我們只需要在前端頁(yè)面放置兩個(gè)圖片控件,圖片內(nèi)容通過(guò)MakeQRCode.aspx頁(yè)面進(jìn)行生成就可以了。

nbsp;html>


????<meta>
????<meta>?
????<title>微信支付樣例-掃碼支付</title>


????<div>掃碼支付模式一</div><br>
????<image></image>
????<br><br><br>
????<div>掃碼支付模式二</div><br>
????<image></image>
????

頁(yè)面后臺(tái)的代碼就是綁定二維碼的過(guò)程,代碼如下所示,和Winform的代碼類(lèi)似操作。

protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????????TenPayApi?api?=?new?TenPayApi();

????????????var?productId?=?"123456789";
????????????//生成掃碼支付模式一url
????????????string?url1?=?api.GetPrePayUrl(productId);

????????????//生成掃碼支付模式二url
????????????WxPayOrderData?info?=?new?WxPayOrderData()
????????????{
????????????????product_id?=?"123456789",
????????????????body?=?"測(cè)試支付-模式二",
????????????????attach?=?"愛(ài)奇迪技術(shù)支持",
????????????????detail?=?"測(cè)試掃碼支付-模式二",
????????????????total_fee?=?1,
????????????????goods_tag?=?"test1"
????????????};
????????????string?url2?=?api.GetPayUrl(info);

????????????//將url生成二維碼圖片
????????????Image1.ImageUrl?=?"MakeQRCode.aspx?data="?+?HttpUtility.UrlEncode(url1);
????????????Image2.ImageUrl?=?"MakeQRCode.aspx?data="?+?HttpUtility.UrlEncode(url2);
????????}

實(shí)現(xiàn)后的頁(yè)面效果如下所示。

C# develops WeChat portal and application WeChat payment access and API encapsulation use

實(shí)現(xiàn)并預(yù)覽效果,確定是我們所需的頁(yè)面后,我們可以發(fā)布在公眾號(hào)的菜單連接上進(jìn)行測(cè)試使用了。

C# develops WeChat portal and application WeChat payment access and API encapsulation use

打開(kāi)微信公眾號(hào)-廣州愛(ài)奇迪,我們可以看到對(duì)應(yīng)的菜單發(fā)生改變,并且看到進(jìn)入微信支付的菜單可以進(jìn)行支付了。

C# develops WeChat portal and application WeChat payment access and API encapsulation use C# develops WeChat portal and application WeChat payment access and API encapsulation use C# develops WeChat portal and application WeChat payment access and API encapsulation use

The above is an implementation of the QR code scanning process of WeChat Pay. WeChat Pay also includes many other API interfaces, as follows Opportunities can continue to be introduced. Although the interface implementation of WeChat payment is more complicated than other WeChat interfaces, once we complete a few cases, the rest will be relatively easy because its calling method is basically consistent and similar.

For more C# development WeChat portal and application WeChat payment access and API encapsulation and related articles, please pay attention to 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)