using Edge.Core.Processor; using Edge.Core.Processor.Dispatcher.Attributes; using Edge.Core.UniversalApi; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using PaymentGateway.GatewayApp; using Gateway.Payment.Shared; using System; using System.Collections.Generic; using System.Globalization; using System.Threading; using System.Threading.Tasks; using System.Linq; namespace Gateway.Payment { [MetaPartsDescriptor( "lang-zh-cn:支付网关Applang-en-us:Payment gateway App", "lang-zh-cn:用于支持移动支付功能lang-en-us:Used for support mobile payment, like QR code scan payment and etc.", new[] { "lang-zh-cn:支付网关lang-en-us:PaymentGateway" })] public class App : IAppProcessor { private IServiceProvider services; private ILogger logger; private AppConfigV1 appConfig; public string MetaConfigName { get; set; } public void Init(IEnumerable processors) { //throw new NotImplementedException(); } public class AppConfigV1 { public TongLianPayConfigAndCertificationV1 TongLianPayV1Config { get; set; } public TongLianPayConfigAndCertificationV2 TongLianPayV2Config { get; set; } public AliPayConfigAndCertification AliPayConfig { get; set; } public WxPayConfigAndCertification WxPayConfig { get; set; } } [ParamsJsonSchemas("appCtorParamsJsonSchema")] public App(AppConfigV1 appConfig, IServiceProvider services) { // // // 00191280 // 561599055417A40 // eabd7016a0bddd25dd339e0aefc665e0 // 11 // https://vsp.allinpay.com/apiweb/unitorder // // 3 // 120 // if (appConfig?.TongLianPayV1Config?.Config?.notifyUrl?.Contains("showmethemoney", StringComparison.OrdinalIgnoreCase) ?? false) appConfig.TongLianPayV1Config = new TongLianPayConfigAndCertificationV1() { Config = new TongLianPayConfigV1() { appId = "00191280", cusId = "561599055417A40", appKey = "eabd7016a0bddd25dd339e0aefc665e0", apiVersion = "11", apiUrl = "https://vsp.allinpay.com/apiweb/unitorder", notifyUrl = "", queryInterval = 3, queryTimeout = 120, } }; // // // M00000005 // 1323455910784892929 // 3dab2c7911b74f3cb3241c083a0e055e // S0049 // 01 // 1.0 // utf-8 // MD5 // https://cloud.aipgd.com/gateway // // 10 // 90 // if (appConfig?.TongLianPayV2Config?.Config?.notifyUrl?.Contains("showmethemoney", StringComparison.OrdinalIgnoreCase) ?? false) appConfig.TongLianPayV2Config = new TongLianPayConfigAndCertificationV2() { Config = new TongLianPayConfigV2() { appId = "M00000005", sysId = "1323455910784892929", key = "3dab2c7911b74f3cb3241c083a0e055e", oilStationNo = "S0049", shiftsMask = "01", version = "1.0", charset = "utf-8", signType = "MD5", apiUrl = "https://cloud.aipgd.com/gateway", notifyUrl = "", queryInterval = 10, queryTimeout = 90 } }; this.appConfig = appConfig; this.services = services; var loggerFactory = services.GetRequiredService(); this.logger = loggerFactory.CreateLogger("DynamicPrivate_Gateway.Payment"); } private static Aop.Api.Request.AlipayOpenOperationOpenbizmockBizQueryRequest GetAlipayRequest() { // 初始化Request,并填充Model属性。实际调用时请替换为您想要使用的API对应的Request对象。 Aop.Api.Request.AlipayOpenOperationOpenbizmockBizQueryRequest request = new Aop.Api.Request.AlipayOpenOperationOpenbizmockBizQueryRequest(); Aop.Api.Domain.AlipayOpenOperationOpenbizmockBizQueryModel model = new Aop.Api.Domain.AlipayOpenOperationOpenbizmockBizQueryModel { BizNo = "123456789" }; request.SetBizModel(model); return request; } [UniversalApi] public async Task StartPayment(PaymentOrderDto input) { var mapper = this.services.GetRequiredService(); var order = mapper.Map(input); order.ServerSideTimestamp = DateTime.Now; order.PayResults = new List(); if (!input.IsForRefund) { var billNumber = SequenceNumber.Next(); order.BillNumber = billNumber; } this.logger.LogInformation("New PaymentOrder received for start a payment: " + order.ToFullJsonLogString()); try { var returnCode = ClientSideReturnCode.PAY_ERROR; switch (order.PaymentMethod) { case "WX_SCAN": { //https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=11_1 WechatPaymentProcessor processor = new WechatPaymentProcessor(); //Get WxPayConfig and Certifiction File WxPayConfigAndCertification wxPayConfigAndCertification = null;// ConfigHelper.WxConfigInstances(currentBuId, configServiceUrl, wxPayConfigName); //if (wxPayConfigAndCertification != null) //{ // input.Config = wxPayConfigAndCertification.ConfigInfo?.Config; // order.Certification = wxPayConfigAndCertification.CertificationInfo?.Certification; //} var genericResponseWeChat = order.IsForRefund ? await processor.Return(order).ConfigureAwait(false) : await processor.Process(order).ConfigureAwait(false); var wechatResponse = genericResponseWeChat.WeChatResponse; if (order.TradeStatus == TradeStatusEnum.SUCCESS) { returnCode = ClientSideReturnCode.OK; } else { returnCode = ClientSideReturnCode.PAY_ERROR; if (!order.IsForRefund) { logger.LogInformation("Mark order(WeChat): " + order.ToFullJsonLogString() + " to cancel since returnCode is: " + returnCode); processor.Cancel(order); } } order.PayResults.Add(new PaymentOrderPayResult() { PaymentResultCode = ((int)returnCode).ToString(CultureInfo.InvariantCulture), PaymentResultMessage = returnCode.ToString(), PaymentResultErrorDetail = wechatResponse.IsSet("err_code") ? wechatResponse.GetValue("err_code").ToString() : "", PaymentResultRawResult = wechatResponse.ToXml() }); break; } case "ALI_SCAN": { //https://opendocs.alipay.com/open/54/103419 try { // 1. 创建IAopClient实例 Aop.Api.IAopClient client = new Aop.Api.DefaultAopClient( "https://openapi.alipay.com/gateway.do", "2019091767145019", //请更换为您的AppId "MIIEowIBAAKCAQ ... ...", //请更换为您的PKCS1格式的应用私钥 "json", "1.0", "RSA2", "utf-8", //请更换为您使用的字符集编码,推荐采用utf-8 false, new Aop.Api.CertParams { //请更换为您的应用公钥证书文件路径 AlipayPublicCertPath = "/home/foo/alipayCertPublicKey_RSA2.crt", //请更换您的支付宝公钥证书文件路径 AppCertPath = "/home/foo/appCertPublicKey_2019090366875133.crt", //请更换为支付宝根证书文件路径 RootCertPath = "/home/foo/alipayRootCert.crt" }); // 2. 创建使用的Open API对应的Request请求对象 Aop.Api.Request.AlipayOpenOperationOpenbizmockBizQueryRequest request = GetAlipayRequest(); // 3. 发起请求并处理响应 Aop.Api.Response.AlipayOpenOperationOpenbizmockBizQueryResponse response = client.CertificateExecute(request); if (!response.IsError) { Console.WriteLine("调用成功。"); } else { Console.WriteLine("调用失败,原因:" + response.Msg + "," + response.SubMsg); } } catch (Exception e) { Console.WriteLine("调用遭遇异常,原因:" + e.Message); throw e; } break; } case "ALL_IN_SCAN": var v1Processor = new TongLianAllInPayV1Processor(services); var tongLianPayConfigV1 = this.appConfig.TongLianPayV1Config; if (tongLianPayConfigV1 != null) order.Config = tongLianPayConfigV1.Config; var v1GenericResponse = order.IsForRefund ? await v1Processor.Return(order).ConfigureAwait(false) : await v1Processor.Process(order).ConfigureAwait(false); var v1Response = v1GenericResponse.AllInPayResponse; if (v1Response != null && order.TradeStatus == TradeStatusEnum.SUCCESS) { returnCode = ClientSideReturnCode.OK; } else { returnCode = ClientSideReturnCode.PAY_ERROR; if (!order.IsForRefund) { logger.LogInformation("Mark order(AllInpay): " + order.ToFullJsonLogString() + " to cancel since payment returnCode is: " + returnCode); await v1Processor.Cancel(order); } } order.PayResults.Add(new PaymentOrderPayResult() { BillNumber = order.BillNumber, PaymentResultCode = ((int)returnCode).ToString(CultureInfo.InvariantCulture), PaymentResultMessage = returnCode.ToString(), PaymentResultErrorDetail = v1Response != null && v1Response.ContainsKey("retmsg") ? v1Response["retmsg"] : "", PaymentResultRawResult = v1Response != null && v1Response.ContainsKey("errmsg") ? v1Response["errmsg"] : "" }); break; case "ALL_IN_SCAN_V2": { var v2Processor = new TongLianAllInPayV2Processor(services); TongLianPayConfigAndCertificationV2 tongLianPayConfig = this.appConfig.TongLianPayV2Config; if (tongLianPayConfig != null) order.Config = tongLianPayConfig.Config; var v2GenericResponse = order.IsForRefund ? await v2Processor.Return(order).ConfigureAwait(false) : await v2Processor.Process(order).ConfigureAwait(false); var v2Response = v2GenericResponse.AllInPayResponseV2; if (order.TradeStatus == TradeStatusEnum.SUCCESS) { returnCode = ClientSideReturnCode.OK; } else { returnCode = ClientSideReturnCode.PAY_ERROR; if (!order.IsForRefund) { logger.LogInformation($"Mark an order(AllInpayV2) to cancel since payment returnCode is: { returnCode}, the raw order is: { order.ToFullJsonLogString() }"); await v2Processor.Cancel(order); } } order.PayResults.Add(new PaymentOrderPayResult() { PaymentResultCode = ((int)returnCode).ToString(CultureInfo.InvariantCulture), PaymentResultMessage = returnCode.ToString(), PaymentResultErrorDetail = v2Response.bizMsg + "|" + (v2Response.payStatusMsg ?? ""), PaymentResultRawResult = v2Response.bizCode }); break; } default: { logger.LogError("Unsupport PaymentMethod: " + order.PaymentMethod + " received."); throw new ArgumentException("Unsupport PaymentMethod: " + order.PaymentMethod); } } } catch (Exception ex) { logger.LogError("Error occured in StartPayment, detail: " + ex); order.PayResults.Add(new PaymentOrderPayResult() { PaymentResultCode = "XX4XX4XX", PaymentResultMessage = "订单支付失败于本地", PaymentResultErrorDetail = "本地内部错误" }); } return order; } } }