|
@@ -2,13 +2,14 @@ package service
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
|
|
- "strconv"
|
|
|
+ "gorm.io/gorm"
|
|
|
"time"
|
|
|
"youngee_b_api/app/dao"
|
|
|
"youngee_b_api/app/entity"
|
|
@@ -22,13 +23,15 @@ type RechargeService struct{}
|
|
|
func (s RechargeService) TransferToPublic(param *vo.RechargeTransferParam) (*string, error) {
|
|
|
var rechargeId string
|
|
|
var phone string
|
|
|
- if param.SubAccountId == 0 {
|
|
|
- rechargeId = util.MakeRechargeId(param.EnterpriseId)
|
|
|
- phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
|
|
|
- } else {
|
|
|
- rechargeId = util.MakeRechargeId(strconv.FormatInt(param.SubAccountId, 10))
|
|
|
- phone, _ = dao.SubAccountDao{}.GetSubAccountPhone(param.SubAccountId)
|
|
|
- }
|
|
|
+ //if param.SubAccountId == 0 {
|
|
|
+ // rechargeId = util.MakeRechargeId(param.EnterpriseId)
|
|
|
+ // phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
|
|
|
+ //} else {
|
|
|
+ // rechargeId = util.MakeRechargeId(strconv.FormatInt(param.SubAccountId, 10))
|
|
|
+ // phone, _ = dao.SubAccountDao{}.GetSubAccountPhone(param.SubAccountId)
|
|
|
+ //}
|
|
|
+ rechargeId = util.MakeRechargeId(param.EnterpriseId)
|
|
|
+ phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
|
|
|
rechargeRecord := entity.RechargeRecord{
|
|
|
RechargeID: rechargeId,
|
|
|
EnterpriseID: param.EnterpriseId,
|
|
@@ -47,7 +50,8 @@ func (s RechargeService) TransferToPublic(param *vo.RechargeTransferParam) (*str
|
|
|
return &rechargeId, nil
|
|
|
}
|
|
|
|
|
|
-func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (codeUrl string, err error) {
|
|
|
+// 获取微信支付CodeUrl
|
|
|
+func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (string, *time.Time, error) {
|
|
|
var (
|
|
|
mchID string = "1615933939" // 商户号
|
|
|
mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
|
|
@@ -74,13 +78,14 @@ func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (c
|
|
|
// 采用 Native 支付方式
|
|
|
svc := native.NativeApiService{Client: client}
|
|
|
// 发送请求
|
|
|
+ timeExpire := time.Now().Add(10 * time.Minute)
|
|
|
resp, result, err := svc.Prepay(ctx,
|
|
|
native.PrepayRequest{
|
|
|
Appid: core.String("wxac396a3be7a16844"),
|
|
|
Mchid: core.String("1615933939"),
|
|
|
Description: core.String("样叽微信支付充值"),
|
|
|
OutTradeNo: core.String(tradeId),
|
|
|
- TimeExpire: core.Time(time.Now().Add(10 * time.Minute)),
|
|
|
+ TimeExpire: core.Time(timeExpire),
|
|
|
Attach: core.String("微信支付充值"),
|
|
|
NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
|
|
|
SupportFapiao: core.Bool(true),
|
|
@@ -94,15 +99,17 @@ func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (c
|
|
|
if err != nil {
|
|
|
// 处理错误
|
|
|
log.Printf("call Prepay err:%s", err)
|
|
|
- return "", err
|
|
|
+ return "", nil, err
|
|
|
} else {
|
|
|
// 处理返回结果
|
|
|
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
|
|
|
log.Println("codeUrl:", *resp.CodeUrl)
|
|
|
}
|
|
|
- return *resp.CodeUrl, nil
|
|
|
+ return *resp.CodeUrl, &timeExpire, nil
|
|
|
}
|
|
|
|
|
|
+// 根据交易id查询微信是否扫码付款
|
|
|
+// https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_2.shtml
|
|
|
func (s RechargeService) QueryOrderByTradeId(tradeId string) (tradeState string, err error) {
|
|
|
var (
|
|
|
mchID string = "1615933939" // 商户号
|
|
@@ -145,3 +152,210 @@ func (s RechargeService) QueryOrderByTradeId(tradeId string) (tradeState string,
|
|
|
fmt.Printf("支付状态 %+v\n", *resp.TradeState)
|
|
|
return *resp.TradeState, nil
|
|
|
}
|
|
|
+
|
|
|
+// 余额管理——总金额、可用余额、冻结金额
|
|
|
+func (s RechargeService) ShowBalance(param *vo.BalanceParam) (*vo.ReBalanceShow, error) {
|
|
|
+ reBalanceShow := new(vo.ReBalanceShow)
|
|
|
+ enterprise, err := dao.EnterpriseDao{}.GetEnterpriseInfo(param.EnterpriseId)
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return reBalanceShow, nil
|
|
|
+ } else {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reBalanceShow.TotalBalance = enterprise.Balance
|
|
|
+ reBalanceShow.AvailBalance = enterprise.AvailableBalance
|
|
|
+ reBalanceShow.FrozenBalance = enterprise.FrozenBalance
|
|
|
+ return reBalanceShow, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 余额管理——冻结记录
|
|
|
+func (s RechargeService) FrozenInfoList(param *vo.BalanceParam) (vo.ResultVO, error) {
|
|
|
+ if param.Page <= 0 {
|
|
|
+ param.Page = 1
|
|
|
+ }
|
|
|
+ if param.PageSize <= 0 {
|
|
|
+ param.PageSize = 10
|
|
|
+ }
|
|
|
+ var result vo.ResultVO
|
|
|
+ var reBalanceShows []*vo.ReFrozenInfo
|
|
|
+ var selectionInfos []*entity.SelectionInfo
|
|
|
+ var projects []*entity.Project
|
|
|
+ if param.FrozenState == 1 {
|
|
|
+ // 电商带货
|
|
|
+ selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenList(param.EnterpriseId)
|
|
|
+ // 品牌种草
|
|
|
+ projects, _ = dao.ProjectDAO{}.GetProjectFrozenList(param.EnterpriseId)
|
|
|
+ // 本地生活
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 电商带货
|
|
|
+ selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenCancelList(param.EnterpriseId)
|
|
|
+ // 品牌种草
|
|
|
+ projects, _ = dao.ProjectDAO{}.GetProjectFrozenCancelList(param.EnterpriseId)
|
|
|
+ // 本地生活
|
|
|
+
|
|
|
+ }
|
|
|
+ // 汇总结果
|
|
|
+ for _, selection := range selectionInfos {
|
|
|
+ // 获取商品详情字段
|
|
|
+ var creatorName string
|
|
|
+ var productName string
|
|
|
+ var productPrice float64
|
|
|
+ var mainImage string
|
|
|
+ if selection.SubAccountId == 0 {
|
|
|
+ enterprise, err := dao.EnterpriseDao{}.GetEnterprise(selection.EnterpriseID)
|
|
|
+ if err == nil && enterprise != nil {
|
|
|
+ creatorName = enterprise.BusinessName
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ subAccount, err := dao.SubAccountDao{}.GetSubAccount(selection.SubAccountId)
|
|
|
+ if err == nil && subAccount != nil {
|
|
|
+ creatorName = subAccount.SubAccountName
|
|
|
+ }
|
|
|
+ }
|
|
|
+ product, err := dao.ProductDAO{}.GetProductByID(selection.ProductID)
|
|
|
+ if err == nil && product != nil {
|
|
|
+ productName = product.ProductName
|
|
|
+ productPrice = product.ProductPrice
|
|
|
+ }
|
|
|
+ mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(selection.ProductID)
|
|
|
+ // 电商带货汇总
|
|
|
+ reBalanceShow := &vo.ReFrozenInfo{
|
|
|
+ ProductId: selection.ProductID,
|
|
|
+ MainImage: mainImage,
|
|
|
+ ProductName: productName,
|
|
|
+ ProductPrice: productPrice,
|
|
|
+ Platform: selection.Platform,
|
|
|
+ CreatorName: creatorName,
|
|
|
+ TaskType: "电商带货",
|
|
|
+ FrozenBalance: selection.EstimatedCost,
|
|
|
+ FrozenTime: selection.PayAt.Format("2006-01-02 15:04:05"),
|
|
|
+ EnterpriseId: selection.EnterpriseID,
|
|
|
+ SubAccountId: selection.SubAccountId,
|
|
|
+ TaskId: selection.SelectionID,
|
|
|
+ }
|
|
|
+ if param.FrozenState == 2 {
|
|
|
+ reBalanceShow.FrozenCancelBalance = selection.SettlementAmount
|
|
|
+ }
|
|
|
+ reBalanceShows = append(reBalanceShows, reBalanceShow)
|
|
|
+ }
|
|
|
+ for _, project := range projects {
|
|
|
+ // 获取商品详情字段
|
|
|
+ var creatorName string
|
|
|
+ var productName string
|
|
|
+ var productPrice float64
|
|
|
+ var mainImage string
|
|
|
+ if project.SubAccountId == 0 {
|
|
|
+ enterprise, err := dao.EnterpriseDao{}.GetEnterprise(project.EnterpriseID)
|
|
|
+ if err == nil && enterprise != nil {
|
|
|
+ creatorName = enterprise.BusinessName
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ subAccount, err := dao.SubAccountDao{}.GetSubAccount(project.SubAccountId)
|
|
|
+ if err == nil && subAccount != nil {
|
|
|
+ creatorName = subAccount.SubAccountName
|
|
|
+ }
|
|
|
+ }
|
|
|
+ product, err := dao.ProductDAO{}.GetProductByID(project.ProductID)
|
|
|
+ if err == nil && product != nil {
|
|
|
+ productName = product.ProductName
|
|
|
+ productPrice = product.ProductPrice
|
|
|
+ }
|
|
|
+ mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(project.ProductID)
|
|
|
+ // 电商带货汇总
|
|
|
+ reBalanceShow := &vo.ReFrozenInfo{
|
|
|
+ ProductId: project.ProductID,
|
|
|
+ MainImage: mainImage,
|
|
|
+ ProductName: productName,
|
|
|
+ ProductPrice: productPrice,
|
|
|
+ Platform: project.ProjectPlatform,
|
|
|
+ CreatorName: creatorName,
|
|
|
+ TaskType: "品牌种草",
|
|
|
+ FrozenBalance: project.PaymentAmount,
|
|
|
+ FrozenTime: project.PayAt.Format("2006-01-02 15:04:05"),
|
|
|
+ EnterpriseId: project.EnterpriseID,
|
|
|
+ SubAccountId: project.SubAccountId,
|
|
|
+ TaskId: project.ProjectId,
|
|
|
+ }
|
|
|
+ if param.FrozenState == 2 {
|
|
|
+ reBalanceShow.FrozenCancelBalance = project.SettlementAmount
|
|
|
+ }
|
|
|
+ reBalanceShows = append(reBalanceShows, reBalanceShow)
|
|
|
+ }
|
|
|
+
|
|
|
+ startIndex := (param.Page - 1) * param.PageSize
|
|
|
+ endIndex := startIndex + param.PageSize
|
|
|
+
|
|
|
+ // 分页
|
|
|
+ if startIndex >= len(reBalanceShows) {
|
|
|
+ return result, nil
|
|
|
+ }
|
|
|
+ if endIndex > len(reBalanceShows) {
|
|
|
+ endIndex = len(reBalanceShows)
|
|
|
+ }
|
|
|
+ result = vo.ResultVO{
|
|
|
+ Page: param.Page,
|
|
|
+ PageSize: param.PageSize,
|
|
|
+ Total: int64(len(reBalanceShows)),
|
|
|
+ Data: reBalanceShows[startIndex:endIndex],
|
|
|
+ }
|
|
|
+ return result, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 充值管理——累计充值金额、确认中金额
|
|
|
+func (s RechargeService) ShowRecharge(param *vo.RechargeParam) (*vo.ReRechargeShow, error) {
|
|
|
+ reRechargeShow := new(vo.ReRechargeShow)
|
|
|
+ confirmingRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 1)
|
|
|
+ totalRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 2)
|
|
|
+ reRechargeShow.ConfirmingRecharge = confirmingRecharge
|
|
|
+ reRechargeShow.TotalRecharge = totalRecharge
|
|
|
+ return reRechargeShow, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 充值管理——充值记录
|
|
|
+func (s RechargeService) RechargeInfoList(param *vo.RechargeParam) (vo.ResultVO, error) {
|
|
|
+ if param.Page <= 0 {
|
|
|
+ param.Page = 1
|
|
|
+ }
|
|
|
+ if param.PageSize <= 0 {
|
|
|
+ param.PageSize = 10
|
|
|
+ }
|
|
|
+ var result vo.ResultVO
|
|
|
+ var reRechargeInfos []*vo.ReRechargeInfo
|
|
|
+ rechargeRecords, total, err := dao.RechargeRecordDao{}.RechargeInfoList(param)
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+ for _, rechargeRecord := range rechargeRecords {
|
|
|
+ var creatorName string
|
|
|
+ enterprise, err := dao.EnterpriseDao{}.GetEnterprise(param.EnterpriseId)
|
|
|
+ if err == nil && enterprise != nil {
|
|
|
+ creatorName = enterprise.BusinessName
|
|
|
+ }
|
|
|
+ reRechargeInfo := &vo.ReRechargeInfo{
|
|
|
+ RechargeId: rechargeRecord.RechargeID,
|
|
|
+ CreatorName: creatorName,
|
|
|
+ RechargeAmount: rechargeRecord.RechargeAmount,
|
|
|
+ RechargeMethod: rechargeRecord.RechargeMethod,
|
|
|
+ TransferVoucherUrl: rechargeRecord.TransferVoucherUrl,
|
|
|
+ }
|
|
|
+ if param.RechargeState == 1 {
|
|
|
+ reRechargeInfo.CommitAt = rechargeRecord.CommitAt.Format("2006-01-02 15:04:05")
|
|
|
+ } else if param.RechargeState == 2 {
|
|
|
+ reRechargeInfo.ConfirmAt = rechargeRecord.ConfirmAt.Format("2006-01-02 15:04:05")
|
|
|
+ } else if param.RechargeState == 3 {
|
|
|
+ reRechargeInfo.RefuseAt = rechargeRecord.RefuseAt.Format("2006-01-02 15:04:05")
|
|
|
+ reRechargeInfo.FailReason = rechargeRecord.FailReason
|
|
|
+ }
|
|
|
+ reRechargeInfos = append(reRechargeInfos, reRechargeInfo)
|
|
|
+ }
|
|
|
+ result = vo.ResultVO{
|
|
|
+ Page: param.Page,
|
|
|
+ PageSize: param.PageSize,
|
|
|
+ Total: total,
|
|
|
+ Data: reRechargeInfos,
|
|
|
+ }
|
|
|
+ return result, nil
|
|
|
+}
|