123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package payment
- import (
- "errors"
- "eta/eta_mini_ht_api/common/exception"
- "eta/eta_mini_ht_api/common/utils/lock"
- "eta/eta_mini_ht_api/common/utils/redis"
- "eta/eta_mini_ht_api/controllers"
- orderDomian "eta/eta_mini_ht_api/domian/order"
- userDomian "eta/eta_mini_ht_api/domian/user"
- "eta/eta_mini_ht_api/service/facade"
- "eta/eta_mini_ht_api/service/order"
- "gorm.io/gorm"
- )
- import "eta/eta_mini_ht_api/service/user"
- type PaymentController struct {
- controllers.BaseController
- }
- // 支付
- type PayRequest struct {
- ProductOrderNo string `json:"productOrderNo"`
- }
- type PayResponse struct {
- ProductOrderNo string `json:"productOrderNo"`
- TradeOrderNo string `json:"tradeOrderNo"`
- }
- // PayOrder 支付订单
- // @Summary 支付订单
- // @Description 支付订单
- // @Success 200 {object} controllers.BaseResponse
- // @router /payOrder [post]
- func (pc *PaymentController) PayOrder() {
- controllers.Wrap(&pc.BaseController, func() (result *controllers.WrapData, err error) {
- result = pc.InitWrapData("创建支付订单失败")
- payRequest := new(PayRequest)
- pc.GetPostParams(payRequest)
- if payRequest.ProductOrderNo == "" {
- pc.FailedResult("订单编号不能为空", result)
- err = exception.New(exception.IllegalOrderNo)
- return
- }
- userInfo := pc.Data["user"].(user.User)
- defer func() {
- lock.ReleaseLock(redis.GeneratePaymentKey(payRequest.ProductOrderNo), pc.Data["threadId"].(string))
- }()
- if lock.AcquireLock(redis.GeneratePaymentKey(payRequest.ProductOrderNo), 10, pc.Data["threadId"].(string)) {
- //校验商品订单状态
- var productOrder orderDomian.ProductOrderDTO
- productOrder, err = order.GetProductOrderByUser(userInfo.Id, payRequest.ProductOrderNo)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- pc.FailedResult("支付失败,商品订单信息不存在", result)
- err = exception.New(exception.IllegalOrderNo)
- } else {
- pc.FailedResult("支付失败,查询商品订单失败", result)
- err = exception.New(exception.IllegalOrderNo)
- }
- return
- }
- if productOrder.Status != "pending" {
- if productOrder.Status == "closed" {
- pc.FailedResult("支付失败,商品订单已关闭", result)
- err = exception.New(exception.IllegalOrderStatus)
- } else {
- pc.FailedResult("支付失败,商品订单状态异常", result)
- err = exception.New(exception.IllegalOrderStatus)
- }
- return
- }
- //1、未发起支付,2、支付到一半退出了,3、支付中
- if productOrder.TradeNo != "" {
- tradeOrder, tradeErr := order.GetTradeOrderByNo(productOrder.TradeNo)
- if tradeErr != nil {
- err = exception.NewWithException(exception.PayTradeOrderFailed, tradeErr.Error())
- pc.FailedResult("支付失败,获取原支付订单信息失败", result)
- return
- }
- if tradeOrder.PaymentStatus != "failed" {
- err = exception.New(exception.PayTradeOrderFailed)
- pc.FailedResult("支付失败,商品订单支付中,请勿重复支付", result)
- return
- }
- }
- //检查商品状态
- err = order.CheckProductStatus(productOrder)
- if err != nil {
- pc.FailedResult("支付失败,商品状态异常", result)
- return
- }
- var officialUser userDomian.OfficialUserDTO
- officialUser, err = user.GetUserByTemplateUserId(userInfo.Id)
- if err != nil {
- pc.FailedResult("支付失败,正式用户信息不存在", result)
- }
- var tradeOrderNo string
- tradeOrderNo, err = facade.PayOrder(officialUser.ID, userInfo.Id, productOrder)
- if err != nil {
- pc.FailedResult("支付订单创建失败", result)
- err = exception.NewWithException(exception.CreatePaymentOrderFailed, err.Error())
- return
- }
- pc.SuccessResult("支付订单创建成功", PayResponse{
- ProductOrderNo: productOrder.OrderID,
- TradeOrderNo: tradeOrderNo,
- }, result)
- return
- } else {
- pc.FailedResult("商品订单处理中,请稍后再试", result)
- err = exception.New(exception.PaymentProcessingError)
- return
- }
- })
- }
- //退款
- //
|