支付模块
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

410 lignes
12 KiB

  1. package pay
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  4. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/iGoogle-ink/gopay"
  9. "github.com/iGoogle-ink/gopay/alipay"
  10. "github.com/iGoogle-ink/gopay/pkg/util"
  11. "github.com/iGoogle-ink/gopay/pkg/xhttp"
  12. "github.com/iGoogle-ink/gopay/pkg/xlog"
  13. "time"
  14. )
  15. const (
  16. baseUrl = "https://openapi.alipay.com/gateway.do"
  17. sandboxBaseUrl = "https://openapi.alipaydev.com/gateway.do"
  18. baseUrlUtf8 = "https://openapi.alipay.com/gateway.do?charset=utf-8"
  19. sandboxBaseUrlUtf8 = "https://openapi.alipaydev.com/gateway.do?charset=utf-8"
  20. )
  21. func Comm(args map[string]string) *alipay.Client {
  22. paySet := AlipayCofing(args)
  23. if args["private_key"] == "" || args["app_id"] == "" {
  24. return nil
  25. }
  26. //初始化支付宝客户端
  27. // appID 是在支付宝申请的APPID
  28. // priKey 是支付宝私钥
  29. // code 支付宝授权码
  30. client := alipay.NewClient(args["app_id"], args["private_key"], true)
  31. client.DebugSwitch = gopay.DebugOn
  32. //判断密钥的类型
  33. rsa_type := alipay.RSA2
  34. pkcs_type := alipay.PKCS1
  35. if args["rsa"] == "1" {
  36. rsa_type = alipay.RSA
  37. }
  38. if args["pkcs"] == "1" {
  39. pkcs_type = alipay.PKCS8
  40. }
  41. if paySet.PayAliUseType == "1" {
  42. rsa_type = alipay.RSA2
  43. pkcs_type = alipay.PKCS8
  44. }
  45. //配置公共参数
  46. client.SetCharset("utf-8").
  47. SetSignType(rsa_type).
  48. SetPrivateKeyType(pkcs_type)
  49. //新支付宝支付
  50. if paySet.PayAliUseType == "1" {
  51. client.SetAppCertSN(paySet.PayAppCertSn)
  52. aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
  53. client.SetAliPayRootCertSN(aliPayRootCertSN)
  54. client.SetAliPayPublicCertSN(paySet.PayAlipayrsaPublicKey)
  55. }
  56. client.IsProd = true
  57. fmt.Println(client)
  58. return client
  59. }
  60. func OpenAuthTokenApp(args map[string]string) (*alipay.SystemOauthTokenResponse, error) {
  61. client := Comm(args)
  62. if client == nil {
  63. return nil, errors.New("获取失败")
  64. }
  65. //请求参数
  66. body := make(gopay.BodyMap)
  67. body.Set("grant_type", "authorization_code")
  68. body.Set("code", args["code"])
  69. payParam, err := client.SystemOauthToken(body)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return payParam, nil
  74. }
  75. func UserInfoShare(args map[string]string) (*alipay.UserInfoShareResponse, error) {
  76. client := Comm(args)
  77. client.AuthToken = args["auth_token"]
  78. if client == nil {
  79. return nil, errors.New("获取失败")
  80. }
  81. payParam, err := client.UserInfoShare()
  82. if err != nil {
  83. return nil, err
  84. }
  85. return payParam, nil
  86. }
  87. func CreditagreementSign(args map[string]string) (string, error) {
  88. client := Comm(args)
  89. client.AppAuthToken = args["app_auth_token"]
  90. if client == nil {
  91. return "", errors.New("获取失败")
  92. }
  93. //请求参数
  94. bm := make(gopay.BodyMap)
  95. // biz_content
  96. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  97. bz.Set("out_agreement_no", args["out_agreement_no"])
  98. bz.Set("zm_service_id", args["zm_service_id"])
  99. bz.Set("category_id", args["category_id"])
  100. })
  101. aliPsp := ""
  102. bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditagreement.sign", &aliPsp)
  103. if err != nil {
  104. xlog.Error(err)
  105. return "", err
  106. }
  107. return zhios_pay_utils.AnyToString(bs), nil
  108. }
  109. func CreditagreementQuery(args map[string]string) (*md.ZhimaCreditPayafteruseCreditagreementQuery, error) {
  110. client := Comm(args)
  111. client.AppAuthToken = args["app_auth_token"]
  112. if client == nil {
  113. return nil, errors.New("获取失败")
  114. }
  115. //请求参数
  116. bm := make(gopay.BodyMap)
  117. // biz_content
  118. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  119. bz.Set("out_agreement_no", args["out_agreement_no"])
  120. })
  121. var aliPsp md.ZhimaCreditPayafteruseCreditagreementQuery
  122. bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditagreement.query", &aliPsp)
  123. if err != nil {
  124. xlog.Error(err)
  125. return nil, err
  126. }
  127. res := bs.(*md.ZhimaCreditPayafteruseCreditagreementQuery)
  128. return res, nil
  129. }
  130. //信用下单
  131. func CreditagreementOrder(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderOrder, error) {
  132. client := Comm(args)
  133. client.AppAuthToken = args["app_auth_token"]
  134. if client == nil {
  135. return nil, errors.New("获取失败")
  136. }
  137. //请求参数
  138. bm := make(gopay.BodyMap)
  139. // biz_content
  140. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  141. bz.Set("out_order_no", args["out_order_no"])
  142. bz.Set("credit_agreement_id", args["credit_agreement_id"])
  143. bz.Set("category_id", args["category_id"])
  144. bz.Set("order_amount", args["order_amount"])
  145. bz.Set("amount_type", args["amount_type"])
  146. bz.Set("subject", args["subject"])
  147. })
  148. var aliPsp md.ZhimaCreditPayafteruseCreditbizorderOrder
  149. bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.order", &aliPsp)
  150. if err != nil {
  151. xlog.Error(err)
  152. return nil, err
  153. }
  154. res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderOrder)
  155. return res, nil
  156. }
  157. //查询信用订单
  158. func CreditagreementOrderQuery(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderQuery, error) {
  159. client := Comm(args)
  160. client.AppAuthToken = args["app_auth_token"]
  161. if client == nil {
  162. return nil, errors.New("获取失败")
  163. }
  164. //请求参数
  165. bm := make(gopay.BodyMap)
  166. // biz_content
  167. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  168. bz.Set("credit_biz_order_id", args["credit_biz_order_id"])
  169. })
  170. var aliPsp md.ZhimaCreditPayafteruseCreditbizorderQuery
  171. bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.query", &aliPsp)
  172. if err != nil {
  173. xlog.Error(err)
  174. return nil, err
  175. }
  176. res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderQuery)
  177. return res, nil
  178. }
  179. //转账
  180. func AlipayFundTransUniTransfer(args map[string]string) (*md.AlipayFundTransUniTransfer, error) {
  181. client := Comm(args)
  182. if client == nil {
  183. return nil, errors.New("获取失败")
  184. }
  185. //请求参数
  186. bm := make(gopay.BodyMap)
  187. // biz_content
  188. payeeInfo := map[string]string{
  189. "identity": args["identity"],
  190. "identity_type": args["identity_type"],
  191. "name": args["name"],
  192. }
  193. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  194. bz.Set("out_biz_no", args["out_biz_no"])
  195. bz.Set("trans_amount", args["trans_amount"])
  196. bz.Set("product_code", args["product_code"])
  197. bz.Set("biz_scene", args["biz_scene"])
  198. bz.Set("order_title", args["order_title"])
  199. bz.Set("payee_info", payeeInfo)
  200. })
  201. var aliPsp md.AlipayFundTransUniTransfer
  202. bs, err := PostAliPayAPISelfV2(client, bm, "alipay.fund.trans.uni.transfer", &aliPsp)
  203. if err != nil {
  204. xlog.Error(err)
  205. return nil, err
  206. }
  207. res := bs.(*md.AlipayFundTransUniTransfer)
  208. return res, nil
  209. }
  210. //扣款
  211. func AlipayTradePay(args map[string]string) {
  212. client := Comm(args)
  213. if client == nil {
  214. return
  215. }
  216. //请求参数
  217. bm := make(gopay.BodyMap)
  218. // biz_content
  219. extendParams := map[string]string{
  220. "creditTradeScene": args["creditTradeScene"],
  221. }
  222. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  223. bz.Set("out_trade_no", args["out_trade_no"])
  224. bz.Set("is_async_pay", args["is_async_pay"])
  225. bz.Set("total_amount", args["total_amount"])
  226. bz.Set("auth_code", args["auth_code"])
  227. bz.Set("product_code", args["product_code"])
  228. bz.Set("scene", args["scene"])
  229. bz.Set("subject", args["subject"])
  230. bz.Set("extend_params", extendParams)
  231. })
  232. var aliPsp = ""
  233. PostAliPayAPISelfV2(client, bm, "alipay.trade.pay", &aliPsp)
  234. return
  235. }
  236. func AlipayTradeQuery(args map[string]string) (*md.AlipayFundTransUniTransfer, error) {
  237. client := Comm(args)
  238. if client == nil {
  239. return nil, errors.New("获取失败")
  240. }
  241. //请求参数
  242. bm := make(gopay.BodyMap)
  243. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  244. bz.Set("out_trade_no", args["out_trade_no"])
  245. })
  246. var aliPsp md.AlipayFundTransUniTransfer
  247. bs, err := PostAliPayAPISelfV2(client, bm, "alipay.trade.query", &aliPsp)
  248. if err != nil {
  249. xlog.Error(err)
  250. return nil, err
  251. }
  252. res := bs.(*md.AlipayFundTransUniTransfer)
  253. return res, nil
  254. }
  255. //结束信用服务订单
  256. func CreditagreementOrderFinish(args map[string]string) (*md.ZhimaCreditPayafteruseCreditbizorderFinish, error) {
  257. client := Comm(args)
  258. client.AppAuthToken = args["app_auth_token"]
  259. if client == nil {
  260. return nil, errors.New("获取失败")
  261. }
  262. //请求参数
  263. bm := make(gopay.BodyMap)
  264. // biz_content
  265. bm.SetBodyMap("biz_content", func(bz gopay.BodyMap) {
  266. bz.Set("out_request_no", args["out_request_no"])
  267. bz.Set("credit_biz_order_id", args["credit_biz_order_id"])
  268. bz.Set("is_fulfilled", args["is_fulfilled"])
  269. bz.Set("remark", args["remark"])
  270. })
  271. var aliPsp md.ZhimaCreditPayafteruseCreditbizorderFinish
  272. bs, err := PostAliPayAPISelfV2(client, bm, "zhima.credit.payafteruse.creditbizorder.finish", &aliPsp)
  273. if err != nil {
  274. xlog.Error(err)
  275. return nil, err
  276. }
  277. res := bs.(*md.ZhimaCreditPayafteruseCreditbizorderFinish)
  278. return res, nil
  279. }
  280. func PostAliPayAPISelfV2(a *alipay.Client, bm gopay.BodyMap, method string, aliRsp interface{}) (aliRsps interface{}, err error) {
  281. var (
  282. bs, bodyBs []byte
  283. )
  284. // check if there is biz_content
  285. bz := bm.GetInterface("biz_content")
  286. if bzBody, ok := bz.(gopay.BodyMap); ok {
  287. if bodyBs, err = json.Marshal(bzBody); err != nil {
  288. return aliRsp, fmt.Errorf("json.Marshal(%v):%w", bzBody, err)
  289. }
  290. bm.Set("biz_content", string(bodyBs))
  291. }
  292. if bs, err = doAliPaySelf(a, bm, method); err != nil {
  293. return aliRsp, err
  294. }
  295. switch method {
  296. case "alipay.trade.app.pay":
  297. return string(bs), nil
  298. case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify", "zhima.credit.payafteruse.creditagreement.sign":
  299. return string(bs), nil
  300. default:
  301. if err = json.Unmarshal(bs, aliRsp); err != nil {
  302. return aliRsp, err
  303. }
  304. return aliRsp, nil
  305. }
  306. }
  307. // 向支付宝发送自定义请求
  308. func doAliPaySelf(a *alipay.Client, bm gopay.BodyMap, method string) (bs []byte, err error) {
  309. var (
  310. url, sign string
  311. )
  312. bm.Set("method", method)
  313. // check public parameter
  314. checkPublicParam(a, bm)
  315. // check sign
  316. if bm.GetString("sign") == "" {
  317. sign, err = alipay.GetRsaSign(bm, bm.GetString("sign_type"), a.PrivateKeyType, a.PrivateKey)
  318. if err != nil {
  319. return nil, fmt.Errorf("GetRsaSign Error: %v", err)
  320. }
  321. bm.Set("sign", sign)
  322. }
  323. if a.DebugSwitch == gopay.DebugOn {
  324. req, _ := json.Marshal(bm)
  325. xlog.Debugf("Alipay_Request: %s", req)
  326. }
  327. param := alipay.FormatURLParam(bm)
  328. switch method {
  329. case "alipay.trade.app.pay":
  330. return []byte(param), nil
  331. case "alipay.trade.wap.pay", "alipay.trade.page.pay", "alipay.user.certify.open.certify", "zhima.credit.payafteruse.creditagreement.sign":
  332. return []byte(baseUrl + "?" + param), nil
  333. default:
  334. httpClient := xhttp.NewClient()
  335. url = baseUrlUtf8
  336. res, bs, errs := httpClient.Type(xhttp.TypeForm).Post(url).SendString(param).EndBytes()
  337. if len(errs) > 0 {
  338. return nil, errs[0]
  339. }
  340. if a.DebugSwitch == gopay.DebugOn {
  341. xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
  342. }
  343. if res.StatusCode != 200 {
  344. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  345. }
  346. return bs, nil
  347. }
  348. }
  349. func checkPublicParam(a *alipay.Client, bm gopay.BodyMap) {
  350. bm.Set("format", "JSON")
  351. if bm.GetString("app_id") == "" && a.AppId != util.NULL {
  352. bm.Set("app_id", a.AppId)
  353. }
  354. if bm.GetString("app_cert_sn") == "" && a.AppCertSN != util.NULL {
  355. bm.Set("app_cert_sn", a.AppCertSN)
  356. }
  357. if bm.GetString("alipay_root_cert_sn") == "" && a.AliPayRootCertSN != util.NULL {
  358. bm.Set("alipay_root_cert_sn", a.AliPayRootCertSN)
  359. }
  360. if bm.GetString("return_url") == "" && a.ReturnUrl != util.NULL {
  361. bm.Set("return_url", a.ReturnUrl)
  362. }
  363. bm.Set("charset", "utf-8")
  364. if bm.GetString("charset") == "" && a.Charset != util.NULL {
  365. bm.Set("charset", a.Charset)
  366. }
  367. bm.Set("sign_type", alipay.RSA2)
  368. if bm.GetString("sign_type") == "" && a.SignType != util.NULL {
  369. bm.Set("sign_type", a.SignType)
  370. }
  371. bm.Set("timestamp", time.Now().Format(util.TimeLayout))
  372. bm.Set("version", "1.0")
  373. if bm.GetString("notify_url") == "" && a.NotifyUrl != util.NULL {
  374. bm.Set("notify_url", a.NotifyUrl)
  375. }
  376. if bm.GetString("app_auth_token") == "" && a.AppAuthToken != util.NULL {
  377. bm.Set("app_auth_token", a.AppAuthToken)
  378. }
  379. if bm.GetString("auth_token") == "" && a.AuthToken != util.NULL {
  380. bm.Set("auth_token", a.AuthToken)
  381. }
  382. }