1,接口
csharp
/// <summary>
/// Web页面支付
/// </summary>
public interface IWebPagePay
{
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice);
}
2,实现接口
实现阿里支付
csharp
public class AliPagePay : IWebPagePay
{
/// <summary>
/// 创建支付
/// </summary>
/// <param name="productName"></param>
/// <param name="orderSn"></param>
/// <param name="totalPrice"></param>
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice)
{}
}
实现微信支付
csharp
public class WxNativePay : IWebPagePay
{
private readonly WxPayHttpClient _wxPayHttpClient;
private const string nativeUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";// 支付接口
private const string mchid = ""; // 商户Id
private const string certpath = "/Pays/WxPay/certs/apiclient_cert.p12"; // 商户证书路径
private const string certSerialNo = " "; // 证书序列号
public IVirtualFileProvider _virtualFileProvider { set; get; }
public WxNativePay(WxPayHttpClient wxPayHttpClient)
{
_wxPayHttpClient = wxPayHttpClient;
}
/// <summary>
/// 创建支付
/// </summary>
/// <param name="productName"></param>
/// <param name="orderSn"></param>
/// <param name="totalPrice"></param>
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice)
{}
}
3,注入ico
csharp
services.AddSingleton<IWebPagePay, AliPagePay>();// 支付宝封装类注册
services.AddSingleton<IWebPagePay, WxNativePay>();// 微信支付封装
4.使用
使用 GetWebPagePay 根据pagetype 获取到具体的支付实例
csharp
/// <summary>
/// 支付接口选择
/// </summary>
public class WebPagePayFactory
{
private readonly Dictionary<string, IWebPagePay> _webPagePays =null;
public WebPagePayFactory(IEnumerable<IWebPagePay> webPagePays)
{
_webPagePays = webPagePays.ToDictionary(pay =>
{
return pay.GetType().Name; // AliPagePay WxNativePay
});
}
/// <summary>
/// 获取WebPagePay支付
/// </summary>
/// <param name="pageType"></param>
public IWebPagePay GetWebPagePay(string pageType)
{
if (string.IsNullOrEmpty(pageType))
{
return _webPagePays["WxNativePay"];
}
return _webPagePays[pageType];
}
}