互动
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.

176 lines
5.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pay_service
  2. import (
  3. "fmt"
  4. "github.com/chanxuehong/wechat/mch/mmpaymkttransfers"
  5. "github.com/chanxuehong/wechat/mch/mmpaymkttransfers/promotion"
  6. "hudongzhuanjia/models"
  7. "hudongzhuanjia/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // 企业向微信用户个人付款(不支持沙箱环境)
  12. type TransferResponse struct {
  13. DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
  14. NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
  15. PartnerTradeNo string `xml:"partner_trade_no,omitempty" json:"partner_trade_no,omitempty"`
  16. PaymentNo string `xml:"payment_no,omitempty" json:"payment_no,omitempty"`
  17. PaymentTime string `xml:"payment_time,omitempty" json:"payment_time,omitempty"`
  18. }
  19. func Transfer(desc, openId, partnerTradeNo string, amount int) (*TransferResponse, error) {
  20. nonceStr := utils.RandomStr(32)
  21. if partnerTradeNo == "" { // 需要提前存入
  22. partnerTradeNo = utils.RandomStr(32)
  23. }
  24. // 初始化参数结构体
  25. body := make(map[string]string)
  26. body["nonce_str"] = nonceStr
  27. body["partner_trade_no"] = partnerTradeNo
  28. body["openid"] = openId
  29. body["check_name"] = "NO_CHECK" // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
  30. body["amount"] = fmt.Sprintf("%d", amount) // 企业付款金额,单位为分
  31. body["desc"] = desc // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
  32. body["mchid"] = Mchid
  33. body["mch_appid"] = Appid
  34. client, err := Client()
  35. m, err := promotion.Transfers(client, body)
  36. if err != nil {
  37. return nil, err
  38. }
  39. res := &TransferResponse{
  40. DeviceInfo: m["device_info"],
  41. NonceStr: m["nonce_str"],
  42. PartnerTradeNo: m["partner_trade_no"],
  43. PaymentNo: m["payment_no"],
  44. PaymentTime: m["payment_time"],
  45. }
  46. return res, nil
  47. }
  48. type TransferInfoResponse struct {
  49. Status string `json:"status"`
  50. Reason string `json:"reason"`
  51. Openid string `json:"openid"`
  52. PaymentAmount int `json:"payment_amount"`
  53. PaymentTime string `json:"payment_time"`
  54. TransferTime string `json:"transfer_time"`
  55. Desc string `json:"desc"`
  56. }
  57. func TransferInfo(partnerTradeNo string) (*TransferInfoResponse, error) {
  58. client, err := Client()
  59. if err != nil {
  60. return nil, err
  61. }
  62. nonceStr := utils.RandomStr(32)
  63. var body = make(map[string]string, 0)
  64. body["nonce_str"] = nonceStr
  65. body["partner_trade_no"] = partnerTradeNo
  66. m, err := mmpaymkttransfers.GetTransferInfo(client, body)
  67. if err != nil {
  68. return nil, err
  69. }
  70. amount, _ := strconv.Atoi(m["payment_amount"])
  71. resp := &TransferInfoResponse{
  72. Status: m["status"],
  73. Reason: m["reason"],
  74. Openid: m["openid"],
  75. PaymentAmount: amount,
  76. PaymentTime: m["payment_time"],
  77. TransferTime: m["transfer_time"],
  78. Desc: m["desc"],
  79. }
  80. return resp, nil
  81. }
  82. type RedPackResult struct {
  83. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  84. ReOpenid string `xml:"re_openid,omitempty" json:"re_openid,omitempty"`
  85. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  86. SendListid string `xml:"send_listid,omitempty" json:"send_listid,omitempty"`
  87. }
  88. // 发送红包
  89. func SendRedPack(sendName, openId, wishing, ip, actName, remark string, totalAmount, totalNum, scene int) (*RedPackResult, error) {
  90. client, err := Client()
  91. if err != nil {
  92. return nil, err
  93. }
  94. mchBillNo := utils.RandomStr(28)
  95. nonceStr := utils.RandomStr(32)
  96. body := make(map[string]string, 0)
  97. body["wxappid"] = client.AppId()
  98. body["mch_id"] = client.MchId()
  99. body["nonce_str"] = nonceStr
  100. body["mch_billno"] = mchBillNo
  101. body["send_name"] = sendName
  102. body["re_openid"] = openId
  103. body["total_amount"] = fmt.Sprintf("%d", totalAmount)
  104. body["total_num"] = fmt.Sprintf("%d", totalNum)
  105. body["wishing"] = wishing
  106. body["client_ip"] = ip
  107. body["act_name"] = actName
  108. body["remark"] = remark
  109. body["scene_id"] = fmt.Sprintf("PRODUCT_%d", scene)
  110. m, err := mmpaymkttransfers.SendRedPack(client, body)
  111. if err != nil {
  112. return nil, err
  113. }
  114. res := &RedPackResult{
  115. MchBillno: m["mch_billno"],
  116. ReOpenid: m["re_openid"],
  117. TotalAmount: totalAmount,
  118. SendListid: m["send_listid"],
  119. }
  120. userRedPack := new(models.UserRedPack)
  121. userRedPack.MchBillno = mchBillNo
  122. userRedPack.ReOpenid = openId
  123. userRedPack.SceneId = scene
  124. userRedPack.SendListid = res.SendListid
  125. userRedPack.TotalAmount = totalAmount
  126. userRedPack.TotalNum = totalNum
  127. userRedPack.IsDelete = false
  128. userRedPack.UpdatedAt = time.Now()
  129. userRedPack.CreatedAt = time.Now()
  130. _, err = userRedPack.AddRedPack()
  131. if err != nil {
  132. return nil, err
  133. }
  134. return res, nil
  135. }
  136. type QueryRedPackResult struct {
  137. CommonReturn
  138. CommonResult
  139. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  140. Mchid string `xml:"mchid,omitempty" json:"mchid,omitempty"`
  141. DetailId string `xml:"detail_id,omitempty" json:"detail_id,omitempty"`
  142. Status string `xml:"status,omitempty" json:"status,omitempty"`
  143. SendType string `xml:"send_type,omitempty" json:"send_type,omitempty"`
  144. HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
  145. TotalNum int `xml:"total_num,omitempty" json:"total_num,omitempty"`
  146. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  147. Reason string `xml:"reason,omitempty" json:"reason,omitempty"`
  148. SendTime string `xml:"send_time,omitempty" json:"send_time,omitempty"`
  149. RefundTime string `xml:"refund_time,omitempty" json:"refund_time,omitempty"`
  150. RefundAmount int `xml:"refund_amount,omitempty" json:"refund_amount,omitempty"`
  151. Wishing string `xml:"wishing,omitempty" json:"wishing,omitempty"`
  152. Remark string `xml:"remark,omitempty" json:"remark,omitempty"`
  153. ActName string `xml:"act_name,omitempty" json:"act_name,omitempty"`
  154. }