317 lines
8.4 KiB
Go
317 lines
8.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"order/model/dto"
|
|
"order/service"
|
|
)
|
|
|
|
type PaymentConfigController struct{}
|
|
|
|
var PaymentConfig = &PaymentConfigController{}
|
|
|
|
// CreatePaymentConfig 创建支付配置
|
|
func (c *PaymentConfigController) CreatePaymentConfig(r *ghttp.Request) {
|
|
var req dto.CreatePaymentConfigReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// 构建支付配置实体
|
|
config := &service.PaymentConfig{
|
|
TenantID: req.TenantID,
|
|
PayMethod: req.PayMethod,
|
|
ConfigType: req.ConfigType,
|
|
Environment: req.Environment,
|
|
IsEnabled: req.IsEnabled,
|
|
}
|
|
|
|
if req.WechatConfig != nil {
|
|
config.WechatConfig = &service.WechatConfig{
|
|
AppID: req.WechatConfig.AppID,
|
|
MchID: req.WechatConfig.MchID,
|
|
APIKey: req.WechatConfig.APIKey,
|
|
APIClientCert: req.WechatConfig.APIClientCert,
|
|
APIClientKey: req.WechatConfig.APIClientKey,
|
|
NotifyURL: req.WechatConfig.NotifyURL,
|
|
RefundNotifyURL: req.WechatConfig.RefundNotifyURL,
|
|
SubAppID: req.WechatConfig.SubAppID,
|
|
SubMchID: req.WechatConfig.SubMchID,
|
|
}
|
|
}
|
|
|
|
if req.AlipayConfig != nil {
|
|
config.AlipayConfig = &service.AlipayConfig{
|
|
AppID: req.AlipayConfig.AppID,
|
|
PrivateKey: req.AlipayConfig.PrivateKey,
|
|
PublicKey: req.AlipayConfig.PublicKey,
|
|
AlipayPublicKey: req.AlipayConfig.AlipayPublicKey,
|
|
NotifyURL: req.AlipayConfig.NotifyURL,
|
|
RefundNotifyURL: req.AlipayConfig.RefundNotifyURL,
|
|
Format: req.AlipayConfig.Format,
|
|
Charset: req.AlipayConfig.Charset,
|
|
SignType: req.AlipayConfig.SignType,
|
|
IsSandbox: req.AlipayConfig.IsSandbox,
|
|
}
|
|
}
|
|
|
|
if err := service.PaymentService.CreatePaymentConfig(ctx, config); err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 200,
|
|
"message": "success",
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
// UpdatePaymentConfig 更新支付配置
|
|
func (c *PaymentConfigController) UpdatePaymentConfig(r *ghttp.Request) {
|
|
var req dto.UpdatePaymentConfigReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
update := bson.M{}
|
|
if req.ConfigType != "" {
|
|
update["config_type"] = req.ConfigType
|
|
}
|
|
if req.Environment != "" {
|
|
update["environment"] = req.Environment
|
|
}
|
|
if req.IsEnabled != nil {
|
|
update["is_enabled"] = *req.IsEnabled
|
|
}
|
|
|
|
if req.WechatConfig != nil {
|
|
wechatConfig := bson.M{}
|
|
if req.WechatConfig.AppID != "" {
|
|
wechatConfig["app_id"] = req.WechatConfig.AppID
|
|
}
|
|
if req.WechatConfig.MchID != "" {
|
|
wechatConfig["mch_id"] = req.WechatConfig.MchID
|
|
}
|
|
if req.WechatConfig.APIKey != "" {
|
|
wechatConfig["api_key"] = req.WechatConfig.APIKey
|
|
}
|
|
if req.WechatConfig.APIClientCert != "" {
|
|
wechatConfig["api_client_cert"] = req.WechatConfig.APIClientCert
|
|
}
|
|
if req.WechatConfig.APIClientKey != "" {
|
|
wechatConfig["api_client_key"] = req.WechatConfig.APIClientKey
|
|
}
|
|
if req.WechatConfig.NotifyURL != "" {
|
|
wechatConfig["notify_url"] = req.WechatConfig.NotifyURL
|
|
}
|
|
if req.WechatConfig.RefundNotifyURL != "" {
|
|
wechatConfig["refund_notify_url"] = req.WechatConfig.RefundNotifyURL
|
|
}
|
|
if req.WechatConfig.SubAppID != "" {
|
|
wechatConfig["sub_app_id"] = req.WechatConfig.SubAppID
|
|
}
|
|
if req.WechatConfig.SubMchID != "" {
|
|
wechatConfig["sub_mch_id"] = req.WechatConfig.SubMchID
|
|
}
|
|
update["wechat_config"] = wechatConfig
|
|
}
|
|
|
|
if req.AlipayConfig != nil {
|
|
alipayConfig := bson.M{}
|
|
if req.AlipayConfig.AppID != "" {
|
|
alipayConfig["app_id"] = req.AlipayConfig.AppID
|
|
}
|
|
if req.AlipayConfig.PrivateKey != "" {
|
|
alipayConfig["private_key"] = req.AlipayConfig.PrivateKey
|
|
}
|
|
if req.AlipayConfig.PublicKey != "" {
|
|
alipayConfig["public_key"] = req.AlipayConfig.PublicKey
|
|
}
|
|
if req.AlipayConfig.AlipayPublicKey != "" {
|
|
alipayConfig["alipay_public_key"] = req.AlipayConfig.AlipayPublicKey
|
|
}
|
|
if req.AlipayConfig.NotifyURL != "" {
|
|
alipayConfig["notify_url"] = req.AlipayConfig.NotifyURL
|
|
}
|
|
if req.AlipayConfig.RefundNotifyURL != "" {
|
|
alipayConfig["refund_notify_url"] = req.AlipayConfig.RefundNotifyURL
|
|
}
|
|
if req.AlipayConfig.Format != "" {
|
|
alipayConfig["format"] = req.AlipayConfig.Format
|
|
}
|
|
if req.AlipayConfig.Charset != "" {
|
|
alipayConfig["charset"] = req.AlipayConfig.Charset
|
|
}
|
|
if req.AlipayConfig.SignType != "" {
|
|
alipayConfig["sign_type"] = req.AlipayConfig.SignType
|
|
}
|
|
if req.AlipayConfig.IsSandbox != false {
|
|
alipayConfig["is_sandbox"] = req.AlipayConfig.IsSandbox
|
|
}
|
|
update["alipay_config"] = alipayConfig
|
|
}
|
|
|
|
if err := service.PaymentService.UpdatePaymentConfig(ctx, req.ID, update); err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 200,
|
|
"message": "success",
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
// QueryPaymentConfig 查询支付配置
|
|
func (c *PaymentConfigController) QueryPaymentConfig(r *ghttp.Request) {
|
|
tentID := r.Get("tenant_id").String()
|
|
payMethod := r.Get("pay_method").String()
|
|
configType := r.Get("config_type").String()
|
|
environment := r.Get("environment").String()
|
|
|
|
ctx := context.Background()
|
|
|
|
config, err := service.PaymentService.QueryPaymentConfig(ctx, tentID, payMethod, configType, environment)
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 200,
|
|
"message": "success",
|
|
"data": config,
|
|
})
|
|
}
|
|
|
|
// ListPaymentConfigs 查询支付配置列表
|
|
func (c *PaymentConfigController) ListPaymentConfigs(r *ghttp.Request) {
|
|
var req dto.QueryPaymentConfigReq
|
|
if err := r.Parse(&req); err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
filter := bson.M{"tenant_id": req.TenantID}
|
|
if req.PayMethod != "" {
|
|
filter["pay_method"] = req.PayMethod
|
|
}
|
|
if req.ConfigType != "" {
|
|
filter["config_type"] = req.ConfigType
|
|
}
|
|
if req.Environment != "" {
|
|
filter["environment"] = req.Environment
|
|
}
|
|
if req.IsEnabled != nil {
|
|
filter["is_enabled"] = *req.IsEnabled
|
|
}
|
|
|
|
if req.Page <= 0 {
|
|
req.Page = 1
|
|
}
|
|
if req.PageSize <= 0 {
|
|
req.PageSize = 10
|
|
}
|
|
|
|
configs, total, err := service.PaymentService.ListPaymentConfigs(ctx, filter, req.Page, req.PageSize)
|
|
if err != nil {
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
resp := dto.PaymentConfigListResp{
|
|
List: make([]dto.PaymentConfigResp, 0, len(configs)),
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
TotalPage: int((total + int64(req.PageSize) - 1) / int64(req.PageSize)),
|
|
}
|
|
|
|
for _, config := range configs {
|
|
configResp := dto.PaymentConfigResp{
|
|
ID: config.ID.Hex(),
|
|
TenantID: config.TenantID,
|
|
PayMethod: config.PayMethod,
|
|
ConfigType: config.ConfigType,
|
|
Environment: config.Environment,
|
|
IsEnabled: config.IsEnabled,
|
|
CreatedAt: config.CreatedAt,
|
|
UpdatedAt: config.UpdatedAt,
|
|
CreatedBy: config.CreatedBy,
|
|
UpdatedBy: config.UpdatedBy,
|
|
}
|
|
|
|
if config.WechatConfig != nil {
|
|
configResp.WechatConfig = &dto.WechatConfigResp{
|
|
AppID: config.WechatConfig.AppID,
|
|
MchID: config.WechatConfig.MchID,
|
|
NotifyURL: config.WechatConfig.NotifyURL,
|
|
RefundNotifyURL: config.WechatConfig.RefundNotifyURL,
|
|
SubAppID: config.WechatConfig.SubAppID,
|
|
SubMchID: config.WechatConfig.SubMchID,
|
|
}
|
|
}
|
|
|
|
if config.AlipayConfig != nil {
|
|
configResp.AlipayConfig = &dto.AlipayConfigResp{
|
|
AppID: config.AlipayConfig.AppID,
|
|
NotifyURL: config.AlipayConfig.NotifyURL,
|
|
RefundNotifyURL: config.AlipayConfig.RefundNotifyURL,
|
|
Format: config.AlipayConfig.Format,
|
|
Charset: config.AlipayConfig.Charset,
|
|
SignType: config.AlipayConfig.SignType,
|
|
IsSandbox: config.AlipayConfig.IsSandbox,
|
|
}
|
|
}
|
|
|
|
resp.List = append(resp.List, configResp)
|
|
}
|
|
|
|
r.Response.WriteJsonExit(g.Map{
|
|
"code": 200,
|
|
"message": "success",
|
|
"data": resp,
|
|
})
|
|
}
|