UnionPayController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Text;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.UnionPay;
  4. using Essensoft.AspNetCore.Payment.UnionPay.Notify;
  5. using Essensoft.AspNetCore.Payment.UnionPay.Request;
  6. using Microsoft.AspNetCore.Mvc;
  7. using WebApplicationSample.Models;
  8. namespace WebApplicationSample.Controllers
  9. {
  10. public class UnionPayController : Controller
  11. {
  12. private readonly IUnionPayClient _client;
  13. private readonly IUnionPayNotifyClient _notifyClient;
  14. public UnionPayController(IUnionPayClient client, IUnionPayNotifyClient notifyClient)
  15. {
  16. _client = client;
  17. _notifyClient = notifyClient;
  18. }
  19. /// <summary>
  20. /// 银联支付指引页
  21. /// </summary>
  22. /// <returns></returns>
  23. public IActionResult Index()
  24. {
  25. return View();
  26. }
  27. /// <summary>
  28. /// 跳转网关页面支付
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet]
  32. public IActionResult GatewayPayFrontConsume()
  33. {
  34. return View();
  35. }
  36. /// <summary>
  37. /// 跳转网关页面支付
  38. /// </summary>
  39. /// <param name="viewModel"></param>
  40. /// <returns></returns>
  41. [HttpPost]
  42. public async Task<IActionResult> GatewayPayFrontConsume(GatewayPayFrontConsumeFrontConsumeViewModel viewModel)
  43. {
  44. var request = new UnionPayGatewayPayFrontConsumeRequest
  45. {
  46. TxnType = "01",
  47. TxnSubType = "01",
  48. BizType = "000201",
  49. ChannelType = "07",
  50. OrderId = viewModel.OrderId,
  51. TxnTime = viewModel.TxnTime,
  52. TxnAmt = viewModel.TxnAmt,
  53. CurrencyCode = viewModel.CurrencyCode,
  54. PayTimeOut = viewModel.PayTimeOut,
  55. FrontUrl = viewModel.FrontUrl,
  56. BackUrl = viewModel.BackUrl
  57. };
  58. var response = await _client.PageExecuteAsync(request);
  59. return Content(response.Body, "text/html", Encoding.UTF8);
  60. }
  61. /// <summary>
  62. /// 网关页面支付 - 同步跳转
  63. /// </summary>
  64. /// <returns></returns>
  65. [HttpPost]
  66. public async Task<IActionResult> GatewayPayFrontConsumeReturn()
  67. {
  68. try
  69. {
  70. var notify = await _notifyClient.ExecuteAsync<UnionPayGatewayPayFrontConsumeReturn>(Request);
  71. ViewData["response"] = "支付成功";
  72. return View();
  73. }
  74. catch
  75. {
  76. ViewData["response"] = "出现错误";
  77. return View();
  78. }
  79. }
  80. }
  81. }