122 lines
4.9 KiB
Go
122 lines
4.9 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"`
|
||
AliPayPayChannel *AliPayPayChannel `json:"ali_pay_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
|
||
p.ExpireTime = db.ExpireTime.Format("2006-01-02 15:04:05")
|
||
p.CreateTime = db.CreateTime.Format("2006-01-02 15:04:05")
|
||
|
||
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:
|
||
_ = 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.AliPayPayChannel)
|
||
}
|
||
}
|
||
|
||
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"`
|
||
AliPayPayChannel AliPayPayChannel `json:"ali_pay_pay_channel,omitempty"`
|
||
WechatPayChannel WechatPayChannel `json:"wechat_pay_channel,omitempty"`
|
||
}
|
||
|
||
type WechatPayChannel struct {
|
||
MchId string `json:"mch_id"` //直连商户号
|
||
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"` //商户证书序列号
|
||
MchAPIv3Key string `json:"mch_APIv3_key"` //商户APIv3密钥
|
||
PrivateKeyPath string `json:"private_key_path"` //商户私钥文件路径
|
||
}
|
||
|
||
type AliPayPayChannel struct {
|
||
AliPublicKey string `json:"ali_public_key"` //支付宝公钥
|
||
PrivateKeyPath string `json:"private_key_path"` //应用私钥
|
||
SignType string `json:"sign_type"` //商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
|
||
}
|
||
|
||
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.AliPayPayChannel)
|
||
db.ExtJson = string(b)
|
||
default:
|
||
err = errors.New("支付渠道类型错误")
|
||
}
|
||
return
|
||
}
|
||
|
||
type PayChannelListRequest struct {
|
||
MerchantId int64 `json:"merchant_id"`
|
||
entities.PageRequest
|
||
}
|
||
|
||
type PayChannelUpdateRequest struct {
|
||
Id int64 `json:"id" validate:"required"`
|
||
PayChannelCreateRequest
|
||
}
|
||
|
||
func (p PayChannelUpdateRequest) RequestToDb() (db paychannelmodel.PayChannel, err error) {
|
||
db.Id = p.Id
|
||
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.AliPayPayChannel)
|
||
db.ExtJson = string(b)
|
||
default:
|
||
err = errors.New("支付渠道类型错误")
|
||
}
|
||
return
|
||
}
|