payment_controller.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package payment
  2. import (
  3. "errors"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/lock"
  6. "eta/eta_mini_ht_api/common/utils/redis"
  7. "eta/eta_mini_ht_api/controllers"
  8. orderDomian "eta/eta_mini_ht_api/domian/order"
  9. userDomian "eta/eta_mini_ht_api/domian/user"
  10. "eta/eta_mini_ht_api/service/facade"
  11. "eta/eta_mini_ht_api/service/order"
  12. "gorm.io/gorm"
  13. )
  14. import "eta/eta_mini_ht_api/service/user"
  15. type PaymentController struct {
  16. controllers.BaseController
  17. }
  18. // 支付
  19. type PayRequest struct {
  20. ProductOrderNo string `json:"productOrderNo"`
  21. }
  22. type PayResponse struct {
  23. ProductOrderNo string `json:"productOrderNo"`
  24. TradeOrderNo string `json:"tradeOrderNo"`
  25. }
  26. // PayOrder 支付订单
  27. // @Summary 支付订单
  28. // @Description 支付订单
  29. // @Success 200 {object} controllers.BaseResponse
  30. // @router /payOrder [post]
  31. func (pc *PaymentController) PayOrder() {
  32. controllers.Wrap(&pc.BaseController, func() (result *controllers.WrapData, err error) {
  33. result = pc.InitWrapData("创建支付订单失败")
  34. payRequest := new(PayRequest)
  35. pc.GetPostParams(payRequest)
  36. if payRequest.ProductOrderNo == "" {
  37. pc.FailedResult("订单编号不能为空", result)
  38. err = exception.New(exception.IllegalOrderNo)
  39. return
  40. }
  41. userInfo := pc.Data["user"].(user.User)
  42. defer func() {
  43. lock.ReleaseLock(redis.GeneratePaymentKey(payRequest.ProductOrderNo), pc.Data["threadId"].(string))
  44. }()
  45. if lock.AcquireLock(redis.GeneratePaymentKey(payRequest.ProductOrderNo), 10, pc.Data["threadId"].(string)) {
  46. //校验商品订单状态
  47. var productOrder orderDomian.ProductOrderDTO
  48. productOrder, err = order.GetProductOrderByUser(userInfo.Id, payRequest.ProductOrderNo)
  49. if err != nil {
  50. if errors.Is(err, gorm.ErrRecordNotFound) {
  51. pc.FailedResult("支付失败,商品订单信息不存在", result)
  52. err = exception.New(exception.IllegalOrderNo)
  53. } else {
  54. pc.FailedResult("支付失败,查询商品订单失败", result)
  55. err = exception.New(exception.IllegalOrderNo)
  56. }
  57. return
  58. }
  59. if productOrder.Status != "pending" {
  60. if productOrder.Status == "closed" {
  61. pc.FailedResult("支付失败,商品订单已关闭", result)
  62. err = exception.New(exception.IllegalOrderStatus)
  63. } else {
  64. pc.FailedResult("支付失败,商品订单状态异常", result)
  65. err = exception.New(exception.IllegalOrderStatus)
  66. }
  67. return
  68. }
  69. //1、未发起支付,2、支付到一半退出了,3、支付中
  70. if productOrder.TradeNo != "" {
  71. tradeOrder, tradeErr := order.GetTradeOrderByNo(productOrder.TradeNo)
  72. if tradeErr != nil {
  73. err = exception.NewWithException(exception.PayTradeOrderFailed, tradeErr.Error())
  74. pc.FailedResult("支付失败,获取原支付订单信息失败", result)
  75. return
  76. }
  77. if tradeOrder.PaymentStatus != "failed" {
  78. err = exception.New(exception.PayTradeOrderFailed)
  79. pc.FailedResult("支付失败,商品订单支付中,请勿重复支付", result)
  80. return
  81. }
  82. }
  83. //检查商品状态
  84. err = order.CheckProductStatus(productOrder)
  85. if err != nil {
  86. pc.FailedResult("支付失败,商品状态异常", result)
  87. return
  88. }
  89. var officialUser userDomian.OfficialUserDTO
  90. officialUser, err = user.GetUserByTemplateUserId(userInfo.Id)
  91. if err != nil {
  92. pc.FailedResult("支付失败,正式用户信息不存在", result)
  93. }
  94. var tradeOrderNo string
  95. tradeOrderNo, err = facade.PayOrder(officialUser.ID, userInfo.Id, productOrder)
  96. if err != nil {
  97. pc.FailedResult("支付订单创建失败", result)
  98. err = exception.NewWithException(exception.CreatePaymentOrderFailed, err.Error())
  99. return
  100. }
  101. pc.SuccessResult("支付订单创建成功", PayResponse{
  102. ProductOrderNo: productOrder.OrderID,
  103. TradeOrderNo: tradeOrderNo,
  104. }, result)
  105. return
  106. } else {
  107. pc.FailedResult("商品订单处理中,请稍后再试", result)
  108. err = exception.New(exception.PaymentProcessingError)
  109. return
  110. }
  111. })
  112. }
  113. //退款
  114. //