alipay_client.go 20 KB

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