互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB

5 years ago
  1. package pay_service
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/iGoogle-ink/gopay/v2"
  6. "github.com/iGoogle-ink/gopay/v2/wechat"
  7. "github.com/ouxuanserver/osmanthuswine/src/core"
  8. "hudongzhuanjia/libs/wx"
  9. "hudongzhuanjia/logger"
  10. "hudongzhuanjia/models"
  11. "time"
  12. )
  13. func TransferExtend(bm gopay.BodyMap) (*wechat.TransfersResponse, error) {
  14. err := bm.CheckEmptyError("nonce_str", "partner_trade_no", "openid", "check_name", "amount", "desc", "spbill_create_ip")
  15. if err != nil {
  16. return nil, err
  17. }
  18. bs, err := ClientExtend(bm, wx.TransfersURL)
  19. if err != nil {
  20. return nil, err
  21. }
  22. wxRsp := new(wechat.TransfersResponse)
  23. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  24. return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err)
  25. }
  26. return wxRsp, nil
  27. }
  28. // 企业向微信用户个人付款(不支持沙箱环境)
  29. func Transfer(desc, ip, openId string, amount int) (*wechat.TransfersResponse, error) {
  30. nonceStr := gopay.GetRandomString(32)
  31. partnerTradeNo := gopay.GetRandomString(32)
  32. // 初始化参数结构体
  33. body := make(gopay.BodyMap)
  34. body.Set("nonce_str", nonceStr)
  35. body.Set("partner_trade_no", partnerTradeNo)
  36. body.Set("openid", openId)
  37. body.Set("check_name", "NO_CHECK") // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
  38. body.Set("amount", amount) // 企业付款金额,单位为分
  39. body.Set("desc", desc) // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
  40. body.Set("spbill_create_ip", ip)
  41. body.Set("mchid", wx.Mchid)
  42. body.Set("mch_appid", wx.Appid)
  43. res, err := TransferExtend(body)
  44. if err != nil {
  45. fmt.Println("Error:", err)
  46. return nil, err
  47. }
  48. fmt.Println("wxRsp:", *res)
  49. // 保存到user_transfer
  50. return res, nil
  51. }
  52. func QueryTransfer(partnerTradeNo string) {
  53. body := make(gopay.BodyMap)
  54. body.Set("nonce_str", gopay.GetRandomString(32))
  55. body.Set("partner_trade_no", partnerTradeNo)
  56. // todo: 查询, 遇到查询失败,需要原订单号重新请求
  57. }
  58. // 发送红包
  59. func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmount, totalNum, scene int) (*wx.RedPackResult, error) {
  60. mchBillNo := gopay.GetRandomString(28)
  61. body := make(gopay.BodyMap)
  62. body.Set("nonce_str", gopay.GetRandomString(32))
  63. body.Set("mch_billno", mchBillNo)
  64. body.Set("send_name", sendName)
  65. body.Set("re_openid", openId)
  66. body.Set("total_amount", totalAmount)
  67. body.Set("total_num", totalNum)
  68. body.Set("wishing", wishing)
  69. body.Set("client_ip", ip)
  70. body.Set("act_name", actName)
  71. body.Set("remark", remark)
  72. body.Set("scene_id", fmt.Sprintf("PRODUCT_%d", scene))
  73. body.Set("wxappid", wx.Appid)
  74. body.Set("mch_id", wx.Mchid)
  75. bs, err := ClientExtend(body, wx.SendRedPackURL)
  76. if err != nil {
  77. return nil, err
  78. }
  79. res := new(wx.RedPackResult)
  80. if err = xml.Unmarshal(bs, res); err != nil {
  81. logger.Sugar.Errorf("xml.Unmarshal(%s): %s", string(bs), err)
  82. return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err)
  83. }
  84. if res.ReturnCode != wx.CODE_SUCCESS {
  85. err = fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg)
  86. logger.Sugar.Error(err)
  87. return nil, err
  88. }
  89. userRedPack := new(models.UserRedPack)
  90. userRedPack.ErrCode = res.ErrCode
  91. userRedPack.ErrCodeDes = res.ErrCodeDes
  92. userRedPack.MchBillno = mchBillNo
  93. userRedPack.ReOpenid = openId
  94. userRedPack.SceneId = scene
  95. userRedPack.SendListid = res.SendListid
  96. userRedPack.TotalAmount = totalAmount
  97. userRedPack.TotalNum = totalNum
  98. userRedPack.ResultCode = res.ResultCode
  99. userRedPack.IsDelete = false
  100. userRedPack.UpdatedAt = time.Now()
  101. userRedPack.CreatedAt = time.Now()
  102. if _, err := core.GetXormAuto().InsertOne(userRedPack); err != nil {
  103. logger.Sugar.Errorf("insert into user red pack error: %s", err)
  104. return nil, err
  105. }
  106. return res, nil
  107. }
  108. // todo: 小心谨慎查询
  109. func QueryRedPack(mchBillno string) (*wx.QueryRedPackResult, error) {
  110. body := make(gopay.BodyMap)
  111. body.Set("nonce_str", gopay.GetRandomString(32))
  112. body.Set("mch_billno", mchBillno)
  113. body.Set("bill_type", "MCHT")
  114. body.Set("appid", wx.Appid)
  115. body.Set("mch_id", wx.Mchid)
  116. bs, err := ClientExtend(body, wx.QueryRedPackURL)
  117. if err != nil {
  118. logger.Sugar.Errorf("client extend request wx error: %s", err)
  119. return nil, err
  120. }
  121. res := new(wx.QueryRedPackResult)
  122. if err = xml.Unmarshal(bs, res); err != nil {
  123. logger.Sugar.Errorf("xml.Unmarshal(%s):%s", string(bs), err)
  124. return nil, fmt.Errorf("xml.Unmarshal(%s):%s", string(bs), err)
  125. }
  126. if res.ReturnCode != wx.CODE_SUCCESS {
  127. err = fmt.Errorf("network error, retrun_code: %v and return_msg: %v", res.ReturnCode, res.ReturnMsg)
  128. logger.Sugar.Error(err)
  129. return nil, err
  130. }
  131. return res, nil
  132. }