PaymentCenter/app/services/pay_page.go

104 lines
2.7 KiB
Go

package services
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/ordersmodel"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/utils"
"strconv"
"time"
"xorm.io/builder"
)
// 收银台支付渠道列表
func PayPageChannelList(reqParam front.PayChannelListRequest) (resultPayChannelList []paychannelmodel.PayChannel, code int) {
orderId, err := strconv.ParseInt(reqParam.OrderId, 10, 64)
if err != nil {
code = errorcode.ParamError
return
}
// 订单检查
orderInfo := &ordersmodel.Orders{Id: orderId}
orderInfo, code = OrderFindOne(orderInfo, builder.Eq{"id": orderId})
if code != errorcode.Success {
return
}
if !(orderInfo.Status == common.ORDER_STATUS_PAYING || orderInfo.Status == common.ORDER_STATUS_WAITPAY) {
code = errorcode.OrderStatusErr
return
}
// 商户拥有的支付渠道检查
req := backend.PayChannelListRequest{
MerchantId: orderInfo.MerchantId,
PageRequest: entities.PageRequest{},
}
payList := make([]paychannelmodel.PayChannel, 0)
payList, _, code = PayChannelList(req)
if code != errorcode.Success {
return
}
if len(payList) == 0 {
code = errorcode.PayChannelNotFound
return
}
merchantPayChannelMap := make(map[int]paychannelmodel.PayChannel, 0)
for _, pay := range payList {
if !pay.ExpireTime.IsZero() && pay.ExpireTime.Unix() < time.Now().Unix() {
continue
}
merchantPayChannelMap[pay.ChannelType] = pay
}
// 客户端环境检查
openType := ClientEnvCheck(reqParam.UserAgent)
if channels, ok := common.OpenInPayChannelMap[openType]; !ok {
code = errorcode.ClientEnvErr
return
} else {
for _, channel := range channels {
if _, ok = merchantPayChannelMap[channel]; ok {
resultPayChannelList = append(resultPayChannelList, merchantPayChannelMap[channel])
}
}
}
return resultPayChannelList, code
}
// 客户端环境检查
func ClientEnvCheck(ua string) int {
if utils.IsWeChatClient(ua) {
return common.OpenInWeChat
} else if utils.IsPC(ua) {
return common.OpenInWindows
} else if utils.IsMobile(ua) {
return common.OpenInMobile
}
return common.OpenInUnknown
}
// 订单状态检查是否是支付状态
func OrderStatusCheck(order ordersmodel.Orders) (code int) {
switch order.Status {
case common.ORDER_STATUS_CLOSE:
code = errorcode.OrderClosed
case common.ORDER_STATUS_PAYED:
code = errorcode.OrderPayed
case common.ORDER_STATUS_FAILED:
code = errorcode.OrderFailed
case common.ORDER_STATUS_WAITPAY, common.ORDER_STATUS_PAYING:
code = errorcode.Success
default:
code = errorcode.OrderStatusErr
}
return code
}