123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- 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<IProcessor> 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)
- {
- //<?xml version="1.0" encoding="utf-8"?>
- //<TongLianPayConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- // <appId>00191280</appId>
- // <cusId>561599055417A40</cusId>
- // <appKey>eabd7016a0bddd25dd339e0aefc665e0</appKey>
- // <apiVersion>11</apiVersion>
- // <apiUrl>https://vsp.allinpay.com/apiweb/unitorder</apiUrl>
- // <notifyUrl></notifyUrl>
- // <queryInterval>3</queryInterval>
- // <queryTimeout>120</queryTimeout>
- //</TongLianPayConfig>
- 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,
- }
- };
- //<? xml version="1.0" encoding="utf-8"?>
- //<TongLianPayConfigV2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- // <appId>M00000005</appId>
- // <sysId>1323455910784892929</sysId>
- // <key>3dab2c7911b74f3cb3241c083a0e055e</key>
- // <oilStationNo>S0049</oilStationNo>
- // <shiftsMask>01</shiftsMask>
- // <version>1.0</version>
- // <charset>utf-8</charset>
- // <signType>MD5</signType>
- // <apiUrl>https://cloud.aipgd.com/gateway</apiUrl>
- // <notifyUrl></notifyUrl>
- // <queryInterval>10</queryInterval>
- // <queryTimeout>90</queryTimeout>
- //</TongLianPayConfigV2>
- 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<ILoggerFactory>();
- 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<PaymentOrder> StartPayment(PaymentOrderDto input)
- {
- var mapper = this.services.GetRequiredService<AutoMapper.IMapper>();
- var order = mapper.Map<PaymentOrder>(input);
- order.ServerSideTimestamp = DateTime.Now;
- order.PayResults = new List<PaymentOrderPayResult>();
- 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;
- }
- }
- }
|