recharge_service.go 16 KB

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