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

215 lines
6.8 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
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/logger"
  7. "hudongzhuanjia/models"
  8. "hudongzhuanjia/utils"
  9. "hudongzhuanjia/utils/define"
  10. "strconv"
  11. )
  12. // 企业向微信用户个人付款(不支持沙箱环境)
  13. type TransferResponse struct {
  14. DeviceInfo string `xml:"device_info,omitempty" json:"device_info,omitempty"`
  15. NonceStr string `xml:"nonce_str,omitempty" json:"nonce_str,omitempty"`
  16. PartnerTradeNo string `xml:"partner_trade_no,omitempty" json:"partner_trade_no,omitempty"`
  17. PaymentNo string `xml:"payment_no,omitempty" json:"payment_no,omitempty"`
  18. PaymentTime string `xml:"payment_time,omitempty" json:"payment_time,omitempty"`
  19. }
  20. func Transfer(desc, openId, partnerTradeNo string, amount int) (*TransferResponse, error) {
  21. client, err := Client()
  22. if err != nil {
  23. return nil, err
  24. }
  25. nonceStr := utils.RandomStr(32)
  26. if partnerTradeNo == "" { // 需要提前存入
  27. partnerTradeNo = utils.RandomStr(32)
  28. }
  29. // 初始化参数结构体
  30. body := make(map[string]string)
  31. body["nonce_str"] = nonceStr
  32. body["partner_trade_no"] = partnerTradeNo
  33. body["openid"] = openId
  34. body["check_name"] = "NO_CHECK" // NO_CHECK:不校验真实姓名 , FORCE_CHECK:强校验真实姓名
  35. body["amount"] = fmt.Sprintf("%d", amount) // 企业付款金额,单位为分
  36. body["desc"] = desc // 企业付款备注,必填。注意:备注中的敏感词会被转成字符*
  37. body["mchid"] = define.MchId
  38. body["mch_appid"] = client.AppId()
  39. m, err := promotion.Transfers(client, body)
  40. // 记录错误信息
  41. if err != nil {
  42. transfer := new(models.UserTransfer)
  43. transfer.UpdateErrMsg(partnerTradeNo, err.Error())
  44. return nil, err
  45. }
  46. res := &TransferResponse{
  47. DeviceInfo: m["device_info"],
  48. NonceStr: m["nonce_str"],
  49. PartnerTradeNo: m["partner_trade_no"],
  50. PaymentNo: m["payment_no"],
  51. PaymentTime: m["payment_time"],
  52. }
  53. // 获取某些信息
  54. transfer := new(models.UserTransfer)
  55. exist, err := transfer.GetByPartnerTradeNo(partnerTradeNo)
  56. if err != nil {
  57. return nil, err
  58. }
  59. transfer.Desc = desc
  60. transfer.OpenId = openId
  61. transfer.PartnerTradeNo = partnerTradeNo
  62. transfer.PaymentAmount = amount
  63. transfer.PaymentNo = res.PaymentNo
  64. transfer.DeviceInfo = res.DeviceInfo
  65. transfer.PaymentTime = res.PaymentTime
  66. if !exist {
  67. _, err = transfer.Add()
  68. if err != nil {
  69. return nil, err
  70. }
  71. } else {
  72. _, err = transfer.UpdateByPartnerTradeNo(partnerTradeNo)
  73. if err != nil {
  74. return nil, err
  75. }
  76. }
  77. return res, nil
  78. }
  79. type TransferInfoResponse struct {
  80. ErrCode string `json:"err_code"`
  81. Status string `json:"status"`
  82. Reason string `json:"reason"`
  83. Openid string `json:"openid"`
  84. PaymentAmount int `json:"payment_amount"`
  85. PaymentTime string `json:"payment_time"`
  86. TransferTime string `json:"transfer_time"`
  87. Desc string `json:"desc"`
  88. }
  89. func TransferInfo(partnerTradeNo string) (*TransferInfoResponse, error) {
  90. client, err := Client()
  91. if err != nil {
  92. return nil, err
  93. }
  94. nonceStr := utils.RandomStr(32)
  95. var body = make(map[string]string, 0)
  96. body["nonce_str"] = nonceStr
  97. body["partner_trade_no"] = partnerTradeNo
  98. m, err := mmpaymkttransfers.GetTransferInfo(client, body)
  99. if err != nil {
  100. transfer := new(models.UserTransfer)
  101. transfer.UpdateErrMsg(partnerTradeNo, err.Error())
  102. return nil, err
  103. }
  104. amount, _ := strconv.Atoi(m["payment_amount"])
  105. res := &TransferInfoResponse{
  106. Status: m["status"],
  107. Reason: m["reason"],
  108. Openid: m["openid"],
  109. PaymentAmount: amount,
  110. PaymentTime: m["payment_time"],
  111. TransferTime: m["transfer_time"],
  112. Desc: m["desc"],
  113. ErrCode: m["err_code"],
  114. }
  115. transfer := new(models.UserTransfer)
  116. if res.Status == define.CODE_SUCCESS {
  117. transfer.Status = 2
  118. } else if res.Status == define.CODE_TRANSFER_PROCESSING {
  119. transfer.Status = 1
  120. } else if res.Status == define.CODE_FAIL {
  121. transfer.Status = 3
  122. }
  123. transfer.Reason = res.Reason
  124. transfer.TransferTime = res.TransferTime
  125. _, err = transfer.UpdateByPartnerTradeNo(partnerTradeNo)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return res, nil
  130. }
  131. type RedPackResult struct {
  132. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  133. ReOpenid string `xml:"re_openid,omitempty" json:"re_openid,omitempty"`
  134. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  135. SendListid string `xml:"send_listid,omitempty" json:"send_listid,omitempty"`
  136. }
  137. // 发送红包
  138. func SendRedPack(sendName, openId, wishing, actName, remark string, totalAmount, totalNum, scene int) (*RedPackResult, error) {
  139. client, err := Client()
  140. if err != nil {
  141. logger.Error("client init error: %v", err)
  142. return nil, err
  143. }
  144. mchBillNo := utils.RandomStr(28)
  145. nonceStr := utils.RandomStr(32)
  146. body := make(map[string]string, 0)
  147. body["mch_id"] = client.MchId()
  148. //body["sub_mch_id"] = client.SubMchId()
  149. body["wxappid"] = client.AppId()
  150. //body["msgappid"] = define.WxAppId
  151. body["nonce_str"] = nonceStr
  152. body["mch_billno"] = mchBillNo
  153. body["send_name"] = sendName
  154. body["re_openid"] = openId
  155. body["total_amount"] = fmt.Sprintf("%d", totalAmount)
  156. body["total_num"] = fmt.Sprintf("%d", totalNum)
  157. body["wishing"] = wishing
  158. body["client_ip"] = define.ClientIp
  159. body["act_name"] = actName
  160. body["remark"] = remark
  161. body["scene_id"] = fmt.Sprintf("PRODUCT_%d", scene)
  162. m, err := mmpaymkttransfers.SendRedPack(client, body)
  163. if err != nil {
  164. return nil, err
  165. }
  166. res := &RedPackResult{
  167. MchBillno: m["mch_billno"],
  168. ReOpenid: m["re_openid"],
  169. TotalAmount: totalAmount,
  170. SendListid: m["send_listid"],
  171. }
  172. return res, nil
  173. }
  174. type QueryRedPackResult struct {
  175. CommonReturn
  176. CommonResult
  177. MchBillno string `xml:"mch_billno,omitempty" json:"mch_billno,omitempty"`
  178. Mchid string `xml:"mchid,omitempty" json:"mchid,omitempty"`
  179. DetailId string `xml:"detail_id,omitempty" json:"detail_id,omitempty"`
  180. Status string `xml:"status,omitempty" json:"status,omitempty"`
  181. SendType string `xml:"send_type,omitempty" json:"send_type,omitempty"`
  182. HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
  183. TotalNum int `xml:"total_num,omitempty" json:"total_num,omitempty"`
  184. TotalAmount int `xml:"total_amount,omitempty" json:"total_amount,omitempty"`
  185. Reason string `xml:"reason,omitempty" json:"reason,omitempty"`
  186. SendTime string `xml:"send_time,omitempty" json:"send_time,omitempty"`
  187. RefundTime string `xml:"refund_time,omitempty" json:"refund_time,omitempty"`
  188. RefundAmount int `xml:"refund_amount,omitempty" json:"refund_amount,omitempty"`
  189. Wishing string `xml:"wishing,omitempty" json:"wishing,omitempty"`
  190. Remark string `xml:"remark,omitempty" json:"remark,omitempty"`
  191. ActName string `xml:"act_name,omitempty" json:"act_name,omitempty"`
  192. }