123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Security.Cryptography.X509Certificates;
- using System.Threading.Tasks;
- using Gateway.Payment.Shared;
- using Wechat.PayAPI;
- using Microsoft.Extensions.Logging.Abstractions;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.DependencyInjection;
- namespace PaymentGateway.GatewayApp
- {
- public class WechatPaymentProcessor : IPaymentProcessor
- {
- public static Microsoft.Extensions.Logging.ILogger Log = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;
- public async Task<GenericProcessResponse> Process(PaymentOrder order)
- {
- Log.LogInformation("MicroPay", "MicroPay is processing order " + order.BillNumber);
- TradeStatusEnum tradeStatus = TradeStatusEnum.PAYERROR;
- var response = new GenericProcessResponse()
- {
- WeChatResponse = new WxPayData()
- };
- if (order.ExtraCommand == "preCreatedBillNumber")
- {
- response = await Query(order, 40, 2000);
- if (response.WeChatResponse.IsSet("trade_state") &&
- response.WeChatResponse.GetValue("trade_state") as string == "SUCCESS")
- {
- tradeStatus = TradeStatusEnum.SUCCESS;
- Log.LogInformation("MicroPay TradeStatus.SUCCESS", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
- }
- else
- {
- if (response.WeChatResponse.IsSet("trade_state"))
- {
- Log.LogInformation("MicroPay", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
- }
- else
- {
- Log.LogInformation("MicroPay", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
- }
- }
- }
- else
- {
- response.WeChatResponse = MicroPay.Run(order, out tradeStatus);
- }
- order.TradeStatus = tradeStatus;
- Log.LogInformation("MicroPay", "The final MicroPay response is: \r\n" + response.WeChatResponse.ToXml());
- return response;
- }
- public async Task<GenericProcessResponse> Cancel(PaymentOrder order)
- {
- Log.LogInformation("MicroPay", "Micropay failure, reverse order " + order.BillNumber);
- order.TradeStatus = TradeStatusEnum.CANCELLING;
- var result = MicroPay.Cancel(order.BillNumber, (WxPayConfig)order.Config, (X509Certificate2)order.Certification);
- if (result)
- {
- Log.LogInformation("MicroPay", "Micropay Cancel Order successed, order " + order.BillNumber + " has been closed.");
- order.TradeStatus = TradeStatusEnum.CLOSED;
- }
- //try
- //{
- // var db = new ApplicationDbContext();
- // //update order status in DB
- // db.Entry(order).State = EntityState.Modified;
- // await db.SaveChangesAsync();
- //}
- //catch (Exception exx)
- //{
- // Log.Error("MicroPay", "Error in updating order status, detail: " + exx);
- //}
- return new GenericProcessResponse();
- }
- public async Task<GenericProcessResponse> Query(PaymentOrder order)
- {
- return await Query(order, 1, 2000);
- }
- public async Task<GenericProcessResponse> Query(PaymentOrder order, int count, int interval)
- {
- Log.LogInformation("MicroPay", "Wechat OrderQuery is processing order: " + order.BillNumber);
- var result = MicroPay.RunQuery(order, count, interval);
- Log.LogInformation("MicroPay", "Wechat OrderQuery process complete, result : \r\n" + result.ToXml());
- return new GenericProcessResponse()
- {
- WeChatResponse = result
- };
- }
- public async Task<GenericProcessResponse> Return(PaymentOrder order)
- {
- Log.LogInformation("MicroPay", "MicroPay is processing refund, BillNumber = " + order.BillNumber);
- TradeStatusEnum tradeStatus;
- //var result = Refund.Run("", order.BillNumber,
- // order.TotalAmount.ToString(), order.NetAmount.ToString(), out tradeStatus);
- var result = Refund.Run("", order, out tradeStatus);
- order.TradeStatus = tradeStatus;
- Log.LogInformation("MicroPay", "The final MicroPay response is: \r\n" + result.ToXml());
- return new GenericProcessResponse()
- {
- WeChatResponse = result
- };
- }
- public async Task<GenericProcessResponse> UnifiedOrder(PaymentOrder order)
- {
- Log.LogInformation("UnifiedOrder", "UnifiedOrder is processing order " + order.BillNumber);
- TradeStatusEnum tradeStatus;
- var result = MicroPay.UnifiedOrder(order, out tradeStatus);
- order.TradeStatus = tradeStatus;
- Log.LogInformation("UnifiedOrder", "The final MicroPay UnifiedOrder is: \r\n" + result.ToXml());
- return new GenericProcessResponse()
- {
- WeChatResponse = result
- };
- }
- }
- }
|