client.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. package wechat
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "encoding/xml"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "sync"
  10. "github.com/iGoogle-ink/gopay"
  11. )
  12. type Client struct {
  13. AppId string
  14. MchId string
  15. ApiKey string
  16. BaseURL string
  17. IsProd bool
  18. certificate tls.Certificate
  19. certPool *x509.CertPool
  20. mu sync.RWMutex
  21. }
  22. // 初始化微信客户端
  23. // appId:应用ID
  24. // mchId:商户ID
  25. // ApiKey:API秘钥值
  26. // IsProd:是否是正式环境
  27. func NewClient(appId, mchId, apiKey string, isProd bool) (client *Client) {
  28. return &Client{
  29. AppId: appId,
  30. MchId: mchId,
  31. ApiKey: apiKey,
  32. IsProd: isProd}
  33. }
  34. // 提交付款码支付
  35. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
  36. func (w *Client) Micropay(bm gopay.BodyMap) (wxRsp *MicropayResponse, err error) {
  37. err = bm.CheckEmptyError("nonce_str", "body", "out_trade_no", "total_fee", "spbill_create_ip", "auth_code")
  38. if err != nil {
  39. return nil, err
  40. }
  41. var bs []byte
  42. if w.IsProd {
  43. bs, err = w.doProdPost(bm, microPay, nil)
  44. } else {
  45. bm.Set("total_fee", 1)
  46. bs, err = w.doSanBoxPost(bm, sandboxMicroPay)
  47. }
  48. if err != nil {
  49. return nil, err
  50. }
  51. wxRsp = new(MicropayResponse)
  52. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  53. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  54. }
  55. return wxRsp, nil
  56. }
  57. // 授权码查询openid(正式)
  58. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9
  59. func (w *Client) AuthCodeToOpenId(bm gopay.BodyMap) (wxRsp *AuthCodeToOpenIdResponse, err error) {
  60. err = bm.CheckEmptyError("nonce_str", "auth_code")
  61. if err != nil {
  62. return nil, err
  63. }
  64. bs, err := w.doProdPost(bm, authCodeToOpenid, nil)
  65. if err != nil {
  66. return nil, err
  67. }
  68. wxRsp = new(AuthCodeToOpenIdResponse)
  69. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  70. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  71. }
  72. return wxRsp, nil
  73. }
  74. // 统一下单
  75. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
  76. func (w *Client) UnifiedOrder(bm gopay.BodyMap) (wxRsp *UnifiedOrderResponse, err error) {
  77. err = bm.CheckEmptyError("nonce_str", "body", "out_trade_no", "total_fee", "spbill_create_ip", "notify_url", "trade_type")
  78. if err != nil {
  79. return nil, err
  80. }
  81. var bs []byte
  82. if w.IsProd {
  83. bs, err = w.doProdPost(bm, unifiedOrder, nil)
  84. } else {
  85. bm.Set("total_fee", 101)
  86. bs, err = w.doSanBoxPost(bm, sandboxUnifiedOrder)
  87. }
  88. if err != nil {
  89. return nil, err
  90. }
  91. wxRsp = new(UnifiedOrderResponse)
  92. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  93. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  94. }
  95. return wxRsp, nil
  96. }
  97. // 查询订单
  98. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
  99. func (w *Client) QueryOrder(bm gopay.BodyMap) (wxRsp *QueryOrderResponse, err error) {
  100. err = bm.CheckEmptyError("nonce_str")
  101. if err != nil {
  102. return nil, err
  103. }
  104. if bm.Get("out_trade_no") == gopay.NULL && bm.Get("transaction_id") == gopay.NULL {
  105. return nil, errors.New("out_trade_no and transaction_id are not allowed to be null at the same time")
  106. }
  107. var bs []byte
  108. if w.IsProd {
  109. bs, err = w.doProdPost(bm, orderQuery, nil)
  110. } else {
  111. bs, err = w.doSanBoxPost(bm, sandboxOrderQuery)
  112. }
  113. if err != nil {
  114. return nil, err
  115. }
  116. wxRsp = new(QueryOrderResponse)
  117. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  118. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  119. }
  120. return wxRsp, nil
  121. }
  122. // 关闭订单
  123. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3
  124. func (w *Client) CloseOrder(bm gopay.BodyMap) (wxRsp *CloseOrderResponse, err error) {
  125. err = bm.CheckEmptyError("nonce_str", "out_trade_no")
  126. if err != nil {
  127. return nil, err
  128. }
  129. var bs []byte
  130. if w.IsProd {
  131. bs, err = w.doProdPost(bm, closeOrder, nil)
  132. } else {
  133. bs, err = w.doSanBoxPost(bm, sandboxCloseOrder)
  134. }
  135. if err != nil {
  136. return nil, err
  137. }
  138. wxRsp = new(CloseOrderResponse)
  139. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  140. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  141. }
  142. return wxRsp, nil
  143. }
  144. // 撤销订单
  145. // 注意:如已使用client.AddCertFilePath()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",否则,3证书Path均不可空
  146. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
  147. func (w *Client) Reverse(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *ReverseResponse, err error) {
  148. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  149. return nil, err
  150. }
  151. err = bm.CheckEmptyError("nonce_str", "out_trade_no")
  152. if err != nil {
  153. return nil, err
  154. }
  155. var (
  156. bs []byte
  157. tlsConfig *tls.Config
  158. )
  159. if w.IsProd {
  160. if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  161. return nil, err
  162. }
  163. bs, err = w.doProdPost(bm, reverse, tlsConfig)
  164. } else {
  165. bs, err = w.doSanBoxPost(bm, sandboxReverse)
  166. }
  167. if err != nil {
  168. return nil, err
  169. }
  170. wxRsp = new(ReverseResponse)
  171. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  172. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  173. }
  174. return wxRsp, nil
  175. }
  176. // 申请退款
  177. // 注意:如已使用client.AddCertFilePath()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",否则,3证书Path均不可空
  178. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
  179. func (w *Client) Refund(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *RefundResponse, err error) {
  180. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  181. return nil, err
  182. }
  183. err = bm.CheckEmptyError("nonce_str", "out_refund_no", "total_fee", "refund_fee")
  184. if err != nil {
  185. return nil, err
  186. }
  187. if bm.Get("out_trade_no") == gopay.NULL && bm.Get("transaction_id") == gopay.NULL {
  188. return nil, errors.New("out_trade_no and transaction_id are not allowed to be null at the same time")
  189. }
  190. var (
  191. bs []byte
  192. tlsConfig *tls.Config
  193. )
  194. if w.IsProd {
  195. if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  196. return nil, err
  197. }
  198. bs, err = w.doProdPost(bm, refund, tlsConfig)
  199. } else {
  200. bs, err = w.doSanBoxPost(bm, sandboxRefund)
  201. }
  202. if err != nil {
  203. return nil, err
  204. }
  205. wxRsp = new(RefundResponse)
  206. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  207. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  208. }
  209. return wxRsp, nil
  210. }
  211. // 查询退款
  212. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5
  213. func (w *Client) QueryRefund(bm gopay.BodyMap) (wxRsp *QueryRefundResponse, err error) {
  214. err = bm.CheckEmptyError("nonce_str")
  215. if err != nil {
  216. return nil, err
  217. }
  218. if bm.Get("refund_id") == gopay.NULL && bm.Get("out_refund_no") == gopay.NULL && bm.Get("transaction_id") == gopay.NULL && bm.Get("out_trade_no") == gopay.NULL {
  219. return nil, errors.New("refund_id, out_refund_no, out_trade_no, transaction_id are not allowed to be null at the same time")
  220. }
  221. var bs []byte
  222. if w.IsProd {
  223. bs, err = w.doProdPost(bm, refundQuery, nil)
  224. } else {
  225. bs, err = w.doSanBoxPost(bm, sandboxRefundQuery)
  226. }
  227. if err != nil {
  228. return nil, err
  229. }
  230. wxRsp = new(QueryRefundResponse)
  231. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  232. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  233. }
  234. return wxRsp, nil
  235. }
  236. // 下载对账单
  237. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6
  238. func (w *Client) DownloadBill(bm gopay.BodyMap) (wxRsp string, err error) {
  239. err = bm.CheckEmptyError("nonce_str", "bill_date", "bill_type")
  240. if err != nil {
  241. return gopay.NULL, err
  242. }
  243. billType := bm.Get("bill_type")
  244. if billType != "ALL" && billType != "SUCCESS" && billType != "REFUND" && billType != "RECHARGE_REFUND" {
  245. return gopay.NULL, errors.New("bill_type error, please reference: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6")
  246. }
  247. var bs []byte
  248. if w.IsProd {
  249. bs, err = w.doProdPost(bm, downloadBill, nil)
  250. } else {
  251. bs, err = w.doSanBoxPost(bm, sandboxDownloadBill)
  252. }
  253. if err != nil {
  254. return gopay.NULL, err
  255. }
  256. return string(bs), nil
  257. }
  258. // 下载资金账单(正式)
  259. // 注意:如已使用client.AddCertFilePath()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",否则,3证书Path均不可空
  260. // 貌似不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
  261. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18&index=7
  262. func (w *Client) DownloadFundFlow(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp string, err error) {
  263. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  264. return gopay.NULL, err
  265. }
  266. err = bm.CheckEmptyError("nonce_str", "bill_date", "account_type")
  267. if err != nil {
  268. return gopay.NULL, err
  269. }
  270. accountType := bm.Get("account_type")
  271. if accountType != "Basic" && accountType != "Operation" && accountType != "Fees" {
  272. return gopay.NULL, errors.New("account_type error, please reference: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18&index=7")
  273. }
  274. bm.Set("sign_type", SignType_HMAC_SHA256)
  275. tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
  276. if err != nil {
  277. return gopay.NULL, err
  278. }
  279. bs, err := w.doProdPost(bm, downloadFundFlow, tlsConfig)
  280. if err != nil {
  281. return gopay.NULL, err
  282. }
  283. wxRsp = string(bs)
  284. return
  285. }
  286. // 交易保障
  287. // 文档地址:(JSAPI)https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_8&index=9
  288. // 文档地址:(付款码)https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_14&index=8
  289. // 文档地址:(Native)https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_8&index=9
  290. // 文档地址:(APP)https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_8&index=10
  291. // 文档地址:(H5)https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_8&index=9
  292. // 文档地址:(微信小程序)https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_8&index=9
  293. func (w *Client) Report(bm gopay.BodyMap) (wxRsp *ReportResponse, err error) {
  294. err = bm.CheckEmptyError("nonce_str", "interface_url", "execute_time", "return_code", "return_msg", "result_code", "user_ip")
  295. if err != nil {
  296. return nil, err
  297. }
  298. var bs []byte
  299. if w.IsProd {
  300. bs, err = w.doProdPost(bm, report, nil)
  301. } else {
  302. bs, err = w.doSanBoxPost(bm, sandboxReport)
  303. }
  304. if err != nil {
  305. return nil, err
  306. }
  307. wxRsp = new(ReportResponse)
  308. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  309. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  310. }
  311. return wxRsp, nil
  312. }
  313. // 拉取订单评价数据(正式)
  314. // 注意:如已使用client.AddCertFilePath()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传空字符串 "",否则,3证书Path均不可空
  315. // 貌似不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
  316. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=11
  317. func (w *Client) BatchQueryComment(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp string, err error) {
  318. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  319. return gopay.NULL, err
  320. }
  321. err = bm.CheckEmptyError("nonce_str", "begin_time", "end_time", "offset")
  322. if err != nil {
  323. return gopay.NULL, err
  324. }
  325. bm.Set("sign_type", SignType_HMAC_SHA256)
  326. tlsConfig, err := w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath)
  327. if err != nil {
  328. return gopay.NULL, err
  329. }
  330. bs, err := w.doProdPost(bm, batchQueryComment, tlsConfig)
  331. if err != nil {
  332. return gopay.NULL, err
  333. }
  334. return string(bs), nil
  335. }
  336. // 企业向微信用户个人付款(正式)
  337. // 注意:如已使用client.AddCertFilePath()添加过证书,参数certFilePath、keyFilePath、pkcs12FilePath全传 nil,否则3证书Path均不可为nil(string类型)
  338. // 注意:此方法未支持沙箱环境,默认正式环境,转账请慎重
  339. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
  340. func (w *Client) Transfer(bm gopay.BodyMap, certFilePath, keyFilePath, pkcs12FilePath interface{}) (wxRsp *TransfersResponse, err error) {
  341. if err = checkCertFilePath(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  342. return nil, err
  343. }
  344. if err = bm.CheckEmptyError("nonce_str", "partner_trade_no", "openid", "check_name", "amount", "desc", "spbill_create_ip"); err != nil {
  345. return nil, err
  346. }
  347. bm.Set("mch_appid", w.AppId)
  348. bm.Set("mchid", w.MchId)
  349. var (
  350. tlsConfig *tls.Config
  351. url = baseUrlCh + transfers
  352. )
  353. if tlsConfig, err = w.addCertConfig(certFilePath, keyFilePath, pkcs12FilePath); err != nil {
  354. return nil, err
  355. }
  356. bm.Set("sign", getReleaseSign(w.ApiKey, SignType_MD5, bm))
  357. httpClient := gopay.NewHttpClient().SetTLSConfig(tlsConfig).Type(gopay.TypeXML)
  358. if w.BaseURL != gopay.NULL {
  359. w.mu.RLock()
  360. url = w.BaseURL + transfers
  361. w.mu.RUnlock()
  362. }
  363. wxRsp = new(TransfersResponse)
  364. res, errs := httpClient.Post(url).SendString(generateXml(bm)).EndStruct(wxRsp)
  365. if len(errs) > 0 {
  366. return nil, errs[0]
  367. }
  368. if res.StatusCode != 200 {
  369. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  370. }
  371. return wxRsp, nil
  372. }
  373. // 公众号纯签约(正式)
  374. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/pap.php?chapter=18_1&index=1
  375. func (w *Client) EntrustPublic(bm gopay.BodyMap) (wxRsp *EntrustPublicResponse, err error) {
  376. err = bm.CheckEmptyError("plan_id", "contract_code", "request_serial", "contract_display_account", "notify_url", "version", "timestamp")
  377. if err != nil {
  378. return nil, err
  379. }
  380. bs, err := w.doProdGet(bm, entrustPublic, SignType_MD5)
  381. if err != nil {
  382. return nil, err
  383. }
  384. wxRsp = new(EntrustPublicResponse)
  385. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  386. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  387. }
  388. return wxRsp, nil
  389. }
  390. // APP纯签约-预签约接口-获取预签约ID(正式)
  391. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/pap.php?chapter=18_5&index=2
  392. func (w *Client) EntrustAppPre(bm gopay.BodyMap) (wxRsp *EntrustAppPreResponse, err error) {
  393. err = bm.CheckEmptyError("plan_id", "contract_code", "request_serial", "contract_display_account", "notify_url", "version", "timestamp")
  394. if err != nil {
  395. return nil, err
  396. }
  397. bs, err := w.doProdPost(bm, entrustApp, nil)
  398. if err != nil {
  399. return nil, err
  400. }
  401. wxRsp = new(EntrustAppPreResponse)
  402. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  403. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  404. }
  405. return wxRsp, nil
  406. }
  407. // H5纯签约(正式)
  408. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/pap.php?chapter=18_16&index=4
  409. func (w *Client) EntrustH5(bm gopay.BodyMap) (wxRsp *EntrustH5Response, err error) {
  410. err = bm.CheckEmptyError("plan_id", "contract_code", "request_serial", "contract_display_account", "notify_url", "version", "timestamp", "clientip")
  411. if err != nil {
  412. return nil, err
  413. }
  414. bs, err := w.doProdGet(bm, entrustH5, SignType_HMAC_SHA256)
  415. if err != nil {
  416. return nil, err
  417. }
  418. wxRsp = new(EntrustH5Response)
  419. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  420. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  421. }
  422. return wxRsp, nil
  423. }
  424. // 支付中签约(正式)
  425. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/pap.php?chapter=18_13&index=5
  426. func (w *Client) EntrustPaying(bm gopay.BodyMap) (wxRsp *EntrustPayingResponse, err error) {
  427. err = bm.CheckEmptyError("contract_mchid", "contract_appid",
  428. "out_trade_no", "nonce_str", "body", "notify_url", "total_fee",
  429. "spbill_create_ip", "trade_type", "plan_id", "contract_code",
  430. "request_serial", "contract_display_account", "contract_notify_url")
  431. if err != nil {
  432. return nil, err
  433. }
  434. bs, err := w.doProdPost(bm, entrustPaying, nil)
  435. if err != nil {
  436. return nil, err
  437. }
  438. wxRsp = new(EntrustPayingResponse)
  439. if err = xml.Unmarshal(bs, wxRsp); err != nil {
  440. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(bs), err)
  441. }
  442. return wxRsp, nil
  443. }
  444. // doSanBoxPost sanbox环境post请求
  445. func (w *Client) doSanBoxPost(bm gopay.BodyMap, path string) (bs []byte, err error) {
  446. var url = baseUrlCh + path
  447. w.mu.RLock()
  448. defer w.mu.RUnlock()
  449. bm.Set("appid", w.AppId)
  450. bm.Set("mch_id", w.MchId)
  451. if bm.Get("sign") == gopay.NULL {
  452. bm.Set("sign_type", SignType_MD5)
  453. sign, err := getSignBoxSign(w.MchId, w.ApiKey, bm)
  454. if err != nil {
  455. return nil, err
  456. }
  457. bm.Set("sign", sign)
  458. }
  459. if w.BaseURL != gopay.NULL {
  460. url = w.BaseURL + path
  461. }
  462. res, bs, errs := gopay.NewHttpClient().Type(gopay.TypeXML).Post(url).SendString(generateXml(bm)).EndBytes()
  463. if len(errs) > 0 {
  464. return nil, errs[0]
  465. }
  466. if res.StatusCode != 200 {
  467. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  468. }
  469. if strings.Contains(string(bs), "HTML") || strings.Contains(string(bs), "html") {
  470. return nil, errors.New(string(bs))
  471. }
  472. return bs, nil
  473. }
  474. // Post请求、正式
  475. func (w *Client) doProdPost(bm gopay.BodyMap, path string, tlsConfig *tls.Config) (bs []byte, err error) {
  476. var url = baseUrlCh + path
  477. w.mu.RLock()
  478. defer w.mu.RUnlock()
  479. bm.Set("appid", w.AppId)
  480. bm.Set("mch_id", w.MchId)
  481. if bm.Get("sign") == gopay.NULL {
  482. sign := getReleaseSign(w.ApiKey, bm.Get("sign_type"), bm)
  483. bm.Set("sign", sign)
  484. }
  485. httpClient := gopay.NewHttpClient()
  486. if w.IsProd && tlsConfig != nil {
  487. httpClient.SetTLSConfig(tlsConfig)
  488. }
  489. if w.BaseURL != gopay.NULL {
  490. url = w.BaseURL + path
  491. }
  492. res, bs, errs := httpClient.Type(gopay.TypeXML).Post(url).SendString(generateXml(bm)).EndBytes()
  493. if len(errs) > 0 {
  494. return nil, errs[0]
  495. }
  496. if res.StatusCode != 200 {
  497. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  498. }
  499. if strings.Contains(string(bs), "HTML") || strings.Contains(string(bs), "html") {
  500. return nil, errors.New(string(bs))
  501. }
  502. return bs, nil
  503. }
  504. // Get请求、正式
  505. func (w *Client) doProdGet(bm gopay.BodyMap, path, signType string) (bs []byte, err error) {
  506. var url = baseUrlCh + path
  507. w.mu.RLock()
  508. defer w.mu.RUnlock()
  509. bm.Set("appid", w.AppId)
  510. bm.Set("mch_id", w.MchId)
  511. bm.Remove("sign")
  512. sign := getReleaseSign(w.ApiKey, signType, bm)
  513. bm.Set("sign", sign)
  514. if w.BaseURL != gopay.NULL {
  515. w.mu.RLock()
  516. url = w.BaseURL + path
  517. w.mu.RUnlock()
  518. }
  519. param := bm.EncodeGetParams()
  520. url = url + "?" + param
  521. res, bs, errs := gopay.NewHttpClient().Get(url).EndBytes()
  522. if len(errs) > 0 {
  523. return nil, errs[0]
  524. }
  525. if res.StatusCode != 200 {
  526. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  527. }
  528. if strings.Contains(string(bs), "HTML") || strings.Contains(string(bs), "html") {
  529. return nil, errors.New(string(bs))
  530. }
  531. return bs, nil
  532. }