近期做了一个商城类微信小程序,涉及支付方便查了一些资料,在此记录一下
一套基于.Net Core的开发支付SDK
引用包:Essensoft.AspNetCore.Payment.WeChatPay
示例链接
包含微信支付、支付宝支付
配置文件
Startup.cs 注入
请求参数参考
//微信统一下单
public async Task<WeChatPayUnifiedOrderResponse> WeChatPay(string appId, string mchId, string subMchId,
string openId, string body, string OutTradeNo , string url,
DateTimeOffset startTime, DateTime expireTime, int totalFee, string key, string nonceStr, string ip,
IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
{
try
{
//payment微信下单
var request = new WeChatPayUnifiedOrderRequest
{
Body = body,//描述
OutTradeNo = OutTradeNo ,
TotalFee = totalFee,
SpBillCreateIp = ip,
NotifyUrl = url,
TradeType = "JSAPI",
OpenId = openId,
TimeStart = startTime.ToString("yyyyMMddHHmmss"),
TimeExpire = expireTime.ToString("yyyyMMddHHmmss")
};
var response = await client.ExecuteAsync(request, optionsAccessor.Value);
return response;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// 微信退款
/// </summary>
/// <param name="OutRefundNo">商户退款单号</param>
/// <param name="TransactionId">微信支付订单号</param>
/// <param name="OutTradeNo">商户订单号</param>
/// <param name="TotalFee">订单金额</param>
/// <param name="RefundFee">退款金额</param>
/// <param name="RefundDesc">退款原因</param>
/// <param name="NotifyUrl">退款结果通知url</param>
/// <param name="client"></param>
/// <param name="optionsAccessor"></param>
/// <returns></returns>
public async Task<WeChatPayRefundResponse> Refund(string mchId,string OutRefundNo, string TransactionId,string OutTradeNo,
int TotalFee,int RefundFee,string RefundDesc,string NotifyUrl,
IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
{
try
{
OutRefundNo = GenerateOutTradeNo(mchId);
var request = new WeChatPayRefundRequest
{
OutRefundNo = OutRefundNo,
TransactionId = TransactionId,
OutTradeNo = OutTradeNo,
TotalFee = TotalFee,
RefundFee = RefundFee,
RefundDesc = RefundDesc,
NotifyUrl = NotifyUrl
};
var response = await client.ExecuteAsync(request, optionsAccessor.Value);
_nlog.Info($"order code:{OutTradeNo}---return info:{response}");
return response;
}
catch (Exception e)
{
_nlog.Error($"order code:{OutTradeNo}---ex:{e}");
return null;
}
}
public string GenerateOutTradeNo(string mchId)
{
var ran = new Random();
return string.Format("{0}{1}{2}", mchId, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
}