131 lines
4.5 KiB
C#
131 lines
4.5 KiB
C#
using Alipay.EasySDK.Factory;
|
|
using Alipay.EasySDK.Kernel;
|
|
|
|
using HuanMeng.MiaoYu.Code.Payment.Alipay;
|
|
using HuanMeng.MiaoYu.Code.Payment.Contract;
|
|
using HuanMeng.MiaoYu.Code.Payment.WeChat;
|
|
using HuanMeng.MiaoYu.Model.Dto.Cache;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities;
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
|
|
|
|
|
|
|
namespace HuanMeng.MiaoYu.Code.Payment
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class PaymentExtend
|
|
{
|
|
private static WechatTenpayClientOptions wechatTenpayClientOptions { get; set; } = new WechatTenpayClientOptions();
|
|
private static WeChatConfig weChatConfig { get; set; }
|
|
/// <summary>
|
|
/// 支付
|
|
/// </su0mmary>
|
|
/// <param name="builder"></param>
|
|
/// <returns></returns>
|
|
public static IHostApplicationBuilder AddPayment(this IHostApplicationBuilder builder)
|
|
{
|
|
|
|
AddAlipay(builder.Configuration);
|
|
weChatConfig = builder.Configuration.GetSection("Payment:WeChatConfig").Get<WeChatConfig>();
|
|
if (weChatConfig == null)
|
|
{
|
|
throw new Exception("微信支付失败");
|
|
}
|
|
var manager = new InMemoryCertificateManager();
|
|
/* 仅列出必须配置项。也包含一些诸如超时时间、UserAgent 等的配置项 */
|
|
wechatTenpayClientOptions = new WechatTenpayClientOptions()
|
|
{
|
|
|
|
MerchantId = weChatConfig.MchId,
|
|
MerchantV3Secret = weChatConfig.Key,
|
|
MerchantCertificateSerialNumber = weChatConfig.MerchantCertificateSerialNumber,
|
|
MerchantCertificatePrivateKey = weChatConfig.MerchantCertificatePrivateKey,
|
|
PlatformCertificateManager = manager
|
|
};
|
|
|
|
|
|
return builder;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 支付
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public static void AddAlipay(IConfiguration configuration)
|
|
{
|
|
var x = configuration.GetSection("Payment:AlipayConfig").Get<Config>();
|
|
|
|
if (x != null)
|
|
{
|
|
var config = new Config()
|
|
{
|
|
Protocol = "https",
|
|
GatewayHost = "openapi.alipay.com",
|
|
SignType = "RSA2",
|
|
AppId = x.AppId,
|
|
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
|
MerchantPrivateKey = x.MerchantPrivateKey,
|
|
// 如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
|
AlipayPublicKey = x.AlipayPublicKey,
|
|
//可设置异步通知接收服务地址(可选)
|
|
NotifyUrl = x.NotifyUrl,
|
|
};
|
|
Factory.SetOptions(config);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("接入支付失败");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///获取支付方式
|
|
/// </summary>
|
|
/// <param name="payment"></param>
|
|
/// <returns></returns>
|
|
public static IPayment GetPayment(string payment)
|
|
{
|
|
|
|
if (payment == "zfb")
|
|
{
|
|
return new AlipayPayment();
|
|
}
|
|
return new WeChatPayment(wechatTenpayClientOptions, weChatConfig);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="productCache"></param>
|
|
/// <param name="payment"></param>
|
|
/// <param name="orderId"></param>
|
|
/// <returns></returns>
|
|
public static T_User_IntentOrder ToIntentOrder(this ProductCache productCache, string payment, string orderId)
|
|
{
|
|
T_User_IntentOrder t_User_IntentOrder = new T_User_IntentOrder()
|
|
{
|
|
Price = productCache.Price,
|
|
CreatedAt = DateTime.Now,
|
|
IntentDate = DateTime.Now,
|
|
Method = payment,
|
|
Notes = "",
|
|
ProductId = productCache.ProductId,
|
|
Quantity = 1,
|
|
Status = 0,
|
|
UpdatedAt = DateTime.Now,
|
|
OrderId = orderId,
|
|
};
|
|
return t_User_IntentOrder;
|
|
}
|
|
}
|
|
}
|