WechatPaymentProcessor.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Threading.Tasks;
  4. using Gateway.Payment.Shared;
  5. using Wechat.PayAPI;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.DependencyInjection;
  9. namespace PaymentGateway.GatewayApp
  10. {
  11. public class WechatPaymentProcessor : IPaymentProcessor
  12. {
  13. public static Microsoft.Extensions.Logging.ILogger Log = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;
  14. public async Task<GenericProcessResponse> Process(PaymentOrder order)
  15. {
  16. Log.LogInformation("MicroPay", "MicroPay is processing order " + order.BillNumber);
  17. TradeStatusEnum tradeStatus = TradeStatusEnum.PAYERROR;
  18. var response = new GenericProcessResponse()
  19. {
  20. WeChatResponse = new WxPayData()
  21. };
  22. if (order.ExtraCommand == "preCreatedBillNumber")
  23. {
  24. response = await Query(order, 40, 2000);
  25. if (response.WeChatResponse.IsSet("trade_state") &&
  26. response.WeChatResponse.GetValue("trade_state") as string == "SUCCESS")
  27. {
  28. tradeStatus = TradeStatusEnum.SUCCESS;
  29. Log.LogInformation("MicroPay TradeStatus.SUCCESS", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
  30. }
  31. else
  32. {
  33. if (response.WeChatResponse.IsSet("trade_state"))
  34. {
  35. Log.LogInformation("MicroPay", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
  36. }
  37. else
  38. {
  39. Log.LogInformation("MicroPay", $"The final MicroPay response is: {response.WeChatResponse.GetValue("trade_state")}");
  40. }
  41. }
  42. }
  43. else
  44. {
  45. response.WeChatResponse = MicroPay.Run(order, out tradeStatus);
  46. }
  47. order.TradeStatus = tradeStatus;
  48. Log.LogInformation("MicroPay", "The final MicroPay response is: \r\n" + response.WeChatResponse.ToXml());
  49. return response;
  50. }
  51. public async Task<GenericProcessResponse> Cancel(PaymentOrder order)
  52. {
  53. Log.LogInformation("MicroPay", "Micropay failure, reverse order " + order.BillNumber);
  54. order.TradeStatus = TradeStatusEnum.CANCELLING;
  55. var result = MicroPay.Cancel(order.BillNumber, (WxPayConfig)order.Config, (X509Certificate2)order.Certification);
  56. if (result)
  57. {
  58. Log.LogInformation("MicroPay", "Micropay Cancel Order successed, order " + order.BillNumber + " has been closed.");
  59. order.TradeStatus = TradeStatusEnum.CLOSED;
  60. }
  61. //try
  62. //{
  63. // var db = new ApplicationDbContext();
  64. // //update order status in DB
  65. // db.Entry(order).State = EntityState.Modified;
  66. // await db.SaveChangesAsync();
  67. //}
  68. //catch (Exception exx)
  69. //{
  70. // Log.Error("MicroPay", "Error in updating order status, detail: " + exx);
  71. //}
  72. return new GenericProcessResponse();
  73. }
  74. public async Task<GenericProcessResponse> Query(PaymentOrder order)
  75. {
  76. return await Query(order, 1, 2000);
  77. }
  78. public async Task<GenericProcessResponse> Query(PaymentOrder order, int count, int interval)
  79. {
  80. Log.LogInformation("MicroPay", "Wechat OrderQuery is processing order: " + order.BillNumber);
  81. var result = MicroPay.RunQuery(order, count, interval);
  82. Log.LogInformation("MicroPay", "Wechat OrderQuery process complete, result : \r\n" + result.ToXml());
  83. return new GenericProcessResponse()
  84. {
  85. WeChatResponse = result
  86. };
  87. }
  88. public async Task<GenericProcessResponse> Return(PaymentOrder order)
  89. {
  90. Log.LogInformation("MicroPay", "MicroPay is processing refund, BillNumber = " + order.BillNumber);
  91. TradeStatusEnum tradeStatus;
  92. //var result = Refund.Run("", order.BillNumber,
  93. // order.TotalAmount.ToString(), order.NetAmount.ToString(), out tradeStatus);
  94. var result = Refund.Run("", order, out tradeStatus);
  95. order.TradeStatus = tradeStatus;
  96. Log.LogInformation("MicroPay", "The final MicroPay response is: \r\n" + result.ToXml());
  97. return new GenericProcessResponse()
  98. {
  99. WeChatResponse = result
  100. };
  101. }
  102. public async Task<GenericProcessResponse> UnifiedOrder(PaymentOrder order)
  103. {
  104. Log.LogInformation("UnifiedOrder", "UnifiedOrder is processing order " + order.BillNumber);
  105. TradeStatusEnum tradeStatus;
  106. var result = MicroPay.UnifiedOrder(order, out tradeStatus);
  107. order.TradeStatus = tradeStatus;
  108. Log.LogInformation("UnifiedOrder", "The final MicroPay UnifiedOrder is: \r\n" + result.ToXml());
  109. return new GenericProcessResponse()
  110. {
  111. WeChatResponse = result
  112. };
  113. }
  114. }
  115. }