http.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "go-common/app/job/main/ugcpay/conf"
  9. "go-common/app/job/main/ugcpay/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "github.com/pkg/errors"
  13. )
  14. // PayRechargeShell .
  15. func (d *Dao) PayRechargeShell(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.RechargeShellURL, 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. // PayCheckRefundOrder return map[txID]*model.PayCheckOrder
  30. func (d *Dao) PayCheckRefundOrder(c context.Context, dataJSON string) (orders map[string][]*model.PayCheckRefundOrderEle, err error) {
  31. resp := new(struct {
  32. Code int `json:"code"`
  33. Msg string `json:"message"`
  34. Data []*model.PayCheckRefundOrder `json:"data"`
  35. })
  36. if err = d.paySend(c, conf.Conf.Biz.Pay.CheckRefundOrderURL, dataJSON, resp); err != nil {
  37. err = errors.WithStack(err)
  38. return
  39. }
  40. if resp.Code != ecode.OK.Code() {
  41. err = ecode.Int(resp.Code)
  42. return
  43. }
  44. if resp.Data == nil {
  45. err = errors.Errorf("PayCheckRefundOrder got nil data, url: %s, body: %s, resp: %+v", conf.Conf.Biz.Pay.CheckOrderURL, dataJSON, resp)
  46. return
  47. }
  48. orders = make(map[string][]*model.PayCheckRefundOrderEle)
  49. for _, o := range resp.Data {
  50. orders[o.TXID] = append(orders[o.TXID], o.Elements...)
  51. }
  52. return
  53. }
  54. // PayCheckOrder return map[txID]*model.PayCheckOrder
  55. func (d *Dao) PayCheckOrder(c context.Context, dataJSON string) (orders map[string]*model.PayCheckOrder, err error) {
  56. resp := new(struct {
  57. Code int `json:"code"`
  58. Msg string `json:"message"`
  59. Data []*model.PayCheckOrder `json:"data"`
  60. })
  61. if err = d.paySend(c, conf.Conf.Biz.Pay.CheckOrderURL, dataJSON, resp); err != nil {
  62. err = errors.WithStack(err)
  63. return
  64. }
  65. if resp.Code != ecode.OK.Code() {
  66. err = ecode.Int(resp.Code)
  67. return
  68. }
  69. if resp.Data == nil {
  70. err = errors.Errorf("PayCheckOrder got nil data, url: %s, body: %s, resp: %+v", conf.Conf.Biz.Pay.CheckOrderURL, dataJSON, resp)
  71. return
  72. }
  73. orders = make(map[string]*model.PayCheckOrder)
  74. for _, o := range resp.Data {
  75. orders[o.TxID] = o
  76. }
  77. return
  78. }
  79. func (d *Dao) paySend(c context.Context, url string, jsonData string, respData interface{}) (err error) {
  80. var (
  81. req *http.Request
  82. client = new(http.Client)
  83. resp *http.Response
  84. bs []byte
  85. )
  86. log.Info("paySend call url: %s, body: %s", url, jsonData)
  87. defer func() {
  88. log.Info("paySend call url: %v, body: %s, resp: %+v, err: %+v", url, jsonData, respData, err)
  89. }()
  90. if req, err = http.NewRequest(http.MethodPost, url, strings.NewReader(jsonData)); err != nil {
  91. err = errors.WithStack(err)
  92. return
  93. }
  94. req.Header.Add("Content-Type", "application/json")
  95. if resp, err = client.Do(req); err != nil {
  96. err = errors.Wrapf(err, "call url: %s, body: %s", url, jsonData)
  97. return
  98. }
  99. defer resp.Body.Close()
  100. if resp.StatusCode >= http.StatusBadRequest {
  101. err = errors.Errorf("d.paySend incorrect http status: %d, host: %s, url: %s", resp.StatusCode, req.URL.Host, req.URL.String())
  102. return
  103. }
  104. if bs, err = ioutil.ReadAll(resp.Body); err != nil {
  105. err = errors.Wrapf(err, "d.paySend ioutil.ReadAll")
  106. return
  107. }
  108. if err = json.Unmarshal(bs, respData); err != nil {
  109. err = errors.WithStack(err)
  110. return
  111. }
  112. return
  113. }