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

122 lines
3.0 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
  1. package pay_service
  2. import (
  3. "errors"
  4. core2 "github.com/chanxuehong/wechat/mch/core"
  5. "github.com/chanxuehong/wechat/mch/pay"
  6. "github.com/ouxuanserver/osmanthuswine/src/core"
  7. "hudongzhuanjia/models"
  8. "hudongzhuanjia/utils"
  9. "time"
  10. )
  11. const CALLBACK_REFUND_URL = "https://api.ouxuanhudong.com/PcClient/common/WeChatOauthCtl/callbackRefund"
  12. func Refund(reason, outTradeNo, openId string, goodType, totalFee, refundFee int, userId, activityId int64) (*pay.RefundResponse, error) {
  13. client, err := Client()
  14. outRefundNo := utils.RandomStr(64)
  15. nonceStr := utils.RandomStr(32)
  16. res, err := pay.Refund2(client, &pay.RefundRequest{
  17. TransactionId: "",
  18. OutTradeNo: outTradeNo,
  19. OutRefundNo: outRefundNo,
  20. TotalFee: int64(totalFee),
  21. RefundFee: int64(refundFee),
  22. NonceStr: nonceStr,
  23. SignType: core2.SignType_MD5,
  24. RefundFeeType: "CNY",
  25. RefundDesc: reason,
  26. })
  27. //
  28. if err != nil {
  29. return nil, err
  30. }
  31. refund := new(models.UserRefund)
  32. refund.OutTradeNo = outTradeNo
  33. refund.Status = 0
  34. refund.TotalFee = totalFee
  35. refund.GoodType = goodType
  36. refund.RefundDesc = reason
  37. refund.FeeType = "CNY"
  38. refund.OpenId = openId
  39. refund.UserId = userId
  40. refund.ActivityId = activityId
  41. refund.IsDelete = false
  42. refund.CreatedAt = time.Now()
  43. refund.UpdatedAt = time.Now()
  44. _, err = refund.AddUserRefund()
  45. if err != nil {
  46. return nil, err
  47. }
  48. return res, err
  49. }
  50. func QueryRefund(outTradeNo string) (*pay.RefundQueryResponse, error) {
  51. refund := new(models.UserRefund)
  52. exist, err := refund.GetByOutTradeNo(outTradeNo)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if !exist {
  57. return nil, errors.New("不存在改笔退款")
  58. }
  59. client, err := Client()
  60. res, err := pay.RefundQuery2(client, &pay.RefundQueryRequest{
  61. OutTradeNo: outTradeNo,
  62. NonceStr: utils.RandomStr(32),
  63. SignType: core2.SignType_MD5,
  64. })
  65. //请求申请退款
  66. if err != nil {
  67. return nil, err
  68. }
  69. refund.RefundAccount = res.RefundList[0].RefundAccount
  70. refund.RefundRecvAccount = res.RefundList[0].RefundRecvAccout
  71. refund.RefundStatus = res.RefundList[0].RefundStatus
  72. refund.Status = 1
  73. refund.SuccessTime = res.RefundList[0].RefundSuccessTime
  74. refund.WxRefundId = res.RefundList[0].RefundId
  75. refund.TransactionId = res.TransactionId
  76. _, err = refund.QueryUpdate()
  77. if err != nil {
  78. return nil, err
  79. }
  80. return res, nil
  81. }
  82. func BatchQueryRefundByUserId(userId int64) error {
  83. refunds := make([]*models.UserRefund, 0)
  84. if err := core.GetXormAuto().Where("status = 0 and user_id=?", userId).Find(&refunds); err != nil {
  85. return err
  86. }
  87. if err := batchQueryRefund(refunds); err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. func BatchQueryRefundByActivityId(activityId int64) error {
  93. refunds := make([]*models.UserRefund, 0)
  94. if err := core.GetXormAuto().Where("status = 0 and user_id=?", activityId).Find(&refunds); err != nil {
  95. return err
  96. }
  97. if err := batchQueryRefund(refunds); err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func batchQueryRefund(refunds []*models.UserRefund) error {
  103. for _, refund := range refunds {
  104. _, err := QueryRefund(refund.OutTradeNo)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }