微信扫码支付引用的是第三方的:Senparc.Weixin
引用:
using Senparc.Weixin.MP.TenPayLibV3;
首先,在Form_Load 里面调用生成支付二维码的方法:
/// <summary>
/// Form_Load事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormWeChet_Load(object sender, EventArgs e)
{
//调用微信方法,生成支付二维码
WeChatCode();
}
然后写如何生成二维码的方法实现:
/// <summary>
/// 微信二维码生成
/// </summary>
public void WeChatCode()
{
string out_trade_no =System.DateTime.Now.ToString("yyyymmddhhmmss") + Code();//微信订单号要唯一,我这里只是写了一个时间+随机数
var data = new Senparc.Weixin.MP.TenPayLibV3.TenPayV3UnifiedorderRequestData(WeChetConfig.AppId, WeChetConfig.MchId, WeChetConfig.Body, out_trade_no, WeChetConfig.TotalFee, WeChetConfig.SpbillCreateIp, WeChetConfig.NotifyUrl, Senparc.Weixin.MP.TenPayV3Type.NATIVE, WeChetConfig.OpenId, WeChetConfig.PayKey, TenPayV3Util.GetNoncestr());//这个类内容在最下面
var response = TenPayV3.Unifiedorder(data);
var QrCode_url = response.code_url;
var qrdata = new QRCoder.QRCodeGenerator().CreateQrCode(QrCode_url, QRCoder.QRCodeGenerator.ECCLevel.H);
var qrImg = new QRCoder.QRCode(qrdata).GetGraphic(256);
this.pictureBox1.Image = qrImg;
//定时查询支付状态
var timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;//间隔5秒
timer.Tick += (sender2, e2) =>
{
var dataQueryOrder = new Senparc.Weixin.MP.TenPayLibV3.TenPayV3OrderQueryRequestData(WeChetConfig.AppId, WeChetConfig.MchId, null, TenPayV3Util.GetNoncestr(), WeChetConfig.OutTradeNo, WeChetConfig.PayKey);
var queryResult = TenPayV3.OrderQuery(dataQueryOrder);
if (queryResult.trade_state == "SUCCESS")//支付成功
{
//这里写自己的数据库逻辑
}
else if (queryResult.trade_state == "CLOSED")//已关闭
{
MessageBox.Show("该订单已关闭,请重新扫码支付");
WeChatCode();//重新调用生成方法
timer.Stop();
timer.Dispose();
}
else if (queryResult.trade_state == "PAYERROR")//支付失败(其他原因,如银行返回失败)
{
CacelRequest(queryResult);
}
};
timer.Start();
}
写撤销支付的方法:
/// <summary>
/// 撤销支付的订单
/// </summary>
/// <param name="out_trade_no"></param>
public void CacelRequest(OrderQueryResult queryResult)
{
var data = new TenPayV3CloseOrderRequestData(queryResult.appid, queryResult.mch_id, queryResult.out_trade_no, WeChetConfig.PayKey, queryResult.nonce_str);
var closeOrder = TenPayV3.CloseOrder(data);
if (closeOrder.result_code == "FAIL")
{
MessageBox.Show("该订单撤销失败,请联系管理员");
this.DialogResult = DialogResult.Cancel;
}
else
{
MessageBox.Show("该订单已撤销,请重新发起支付请求");
this.DialogResult = DialogResult.Cancel;
}
}
微信订单号的随机数方法:
/// <summary>
/// 微信订单单号的随机数方法
/// </summary>
/// <returns></returns>
private string Code()
{
string RandomCode = "";
for (int i = 0; i < 6; i++)
{
//获取随机数的方法
Random rand = new Random();
int RandKey = rand.Next(0, 10);
RandomCode += RandKey.ToString();
}
return RandomCode;
}
最后是所需要的类:
public class WeChetConfig
{
private readonly static string appId;
private readonly static string mchId;
private readonly static string payKey;
private readonly static string body;
private readonly static string ipAddress;
private readonly static string out_trade_no;
private readonly static int totalfee;
private readonly static string notifyurl;
static WeChetConfig()
{
appId = “”;//这里写APPID
mchId = “”;//这里写商户ID
payKey = “”;//这里写PayKey
body = "商品描述";
totalfee = 1;
notifyurl = "";
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ipa.ToString(); break;
}
}
}
public static string AppId => appId; //微信支付分配的公众账号ID(企业号corpid即为此appId)
public static string MchId => mchId;//微信支付分配的商户号
public static string Body => body; //商品描述
public static string OutTradeNo => out_trade_no; //商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|* 且在同一个商户号下唯一。
public static int TotalFee => totalfee; //订单总金额,单位为分
public static string SpbillCreateIp => ipAddress; //APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
public static string NotifyUrl => notifyurl;//异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。
public static string OpenId = ""; //trade_type=JSAPI时(即公众号支付),此参数必传,此参数为微信用户在商户对应appid下的唯一标识。
public static string PayKey => payKey;
}
标签:扫码,queryResult,string,微信,static,WeChetConfig,public,Winform From: https://www.cnblogs.com/guangzhiruijie/p/16985739.html