http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "go-common/app/service/main/ugcpay/conf"
  9. "go-common/app/service/main/ugcpay/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "github.com/pkg/errors"
  13. )
  14. // PayRefund 调用支付平台退款接口
  15. func (d *Dao) PayRefund(c context.Context, dataJSON string) (err error) {
  16. resp := new(struct {
  17. Code int `json:"errno"`
  18. Msg string `json:"msg"`
  19. })
  20. if err = d.paySend(c, conf.Conf.Biz.Pay.URLRefund, dataJSON, resp); err != nil {
  21. err = errors.WithStack(err)
  22. return
  23. }
  24. if resp.Code != ecode.OK.Code() {
  25. err = ecode.Int(resp.Code)
  26. }
  27. return
  28. }
  29. // PayCancel 调用支付平台订单取消接口
  30. func (d *Dao) PayCancel(c context.Context, dataJSON string) (err error) {
  31. resp := new(struct {
  32. Code int `json:"errno"`
  33. Msg string `json:"msg"`
  34. })
  35. if err = d.paySend(c, conf.Conf.Biz.Pay.URLCancel, dataJSON, resp); err != nil {
  36. err = errors.WithStack(err)
  37. return
  38. }
  39. if resp.Code != ecode.OK.Code() {
  40. err = ecode.Int(resp.Code)
  41. }
  42. return
  43. }
  44. // PayQuery 调用支付平台订单查询接口 return map[orderID]*model.PayOrder
  45. func (d *Dao) PayQuery(c context.Context, dataJSON string) (orders map[string][]*model.PayOrder, err error) {
  46. resp := new(struct {
  47. Code int `json:"errno"`
  48. Msg string `json:"msg"`
  49. Data *model.PayQuery `json:"data"`
  50. })
  51. if err = d.paySend(c, conf.Conf.Biz.Pay.URLQuery, dataJSON, resp); err != nil {
  52. err = errors.WithStack(err)
  53. return
  54. }
  55. if resp.Code != ecode.OK.Code() {
  56. err = ecode.Int(resp.Code)
  57. return
  58. }
  59. if resp.Data == nil {
  60. err = errors.Errorf("PayQuery got nil data, resp: %+v", resp)
  61. return
  62. }
  63. orders = make(map[string][]*model.PayOrder)
  64. for _, o := range resp.Data.Orders {
  65. orders[o.OrderID] = append(orders[o.OrderID], o)
  66. }
  67. return
  68. }
  69. func (d *Dao) paySend(c context.Context, url string, jsonData string, respData interface{}) (err error) {
  70. var (
  71. req *http.Request
  72. client = new(http.Client)
  73. resp *http.Response
  74. bs []byte
  75. )
  76. if req, err = http.NewRequest(http.MethodPost, url, strings.NewReader(jsonData)); err != nil {
  77. err = errors.WithStack(err)
  78. return
  79. }
  80. req.Header.Add("Content-Type", "application/json")
  81. if resp, err = client.Do(req); err != nil {
  82. err = errors.Wrapf(err, "call url: %s, body: %s", url, jsonData)
  83. return
  84. }
  85. defer resp.Body.Close()
  86. if resp.StatusCode >= http.StatusBadRequest {
  87. err = errors.Errorf("d.paySend incorrect http status: %d, host: %s, url: %s", resp.StatusCode, req.URL.Host, req.URL.String())
  88. return
  89. }
  90. if bs, err = ioutil.ReadAll(resp.Body); err != nil {
  91. err = errors.Wrapf(err, "d.paySend ioutil.ReadAll")
  92. return
  93. }
  94. log.Info("paySend call url: %s, body: %s, resp: %s", url, jsonData, bs)
  95. if err = json.Unmarshal(bs, respData); err != nil {
  96. err = errors.WithStack(err)
  97. return
  98. }
  99. log.Info("paySend call url: %v, body: %s, resp: %+v", url, jsonData, respData)
  100. return
  101. }