recharge_service.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/wechatpay-apiv3/wechatpay-go/core"
  7. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  8. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
  9. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  10. "strconv"
  11. "time"
  12. "youngee_b_api/app/dao"
  13. "youngee_b_api/app/entity"
  14. "youngee_b_api/app/util"
  15. "youngee_b_api/app/vo"
  16. )
  17. type RechargeService struct{}
  18. // 充值管理——对公转账
  19. func (s RechargeService) TransferToPublic(param *vo.RechargeTransferParam) (*string, error) {
  20. var rechargeId string
  21. var phone string
  22. if param.SubAccountId == 0 {
  23. rechargeId = util.MakeRechargeId(param.EnterpriseId)
  24. phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
  25. } else {
  26. rechargeId = util.MakeRechargeId(strconv.FormatInt(param.SubAccountId, 10))
  27. phone, _ = dao.SubAccountDao{}.GetSubAccountPhone(param.SubAccountId)
  28. }
  29. rechargeRecord := entity.RechargeRecord{
  30. RechargeID: rechargeId,
  31. EnterpriseID: param.EnterpriseId,
  32. RechargeAmount: param.Amount,
  33. TransferVoucherUrl: param.TransferVoucherUrl,
  34. Phone: phone,
  35. RechargeMethod: 1,
  36. Status: 1,
  37. InvoiceStatus: 1,
  38. CommitAt: time.Now(),
  39. }
  40. err := dao.RechargeRecordDao{}.Insert(&rechargeRecord)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &rechargeId, nil
  45. }
  46. func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (codeUrl string, err error) {
  47. var (
  48. mchID string = "1615933939" // 商户号
  49. mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
  50. mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥,用于加密和解密平台证书
  51. )
  52. fmt.Println("充值的金额为:", amount)
  53. // 使用 utils 提供的函数从本地文件中加载商户私钥
  54. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem") // 商户私钥,用于生成请求的签名
  55. if err != nil {
  56. log.Print("load merchant private key error")
  57. }
  58. ctx := context.Background()
  59. // 使用商户私钥等初始化 微信支付client,并使它具有自动定时获取微信支付平台证书的能力
  60. opts := []core.ClientOption{
  61. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  62. }
  63. client, err := core.NewClient(ctx, opts...)
  64. if err != nil {
  65. log.Printf("new wechat pay client err: %s", err)
  66. }
  67. // 采用 Native 支付方式
  68. svc := native.NativeApiService{Client: client}
  69. // 发送请求
  70. resp, result, err := svc.Prepay(ctx,
  71. native.PrepayRequest{
  72. Appid: core.String("wxac396a3be7a16844"),
  73. Mchid: core.String("1615933939"),
  74. Description: core.String("样叽微信支付充值"),
  75. OutTradeNo: core.String(tradeId),
  76. TimeExpire: core.Time(time.Now().Add(10 * time.Minute)),
  77. Attach: core.String("微信支付充值"),
  78. NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
  79. SupportFapiao: core.Bool(true),
  80. Amount: &native.Amount{
  81. Currency: core.String("CNY"),
  82. Total: core.Int64(amount),
  83. },
  84. },
  85. )
  86. if err != nil {
  87. // 处理错误
  88. log.Printf("call Prepay err:%s", err)
  89. return "", err
  90. } else {
  91. // 处理返回结果
  92. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  93. log.Println("codeUrl:", *resp.CodeUrl)
  94. }
  95. return *resp.CodeUrl, nil
  96. }
  97. func (s RechargeService) QueryOrderByTradeId(tradeId string) (tradeState string, err error) {
  98. var (
  99. mchID string = "1615933939" // 商户号
  100. mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
  101. mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥
  102. )
  103. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  104. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem")
  105. if err != nil {
  106. log.Print("load merchant private key error")
  107. }
  108. ctx := context.Background()
  109. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  110. opts := []core.ClientOption{
  111. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  112. }
  113. client, err := core.NewClient(ctx, opts...)
  114. if err != nil {
  115. log.Printf("new wechat pay client err: %s", err)
  116. }
  117. svc := native.NativeApiService{Client: client}
  118. resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
  119. native.QueryOrderByOutTradeNoRequest{
  120. OutTradeNo: core.String(tradeId),
  121. Mchid: core.String("1615933939"),
  122. },
  123. )
  124. fmt.Printf("支付 %+v\n", resp)
  125. if err != nil {
  126. // 处理错误
  127. log.Printf("call QueryOrderByOutTradeNo err: %s", err)
  128. return "", err
  129. } else {
  130. // 处理返回结果
  131. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  132. }
  133. fmt.Printf("支付状态 %+v\n", *resp.TradeState)
  134. return *resp.TradeState, nil
  135. }