1,接口
public interface IWebPagePay
{
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice);
}
2,实现接口
实现阿里支付
public class AliPagePay : IWebPagePay
{
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice)
{}
}
实现微信支付
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 = "";
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;
}
public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice)
{}
}
3,注入ico
services.AddSingleton<IWebPagePay, AliPagePay>();
services.AddSingleton<IWebPagePay, WxNativePay>();
4.使用
使用 GetWebPagePay 根据pagetype 获取到具体的支付实例
public class WebPagePayFactory
{
private readonly Dictionary<string, IWebPagePay> _webPagePays =null;
public WebPagePayFactory(IEnumerable<IWebPagePay> webPagePays)
{
_webPagePays = webPagePays.ToDictionary(pay =>
{
return pay.GetType().Name;
});
}
public IWebPagePay GetWebPagePay(string pageType)
{
if (string.IsNullOrEmpty(pageType))
{
return _webPagePays["WxNativePay"];
}
return _webPagePays[pageType];
}
}