recharge_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/wechatpay-apiv3/wechatpay-go/core"
  8. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  9. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
  10. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  11. "gorm.io/gorm"
  12. "time"
  13. "youngee_m_api/app/dao"
  14. "youngee_m_api/app/entity"
  15. "youngee_m_api/app/util"
  16. "youngee_m_api/app/vo"
  17. )
  18. type RechargeService struct{}
  19. // 充值管理——对公转账
  20. func (s RechargeService) TransferToPublic(param *vo.RechargeTransferParam) (*string, error) {
  21. var rechargeId string
  22. var phone string
  23. //if param.SubAccountId == 0 {
  24. // rechargeId = util.MakeRechargeId(param.EnterpriseId)
  25. // phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
  26. //} else {
  27. // rechargeId = util.MakeRechargeId(strconv.FormatInt(param.SubAccountId, 10))
  28. // phone, _ = dao.SubAccountDao{}.GetSubAccountPhone(param.SubAccountId)
  29. //}
  30. rechargeId = util.GenerateDateRelatedUUID(16)
  31. phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
  32. rechargeRecord := entity.RechargeRecord{
  33. RechargeID: rechargeId,
  34. EnterpriseID: param.EnterpriseId,
  35. SubAccountId: param.SubAccountId,
  36. RechargeAmount: param.Amount,
  37. TransferVoucherUrl: param.TransferVoucherUrl,
  38. Phone: phone,
  39. RechargeMethod: 1,
  40. Status: 1,
  41. CommitAt: time.Now(),
  42. }
  43. err := dao.RechargeRecordDao{}.Insert(&rechargeRecord)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &rechargeId, nil
  48. }
  49. // 获取微信支付CodeUrl
  50. func (s RechargeService) NativeApiServicePrepay(enterpriseId string, subAccountId int64, tradeId string, amount int64) (string, *time.Time, error) {
  51. var (
  52. mchID string = "1615933939" // 商户号
  53. mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
  54. mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥,用于加密和解密平台证书
  55. )
  56. // 使用 utils 提供的函数从本地文件中加载商户私钥
  57. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem") // 商户私钥,用于生成请求的签名
  58. if err != nil {
  59. log.Print("load merchant private key error")
  60. }
  61. ctx := context.Background()
  62. // 使用商户私钥等初始化 微信支付client,并使它具有自动定时获取微信支付平台证书的能力
  63. opts := []core.ClientOption{
  64. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  65. }
  66. client, err := core.NewClient(ctx, opts...)
  67. if err != nil {
  68. log.Printf("new wechat pay client err: %s", err)
  69. }
  70. // 采用 Native 支付方式
  71. svc := native.NativeApiService{Client: client}
  72. // 发送请求
  73. timeExpire := time.Now().Add(5 * time.Minute)
  74. resp, result, err := svc.Prepay(ctx,
  75. native.PrepayRequest{
  76. Appid: core.String("wxac396a3be7a16844"),
  77. Mchid: core.String("1615933939"),
  78. Description: core.String("样叽微信支付充值"),
  79. OutTradeNo: core.String(tradeId),
  80. TimeExpire: core.Time(timeExpire),
  81. Attach: core.String("微信支付充值"),
  82. NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
  83. SupportFapiao: core.Bool(true),
  84. Amount: &native.Amount{
  85. Currency: core.String("CNY"),
  86. Total: core.Int64(amount),
  87. },
  88. },
  89. )
  90. if err != nil {
  91. // 处理错误
  92. log.Printf("call Prepay err:%s", err)
  93. return "", nil, err
  94. } else {
  95. // 处理返回结果
  96. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  97. log.Println("codeUrl:", *resp.CodeUrl)
  98. }
  99. // 生成充值记录
  100. phone, _ := dao.EnterpriseDao{}.GetEnterprisePhone(enterpriseId)
  101. rechargeRecord := entity.RechargeRecord{
  102. RechargeID: tradeId,
  103. EnterpriseID: enterpriseId,
  104. SubAccountId: subAccountId,
  105. RechargeAmount: float64(amount) / 100,
  106. Phone: phone,
  107. RechargeMethod: 2,
  108. Status: 1,
  109. CommitAt: time.Now(),
  110. RefuseAt: timeExpire,
  111. }
  112. err = dao.RechargeRecordDao{}.Insert(&rechargeRecord)
  113. if err != nil {
  114. return "", nil, err
  115. }
  116. return *resp.CodeUrl, &timeExpire, nil
  117. }
  118. // 根据交易id查询微信是否扫码付款
  119. // https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_2.shtml
  120. func (s RechargeService) QueryOrderByTradeId(enterpriseId string, subAccountId int64, tradeId string) (tradeState string, err error) {
  121. var (
  122. mchID string = "1615933939" // 商户号
  123. mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
  124. mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥
  125. )
  126. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  127. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem")
  128. if err != nil {
  129. log.Print("load merchant private key error")
  130. }
  131. ctx := context.Background()
  132. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  133. opts := []core.ClientOption{
  134. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  135. }
  136. client, err := core.NewClient(ctx, opts...)
  137. if err != nil {
  138. log.Printf("new wechat pay client err: %s", err)
  139. }
  140. svc := native.NativeApiService{Client: client}
  141. resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
  142. native.QueryOrderByOutTradeNoRequest{
  143. OutTradeNo: core.String(tradeId),
  144. Mchid: core.String("1615933939"),
  145. },
  146. )
  147. fmt.Printf("支付 %+v\n", resp)
  148. if err != nil {
  149. // 处理错误
  150. log.Printf("call QueryOrderByOutTradeNo err: %s", err)
  151. return "", err
  152. } else {
  153. // 处理返回结果
  154. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  155. }
  156. // 更新充值记录/账户金额相关信息
  157. if "SUCCESS" == *resp.TradeState {
  158. payTime := resp.SuccessTime
  159. t, _ := time.Parse("2006-01-02 15:04:05", *payTime)
  160. err = dao.RechargeRecordDao{}.UpdateRechargeStatus(tradeId, 2, t)
  161. amount := float64(*resp.Amount.Total) / 100
  162. _, err = dao.EnterpriseDao{}.UpdateEnterpriseBalance(enterpriseId, amount)
  163. } else if "CLOSED" == *resp.TradeState {
  164. err = dao.RechargeRecordDao{}.UpdateRechargeStatus(tradeId, 3, time.Now())
  165. }
  166. return *resp.TradeState, nil
  167. }
  168. // 余额管理——总金额、可用余额、冻结金额
  169. func (s RechargeService) ShowBalance(param *vo.BalanceParam) (*vo.ReBalanceShow, error) {
  170. reBalanceShow := new(vo.ReBalanceShow)
  171. var enterprises []entity.Enterprise
  172. err := dao.Db.Model(&entity.Enterprise{}).Find(&enterprises).Error
  173. if err != nil {
  174. if errors.Is(err, gorm.ErrRecordNotFound) {
  175. return nil, nil
  176. } else {
  177. return nil, err
  178. }
  179. }
  180. var balance float64
  181. var availBalance float64
  182. var frozenBalance float64
  183. for _, enterprise := range enterprises {
  184. balance += enterprise.Balance
  185. availBalance += enterprise.AvailableBalance
  186. frozenBalance += enterprise.FrozenBalance
  187. }
  188. reBalanceShow.TotalBalance = balance
  189. reBalanceShow.AvailBalance = availBalance
  190. reBalanceShow.FrozenBalance = frozenBalance
  191. return reBalanceShow, nil
  192. }
  193. // 余额管理——冻结记录
  194. func (s RechargeService) FrozenInfoList(param *vo.BalanceParam) (vo.ResultVO, error) {
  195. if param.Page <= 0 {
  196. param.Page = 1
  197. }
  198. if param.PageSize <= 0 {
  199. param.PageSize = 10
  200. }
  201. var result vo.ResultVO
  202. var reBalanceShows []*vo.ReFrozenInfo
  203. var selectionInfos []*entity.SelectionInfo
  204. var projects []*entity.Project
  205. if param.FrozenState == 1 {
  206. // 电商带货
  207. selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenListAll()
  208. // 品牌种草
  209. projects, _ = dao.ProjectDAO{}.GetProjectFrozenListAll()
  210. // 本地生活
  211. } else {
  212. // 电商带货
  213. selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenCancelListAll()
  214. // 品牌种草
  215. projects, _ = dao.ProjectDAO{}.GetProjectFrozenCancelListAll()
  216. // 本地生活
  217. }
  218. // 汇总结果
  219. for _, selection := range selectionInfos {
  220. // 获取商品详情字段
  221. var creatorName string
  222. var productName string
  223. var productPrice float64
  224. var mainImage string
  225. if selection.SubAccountId == 0 {
  226. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(selection.EnterpriseID)
  227. if err == nil && enterprise != nil {
  228. creatorName = enterprise.BusinessName
  229. }
  230. } else {
  231. subAccount, err := dao.SubAccountDao{}.GetSubAccount(selection.SubAccountId)
  232. if err == nil && subAccount != nil {
  233. creatorName = subAccount.SubAccountName
  234. }
  235. }
  236. product, err := dao.ProductDAO{}.GetProductByID(selection.ProductID)
  237. if err == nil && product != nil {
  238. productName = product.ProductName
  239. productPrice = product.ProductPrice
  240. }
  241. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(selection.ProductID)
  242. // 电商带货汇总
  243. reBalanceShow := &vo.ReFrozenInfo{
  244. ProductId: selection.ProductID,
  245. MainImage: mainImage,
  246. ProductName: productName,
  247. ProductPrice: productPrice,
  248. Platform: selection.Platform,
  249. CreatorName: creatorName,
  250. TaskType: "电商带货",
  251. FrozenBalance: selection.EstimatedCost,
  252. FrozenTime: selection.PayAt.Format("2006-01-02 15:04:05"),
  253. EnterpriseId: selection.EnterpriseID,
  254. SubAccountId: selection.SubAccountId,
  255. TaskId: selection.SelectionID,
  256. }
  257. if param.FrozenState == 2 {
  258. reBalanceShow.FrozenCancelBalance = selection.SettlementAmount
  259. }
  260. reBalanceShows = append(reBalanceShows, reBalanceShow)
  261. }
  262. for _, project := range projects {
  263. // 获取商品详情字段
  264. var creatorName string
  265. var productName string
  266. var productPrice float64
  267. var mainImage string
  268. if project.SubAccountId == 0 {
  269. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(project.EnterpriseID)
  270. if err == nil && enterprise != nil {
  271. creatorName = enterprise.BusinessName
  272. }
  273. } else {
  274. subAccount, err := dao.SubAccountDao{}.GetSubAccount(project.SubAccountId)
  275. if err == nil && subAccount != nil {
  276. creatorName = subAccount.SubAccountName
  277. }
  278. }
  279. product, err := dao.ProductDAO{}.GetProductByID(project.ProductID)
  280. if err == nil && product != nil {
  281. productName = product.ProductName
  282. productPrice = product.ProductPrice
  283. }
  284. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(project.ProductID)
  285. // 电商带货汇总
  286. reBalanceShow := &vo.ReFrozenInfo{
  287. ProductId: project.ProductID,
  288. MainImage: mainImage,
  289. ProductName: productName,
  290. ProductPrice: productPrice,
  291. Platform: project.ProjectPlatform,
  292. CreatorName: creatorName,
  293. TaskType: "品牌种草",
  294. FrozenBalance: project.NeedPay,
  295. FrozenTime: project.PayAt.Format("2006-01-02 15:04:05"),
  296. EnterpriseId: project.EnterpriseID,
  297. SubAccountId: project.SubAccountId,
  298. TaskId: project.ProjectId,
  299. }
  300. if param.FrozenState == 2 {
  301. reBalanceShow.FrozenCancelBalance = project.SettlementAmount
  302. }
  303. reBalanceShows = append(reBalanceShows, reBalanceShow)
  304. }
  305. startIndex := (param.Page - 1) * param.PageSize
  306. endIndex := startIndex + param.PageSize
  307. // 分页
  308. if startIndex >= len(reBalanceShows) {
  309. result = vo.ResultVO{
  310. Page: param.Page,
  311. PageSize: param.PageSize,
  312. Total: int64(len(reBalanceShows)),
  313. Data: nil,
  314. }
  315. return result, nil
  316. }
  317. if endIndex > len(reBalanceShows) {
  318. endIndex = len(reBalanceShows)
  319. }
  320. result = vo.ResultVO{
  321. Page: param.Page,
  322. PageSize: param.PageSize,
  323. Total: int64(len(reBalanceShows)),
  324. Data: reBalanceShows[startIndex:endIndex],
  325. }
  326. return result, nil
  327. }
  328. // 余额管理——冻结记录-角标
  329. func (t RechargeService) FrozenInfoCount(param *vo.BalanceParam) map[string]int64 {
  330. res := make(map[string]int64)
  331. var blockNum1 int64
  332. var blockNum2 int64
  333. var blockReleaseNum1 int64
  334. var blockReleaseNum2 int64
  335. // 冻结中
  336. dao.Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("selection_status between 5 and 6")).Count(&blockNum1)
  337. dao.Db.Model(entity.Project{}).Where(fmt.Sprintf("project_type = ? AND (project_status between 7 and 8) "), 1).Count(&blockNum2)
  338. // 冻结解除
  339. dao.Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("selection_status between 7 and 8")).Count(&blockReleaseNum1)
  340. dao.Db.Model(entity.Project{}).Where(fmt.Sprintf("project_type = ? AND (project_status between 9 and 10) "), 1).Count(&blockReleaseNum2)
  341. res["blockNum"] = blockNum1 + blockNum2
  342. res["blockReleaseNum"] = blockReleaseNum1 + blockReleaseNum2
  343. return res
  344. }
  345. // 充值管理——累计充值金额、确认中金额
  346. func (s RechargeService) ShowRecharge(param *vo.RechargeParam) (*vo.ReRechargeShow, error) {
  347. reRechargeShow := new(vo.ReRechargeShow)
  348. confirmingRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 1)
  349. totalRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 2)
  350. reRechargeShow.ConfirmingRecharge = confirmingRecharge
  351. reRechargeShow.TotalRecharge = totalRecharge
  352. return reRechargeShow, nil
  353. }
  354. // 充值管理——充值记录
  355. func (s RechargeService) RechargeInfoList(param *vo.RechargeParam) (vo.ResultVO, error) {
  356. if param.Page <= 0 {
  357. param.Page = 1
  358. }
  359. if param.PageSize <= 0 {
  360. param.PageSize = 10
  361. }
  362. var result vo.ResultVO
  363. var reRechargeInfos []*vo.ReRechargeInfo
  364. rechargeRecords, total, err := dao.RechargeRecordDao{}.RechargeInfoList(param)
  365. if err != nil {
  366. return result, err
  367. }
  368. for _, rechargeRecord := range rechargeRecords {
  369. var creatorName string
  370. if rechargeRecord.SubAccountId == 0 {
  371. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(rechargeRecord.EnterpriseID)
  372. if err == nil && enterprise != nil {
  373. creatorName = enterprise.BusinessName
  374. }
  375. } else {
  376. subAccount, err := dao.SubAccountDao{}.GetSubAccount(rechargeRecord.SubAccountId)
  377. if err == nil && subAccount != nil {
  378. creatorName = subAccount.SubAccountName
  379. }
  380. }
  381. reRechargeInfo := &vo.ReRechargeInfo{
  382. RechargeId: rechargeRecord.RechargeID,
  383. CreatorName: creatorName,
  384. RechargeAmount: rechargeRecord.RechargeAmount,
  385. RechargeMethod: rechargeRecord.RechargeMethod,
  386. TransferVoucherUrl: rechargeRecord.TransferVoucherUrl,
  387. }
  388. if param.RechargeState == 1 {
  389. reRechargeInfo.CommitAt = rechargeRecord.CommitAt.Format("2006-01-02 15:04:05")
  390. } else if param.RechargeState == 2 {
  391. reRechargeInfo.ConfirmAt = rechargeRecord.ConfirmAt.Format("2006-01-02 15:04:05")
  392. } else if param.RechargeState == 3 {
  393. reRechargeInfo.RefuseAt = rechargeRecord.RefuseAt.Format("2006-01-02 15:04:05")
  394. reRechargeInfo.FailReason = rechargeRecord.FailReason
  395. }
  396. reRechargeInfos = append(reRechargeInfos, reRechargeInfo)
  397. }
  398. //rechargingNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 1)
  399. //rechargedNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 2)
  400. //failNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 3)
  401. resMap := make(map[string]interface{})
  402. //resMap["rechargingNum"] = rechargingNum
  403. //resMap["rechargedNum"] = rechargedNum
  404. //resMap["failNum"] = failNum
  405. resMap["reRechargeInfos"] = reRechargeInfos
  406. result = vo.ResultVO{
  407. Page: param.Page,
  408. PageSize: param.PageSize,
  409. Total: total,
  410. Data: resMap,
  411. }
  412. return result, nil
  413. }
  414. // 余额管理——冻结记录-角标
  415. func (t RechargeService) RechargeInfoCount(param *vo.RechargeParam) map[string]int64 {
  416. res := make(map[string]int64)
  417. var rechargeConfirming int64
  418. var recharged int64
  419. var rechargeFail int64
  420. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 1).Count(&rechargeConfirming)
  421. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 2).Count(&recharged)
  422. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 3).Count(&rechargeFail)
  423. res["rechargeConfirming"] = rechargeConfirming
  424. res["recharged"] = recharged
  425. res["rechargeFail"] = rechargeFail
  426. return res
  427. }
  428. // 财务待办——充值确认中金额
  429. func (s RechargeService) GetFinance(param *vo.CommonParam) (float64, error) {
  430. confirmingRecharge, err := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 1)
  431. if err != nil {
  432. return 0, err
  433. }
  434. return confirmingRecharge, nil
  435. }