微信支付类:
引用程序集 Senparc.Weixin.TenPay ( Version—1.3.0.1)
/// <summary>
/// 微信支付类
/// </summary>
public class WxPay
{
//以下配置用户填写自己相关配置
public static readonly string AppAppId = Common.GetConfig("WxPay_AppId");
public static readonly string AppMchId = Common.GetConfig("WxPay_MchId");
public static readonly string AppKey = Common.GetConfig("WxPay_Key"); //支付密钥
public static readonly string JsApiAppId = Common.GetConfig("WxPay_JsApi_AppId");
public static readonly string JsApiMchId = Common.GetConfig("WxPay_JsApi_MchId");
public static readonly string JsApiKey = Common.GetConfig("WxPay_JsApi_Key");
public static readonly string JsApiAppSecret = Common.GetConfig("WxPay_JsApi_AppSecret"); //唯一凭证密钥,仅JSAPI支付的时候需要配置,用于获取openId
public static readonly string NotifyUrl = Common.GetConfig("WxPay_NotifyUrl");
#region App
/// <summary>
/// 获取APP支付参数
/// </summary>
public WxPayParameter GetAppPayParameter(string appId, string mchId, string key, string orderNo, decimal amount, string body, string ip, string attach, out string msg)
{
WxPayParameter param = null;
var preOrder = CreatePrePayOrder(appId, mchId, key, TenPayV3Type.APP, orderNo, amount, body, ip, null, null, attach);
msg = preOrder.return_msg.ToJson();
if (preOrder.IsReturnCodeSuccess() && preOrder.IsResultCodeSuccess())
{
param = new WxPayParameter()
{
AppId = preOrder.appid,
PartnerId = preOrder.mch_id,
PrepayId = preOrder.prepay_id,
NonceStr = preOrder.nonce_str,
Timestamp = GetTimestamp(),
Package = "Sign=WXPay",
};
param.Sign = GetAppPaySign(appId, mchId, key, param.NonceStr, param.Package, param.PrepayId, param.Timestamp);
}
else
{
//预支付订单创建失败,记日志
}
return param;
}
/// <summary>
/// 获取APP支付参数
/// </summary>
public WxPayParameter GetAppPayParameter(string orderNo, decimal amount, string body, string ip, string attach)
{
WxPayParameter param = null;
var preOrder = CreatePrePayOrder(TenPayV3Type.APP, orderNo, amount, body, ip, null, null, attach);
if (preOrder.IsReturnCodeSuccess() && preOrder.IsResultCodeSuccess())
{
param = new WxPayParameter()
{
AppId = preOrder.appid,
PartnerId = preOrder.mch_id,
PrepayId = preOrder.prepay_id,
NonceStr = preOrder.nonce_str,
Timestamp = GetTimestamp(),
Package = "Sign=WXPay",
};
param.Sign = GetAppPaySign(param.NonceStr, param.Package, param.PrepayId, param.Timestamp);
}
else
{
//预支付订单创建失败,记日志
}
return param;
}
/// <summary>
/// 获取APP支付签名
/// </summary>
private string GetAppPaySign(string nonceStr, string package, string prepayId, string timestamp)
{
string param = "appid={0}&noncestr={1}&package={2}&partnerid={3}&prepayid={4}×tamp={5}&key={6}";
string str = string.Format(param, AppAppId, nonceStr, package, AppMchId, prepayId, timestamp, AppKey);
string sign = str.ToMd5().ToUpper();
return sign;
}
/// <summary>
/// 获取APP支付签名
/// </summary>
private string GetAppPaySign(string appId, string mchId, string key, string nonceStr, string package, string prepayId, string timestamp)
{
string param = "appid={0}&noncestr={1}&package={2}&partnerid={3}&prepayid={4}×tamp={5}&key={6}";
string str = string.Format(param, appId, nonceStr, package, mchId, prepayId, timestamp, key);
string sign = str.ToMd5().ToUpper();
return sign;
}
/// <summary>
/// APP支付申请退款
/// </summary>
public bool ApplyRefundApp(TenPayV3Type payType, string orderNo, string refundNo, decimal orderAmount, decimal refundAmount, out string errMsg)
{
return ApplyRefund(TenPayV3Type.APP, orderNo, refundNo, orderAmount, refundAmount, out errMsg);
}
#endregion
#region JsApi
/// <summary>
/// 获取JsApi支付参数
/// </summary>
public WxPayParameter GetJsApiPayParameter(string orderNo, decimal amount, string body, string ip, string openId, string attach, ClothingWeChatPaySetting paySetting = null)
{
WxPayParameter param = null;
var preOrder = CreatePrePayOrder(TenPayV3Type.JSAPI, orderNo, amount, body, ip, openId, null, attach, paySetting);
if (preOrder.IsReturnCodeSuccess() && preOrder.IsResultCodeSuccess())
{
param = new WxPayParameter()
{
AppId = preOrder.appid,
PartnerId = preOrder.mch_id,
PrepayId = preOrder.prepay_id,
NonceStr = preOrder.nonce_str,
Timestamp = GetTimestamp(),
Package = string.Format("prepay_id={0}", preOrder.prepay_id),
};
param.Sign = GetJsApiPaySign(param.NonceStr, param.Package, param.SignType, param.Timestamp);
}
else
{
param = new WxPayParameter();
//预支付订单创建失败,记日志
}
return param;
}
/// <summary>
/// 获取JsApi支付签名
/// </summary>
private string GetJsApiPaySign(string nonceStr, string package, string signType, string timeStamp)
{
string template = "appId={0}&nonceStr={1}&package={2}&signType={3}&timeStamp={4}&key={5}";
string str = string.Format(template, JsApiAppId, nonceStr, package, signType, timeStamp, JsApiKey);
string sign = str.ToMd5().ToUpper();
return sign;
}
/// <summary>
/// JsApi支付申请退款
/// </summary>
public bool ApplyRefundJsApi(TenPayV3Type payType, string orderNo, string refundNo, decimal orderAmount, decimal refundAmount, out string errMsg)
{
return ApplyRefund(TenPayV3Type.JSAPI, orderNo, refundNo, orderAmount, refundAmount, out errMsg);
}
#endregion
#region Native
/// <summary>
/// 获取扫码支付的二维码图片链接地址(将链接用二维码工具生成二维码打印出来,顾客可以用微信扫码支付, 有效期为2小时)
/// </summary>
public string GetQrCodeUrl(string orderNo, decimal amount, string body, string ip, string productId, string attach)
{
string url = string.Empty;
var preOrder = CreatePrePayOrder(TenPayV3Type.NATIVE, orderNo, amount, body, ip, null, productId, attach);
if (preOrder.IsReturnCodeSuccess() && preOrder.IsResultCodeSuccess())
{
url = preOrder.code_url;
}
return url;
}
/// <summary>
/// Native支付申请退款
/// </summary>
public bool ApplyRefundNative(TenPayV3Type payType, string orderNo, string refundNo, decimal orderAmount, decimal refundAmount, out string errMsg)
{
return ApplyRefund(TenPayV3Type.NATIVE, orderNo, refundNo, orderAmount, refundAmount, out errMsg);
}
#endregion
/// <summary>
/// 创建预支付交易单
/// </summary>
private UnifiedorderResult CreatePrePayOrder(TenPayV3Type payType, string orderNo, decimal amount, string body, string ip, string openId, string productId, string attach, ClothingWeChatPaySetting paySetting = null)
{
string nonceStr = Guid.NewGuid().ToString("N");
int cent = (int)(amount * 100);
string appId = JsApiAppId;
string mchId = JsApiMchId;
string key = JsApiKey;
switch (payType)
{
case TenPayV3Type.APP:
appId = AppAppId;
mchId = AppMchId;
key = AppKey;
break;
case TenPayV3Type.JSAPI:
//非公众号-进行在线订单支付时的赋值方式
if (paySetting != null)
{
appId = paySetting.App_Id;
mchId = paySetting.Mchid;
key = paySetting.APIKey;
}
break;
}
var payParams = new { JsApi_AppId = appId, JsApi_Key = key, JsApi_MchId = mchId,OrderNo= orderNo, Amount= cent,Ip= ip, BackEndUrl=NotifyUrl,PayType= payType,OpenId= openId, Body = body,ProductId= productId,Attach= attach };
NLogger.Default.Info(string.Format("获取支付参数-前:{0}", Newtonsoft.Json.JsonConvert.SerializeObject(payParams)));
var param = new TenPayV3UnifiedorderRequestData(appId, mchId, body, orderNo, cent, ip, NotifyUrl, payType, openId, key, nonceStr, productId: productId, attach: attach);
var order = TenPayV3.Unifiedorder(param);
NLogger.Default.Info(string.Format("获取支付参数-后:{0}",Newtonsoft.Json.JsonConvert.SerializeObject(order)));
return order;
}
/// <summary>
/// 创建预支付交易单
/// </summary>
private UnifiedorderResult CreatePrePayOrder(string appId, string mchId, string key, TenPayV3Type payType, string orderNo, decimal amount, string body, string ip, string openId, string productId, string attach)
{
string nonceStr = Guid.NewGuid().ToString("N");
int cent = (int)(amount * 100);
var param = new TenPayV3UnifiedorderRequestData(appId, mchId, body, orderNo, cent, ip, NotifyUrl, payType, openId, key, nonceStr, productId: productId, attach: attach);
var order = TenPayV3.Unifiedorder(param);
return order;
}
/// <summary>
/// 获取时间戳。标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
/// </summary>
private string GetTimestamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 申请退款
/// </summary>
private bool ApplyRefund(TenPayV3Type payType, string orderNo, string refundNo, decimal orderAmount, decimal refundAmount, out string errMsg)
{
bool success = false;
errMsg = string.Empty;
string nonceStr = Guid.NewGuid().ToString("N");
int orderCent = (int)(orderAmount * 100);
int refundCent = (int)(refundAmount * 100);
string appId = JsApiAppId;
string mchId = JsApiMchId;
string key = JsApiKey;
if (payType == TenPayV3Type.APP)
{
appId = AppAppId;
mchId = AppMchId;
key = AppKey;
}
var param = new TenPayV3RefundRequestData(appId, mchId, key, null, nonceStr, null, orderNo, refundNo, orderCent, refundCent, mchId, null);
var cert = @"D:\cert\apiclient_cert_SenparcRobot.p12";
var result = TenPayV3.Refund(param, cert, mchId);
if (result.result_code == "SUCCESS")
{
success = true;
}
else
{
errMsg = result.ToJson();
}
return success;
}
private void QueryOrder(TenPayV3Type payType, string orderNo, decimal amount)
{
string appId = JsApiAppId;
string mchId = JsApiMchId;
string key = JsApiKey;
if (payType == TenPayV3Type.APP)
{
appId = AppAppId;
mchId = AppMchId;
key = AppKey;
}
var param = new TenPayV3OrderQueryRequestData(appId, mchId, "", Guid.NewGuid().ToString("N"), orderNo, key);
var result = TenPayV3.OrderQuery(param);
//result.IsReturnCodeSuccess()
int cent = (int)(amount * 100);
bool s = cent.ToString() == result.total_fee;
}
}
WxPayParameter微信支付参数:
/// <summary>
/// 微信支付参数
/// </summary>
public class WxPayParameter
{
/// <summary>
/// 微信开放平台审核通过的应用APPID / 公众号id(商户注册具有支付权限的公众号成功后即可获得)
/// </summary>
public string AppId { get; set; }
/// <summary>
/// 微信支付分配的商户号
/// </summary>
public string PartnerId { get; set; }
/// <summary>
/// 预支付交易会话ID
/// </summary>
public string PrepayId { get; set; }
/// <summary>
/// 扩展字段 APP为固定值Sign=WXPay / JsApi为统一下单接口返回的prepay_id参数值,提交格式如:prepay_id=***
/// </summary>
public string Package { get; set; }
/// <summary>
/// 随机字符串
/// </summary>
public string NonceStr { get; set; }
/// <summary>
/// 时间戳
/// </summary>
public string Timestamp { get; set; }
/// <summary>
/// 签名
/// </summary>
public string Sign { get; set; }
/// <summary>
/// 签名方式 默认为MD5
/// </summary>
public string SignType { get; } = "MD5";
}