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 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 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 Query(PaymentOrder order) { return await Query(order, 1, 2000); } public async Task 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 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 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 }; } } }