wechat_pay.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package gopay
  2. import (
  3. "encoding/xml"
  4. "errors"
  5. "github.com/parnurzeal/gorequest"
  6. )
  7. type weChatClient struct {
  8. AppId string
  9. MchId string
  10. secretKey string
  11. Params *WeChatPayParams
  12. isProd bool
  13. }
  14. //初始化微信客户端
  15. // appId:应用ID
  16. // mchID:商户ID
  17. // isProd:是否是正式环境
  18. // secretKey:key,(当isProd为true时,此参数必传;false时,此参数为空)
  19. func NewWeChatClient(appId, mchId string, isProd bool, secretKey ...string) *weChatClient {
  20. client := new(weChatClient)
  21. client.AppId = appId
  22. client.MchId = mchId
  23. client.isProd = isProd
  24. if isProd && len(secretKey) > 0 {
  25. client.secretKey = secretKey[0]
  26. }
  27. return client
  28. }
  29. //统一下单
  30. func (this weChatClient) UnifiedOrder(param *WeChatPayParams) (wxRsp *weChatPayResponse, err error) {
  31. this.Params = param
  32. //fmt.Println("reqs:", this.ReqParam)
  33. var sign string
  34. var reqs requestBody
  35. if this.isProd {
  36. sign, reqs = getSignAndRequestBody(this.AppId, this.MchId, this.secretKey, this.Params)
  37. } else {
  38. return nil, errors.New("暂不支持沙箱测试")
  39. //getSanBoxSignKey(this.Appid, this.MchId, this.Params)
  40. }
  41. reqs.Set("sign", sign)
  42. reqXML := generateXml(reqs)
  43. agent := gorequest.New()
  44. if this.isProd {
  45. agent.Post(wX_PayUrl)
  46. } else {
  47. agent.Post(wX_PayUrl_SanBox)
  48. }
  49. agent.Type("xml")
  50. agent.SendString(reqXML)
  51. response, bytes, errs := agent.EndBytes()
  52. defer response.Body.Close()
  53. if len(errs) > 0 {
  54. return nil, errs[0]
  55. }
  56. //fmt.Println("bytes:", string(bytes))
  57. wxRsp = new(weChatPayResponse)
  58. err = xml.Unmarshal(bytes, wxRsp)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return wxRsp, nil
  63. }
  64. //查询订单
  65. func (this weChatClient) QueryOrder() {
  66. }
  67. //关闭订单
  68. func (this weChatClient) CloseOrder() {
  69. }
  70. //申请退款
  71. func (this weChatClient) Refund() {
  72. }
  73. //查询退款
  74. func (this weChatClient) QueryRefund() {
  75. }
  76. //下载对账单
  77. func (this weChatClient) DownloadBill() {
  78. }
  79. //下载资金账单
  80. func (this weChatClient) DownloadFundFlow() {
  81. }
  82. //拉取订单评价数据
  83. func (this weChatClient) BatchQueryComment() {
  84. }