c#发起微信支付
【摘要】
1.项目引用 Senparc.Weixin.TenPay
2.服务端代码
using Senparc.Weixin.TenPay;using Senparc.Weixin.TenPay.V3;using System;using System.Collections.Generic;using System.Linq;us...
1.项目引用 Senparc.Weixin.TenPay
2.服务端代码
-
using Senparc.Weixin.TenPay;
-
using Senparc.Weixin.TenPay.V3;
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Mvc;
-
-
namespace zzzili
-
{
-
public class WXPay
-
{
-
/// <summary>
-
///
-
/// </summary>
-
/// <param name="appId">appid</param>
-
/// <param name="mchId">商户id</param>
-
/// <param name="body">商品描述</param>
-
/// <param name="outTradeNo">第三方订单号</param>
-
/// <param name="totalFee">总金额分</param>
-
/// <param name="spbillCreateIp">客户端ip</param>
-
/// <param name="notifyUrl">通知地址</param>
-
/// <param name="openid">用户openid</param>
-
/// <param name="key">商户API密钥</param>
-
/// <returns></returns>
-
public static Dictionary<string, object> startPay(string appId, string mchId, string body, string outTradeNo, int totalFee, string spbillCreateIp, string notifyUrl, string openid, string key)
-
{
-
var timeStamp = TenPayV3Util.GetTimestamp();//时间戳
-
var nonceStr = TenPayV3Util.GetNoncestr();//随机字符串
-
TenPayV3UnifiedorderRequestData tenPayV3 = new TenPayV3UnifiedorderRequestData(appId, mchId, body, outTradeNo, totalFee, spbillCreateIp, notifyUrl, Senparc.Weixin.TenPay.TenPayV3Type.JSAPI, openid, key, nonceStr);
-
//提交支付接口
-
UnifiedorderResult result = TenPayV3.Unifiedorder(tenPayV3);
-
-
string package = "prepay_id=" + result.prepay_id;
-
Dictionary<string, object> payEntity = new Dictionary<string, object>();
-
payEntity.Add("timeStamp", timeStamp);
-
payEntity.Add("nonceStr", nonceStr);
-
payEntity.Add("package", package);
-
payEntity.Add("signType", "MD5");
-
payEntity.Add("paySign", TenPayV3.GetJsPaySign(appId, timeStamp, nonceStr, package, key, "MD5"));
-
-
return payEntity;
-
}
-
}
-
}
3.小程序端代码
-
wx.requestPayment({
-
"timeStamp": res.data.Data.timeStamp,
-
"nonceStr": res.data.Data.nonceStr,
-
"package": res.data.Data.package,
-
"signType": res.data.Data.signType,
-
"paySign": res.data.Data.paySign,
-
"success": function(res) {
-
console.log(res);
-
},
-
"fail": function(res) {
-
console.log(res);
-
},
-
"complete": function(res) {
-
console.log(res);
-
}
-
});
3.支付结果通知
-
[HttpGet]
-
[HttpPost]
-
public string paynotify()
-
{
-
try
-
{
-
YFLog.logInfo("PayNotify==========================start");
-
ResponseHandler resHandler = new ResponseHandler(HttpContext.Current);
-
string return_code = resHandler.GetParameter("return_code");
-
string return_msg = resHandler.GetParameter("return_msg");
-
resHandler.SetKey(wx_key);//商户APIkey
-
if (resHandler.IsTenpaySign() && return_code.ToUpper() == "SUCCESS")
-
{
-
//直到这里,才能认为交易真正成功了,可以进行数据库操作,但是别忘了返回规定格式的消息!
-
string out_trade_no = resHandler.GetParameter("out_trade_no");
-
string transaction_id = resHandler.GetParameter("transaction_id");
-
string total_fee = resHandler.GetParameter("total_fee");
-
YFLog.logInfo("PayNotifyUrl回调", "支付成功");
-
}
-
else
-
{
-
YFLog.logInfo("PayNotifyUrl回调", "支付失败");
-
}
-
return string.Format(@"<xml><return_code><![CDATA[{0}]]></return_code><return_msg><![CDATA[{1}]]></return_msg></xml>", return_code, return_msg);
-
}
-
catch (Exception ex)
-
{
-
YFLog.logInfo(ex.Message);
-
throw;
-
}
-
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/114631519
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)