alipay_client.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. package gopay
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "strings"
  8. "time"
  9. )
  10. type aliPayClient struct {
  11. AppId string
  12. privateKey string
  13. ReturnUrl string
  14. NotifyUrl string
  15. Charset string
  16. SignType string
  17. AppAuthToken string
  18. AuthToken string
  19. isProd bool
  20. }
  21. //初始化支付宝客户端
  22. // appId:应用ID
  23. // privateKey:应用私钥
  24. // isProd:是否是正式环境
  25. func NewAliPayClient(appId, privateKey string, isProd bool) (client *aliPayClient) {
  26. client = new(aliPayClient)
  27. client.AppId = appId
  28. client.privateKey = privateKey
  29. client.isProd = isProd
  30. return client
  31. }
  32. //alipay.trade.fastpay.refund.query(统一收单交易退款查询)
  33. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query
  34. func (this *aliPayClient) AliPayTradeFastPayRefundQuery(body BodyMap) (aliRsp *AliPayTradeFastpayRefundQueryResponse, err error) {
  35. var bytes []byte
  36. trade1 := body.Get("out_trade_no")
  37. trade2 := body.Get("trade_no")
  38. if trade1 == null && trade2 == null {
  39. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  40. }
  41. bytes, err = this.doAliPay(body, "alipay.trade.fastpay.refund.query")
  42. if err != nil {
  43. return nil, err
  44. }
  45. //log.Println("AliPayTradeFastPayRefundQuery::::", string(bytes))
  46. aliRsp = new(AliPayTradeFastpayRefundQueryResponse)
  47. err = json.Unmarshal(bytes, aliRsp)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if aliRsp.AliPayTradeFastpayRefundQueryResponse.Code != "10000" {
  52. info := aliRsp.AliPayTradeFastpayRefundQueryResponse
  53. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  54. }
  55. aliRsp.SignData = getSignData(bytes)
  56. return aliRsp, nil
  57. }
  58. //alipay.trade.order.settle(统一收单交易结算接口)
  59. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.order.settle
  60. func (this *aliPayClient) AliPayTradeOrderSettle(body BodyMap) (aliRsp *AliPayTradeOrderSettleResponse, err error) {
  61. var bytes []byte
  62. trade1 := body.Get("out_request_no")
  63. trade2 := body.Get("trade_no")
  64. if trade1 == null || trade2 == null {
  65. return nil, errors.New("out_request_no or trade_no are not allowed to be null")
  66. }
  67. bytes, err = this.doAliPay(body, "alipay.trade.order.settle")
  68. if err != nil {
  69. return nil, err
  70. }
  71. //log.Println("AliPayTradeOrderSettle::::", string(bytes))
  72. aliRsp = new(AliPayTradeOrderSettleResponse)
  73. err = json.Unmarshal(bytes, aliRsp)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if aliRsp.AliPayTradeOrderSettleResponse.Code != "10000" {
  78. info := aliRsp.AliPayTradeOrderSettleResponse
  79. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  80. }
  81. aliRsp.SignData = getSignData(bytes)
  82. return aliRsp, nil
  83. }
  84. //alipay.trade.create(统一收单交易创建接口)
  85. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.create
  86. func (this *aliPayClient) AliPayTradeCreate(body BodyMap) (aliRsp *AliPayTradeCreateResponse, err error) {
  87. var bytes []byte
  88. trade1 := body.Get("out_trade_no")
  89. trade2 := body.Get("buyer_id")
  90. if trade1 == null && trade2 == null {
  91. return nil, errors.New("out_trade_no and buyer_id are not allowed to be null at the same time")
  92. }
  93. bytes, err = this.doAliPay(body, "alipay.trade.create")
  94. if err != nil {
  95. return nil, err
  96. }
  97. //log.Println("AliPayTradeCreateResponse::::", string(bytes))
  98. aliRsp = new(AliPayTradeCreateResponse)
  99. err = json.Unmarshal(bytes, aliRsp)
  100. if err != nil {
  101. return nil, err
  102. }
  103. if aliRsp.AliPayTradeCreateResponse.Code != "10000" {
  104. info := aliRsp.AliPayTradeCreateResponse
  105. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  106. }
  107. aliRsp.SignData = getSignData(bytes)
  108. return aliRsp, nil
  109. }
  110. //alipay.trade.close(统一收单交易关闭接口)
  111. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.close
  112. func (this *aliPayClient) AliPayTradeClose(body BodyMap) (aliRsp *AliPayTradeCloseResponse, err error) {
  113. var bytes []byte
  114. trade1 := body.Get("out_trade_no")
  115. trade2 := body.Get("trade_no")
  116. if trade1 == null && trade2 == null {
  117. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  118. }
  119. bytes, err = this.doAliPay(body, "alipay.trade.close")
  120. if err != nil {
  121. return nil, err
  122. }
  123. //log.Println("AliPayTradeCloseResponse::::", string(bytes))
  124. aliRsp = new(AliPayTradeCloseResponse)
  125. err = json.Unmarshal(bytes, aliRsp)
  126. if err != nil {
  127. return nil, err
  128. }
  129. if aliRsp.AliPayTradeCloseResponse.Code != "10000" {
  130. info := aliRsp.AliPayTradeCloseResponse
  131. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  132. }
  133. aliRsp.SignData = getSignData(bytes)
  134. return aliRsp, nil
  135. }
  136. //alipay.trade.cancel(统一收单交易撤销接口)
  137. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.cancel
  138. func (this *aliPayClient) AliPayTradeCancel(body BodyMap) (aliRsp *AliPayTradeCancelResponse, err error) {
  139. var bytes []byte
  140. trade1 := body.Get("out_trade_no")
  141. trade2 := body.Get("trade_no")
  142. if trade1 == null && trade2 == null {
  143. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  144. }
  145. bytes, err = this.doAliPay(body, "alipay.trade.cancel")
  146. if err != nil {
  147. return nil, err
  148. }
  149. //log.Println("AliPayTradeCancel::::", string(bytes))
  150. aliRsp = new(AliPayTradeCancelResponse)
  151. err = json.Unmarshal(bytes, aliRsp)
  152. if err != nil {
  153. return nil, err
  154. }
  155. if aliRsp.AliPayTradeCancelResponse.Code != "10000" {
  156. info := aliRsp.AliPayTradeCancelResponse
  157. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  158. }
  159. aliRsp.SignData = getSignData(bytes)
  160. return aliRsp, nil
  161. }
  162. //alipay.trade.refund(统一收单交易退款接口)
  163. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.refund
  164. func (this *aliPayClient) AliPayTradeRefund(body BodyMap) (aliRsp *AliPayTradeRefundResponse, err error) {
  165. var bytes []byte
  166. trade1 := body.Get("out_trade_no")
  167. trade2 := body.Get("trade_no")
  168. if trade1 == null && trade2 == null {
  169. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  170. }
  171. bytes, err = this.doAliPay(body, "alipay.trade.refund")
  172. if err != nil {
  173. return nil, err
  174. }
  175. //log.Println("AliPayTradeRefundResponse::::", string(bytes))
  176. aliRsp = new(AliPayTradeRefundResponse)
  177. err = json.Unmarshal(bytes, aliRsp)
  178. if err != nil {
  179. return nil, err
  180. }
  181. if aliRsp.AlipayTradeRefundResponse.Code != "10000" {
  182. info := aliRsp.AlipayTradeRefundResponse
  183. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  184. }
  185. aliRsp.SignData = getSignData(bytes)
  186. return aliRsp, nil
  187. }
  188. //alipay.trade.refund(统一收单退款页面接口)
  189. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.refund
  190. func (this *aliPayClient) AliPayTradePageRefund(body BodyMap) (aliRsp *AliPayTradePageRefundResponse, err error) {
  191. var bytes []byte
  192. trade1 := body.Get("out_trade_no")
  193. trade2 := body.Get("trade_no")
  194. if trade1 == null && trade2 == null {
  195. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  196. }
  197. bytes, err = this.doAliPay(body, " alipay.trade.page.refund")
  198. if err != nil {
  199. return nil, err
  200. }
  201. //log.Println("AliPayTradePageRefundResponse::::", string(bytes))
  202. aliRsp = new(AliPayTradePageRefundResponse)
  203. err = json.Unmarshal(bytes, aliRsp)
  204. if err != nil {
  205. return nil, err
  206. }
  207. if aliRsp.AliPayTradePageRefundResponse.Code != "10000" {
  208. info := aliRsp.AliPayTradePageRefundResponse
  209. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  210. }
  211. aliRsp.SignData = getSignData(bytes)
  212. return aliRsp, nil
  213. }
  214. //alipay.trade.precreate(统一收单线下交易预创建)
  215. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.precreate
  216. func (this *aliPayClient) AliPayTradePrecreate(body BodyMap) (aliRsp *AlipayTradePrecreateResponse, err error) {
  217. var bytes []byte
  218. trade1 := body.Get("out_trade_no")
  219. if trade1 == null {
  220. return nil, errors.New("out_trade_no is not allowed to be null")
  221. }
  222. bytes, err = this.doAliPay(body, "alipay.trade.precreate")
  223. if err != nil {
  224. return nil, err
  225. }
  226. //log.Println("AlipayTradePrecreateResponse::::", string(bytes))
  227. aliRsp = new(AlipayTradePrecreateResponse)
  228. err = json.Unmarshal(bytes, aliRsp)
  229. if err != nil {
  230. return nil, err
  231. }
  232. if aliRsp.AlipayTradePrecreateResponse.Code != "10000" {
  233. info := aliRsp.AlipayTradePrecreateResponse
  234. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  235. }
  236. aliRsp.SignData = getSignData(bytes)
  237. return aliRsp, nil
  238. }
  239. //alipay.trade.pay(统一收单交易支付接口)
  240. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.pay
  241. func (this *aliPayClient) AliPayTradePay(body BodyMap) (aliRsp *AliPayTradePayResponse, err error) {
  242. var bytes []byte
  243. trade := body.Get("out_trade_no")
  244. if trade == null {
  245. return nil, errors.New("out_trade_no is not allowed to be null")
  246. }
  247. //===============product_code值===================
  248. //body.Set("product_code", "FACE_TO_FACE_PAYMENT")
  249. bytes, err = this.doAliPay(body, "alipay.trade.pay")
  250. if err != nil {
  251. return nil, err
  252. }
  253. //log.Println("AliPayTradePayResponse::::", string(bytes))
  254. aliRsp = new(AliPayTradePayResponse)
  255. err = json.Unmarshal(bytes, aliRsp)
  256. if err != nil {
  257. return nil, err
  258. }
  259. if aliRsp.AliPayTradePayResponse.Code != "10000" {
  260. info := aliRsp.AliPayTradePayResponse
  261. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  262. }
  263. aliRsp.SignData = getSignData(bytes)
  264. return aliRsp, nil
  265. }
  266. //alipay.trade.query(统一收单线下交易查询)
  267. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.query
  268. func (this *aliPayClient) AliPayTradeQuery(body BodyMap) (aliRsp *AliPayTradeQueryResponse, err error) {
  269. var bytes []byte
  270. trade1 := body.Get("out_trade_no")
  271. trade2 := body.Get("trade_no")
  272. if trade1 == null && trade2 == null {
  273. return nil, errors.New("out_trade_no and trade_no are not allowed to be null at the same time")
  274. }
  275. bytes, err = this.doAliPay(body, "alipay.trade.query")
  276. if err != nil {
  277. return nil, err
  278. }
  279. //log.Println("AliPayTradeQueryResponse::::", string(bytes))
  280. aliRsp = new(AliPayTradeQueryResponse)
  281. err = json.Unmarshal(bytes, aliRsp)
  282. if err != nil {
  283. return nil, err
  284. }
  285. if aliRsp.AliPayTradeQueryResponse.Code != "10000" {
  286. info := aliRsp.AliPayTradeQueryResponse
  287. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  288. }
  289. aliRsp.SignData = getSignData(bytes)
  290. return aliRsp, nil
  291. }
  292. //alipay.trade.app.pay(app支付接口2.0)
  293. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.app.pay
  294. func (this *aliPayClient) AliPayTradeAppPay(body BodyMap) (payParam string, err error) {
  295. var bytes []byte
  296. trade := body.Get("out_trade_no")
  297. if trade == null {
  298. return null, errors.New("out_trade_no is not allowed to be null")
  299. }
  300. //===============product_code值===================
  301. //body.Set("product_code", "QUICK_MSECURITY_PAY")
  302. bytes, err = this.doAliPay(body, "alipay.trade.app.pay")
  303. if err != nil {
  304. return null, err
  305. }
  306. payParam = string(bytes)
  307. return payParam, nil
  308. }
  309. //alipay.trade.wap.pay(手机网站支付接口2.0)
  310. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
  311. func (this *aliPayClient) AliPayTradeWapPay(body BodyMap) (payUrl string, err error) {
  312. var bytes []byte
  313. trade := body.Get("out_trade_no")
  314. if trade == null {
  315. return null, errors.New("out_trade_no is not allowed to be null")
  316. }
  317. //===============product_code值===================
  318. body.Set("product_code", "QUICK_WAP_WAY")
  319. bytes, err = this.doAliPay(body, "alipay.trade.wap.pay")
  320. if err != nil {
  321. //log.Println("err::", err.Error())
  322. return null, err
  323. }
  324. payUrl = string(bytes)
  325. return payUrl, nil
  326. }
  327. //alipay.trade.page.pay(统一收单下单并支付页面接口)
  328. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.page.pay
  329. func (this *aliPayClient) AliPayTradePagePay(body BodyMap) (payUrl string, err error) {
  330. var bytes []byte
  331. trade := body.Get("out_trade_no")
  332. if trade == null {
  333. return null, errors.New("out_trade_no is not allowed to be null")
  334. }
  335. //===============product_code值===================
  336. body.Set("product_code", "FAST_INSTANT_TRADE_PAY")
  337. bytes, err = this.doAliPay(body, "alipay.trade.page.pay")
  338. if err != nil {
  339. //log.Println("err::", err.Error())
  340. return null, err
  341. }
  342. payUrl = string(bytes)
  343. return payUrl, nil
  344. }
  345. //alipay.fund.trans.toaccount.transfer(单笔转账到支付宝账户接口)
  346. // 文档地址:https://docs.open.alipay.com/api_28/alipay.fund.trans.toaccount.transfer
  347. func (this *aliPayClient) AlipayFundTransToaccountTransfer(body BodyMap) (aliRsp *AlipayFundTransToaccountTransferResponse, err error) {
  348. var bytes []byte
  349. trade1 := body.Get("out_biz_no")
  350. if trade1 == null {
  351. return nil, errors.New("out_biz_no is not allowed to be null")
  352. }
  353. bytes, err = this.doAliPay(body, "alipay.fund.trans.toaccount.transfer")
  354. if err != nil {
  355. return nil, err
  356. }
  357. //log.Println("AlipayFundTransToaccountTransferResponse::::", string(bytes))
  358. aliRsp = new(AlipayFundTransToaccountTransferResponse)
  359. err = json.Unmarshal(bytes, aliRsp)
  360. if err != nil {
  361. return nil, err
  362. }
  363. if aliRsp.AlipayFundTransToaccountTransferResponse.Code != "10000" {
  364. info := aliRsp.AlipayFundTransToaccountTransferResponse
  365. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  366. }
  367. aliRsp.SignData = getSignData(bytes)
  368. return aliRsp, nil
  369. }
  370. //alipay.trade.orderinfo.sync(支付宝订单信息同步接口)
  371. // 文档地址:https://docs.open.alipay.com/api_1/alipay.trade.orderinfo.sync
  372. func (this *aliPayClient) AliPayTradeOrderinfoSync(body BodyMap) {
  373. }
  374. //alipay.system.oauth.token(换取授权访问令牌)
  375. // 文档地址:https://docs.open.alipay.com/api_9/alipay.system.oauth.token
  376. func (this *aliPayClient) AliPaySystemOauthToken(body BodyMap) (aliRsp *AliPaySystemOauthTokenResponse, err error) {
  377. var bytes []byte
  378. grantType := body.Get("grant_type")
  379. if grantType == null {
  380. return nil, errors.New("grant_type is not allowed to be null")
  381. }
  382. code := body.Get("code")
  383. refreshToken := body.Get("refresh_token")
  384. if code == null && refreshToken == null {
  385. return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
  386. }
  387. bytes, err = aliPaySystemOauthToken(this.AppId, this.privateKey, body, "alipay.system.oauth.token", this.isProd)
  388. if err != nil {
  389. return nil, err
  390. }
  391. //log.Println("AliPaySystemOauthToken::::", string(bytes))
  392. aliRsp = new(AliPaySystemOauthTokenResponse)
  393. err = json.Unmarshal(bytes, aliRsp)
  394. if err != nil {
  395. return nil, err
  396. }
  397. if aliRsp.AliPaySystemOauthTokenResponse.AccessToken == null {
  398. info := aliRsp.ErrorResponse
  399. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  400. }
  401. aliRsp.SignData = getSignData(bytes)
  402. return aliRsp, nil
  403. }
  404. //alipay.open.auth.token.app(换取应用授权令牌)
  405. // 文档地址:https://docs.open.alipay.com/api_9/alipay.open.auth.token.app
  406. func (this *aliPayClient) AlipayOpenAuthTokenApp(body BodyMap) (aliRsp *AlipayOpenAuthTokenAppResponse, err error) {
  407. var bs []byte
  408. grantType := body.Get("grant_type")
  409. if grantType == null {
  410. return nil, errors.New("grant_type is not allowed to be null")
  411. }
  412. code := body.Get("code")
  413. refreshToken := body.Get("refresh_token")
  414. if code == null && refreshToken == null {
  415. return nil, errors.New("code and refresh_token are not allowed to be null at the same time")
  416. }
  417. bs, err = this.doAliPay(body, "alipay.open.auth.token.app")
  418. if err != nil {
  419. return nil, err
  420. }
  421. //log.Println("AlipayOpenAuthTokenApp::::", string(bs))
  422. aliRsp = new(AlipayOpenAuthTokenAppResponse)
  423. err = json.Unmarshal(bs, aliRsp)
  424. if err != nil {
  425. return nil, err
  426. }
  427. if aliRsp.AlipayOpenAuthTokenAppResponse.Code != "10000" {
  428. info := aliRsp.AlipayOpenAuthTokenAppResponse
  429. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  430. }
  431. aliRsp.SignData = getSignData(bs)
  432. return aliRsp, nil
  433. }
  434. //zhima.credit.score.get(芝麻分)
  435. // 文档地址:https://docs.open.alipay.com/api_8/zhima.credit.score.get
  436. func (this *aliPayClient) ZhimaCreditScoreGet(body BodyMap) (aliRsp *ZhimaCreditScoreGetResponse, err error) {
  437. var bytes []byte
  438. trade1 := body.Get("product_code")
  439. if trade1 == null {
  440. body.Set("product_code", "w1010100100000000001")
  441. }
  442. trade2 := body.Get("transaction_id")
  443. if trade2 == null {
  444. return nil, errors.New("transaction_id is not allowed to be null")
  445. }
  446. bytes, err = this.doAliPay(body, "zhima.credit.score.get")
  447. if err != nil {
  448. return nil, err
  449. }
  450. //log.Println("ZhimaCreditScoreGet::::", string(bytes))
  451. aliRsp = new(ZhimaCreditScoreGetResponse)
  452. err = json.Unmarshal(bytes, aliRsp)
  453. if err != nil {
  454. return nil, err
  455. }
  456. if aliRsp.ZhimaCreditScoreGetResponse.Code != "10000" {
  457. info := aliRsp.ZhimaCreditScoreGetResponse
  458. return nil, fmt.Errorf(`{"code":"%v","msg":"%v","sub_code":"%v","sub_msg":"%v"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
  459. }
  460. aliRsp.SignData = getSignData(bytes)
  461. return aliRsp, nil
  462. }
  463. //向支付宝发送请求
  464. func (this *aliPayClient) doAliPay(body BodyMap, method string) (bytes []byte, err error) {
  465. //===============转换body参数===================
  466. bodyStr, err := json.Marshal(body)
  467. if err != nil {
  468. log.Println("json.Marshal:", err)
  469. return nil, err
  470. }
  471. //fmt.Println(string(bodyStr))
  472. //===============生成参数===================
  473. pubBody := make(BodyMap)
  474. pubBody.Set("app_id", this.AppId)
  475. pubBody.Set("method", method)
  476. pubBody.Set("format", "JSON")
  477. if this.ReturnUrl != null {
  478. pubBody.Set("return_url", this.ReturnUrl)
  479. }
  480. if this.Charset == null {
  481. pubBody.Set("charset", "utf-8")
  482. } else {
  483. pubBody.Set("charset", this.Charset)
  484. }
  485. if this.SignType == null {
  486. pubBody.Set("sign_type", "RSA2")
  487. } else {
  488. pubBody.Set("sign_type", this.SignType)
  489. }
  490. pubBody.Set("timestamp", time.Now().Format(TimeLayout))
  491. pubBody.Set("version", "1.0")
  492. if this.NotifyUrl != null {
  493. pubBody.Set("notify_url", this.NotifyUrl)
  494. }
  495. if this.AppAuthToken != null {
  496. pubBody.Set("app_auth_token", this.AppAuthToken)
  497. }
  498. if this.AuthToken != null {
  499. pubBody.Set("auth_token", this.AuthToken)
  500. }
  501. //fmt.Println("biz_content", string(bodyStr))
  502. pubBody.Set("biz_content", string(bodyStr))
  503. //===============获取签名===================
  504. pKey := FormatPrivateKey(this.privateKey)
  505. sign, err := getRsaSign(pubBody, pubBody.Get("sign_type"), pKey)
  506. if err != nil {
  507. return nil, err
  508. }
  509. pubBody.Set("sign", sign)
  510. //fmt.Println("rsaSign:", sign)
  511. //===============发起请求===================
  512. urlParam := FormatAliPayURLParam(pubBody)
  513. //fmt.Println("urlParam:", urlParam)
  514. if method == "alipay.trade.app.pay" {
  515. return []byte(urlParam), nil
  516. }
  517. if method == "alipay.trade.page.pay" {
  518. if !this.isProd {
  519. //沙箱环境
  520. return []byte(zfb_sanbox_base_url + "?" + urlParam), nil
  521. } else {
  522. //正式环境
  523. return []byte(zfb_base_url + "?" + urlParam), nil
  524. }
  525. }
  526. var url string
  527. agent := HttpAgent()
  528. if !this.isProd {
  529. //沙箱环境
  530. url = zfb_sanbox_base_url_utf8
  531. //fmt.Println(url)
  532. agent.Post(url)
  533. } else {
  534. //正式环境
  535. url = zfb_base_url_utf8
  536. //fmt.Println(url)
  537. agent.Post(url)
  538. }
  539. res, bs, errs := agent.
  540. Type("form-data").
  541. SendString(urlParam).
  542. EndBytes()
  543. if len(errs) > 0 {
  544. return nil, errs[0]
  545. }
  546. //fmt.Println("res:", res.StatusCode)
  547. if res.StatusCode != 200 {
  548. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %v", res.StatusCode)
  549. }
  550. if method == "alipay.trade.wap.pay" {
  551. //fmt.Println("rsp:::", rsp.Body)
  552. if res.Request.URL.String() == zfb_sanbox_base_url || res.Request.URL.String() == zfb_base_url {
  553. return nil, errors.New("请求手机网站支付出错,请检查各个参数或秘钥是否正确")
  554. }
  555. return []byte(res.Request.URL.String()), nil
  556. }
  557. return bs, nil
  558. }
  559. func getSignData(bs []byte) (signData string) {
  560. str := string(bs)
  561. indexStart := strings.Index(str, `":`)
  562. indexEnd := strings.Index(str, `,"sign"`)
  563. signData = str[indexStart+2 : indexEnd]
  564. return
  565. }