136 lines
5.6 KiB
Go
136 lines
5.6 KiB
Go
package backend
|
||
|
||
import (
|
||
"PaymentCenter/app/constants/common"
|
||
"PaymentCenter/app/http/entities"
|
||
"PaymentCenter/app/models/paychannelmodel"
|
||
"PaymentCenter/app/utils"
|
||
"encoding/json"
|
||
"github.com/pkg/errors"
|
||
)
|
||
|
||
type PayChannelResponse struct {
|
||
Id int64 `json:"id"`
|
||
PayName string `json:"pay_name"`
|
||
MerchantId int64 `json:"merchant_id"`
|
||
ChannelType int `json:"channel_type"`
|
||
AppId string `json:"app_id"`
|
||
ExpireTime string `json:"expire_time"`
|
||
CreateTime string `json:"create_time"`
|
||
AliPayChannel *AliPayChannel `json:"ali_pay_channel,omitempty"`
|
||
WechatPayChannel *WechatPayChannel `json:"wechat_pay_channel,omitempty"`
|
||
}
|
||
|
||
func (p *PayChannelResponse) ResponseFromDb(db paychannelmodel.PayChannel) {
|
||
p.Id = db.Id
|
||
p.PayName = db.PayName
|
||
p.MerchantId = db.MerchantId
|
||
p.ChannelType = db.ChannelType
|
||
p.AppId = db.AppId
|
||
if db.ExpireTime.IsZero() {
|
||
p.ExpireTime = ""
|
||
} else {
|
||
p.ExpireTime = db.ExpireTime.Format("2006-01-02 15:04:05")
|
||
}
|
||
if db.CreateTime.IsZero() {
|
||
p.CreateTime = ""
|
||
} else {
|
||
p.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
|
||
}
|
||
|
||
switch db.ChannelType {
|
||
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
||
_ = json.Unmarshal([]byte(db.ExtJson), &p.WechatPayChannel)
|
||
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
||
_ = json.Unmarshal([]byte(db.ExtJson), &p.AliPayChannel)
|
||
}
|
||
}
|
||
|
||
type PayChannelCreateRequest struct {
|
||
PayName string `json:"pay_name" validate:"required" label:"支付渠道名称"`
|
||
MerchantId int64 `json:"merchant_id" validate:"required" label:"商户ID"`
|
||
ChannelType int `json:"channel_type" validate:"required" label:"支付渠道"` //支付渠道枚举,1微信JSAPI,2微信H5,3微信app,4微信Native,5微信小程序,6支付宝网页&移动应用,7支付宝小程序,8支付宝JSAPI
|
||
AppId string `json:"app_id" validate:"required" label:"应用appId"`
|
||
ExpireTime string `json:"expire_time"`
|
||
AliPayChannel AliPayChannel `json:"ali_pay_channel,omitempty"`
|
||
WechatPayChannel WechatPayChannel `json:"wechat_pay_channel,omitempty"`
|
||
}
|
||
|
||
type WechatPayChannel struct {
|
||
MchId string `json:"mch_id"` // 商户ID 或者服务商模式的 sp_mchid
|
||
SerialNo string `json:"serial_no"` // 商户证书的证书序列号
|
||
ApiV3Key string `json:"api_v_3_key"` // apiV3Key,商户平台获取
|
||
PrivateKey string `json:"private_key"` // 私钥 apiclient_key.pem 读取后的内容
|
||
}
|
||
|
||
type AliPayChannel struct {
|
||
PrivateKey string `json:"private_key"` // 应用私钥
|
||
AppPublicCert string `json:"app_public_cert"` // 应用公钥
|
||
AlipayRootCert string `json:"alipay_root_cert"` // 支付宝根证书
|
||
AlipayPublicCert string `json:"alipay_public_cert"` // 支付宝公钥
|
||
}
|
||
|
||
func (p *PayChannelCreateRequest) RequestToDb() (db paychannelmodel.PayChannel, err error) {
|
||
db.PayName = p.PayName
|
||
db.MerchantId = p.MerchantId
|
||
db.ChannelType = p.ChannelType
|
||
db.AppId = p.AppId
|
||
if p.ExpireTime != "" {
|
||
db.ExpireTime, err = utils.StrToTimeShanghai(p.ExpireTime)
|
||
if err != nil {
|
||
err = errors.Wrap(err, "时间格式错误")
|
||
}
|
||
}
|
||
switch p.ChannelType {
|
||
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
||
b, _ := json.Marshal(p.WechatPayChannel)
|
||
db.ExtJson = string(b)
|
||
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
||
b, _ := json.Marshal(p.AliPayChannel)
|
||
db.ExtJson = string(b)
|
||
default:
|
||
err = errors.New("支付渠道类型错误")
|
||
}
|
||
return
|
||
}
|
||
|
||
type PayChannelListRequest struct {
|
||
Id int64 `json:"id" form:"id" `
|
||
MerchantId int64 `json:"merchant_id" form:"merchant_id" validate:"required" label:"商户ID"`
|
||
entities.PageRequest
|
||
}
|
||
|
||
type PayChannelUpdateRequest struct {
|
||
Id int64 `json:"id" validate:"required"`
|
||
PayName string `json:"pay_name" validate:"required" label:"支付渠道名称"`
|
||
ChannelType int `json:"channel_type" validate:"required" label:"支付渠道"` //支付渠道枚举,1微信JSAPI,2微信H5,3微信app,4微信Native,5微信小程序,6支付宝网页&移动应用,7支付宝小程序,8支付宝JSAPI
|
||
AppId string `json:"app_id" validate:"required" label:"应用appId"`
|
||
ExpireTime string `json:"expire_time"`
|
||
AliPayChannel AliPayChannel `json:"ali_pay_channel,omitempty"`
|
||
WechatPayChannel WechatPayChannel `json:"wechat_pay_channel,omitempty"`
|
||
}
|
||
|
||
func (p PayChannelUpdateRequest) RequestToDb() (db paychannelmodel.PayChannel, err error) {
|
||
db.Id = p.Id
|
||
db.PayName = p.PayName
|
||
db.ChannelType = p.ChannelType
|
||
db.AppId = p.AppId
|
||
if p.ExpireTime != "" {
|
||
db.ExpireTime, err = utils.StrToTimeShanghai(p.ExpireTime)
|
||
if err != nil {
|
||
err = errors.Wrap(err, "时间格式错误")
|
||
}
|
||
}
|
||
switch p.ChannelType {
|
||
case common.PAY_CHANNEL_WECHAT_H5, common.PAY_CHANNEL_WECHAT_JSAPI, common.PAY_CHANNEL_WECHAT_NATIVE, common.PAY_CHANNEL_WECHAT_APP, common.PAY_CHANNEL_WECHAT_MINI:
|
||
b, _ := json.Marshal(p.WechatPayChannel)
|
||
db.ExtJson = string(b)
|
||
case common.PAY_CHANNEL_ALIPAY_JSAPI, common.PAY_CHANNEL_ALIPAY_WEB, common.PAY_CHANNEL_ALIPAY_MINI:
|
||
b, _ := json.Marshal(p.AliPayChannel)
|
||
db.ExtJson = string(b)
|
||
default:
|
||
err = errors.New("支付渠道类型错误")
|
||
}
|
||
return
|
||
}
|