UnionPayController.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. public IActionResult Index()
  20. {
  21. return View();
  22. }
  23. [HttpGet]
  24. public IActionResult GatewayPayFrontConsume()
  25. {
  26. return View();
  27. }
  28. [HttpPost]
  29. public async Task<IActionResult> GatewayPayFrontConsume(GatewayPayFrontConsumeFrontConsumeViewModel viewModel)
  30. {
  31. var request = new UnionPayGatewayPayFrontConsumeRequest
  32. {
  33. TxnType = "01",
  34. TxnSubType = "01",
  35. BizType = "000201",
  36. ChannelType = "07",
  37. OrderId = viewModel.OrderId,
  38. TxnTime = viewModel.TxnTime,
  39. TxnAmt = viewModel.TxnAmt,
  40. CurrencyCode = viewModel.CurrencyCode,
  41. PayTimeOut = viewModel.PayTimeOut,
  42. FrontUrl = viewModel.FrontUrl,
  43. BackUrl = viewModel.BackUrl
  44. };
  45. var response = await _client.PageExecuteAsync(request);
  46. return Content(response.Body, "text/html", Encoding.UTF8);
  47. }
  48. [HttpPost]
  49. public async Task<IActionResult> GatewayPayFrontConsumeReturn()
  50. {
  51. try
  52. {
  53. var notify = await _notifyClient.ExecuteAsync<UnionPayGatewayPayFrontConsumeReturnResponse>(Request);
  54. ViewData["response"] = "支付成功";
  55. return View();
  56. }
  57. catch
  58. {
  59. ViewData["response"] = "出现错误";
  60. return View();
  61. }
  62. }
  63. }
  64. }