virtualpayment.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Copyright silenceper/wechat Author(https://silenceper.com/wechat/). All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * You can obtain one at https://github.com/silenceper/wechat.
  17. *
  18. */
  19. package virtualpayment
  20. import (
  21. "context"
  22. "crypto/hmac"
  23. "crypto/sha256"
  24. "encoding/hex"
  25. "encoding/json"
  26. "errors"
  27. "strings"
  28. "github.com/silenceper/wechat/v2/util"
  29. )
  30. // SetSessionKey 设置 sessionKey
  31. func (s *VirtualPayment) SetSessionKey(sessionKey string) {
  32. s.sessionKey = sessionKey
  33. }
  34. // QueryUserBalance 查询虚拟支付余额
  35. func (s *VirtualPayment) QueryUserBalance(ctx context.Context, in *QueryUserBalanceRequest) (out QueryUserBalanceResponse, err error) {
  36. var jsonByte []byte
  37. if jsonByte, err = json.Marshal(in); err != nil {
  38. return
  39. }
  40. var (
  41. params = URLParams{
  42. Path: queryUserBalance,
  43. Content: string(jsonByte),
  44. }
  45. address string
  46. )
  47. if address, err = s.requestAddress(params); err != nil {
  48. return
  49. }
  50. var response []byte
  51. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  52. return
  53. }
  54. // 使用通用方法返回错误
  55. err = util.DecodeWithError(response, &out, "QueryUserBalance")
  56. return
  57. }
  58. // CurrencyPay currency pay 扣减代币(一般用于代币支付)
  59. func (s *VirtualPayment) CurrencyPay(ctx context.Context, in *CurrencyPayRequest) (out CurrencyPayResponse, err error) {
  60. var jsonByte []byte
  61. if jsonByte, err = json.Marshal(in); err != nil {
  62. return
  63. }
  64. var (
  65. params = URLParams{
  66. Path: currencyPay,
  67. Content: string(jsonByte),
  68. }
  69. address string
  70. )
  71. if address, err = s.requestAddress(params); err != nil {
  72. return
  73. }
  74. var response []byte
  75. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  76. return
  77. }
  78. // 使用通用方法返回错误
  79. err = util.DecodeWithError(response, &out, "CurrencyPay")
  80. return
  81. }
  82. // QueryOrder 查询创建的订单(现金单,非代币单)
  83. func (s *VirtualPayment) QueryOrder(ctx context.Context, in *QueryOrderRequest) (out QueryOrderResponse, err error) {
  84. var jsonByte []byte
  85. if jsonByte, err = json.Marshal(in); err != nil {
  86. return
  87. }
  88. var (
  89. params = URLParams{
  90. Path: queryOrder,
  91. Signature: EmptyString,
  92. Content: string(jsonByte),
  93. }
  94. address string
  95. )
  96. if address, err = s.requestAddress(params); err != nil {
  97. return
  98. }
  99. var response []byte
  100. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  101. return
  102. }
  103. // 使用通用方法返回错误
  104. err = util.DecodeWithError(response, &out, "QueryOrder")
  105. return
  106. }
  107. // CancelCurrencyPay 取消订单 代币支付退款 (currency_pay 接口的逆操作)
  108. func (s *VirtualPayment) CancelCurrencyPay(ctx context.Context, in *CancelCurrencyPayRequest) (out CancelCurrencyPayResponse, err error) {
  109. var jsonByte []byte
  110. if jsonByte, err = json.Marshal(in); err != nil {
  111. return
  112. }
  113. var (
  114. params = URLParams{
  115. Path: cancelCurrencyPay,
  116. Content: string(jsonByte),
  117. }
  118. address string
  119. )
  120. if address, err = s.requestAddress(params); err != nil {
  121. return
  122. }
  123. var response []byte
  124. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  125. return
  126. }
  127. // 使用通用方法返回错误
  128. err = util.DecodeWithError(response, &out, "CancelCurrencyPay")
  129. return
  130. }
  131. // NotifyProvideGoods 通知发货
  132. // 通知已经发货完成(只能通知现金单),正常通过 xpay_goods_deliver_notify 消息推送返回成功就不需要调用这个 api 接口。这个接口用于异常情况推送不成功时手动将单改成已发货状态
  133. func (s *VirtualPayment) NotifyProvideGoods(ctx context.Context, in *NotifyProvideGoodsRequest) (out NotifyProvideGoodsResponse, err error) {
  134. var jsonByte []byte
  135. if jsonByte, err = json.Marshal(in); err != nil {
  136. return
  137. }
  138. var (
  139. params = URLParams{
  140. Path: notifyProvideGoods,
  141. Content: string(jsonByte),
  142. Signature: EmptyString,
  143. }
  144. address string
  145. )
  146. if address, err = s.requestAddress(params); err != nil {
  147. return
  148. }
  149. var response []byte
  150. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  151. return
  152. }
  153. // 使用通用方法返回错误
  154. err = util.DecodeWithError(response, &out, "NotifyProvideGoods")
  155. return
  156. }
  157. // PresentCurrency 代币赠送接口,由于目前不支付按单号查赠送单的功能,所以当需要赠送的时候可以一直重试到返回 0 或者返回 268490004(重复操作)为止
  158. func (s *VirtualPayment) PresentCurrency(ctx context.Context, in *PresentCurrencyRequest) (out PresentCurrencyResponse, err error) {
  159. var jsonByte []byte
  160. if jsonByte, err = json.Marshal(in); err != nil {
  161. return
  162. }
  163. var (
  164. params = URLParams{
  165. Path: presentCurrency,
  166. Content: string(jsonByte),
  167. Signature: EmptyString,
  168. }
  169. address string
  170. )
  171. if address, err = s.requestAddress(params); err != nil {
  172. return
  173. }
  174. var response []byte
  175. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  176. return
  177. }
  178. // 使用通用方法返回错误
  179. err = util.DecodeWithError(response, &out, "PresentCurrency")
  180. return
  181. }
  182. // DownloadBill 下载订单交易账单
  183. func (s *VirtualPayment) DownloadBill(ctx context.Context, in *DownloadBillRequest) (out DownloadBillResponse, err error) {
  184. var jsonByte []byte
  185. if jsonByte, err = json.Marshal(in); err != nil {
  186. return
  187. }
  188. var (
  189. params = URLParams{
  190. Path: downloadBill,
  191. Content: string(jsonByte),
  192. Signature: EmptyString,
  193. }
  194. address string
  195. )
  196. if address, err = s.requestAddress(params); err != nil {
  197. return
  198. }
  199. var response []byte
  200. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  201. return
  202. }
  203. // 使用通用方法返回错误
  204. err = util.DecodeWithError(response, &out, "DownloadBill")
  205. return
  206. }
  207. // RefundOrder 退款 对使用 jsapi 接口下的单进行退款
  208. func (s *VirtualPayment) RefundOrder(ctx context.Context, in *RefundOrderRequest) (out RefundOrderResponse, err error) {
  209. var jsonByte []byte
  210. if jsonByte, err = json.Marshal(in); err != nil {
  211. return
  212. }
  213. var (
  214. params = URLParams{
  215. Path: refundOrder,
  216. Content: string(jsonByte),
  217. Signature: EmptyString,
  218. }
  219. address string
  220. )
  221. if address, err = s.requestAddress(params); err != nil {
  222. return
  223. }
  224. var response []byte
  225. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  226. return
  227. }
  228. // 使用通用方法返回错误
  229. err = util.DecodeWithError(response, &out, "RefundOrder")
  230. return
  231. }
  232. // CreateWithdrawOrder 创建提现单
  233. func (s *VirtualPayment) CreateWithdrawOrder(ctx context.Context, in *CreateWithdrawOrderRequest) (out CreateWithdrawOrderResponse, err error) {
  234. var jsonByte []byte
  235. if jsonByte, err = json.Marshal(in); err != nil {
  236. return
  237. }
  238. var (
  239. params = URLParams{
  240. Path: createWithdrawOrder,
  241. Content: string(jsonByte),
  242. Signature: EmptyString,
  243. }
  244. address string
  245. )
  246. if address, err = s.requestAddress(params); err != nil {
  247. return
  248. }
  249. var response []byte
  250. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  251. return
  252. }
  253. // 使用通用方法返回错误
  254. err = util.DecodeWithError(response, &out, "CreateWithdrawOrder")
  255. return
  256. }
  257. // QueryWithdrawOrder 查询提现单
  258. func (s *VirtualPayment) QueryWithdrawOrder(ctx context.Context, in *QueryWithdrawOrderRequest) (out QueryWithdrawOrderResponse, err error) {
  259. var jsonByte []byte
  260. if jsonByte, err = json.Marshal(in); err != nil {
  261. return
  262. }
  263. var (
  264. params = URLParams{
  265. Path: queryWithdrawOrder,
  266. Content: string(jsonByte),
  267. Signature: EmptyString,
  268. }
  269. address string
  270. )
  271. if address, err = s.requestAddress(params); err != nil {
  272. return
  273. }
  274. var response []byte
  275. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  276. return
  277. }
  278. // 使用通用方法返回错误
  279. err = util.DecodeWithError(response, &out, "QueryWithdrawOrder")
  280. return
  281. }
  282. // StartUploadGoods 开始上传商品
  283. func (s *VirtualPayment) StartUploadGoods(ctx context.Context, in *StartUploadGoodsRequest) (out StartUploadGoodsResponse, err error) {
  284. var jsonByte []byte
  285. if jsonByte, err = json.Marshal(in); err != nil {
  286. return
  287. }
  288. var (
  289. params = URLParams{
  290. Path: startUploadGoods,
  291. Content: string(jsonByte),
  292. Signature: EmptyString,
  293. }
  294. address string
  295. )
  296. if address, err = s.requestAddress(params); err != nil {
  297. return
  298. }
  299. var response []byte
  300. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  301. return
  302. }
  303. // 使用通用方法返回错误
  304. err = util.DecodeWithError(response, &out, "StartUploadGoods")
  305. return
  306. }
  307. // QueryUploadGoods 查询上传商品
  308. func (s *VirtualPayment) QueryUploadGoods(ctx context.Context, in *QueryUploadGoodsRequest) (out QueryUploadGoodsResponse, err error) {
  309. var jsonByte []byte
  310. if jsonByte, err = json.Marshal(in); err != nil {
  311. return
  312. }
  313. var (
  314. params = URLParams{
  315. Path: queryUploadGoods,
  316. Content: string(jsonByte),
  317. Signature: EmptyString,
  318. }
  319. address string
  320. )
  321. if address, err = s.requestAddress(params); err != nil {
  322. return
  323. }
  324. var response []byte
  325. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  326. return
  327. }
  328. // 使用通用方法返回错误
  329. err = util.DecodeWithError(response, &out, "QueryUploadGoods")
  330. return
  331. }
  332. // StartPublishGoods 开始发布商品
  333. func (s *VirtualPayment) StartPublishGoods(ctx context.Context, in *StartPublishGoodsRequest) (out StartPublishGoodsResponse, err error) {
  334. var jsonByte []byte
  335. if jsonByte, err = json.Marshal(in); err != nil {
  336. return
  337. }
  338. var (
  339. params = URLParams{
  340. Path: startPublishGoods,
  341. Content: string(jsonByte),
  342. Signature: EmptyString,
  343. }
  344. address string
  345. )
  346. if address, err = s.requestAddress(params); err != nil {
  347. return
  348. }
  349. var response []byte
  350. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  351. return
  352. }
  353. // 使用通用方法返回错误
  354. err = util.DecodeWithError(response, &out, "StartPublishGoods")
  355. return
  356. }
  357. // QueryPublishGoods 查询发布商品
  358. func (s *VirtualPayment) QueryPublishGoods(ctx context.Context, in *QueryPublishGoodsRequest) (out QueryPublishGoodsResponse, err error) {
  359. var jsonByte []byte
  360. if jsonByte, err = json.Marshal(in); err != nil {
  361. return
  362. }
  363. var (
  364. params = URLParams{
  365. Path: queryPublishGoods,
  366. Content: string(jsonByte),
  367. Signature: EmptyString,
  368. }
  369. address string
  370. )
  371. if address, err = s.requestAddress(params); err != nil {
  372. return
  373. }
  374. var response []byte
  375. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  376. return
  377. }
  378. // 使用通用方法返回错误
  379. err = util.DecodeWithError(response, &out, "QueryPublishGoods")
  380. return
  381. }
  382. // StartDownloadOrder 发起下载小程序订单明细任务
  383. func (s *VirtualPayment) StartDownloadOrder(ctx context.Context, in *StartDownloadOrderRequest) (out StartDownloadOrderResponse, err error) {
  384. var jsonByte []byte
  385. if jsonByte, err = json.Marshal(in); err != nil {
  386. return
  387. }
  388. var (
  389. params = URLParams{
  390. Path: startDownloadOrder,
  391. Content: string(jsonByte),
  392. Signature: EmptyString,
  393. }
  394. address string
  395. )
  396. if address, err = s.requestAddress(params); err != nil {
  397. return
  398. }
  399. var response []byte
  400. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  401. return
  402. }
  403. // 使用通用方法返回错误
  404. err = util.DecodeWithError(response, &out, "StartDownloadOrder")
  405. return
  406. }
  407. // QueryDownloadOrder 查询下载订单任务结果
  408. func (s *VirtualPayment) QueryDownloadOrder(ctx context.Context, in *QueryDownloadOrderRequest) (out QueryDownloadOrderResponse, err error) {
  409. var jsonByte []byte
  410. if jsonByte, err = json.Marshal(in); err != nil {
  411. return
  412. }
  413. var (
  414. params = URLParams{
  415. Path: queryDownloadOrder,
  416. Content: string(jsonByte),
  417. Signature: EmptyString,
  418. }
  419. address string
  420. )
  421. if address, err = s.requestAddress(params); err != nil {
  422. return
  423. }
  424. var response []byte
  425. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  426. return
  427. }
  428. // 使用通用方法返回错误
  429. err = util.DecodeWithError(response, &out, "QueryDownloadOrder")
  430. return
  431. }
  432. // QueryBizBalance 查询商家账户可提现余额
  433. func (s *VirtualPayment) QueryBizBalance(ctx context.Context, in *QueryBizBalanceRequest) (out QueryBizBalanceResponse, err error) {
  434. var jsonByte []byte
  435. if jsonByte, err = json.Marshal(in); err != nil {
  436. return
  437. }
  438. var (
  439. params = URLParams{
  440. Path: queryBizBalance,
  441. Content: string(jsonByte),
  442. Signature: EmptyString,
  443. }
  444. address string
  445. )
  446. if address, err = s.requestAddress(params); err != nil {
  447. return
  448. }
  449. var response []byte
  450. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  451. return
  452. }
  453. // 使用通用方法返回错误
  454. err = util.DecodeWithError(response, &out, "QueryBizBalance")
  455. return
  456. }
  457. // QueryTransferAccount 查询广告金充值账户
  458. func (s *VirtualPayment) QueryTransferAccount(ctx context.Context, in *QueryTransferAccountRequest) (out QueryTransferAccountResponse, err error) {
  459. var jsonByte []byte
  460. if jsonByte, err = json.Marshal(in); err != nil {
  461. return
  462. }
  463. var (
  464. params = URLParams{
  465. Path: queryTransferAccount,
  466. Content: string(jsonByte),
  467. Signature: EmptyString,
  468. }
  469. address string
  470. )
  471. if address, err = s.requestAddress(params); err != nil {
  472. return
  473. }
  474. var response []byte
  475. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  476. return
  477. }
  478. // 使用通用方法返回错误
  479. err = util.DecodeWithError(response, &out, "QueryTransferAccount")
  480. return
  481. }
  482. // QueryAdverFunds 查询广告金发放记录
  483. func (s *VirtualPayment) QueryAdverFunds(ctx context.Context, in *QueryAdverFundsRequest) (out QueryAdverFundsResponse, err error) {
  484. var jsonByte []byte
  485. if jsonByte, err = json.Marshal(in); err != nil {
  486. return
  487. }
  488. var (
  489. params = URLParams{
  490. Path: queryAdverFunds,
  491. Content: string(jsonByte),
  492. Signature: EmptyString,
  493. }
  494. address string
  495. )
  496. if address, err = s.requestAddress(params); err != nil {
  497. return
  498. }
  499. var response []byte
  500. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  501. return
  502. }
  503. // 使用通用方法返回错误
  504. err = util.DecodeWithError(response, &out, "QueryAdverFunds")
  505. return
  506. }
  507. // CreateFundsBill 充值广告金
  508. func (s *VirtualPayment) CreateFundsBill(ctx context.Context, in *CreateFundsBillRequest) (out CreateFundsBillResponse, err error) {
  509. var jsonByte []byte
  510. if jsonByte, err = json.Marshal(in); err != nil {
  511. return
  512. }
  513. var (
  514. params = URLParams{
  515. Path: createFundsBill,
  516. Content: string(jsonByte),
  517. Signature: EmptyString,
  518. }
  519. address string
  520. )
  521. if address, err = s.requestAddress(params); err != nil {
  522. return
  523. }
  524. var response []byte
  525. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  526. return
  527. }
  528. // 使用通用方法返回错误
  529. err = util.DecodeWithError(response, &out, "CreateFundsBill")
  530. return
  531. }
  532. // BindTransferAccount 绑定广告金充值账户
  533. func (s *VirtualPayment) BindTransferAccount(ctx context.Context, in *BindTransferAccountRequest) (out BindTransferAccountResponse, err error) {
  534. var jsonByte []byte
  535. if jsonByte, err = json.Marshal(in); err != nil {
  536. return
  537. }
  538. var (
  539. params = URLParams{
  540. Path: bindTransferAccount,
  541. Content: string(jsonByte),
  542. Signature: EmptyString,
  543. }
  544. address string
  545. )
  546. if address, err = s.requestAddress(params); err != nil {
  547. return
  548. }
  549. var response []byte
  550. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  551. return
  552. }
  553. // 使用通用方法返回错误
  554. err = util.DecodeWithError(response, &out, "BindTransferAccount")
  555. return
  556. }
  557. // QueryFundsBill 查询广告金充值记录
  558. func (s *VirtualPayment) QueryFundsBill(ctx context.Context, in *QueryFundsBillRequest) (out QueryFundsBillResponse, err error) {
  559. var jsonByte []byte
  560. if jsonByte, err = json.Marshal(in); err != nil {
  561. return
  562. }
  563. var (
  564. params = URLParams{
  565. Path: queryFundsBill,
  566. Content: string(jsonByte),
  567. Signature: EmptyString,
  568. }
  569. address string
  570. )
  571. if address, err = s.requestAddress(params); err != nil {
  572. return
  573. }
  574. var response []byte
  575. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  576. return
  577. }
  578. // 使用通用方法返回错误
  579. err = util.DecodeWithError(response, &out, "QueryFundsBill")
  580. return
  581. }
  582. // QueryRecoverBill 查询广告金回收记录
  583. func (s *VirtualPayment) QueryRecoverBill(ctx context.Context, in *QueryRecoverBillRequest) (out QueryRecoverBillResponse, err error) {
  584. var jsonByte []byte
  585. if jsonByte, err = json.Marshal(in); err != nil {
  586. return
  587. }
  588. var (
  589. params = URLParams{
  590. Path: queryRecoverBill,
  591. Content: string(jsonByte),
  592. Signature: EmptyString,
  593. }
  594. address string
  595. )
  596. if address, err = s.requestAddress(params); err != nil {
  597. return
  598. }
  599. var response []byte
  600. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  601. return
  602. }
  603. // 使用通用方法返回错误
  604. err = util.DecodeWithError(response, &out, "QueryRecoverBill")
  605. return
  606. }
  607. // DownloadAdverFundsOrder 下载广告金对应的商户订单信息
  608. func (s *VirtualPayment) DownloadAdverFundsOrder(ctx context.Context, in *DownloadAdverFundsOrderRequest) (out DownloadAdverFundsOrderResponse, err error) {
  609. var jsonByte []byte
  610. if jsonByte, err = json.Marshal(in); err != nil {
  611. return
  612. }
  613. var (
  614. params = URLParams{
  615. Path: downloadAdverFundsOrder,
  616. Content: string(jsonByte),
  617. Signature: EmptyString,
  618. }
  619. address string
  620. )
  621. if address, err = s.requestAddress(params); err != nil {
  622. return
  623. }
  624. var response []byte
  625. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  626. return
  627. }
  628. // 使用通用方法返回错误
  629. err = util.DecodeWithError(response, &out, "DownloadAdverFundsOrder")
  630. return
  631. }
  632. // GetComplaintList 获取投诉列表
  633. func (s *VirtualPayment) GetComplaintList(ctx context.Context, in *GetComplaintListRequest) (out GetComplaintListResponse, err error) {
  634. var jsonByte []byte
  635. if jsonByte, err = json.Marshal(in); err != nil {
  636. return
  637. }
  638. var (
  639. params = URLParams{
  640. Path: getComplaintList,
  641. Content: string(jsonByte),
  642. Signature: EmptyString,
  643. }
  644. address string
  645. )
  646. if address, err = s.requestAddress(params); err != nil {
  647. return
  648. }
  649. var response []byte
  650. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  651. return
  652. }
  653. // 使用通用方法返回错误
  654. err = util.DecodeWithError(response, &out, "GetComplaintList")
  655. return
  656. }
  657. // GetComplaintDetail 获取投诉详情
  658. func (s *VirtualPayment) GetComplaintDetail(ctx context.Context, in *GetComplaintDetailRequest) (out GetComplaintDetailResponse, err error) {
  659. var jsonByte []byte
  660. if jsonByte, err = json.Marshal(in); err != nil {
  661. return
  662. }
  663. var (
  664. params = URLParams{
  665. Path: getComplaintDetail,
  666. Content: string(jsonByte),
  667. Signature: EmptyString,
  668. }
  669. address string
  670. )
  671. if address, err = s.requestAddress(params); err != nil {
  672. return
  673. }
  674. var response []byte
  675. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  676. return
  677. }
  678. // 使用通用方法返回错误
  679. err = util.DecodeWithError(response, &out, "GetComplaintDetail")
  680. return
  681. }
  682. // GetNegotiationHistory 获取协商历史
  683. func (s *VirtualPayment) GetNegotiationHistory(ctx context.Context, in *GetNegotiationHistoryRequest) (out GetNegotiationHistoryResponse, err error) {
  684. var jsonByte []byte
  685. if jsonByte, err = json.Marshal(in); err != nil {
  686. return
  687. }
  688. var (
  689. params = URLParams{
  690. Path: getNegotiationHistory,
  691. Content: string(jsonByte),
  692. Signature: EmptyString,
  693. }
  694. address string
  695. )
  696. if address, err = s.requestAddress(params); err != nil {
  697. return
  698. }
  699. var response []byte
  700. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  701. return
  702. }
  703. // 使用通用方法返回错误
  704. err = util.DecodeWithError(response, &out, "GetNegotiationHistory")
  705. return
  706. }
  707. // ResponseComplaint 回复用户
  708. func (s *VirtualPayment) ResponseComplaint(ctx context.Context, in *ResponseComplaintRequest) (out ResponseComplaintResponse, err error) {
  709. var jsonByte []byte
  710. if jsonByte, err = json.Marshal(in); err != nil {
  711. return
  712. }
  713. var (
  714. params = URLParams{
  715. Path: responseComplaint,
  716. Content: string(jsonByte),
  717. Signature: EmptyString,
  718. }
  719. address string
  720. )
  721. if address, err = s.requestAddress(params); err != nil {
  722. return
  723. }
  724. var response []byte
  725. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  726. return
  727. }
  728. // 使用通用方法返回错误
  729. err = util.DecodeWithError(response, &out, "ResponseComplaint")
  730. return
  731. }
  732. // CompleteComplaint 完成投诉处理
  733. func (s *VirtualPayment) CompleteComplaint(ctx context.Context, in *CompleteComplaintRequest) (out CompleteComplaintResponse, err error) {
  734. var jsonByte []byte
  735. if jsonByte, err = json.Marshal(in); err != nil {
  736. return
  737. }
  738. var (
  739. params = URLParams{
  740. Path: completeComplaint,
  741. Content: string(jsonByte),
  742. Signature: EmptyString,
  743. }
  744. address string
  745. )
  746. if address, err = s.requestAddress(params); err != nil {
  747. return
  748. }
  749. var response []byte
  750. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  751. return
  752. }
  753. // 使用通用方法返回错误
  754. err = util.DecodeWithError(response, &out, "CompleteComplaint")
  755. return
  756. }
  757. // UploadVPFile 上传媒体文件
  758. func (s *VirtualPayment) UploadVPFile(ctx context.Context, in *UploadVPFileRequest) (out UploadVPFileResponse, err error) {
  759. var jsonByte []byte
  760. if jsonByte, err = json.Marshal(in); err != nil {
  761. return
  762. }
  763. var (
  764. params = URLParams{
  765. Path: uploadVPFile,
  766. Content: string(jsonByte),
  767. Signature: EmptyString,
  768. }
  769. address string
  770. )
  771. if address, err = s.requestAddress(params); err != nil {
  772. return
  773. }
  774. var response []byte
  775. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  776. return
  777. }
  778. // 使用通用方法返回错误
  779. err = util.DecodeWithError(response, &out, "UploadVPFile")
  780. return
  781. }
  782. // GetUploadFileSign 获取微信支付反馈投诉图片的签名头部
  783. func (s *VirtualPayment) GetUploadFileSign(ctx context.Context, in *GetUploadFileSignRequest) (out GetUploadFileSignResponse, err error) {
  784. var jsonByte []byte
  785. if jsonByte, err = json.Marshal(in); err != nil {
  786. return
  787. }
  788. var (
  789. params = URLParams{
  790. Path: getUploadFileSign,
  791. Content: string(jsonByte),
  792. Signature: EmptyString,
  793. }
  794. address string
  795. )
  796. if address, err = s.requestAddress(params); err != nil {
  797. return
  798. }
  799. var response []byte
  800. if response, err = postJSONBytesContext(ctx, address, jsonByte); err != nil {
  801. return
  802. }
  803. // 使用通用方法返回错误
  804. err = util.DecodeWithError(response, &out, "GetUploadFileSign")
  805. return
  806. }
  807. // hmacSha256 hmac sha256
  808. func (s *VirtualPayment) hmacSha256(key, data string) string {
  809. h := hmac.New(sha256.New, []byte(key))
  810. h.Write([]byte(data))
  811. return hex.EncodeToString(h.Sum(nil))
  812. }
  813. // postJSONBytesContext POST JSON bytes with context
  814. func postJSONBytesContext(ctx context.Context, address string, jsonByte []byte) ([]byte, error) {
  815. return util.HTTPPostContext(ctx, address, jsonByte, map[string]string{
  816. "Content-Type": "application/json;charset=utf-8",
  817. })
  818. }
  819. // PaySign pay sign
  820. func (s *VirtualPayment) PaySign(url, data string) (string, error) {
  821. if strings.TrimSpace(s.ctx.Config.AppKey) == "" {
  822. return "", errors.New("appKey is empty")
  823. }
  824. return s.hmacSha256(s.ctx.Config.AppKey, url+"&"+data), nil
  825. }
  826. // Signature user signature
  827. func (s *VirtualPayment) Signature(data string) (string, error) {
  828. if strings.TrimSpace(s.sessionKey) == "" {
  829. return "", errors.New("sessionKey is empty")
  830. }
  831. return s.hmacSha256(s.sessionKey, data), nil
  832. }
  833. // PaySignature pay sign and signature
  834. func (s *VirtualPayment) PaySignature(url, data string) (paySign, signature string, err error) {
  835. if paySign, err = s.PaySign(url, data); err != nil {
  836. return
  837. }
  838. if signature, err = s.Signature(data); err != nil {
  839. return
  840. }
  841. return
  842. }
  843. // requestURL .组合 URL
  844. func (s *VirtualPayment) requestAddress(params URLParams) (url string, err error) {
  845. switch params.Path {
  846. case queryUserBalance, currencyPay, cancelCurrencyPay:
  847. if params.PaySign, params.Signature, err = s.PaySignature(params.Path, params.Content); err != nil {
  848. return
  849. }
  850. case queryOrder, notifyProvideGoods, presentCurrency, downloadBill, refundOrder,
  851. createWithdrawOrder, queryWithdrawOrder, startUploadGoods, queryUploadGoods,
  852. startPublishGoods, queryPublishGoods, startDownloadOrder, queryDownloadOrder,
  853. queryBizBalance, queryTransferAccount, queryAdverFunds, createFundsBill,
  854. bindTransferAccount, queryFundsBill, queryRecoverBill, downloadAdverFundsOrder,
  855. getComplaintList, getComplaintDetail, getNegotiationHistory, responseComplaint,
  856. completeComplaint, uploadVPFile, getUploadFileSign:
  857. if params.PaySign, err = s.PaySign(params.Path, params.Content); err != nil {
  858. return
  859. }
  860. default:
  861. err = errors.New("path is not exist")
  862. return
  863. }
  864. if params.AccessToken, err = s.ctx.GetAccessToken(); err != nil {
  865. return
  866. }
  867. url = baseSite + params.Path + "?" + accessToken + "=" + params.AccessToken
  868. if params.PaySign != EmptyString {
  869. url += "&" + paySignature + "=" + params.PaySign
  870. }
  871. if params.Signature != EmptyString {
  872. url += "&" + signature + "=" + params.Signature
  873. }
  874. return
  875. }