Files
order/dao/payment_dao.go

208 lines
5.7 KiB
Go
Raw Normal View History

2025-12-10 09:02:41 +08:00
package dao
import (
"context"
"time"
2025-12-10 13:51:09 +08:00
"order/consts"
2025-12-10 09:02:41 +08:00
"order/model/entity"
2025-12-30 11:03:11 +08:00
2026-02-24 17:17:10 +08:00
"gitea.com/red-future/common/db/mongo"
2025-12-30 11:03:11 +08:00
"go.mongodb.org/mongo-driver/v2/bson"
2025-12-10 09:02:41 +08:00
)
type paymentConfig struct {
}
2025-12-10 09:02:41 +08:00
2025-12-10 13:51:09 +08:00
// PaymentConfig 支付配置数据访问对象
2025-12-30 11:03:11 +08:00
var PaymentConfig = &paymentConfig{}
2025-12-10 09:02:41 +08:00
// Create 创建支付配置
2025-12-10 13:51:09 +08:00
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
2025-12-10 09:02:41 +08:00
return err
}
// GetByTenantAndMethod 根据租户和支付方式获取配置
2025-12-10 13:51:09 +08:00
func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
2025-12-10 09:02:41 +08:00
var config entity.PaymentConfig
2025-12-10 13:51:09 +08:00
filter := bson.M{
2025-12-10 09:02:41 +08:00
"tenant_id": tenantID,
"pay_method": payMethod,
"enabled": true,
2025-12-10 13:51:09 +08:00
}
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
2025-12-10 13:51:09 +08:00
if err != nil {
2025-12-10 09:02:41 +08:00
return nil, nil
}
2025-12-10 13:51:09 +08:00
return &config, nil
2025-12-10 09:02:41 +08:00
}
// Update 更新支付配置
2025-12-10 13:51:09 +08:00
func (d *paymentConfig) Update(ctx context.Context, config *entity.PaymentConfig) error {
2025-12-12 16:20:47 +08:00
filter := bson.M{"_id": config.Id}
2025-12-10 13:51:09 +08:00
update := bson.M{
"$set": bson.M{
2025-12-12 16:20:47 +08:00
"tenant_id": config.TenantId,
2025-12-10 13:51:09 +08:00
"pay_method": config.PayMethod,
"config_name": config.ConfigName,
"description": config.Description,
"enabled": config.Enabled,
"app_id": config.AppID,
"mch_id": config.MchID,
"api_key": config.APIKey,
"notify_url": config.NotifyURL,
"return_url": config.ReturnURL,
"cert_path": config.CertPath,
"key_path": config.KeyPath,
"app_private_key": config.AppPrivateKey,
"alipay_public_key": config.AlipayPublicKey,
"sandbox": config.Sandbox,
"gateway_url": config.GatewayURL,
"support_native": config.SupportNative,
"support_jsapi": config.SupportJSAPI,
"support_app": config.SupportAPP,
"support_h5": config.SupportH5,
},
}
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Update(ctx, filter, update, consts.PaymentConfigCollection)
2025-12-10 09:02:41 +08:00
return err
}
2025-12-10 13:51:09 +08:00
// GetByID 根据ID获取配置
func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.PaymentConfig, error) {
var config entity.PaymentConfig
2025-12-10 09:02:41 +08:00
2025-12-10 13:51:09 +08:00
filter := bson.M{"_id": id}
2025-12-30 11:03:11 +08:00
err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
2025-12-10 09:02:41 +08:00
if err != nil {
return nil, err
}
2025-12-10 13:51:09 +08:00
return &config, nil
}
// GetByTenantID 根据租户ID获取配置列表
func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
2025-12-10 09:02:41 +08:00
var configs []entity.PaymentConfig
2025-12-10 13:51:09 +08:00
filter := bson.M{"tenant_id": tenantID}
2025-12-30 11:03:11 +08:00
err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection)
2025-12-10 13:51:09 +08:00
if err != nil {
2025-12-10 09:02:41 +08:00
return nil, err
}
return configs, nil
}
2025-12-10 13:51:09 +08:00
// Delete 删除支付配置
func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error {
filter := bson.M{"_id": id}
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Delete(ctx, filter, consts.PaymentConfigCollection)
2025-12-10 13:51:09 +08:00
return err
2025-12-10 09:02:41 +08:00
}
2025-12-10 13:51:09 +08:00
func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
var configs []entity.PaymentConfig
2025-12-10 09:02:41 +08:00
2025-12-10 13:51:09 +08:00
filter := bson.M{"tenant_id": tenantID}
2025-12-30 11:03:11 +08:00
err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection)
2025-12-10 13:51:09 +08:00
return configs, err
2025-12-10 09:02:41 +08:00
}
type paymentRecord struct {
}
2025-12-10 09:02:41 +08:00
2025-12-10 13:51:09 +08:00
// PaymentRecord 支付记录数据访问对象
2025-12-30 11:03:11 +08:00
var PaymentRecord = &paymentRecord{}
2025-12-10 13:51:09 +08:00
// Create 创建支付记录
func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error {
2025-12-12 16:20:47 +08:00
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
2025-12-10 09:02:41 +08:00
return err
}
// GetByOrderNo 根据订单号获取支付记录
2025-12-10 13:51:09 +08:00
func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
2025-12-10 09:02:41 +08:00
var record entity.PaymentRecord
2025-12-10 13:51:09 +08:00
filter := bson.M{
2025-12-10 09:02:41 +08:00
"tenant_id": tenantID,
"order_no": orderNo,
2025-12-10 13:51:09 +08:00
}
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
err := mongo.DB().FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
2025-12-10 13:51:09 +08:00
if err != nil {
2025-12-10 09:02:41 +08:00
return nil, nil
}
2025-12-10 13:51:09 +08:00
return &record, nil
2025-12-10 09:02:41 +08:00
}
// UpdateStatus 更新支付记录状态
2025-12-10 13:51:09 +08:00
func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, transactionID, tradeNo string) error {
filter := bson.M{"_id": id}
2025-12-10 09:02:41 +08:00
update := bson.M{
"status": status,
"transaction_id": transactionID,
"trade_no": tradeNo,
2025-12-10 13:51:09 +08:00
"updated_at": time.Now(),
2025-12-10 09:02:41 +08:00
}
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
2025-12-10 09:02:41 +08:00
return err
}
type refundRecord struct {
}
2025-12-10 09:02:41 +08:00
2025-12-10 13:51:09 +08:00
// RefundRecord 退款记录数据访问对象
2025-12-30 11:03:11 +08:00
var RefundRecord = &refundRecord{}
2025-12-10 09:02:41 +08:00
// Create 创建退款记录
2025-12-10 13:51:09 +08:00
func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error {
2025-12-12 16:20:47 +08:00
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
2025-12-10 09:02:41 +08:00
return err
}
// GetByRefundNo 根据退款单号获取退款记录
2025-12-10 13:51:09 +08:00
func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
2025-12-10 09:02:41 +08:00
var record entity.RefundRecord
2025-12-10 13:51:09 +08:00
filter := bson.M{
2025-12-10 09:02:41 +08:00
"tenant_id": tenantID,
"refund_no": refundNo,
2025-12-10 13:51:09 +08:00
}
2025-12-10 09:02:41 +08:00
2025-12-30 11:03:11 +08:00
err := mongo.DB().FindOne(ctx, filter, &record, consts.RefundRecordCollection)
2025-12-10 13:51:09 +08:00
if err != nil {
2025-12-10 09:02:41 +08:00
return nil, nil
}
2025-12-10 13:51:09 +08:00
return &record, nil
2025-12-10 09:02:41 +08:00
}
// UpdateRefundStatus 更新退款记录状态
2025-12-10 13:51:09 +08:00
func (d *refundRecord) UpdateRefundStatus(ctx context.Context, id, status, refundID string) error {
filter := bson.M{"_id": id}
2025-12-10 09:02:41 +08:00
update := bson.M{
"status": status,
"refund_id": refundID,
2025-12-10 13:51:09 +08:00
"updated_at": time.Now(),
2025-12-10 09:02:41 +08:00
}
2025-12-30 11:03:11 +08:00
_, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
2025-12-10 09:02:41 +08:00
return err
}