UnionPayController.cs 2.9 KB

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