recharge_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_b_api/app/dao"
  14. "youngee_b_api/app/entity"
  15. "youngee_b_api/app/util"
  16. "youngee_b_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. enterprise, err := dao.EnterpriseDao{}.GetEnterpriseInfo(param.EnterpriseId)
  172. if err != nil {
  173. if errors.Is(err, gorm.ErrRecordNotFound) {
  174. return reBalanceShow, nil
  175. } else {
  176. return nil, err
  177. }
  178. }
  179. reBalanceShow.TotalBalance = enterprise.Balance
  180. reBalanceShow.AvailBalance = enterprise.AvailableBalance
  181. reBalanceShow.FrozenBalance = enterprise.FrozenBalance
  182. return reBalanceShow, nil
  183. }
  184. // 余额管理——冻结记录
  185. func (s RechargeService) FrozenInfoList(param *vo.BalanceParam) (vo.ResultVO, error) {
  186. if param.Page <= 0 {
  187. param.Page = 1
  188. }
  189. if param.PageSize <= 0 {
  190. param.PageSize = 10
  191. }
  192. var result vo.ResultVO
  193. var reBalanceShows []*vo.ReFrozenInfo
  194. var selectionInfos []*entity.SelectionInfo
  195. var projects []*entity.Project
  196. if param.FrozenState == 1 {
  197. // 电商带货
  198. selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenList(param.EnterpriseId)
  199. // 品牌种草
  200. projects, _ = dao.ProjectDAO{}.GetProjectFrozenList(param.EnterpriseId)
  201. // 本地生活
  202. } else {
  203. // 电商带货
  204. selectionInfos, _ = dao.SelectionInfoDAO{}.GetSelectionFrozenCancelList(param.EnterpriseId)
  205. // 品牌种草
  206. projects, _ = dao.ProjectDAO{}.GetProjectFrozenCancelList(param.EnterpriseId)
  207. // 本地生活
  208. }
  209. // 汇总结果
  210. for _, selection := range selectionInfos {
  211. // 获取商品详情字段
  212. var creatorName string
  213. var productName string
  214. var productPrice float64
  215. var mainImage string
  216. if selection.SubAccountId == 0 {
  217. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(selection.EnterpriseID)
  218. if err == nil && enterprise != nil {
  219. creatorName = enterprise.BusinessName
  220. }
  221. } else {
  222. subAccount, err := dao.SubAccountDao{}.GetSubAccount(selection.SubAccountId)
  223. if err == nil && subAccount != nil {
  224. creatorName = subAccount.SubAccountName
  225. }
  226. }
  227. product, err := dao.ProductDAO{}.GetProductByID(selection.ProductID)
  228. if err == nil && product != nil {
  229. productName = product.ProductName
  230. productPrice = product.ProductPrice
  231. }
  232. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(selection.ProductID)
  233. // 电商带货汇总
  234. reBalanceShow := &vo.ReFrozenInfo{
  235. ProductId: selection.ProductID,
  236. MainImage: mainImage,
  237. ProductName: productName,
  238. ProductPrice: productPrice,
  239. Platform: selection.Platform,
  240. CreatorName: creatorName,
  241. TaskType: "电商带货",
  242. FrozenBalance: selection.EstimatedCost,
  243. FrozenTime: selection.PayAt.Format("2006-01-02 15:04:05"),
  244. EnterpriseId: selection.EnterpriseID,
  245. SubAccountId: selection.SubAccountId,
  246. TaskId: selection.SelectionID,
  247. }
  248. if param.FrozenState == 2 {
  249. reBalanceShow.FrozenCancelBalance = selection.SettlementAmount
  250. }
  251. reBalanceShows = append(reBalanceShows, reBalanceShow)
  252. }
  253. for _, project := range projects {
  254. // 获取商品详情字段
  255. var creatorName string
  256. var productName string
  257. var productPrice float64
  258. var mainImage string
  259. if project.SubAccountId == 0 {
  260. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(project.EnterpriseID)
  261. if err == nil && enterprise != nil {
  262. creatorName = enterprise.BusinessName
  263. }
  264. } else {
  265. subAccount, err := dao.SubAccountDao{}.GetSubAccount(project.SubAccountId)
  266. if err == nil && subAccount != nil {
  267. creatorName = subAccount.SubAccountName
  268. }
  269. }
  270. product, err := dao.ProductDAO{}.GetProductByID(project.ProductID)
  271. if err == nil && product != nil {
  272. productName = product.ProductName
  273. productPrice = product.ProductPrice
  274. }
  275. mainImage, err = dao.ProductPhotoDAO{}.GetMainPhotoByProductID(project.ProductID)
  276. // 电商带货汇总
  277. reBalanceShow := &vo.ReFrozenInfo{
  278. ProductId: project.ProductID,
  279. MainImage: mainImage,
  280. ProductName: productName,
  281. ProductPrice: productPrice,
  282. Platform: project.ProjectPlatform,
  283. CreatorName: creatorName,
  284. TaskType: "品牌种草",
  285. FrozenBalance: project.NeedPay,
  286. FrozenTime: project.PayAt.Format("2006-01-02 15:04:05"),
  287. EnterpriseId: project.EnterpriseID,
  288. SubAccountId: project.SubAccountId,
  289. TaskId: project.ProjectId,
  290. }
  291. if param.FrozenState == 2 {
  292. reBalanceShow.FrozenCancelBalance = project.SettlementAmount
  293. }
  294. reBalanceShows = append(reBalanceShows, reBalanceShow)
  295. }
  296. startIndex := (param.Page - 1) * param.PageSize
  297. endIndex := startIndex + param.PageSize
  298. // 分页
  299. if startIndex >= len(reBalanceShows) {
  300. result = vo.ResultVO{
  301. Page: param.Page,
  302. PageSize: param.PageSize,
  303. Total: int64(len(reBalanceShows)),
  304. Data: nil,
  305. }
  306. return result, nil
  307. }
  308. if endIndex > len(reBalanceShows) {
  309. endIndex = len(reBalanceShows)
  310. }
  311. result = vo.ResultVO{
  312. Page: param.Page,
  313. PageSize: param.PageSize,
  314. Total: int64(len(reBalanceShows)),
  315. Data: reBalanceShows[startIndex:endIndex],
  316. }
  317. return result, nil
  318. }
  319. // 余额管理——冻结记录-角标
  320. func (t RechargeService) FrozenInfoCount(param *vo.BalanceParam) map[string]int64 {
  321. res := make(map[string]int64)
  322. var blockNum1 int64
  323. var blockNum2 int64
  324. var blockReleaseNum1 int64
  325. var blockReleaseNum2 int64
  326. // 冻结中
  327. dao.Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 5 and 6) "), param.EnterpriseId).Count(&blockNum1)
  328. dao.Db.Model(entity.Project{}).Where(fmt.Sprintf("enterprise_id = ? AND (project_status between 7 and 8) "), param.EnterpriseId).Count(&blockNum2)
  329. // 冻结解除
  330. dao.Db.Model(entity.SelectionInfo{}).Where(fmt.Sprintf("enterprise_id = ? AND (selection_status between 7 and 8) "), param.EnterpriseId).Count(&blockReleaseNum1)
  331. dao.Db.Model(entity.Project{}).Where(fmt.Sprintf("enterprise_id = ? AND (project_status between 9 and 10) "), param.EnterpriseId).Count(&blockReleaseNum2)
  332. res["blockNum"] = blockNum1 + blockNum2
  333. res["blockReleaseNum"] = blockReleaseNum1 + blockReleaseNum2
  334. return res
  335. }
  336. // 充值管理——累计充值金额、确认中金额
  337. func (s RechargeService) ShowRecharge(param *vo.RechargeParam) (*vo.ReRechargeShow, error) {
  338. reRechargeShow := new(vo.ReRechargeShow)
  339. confirmingRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 1)
  340. totalRecharge, _ := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 2)
  341. reRechargeShow.ConfirmingRecharge = confirmingRecharge
  342. reRechargeShow.TotalRecharge = totalRecharge
  343. return reRechargeShow, nil
  344. }
  345. // 充值管理——充值记录
  346. func (s RechargeService) RechargeInfoList(param *vo.RechargeParam) (vo.ResultVO, error) {
  347. if param.Page <= 0 {
  348. param.Page = 1
  349. }
  350. if param.PageSize <= 0 {
  351. param.PageSize = 10
  352. }
  353. var result vo.ResultVO
  354. var reRechargeInfos []*vo.ReRechargeInfo
  355. rechargeRecords, total, err := dao.RechargeRecordDao{}.RechargeInfoList(param)
  356. if err != nil {
  357. return result, err
  358. }
  359. for _, rechargeRecord := range rechargeRecords {
  360. var creatorName string
  361. if rechargeRecord.SubAccountId == 0 {
  362. enterprise, err := dao.EnterpriseDao{}.GetEnterprise(rechargeRecord.EnterpriseID)
  363. if err == nil && enterprise != nil {
  364. creatorName = enterprise.BusinessName
  365. }
  366. } else {
  367. subAccount, err := dao.SubAccountDao{}.GetSubAccount(rechargeRecord.SubAccountId)
  368. if err == nil && subAccount != nil {
  369. creatorName = subAccount.SubAccountName
  370. }
  371. }
  372. reRechargeInfo := &vo.ReRechargeInfo{
  373. RechargeId: rechargeRecord.RechargeID,
  374. CreatorName: creatorName,
  375. RechargeAmount: rechargeRecord.RechargeAmount,
  376. RechargeMethod: rechargeRecord.RechargeMethod,
  377. TransferVoucherUrl: rechargeRecord.TransferVoucherUrl,
  378. }
  379. if param.RechargeState == 1 {
  380. reRechargeInfo.CommitAt = rechargeRecord.CommitAt.Format("2006-01-02 15:04:05")
  381. } else if param.RechargeState == 2 {
  382. reRechargeInfo.ConfirmAt = rechargeRecord.ConfirmAt.Format("2006-01-02 15:04:05")
  383. } else if param.RechargeState == 3 {
  384. reRechargeInfo.RefuseAt = rechargeRecord.RefuseAt.Format("2006-01-02 15:04:05")
  385. reRechargeInfo.FailReason = rechargeRecord.FailReason
  386. }
  387. reRechargeInfos = append(reRechargeInfos, reRechargeInfo)
  388. }
  389. //rechargingNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 1)
  390. //rechargedNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 2)
  391. //failNum, _ := dao.RechargeRecordDao{}.RechargeStatusCount(param.EnterpriseId, 3)
  392. resMap := make(map[string]interface{})
  393. //resMap["rechargingNum"] = rechargingNum
  394. //resMap["rechargedNum"] = rechargedNum
  395. //resMap["failNum"] = failNum
  396. resMap["reRechargeInfos"] = reRechargeInfos
  397. result = vo.ResultVO{
  398. Page: param.Page,
  399. PageSize: param.PageSize,
  400. Total: total,
  401. Data: resMap,
  402. }
  403. return result, nil
  404. }
  405. // 余额管理——冻结记录-角标
  406. func (t RechargeService) RechargeInfoCount(param *vo.RechargeParam) map[string]int64 {
  407. res := make(map[string]int64)
  408. var rechargeConfirming int64
  409. var recharged int64
  410. var rechargeFail int64
  411. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 1).Count(&rechargeConfirming)
  412. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 2).Count(&recharged)
  413. dao.Db.Model(&entity.RechargeRecord{}).Where("enterprise_id = ? AND status = ?", param.EnterpriseId, 3).Count(&rechargeFail)
  414. res["rechargeConfirming"] = rechargeConfirming
  415. res["recharged"] = recharged
  416. res["rechargeFail"] = rechargeFail
  417. return res
  418. }
  419. // 财务待办——充值确认中金额
  420. func (s RechargeService) GetFinance(param *vo.CommonParam) (float64, error) {
  421. confirmingRecharge, err := dao.RechargeRecordDao{}.GetRechargeAmount(param.EnterpriseId, 1)
  422. if err != nil {
  423. return 0, err
  424. }
  425. return confirmingRecharge, nil
  426. }