支付模块
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.6 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. "crypto/tls"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. )
  11. func UniPayByJoin(payParams *md.JoinPayReq, paySecretKey string,isPrd bool) (*md.JoinPayResponse, error) {
  12. payParams.P0Version = "2.1"
  13. payParams.P1MerchantNo = md.PayTestAccount
  14. payParams.P4Cur = "1"
  15. payParams.P2OrderNo = "95271"
  16. payParams.P3Amount = "0.1"
  17. payParams.P5ProductName = "测试商品"
  18. payParams.P9NotifyUrl = "AB|https://www.baidu.com"
  19. //--交易类型
  20. payParams.Q1FrpCode = md.Q1FrpCodeWEIXINPAYAPP
  21. //payParams.Q1FrpCode = md.Q1FrpCodeALIPAY
  22. //原始微信AppId
  23. //payParams.Q7AppId = "gh_5a236de9850a"
  24. //微信AppId
  25. payParams.Q7AppId = "wx5bb10bb79cb5da1c"
  26. //商家报备编号(分微信和支付宝的);支付宝的:
  27. //payParams.QATradeMerchantNo = md.PayTestAliAccount
  28. //商家报备编号(分微信和支付宝的);微信的:
  29. payParams.QATradeMerchantNo = md.PayTestWeChatAccount
  30. postData := url.Values{}
  31. creatHmac(payParams,paySecretKey, &postData)
  32. zhios_pay_utils.FilePutContents("UniPayByJoin", zhios_pay_utils.SerializeStr(map[string]interface{}{
  33. "data": payParams,
  34. }))
  35. tr := &http.Transport{
  36. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  37. }
  38. client := &http.Client{Transport: tr}
  39. response, err := client.Post(md.JoinPayUrl, "application/x-www-form-urlencoded", strings.NewReader(postData.Encode()))
  40. if err != nil {
  41. return nil, err
  42. }
  43. defer response.Body.Close()
  44. body, err := ioutil.ReadAll(response.Body)
  45. resp := md.JoinPayResponse{}
  46. zhios_pay_utils.Unserialize(body, &resp)
  47. return &resp, err
  48. }
  49. func creatHmac(payParams *md.JoinPayReq,paySecretKey string, postData *url.Values) {
  50. hmac := ""
  51. if payParams.P0Version != "" {
  52. postData.Add("p0_Version", payParams.P0Version)
  53. hmac = hmac + payParams.P0Version
  54. }
  55. if payParams.P1MerchantNo != "" {
  56. postData.Add("p1_MerchantNo", payParams.P1MerchantNo)
  57. hmac = hmac + payParams.P1MerchantNo
  58. }
  59. if payParams.P2OrderNo != "" {
  60. postData.Add("p2_OrderNo", payParams.P2OrderNo)
  61. hmac = hmac + payParams.P2OrderNo
  62. }
  63. if payParams.P3Amount != "" {
  64. postData.Add("p3_Amount", payParams.P3Amount)
  65. hmac = hmac + payParams.P3Amount
  66. }
  67. if payParams.P4Cur != "" {
  68. postData.Add("p4_Cur", payParams.P4Cur)
  69. hmac = hmac + payParams.P4Cur
  70. }
  71. if payParams.P5ProductName != "" {
  72. postData.Add("p5_ProductName", payParams.P5ProductName)
  73. hmac = hmac + payParams.P5ProductName
  74. }
  75. if payParams.P6ProductDesc != "" {
  76. postData.Add("p6_ProductDesc", payParams.P6ProductDesc)
  77. hmac = hmac + payParams.P6ProductDesc
  78. }
  79. if payParams.P7Mp != "" {
  80. postData.Add("p7_Mp", payParams.P7Mp)
  81. hmac = hmac + payParams.P7Mp
  82. }
  83. if payParams.P9NotifyUrl != "" {
  84. postData.Add("p9_NotifyUrl", payParams.P9NotifyUrl)
  85. hmac = hmac + payParams.P9NotifyUrl
  86. }
  87. if payParams.Q1FrpCode != "" {
  88. postData.Add("q1_FrpCode", payParams.Q1FrpCode)
  89. hmac = hmac + payParams.Q1FrpCode
  90. }
  91. if payParams.Q2MerchantBankCode != "" {
  92. postData.Add("q2_MerchantBankCode", payParams.Q2MerchantBankCode)
  93. hmac = hmac + payParams.Q2MerchantBankCode
  94. }
  95. if payParams.Q4IsShowPic != "" {
  96. postData.Add("q4_IsShowPic", payParams.Q4IsShowPic)
  97. hmac = hmac + payParams.Q4IsShowPic
  98. }
  99. if payParams.Q5OpenId != "" {
  100. postData.Add("q5_OpenId", payParams.Q5OpenId)
  101. hmac = hmac + payParams.Q5OpenId
  102. }
  103. if payParams.Q6AuthCode != "" {
  104. postData.Add("q6_AuthCode", payParams.Q6AuthCode)
  105. hmac = hmac + payParams.Q6AuthCode
  106. }
  107. if payParams.Q7AppId != "" {
  108. postData.Add("q7_AppId", payParams.Q7AppId)
  109. hmac = hmac + payParams.Q7AppId
  110. }
  111. if payParams.Q8TerminalNo != "" {
  112. postData.Add("q8_TerminalNo", payParams.Q8TerminalNo)
  113. hmac = hmac + payParams.Q8TerminalNo
  114. }
  115. if payParams.Q9TransactionModel != "" {
  116. postData.Add("q9_TransactionModel", payParams.Q9TransactionModel)
  117. hmac = hmac + payParams.Q9TransactionModel
  118. }
  119. if payParams.QATradeMerchantNo != "" {
  120. postData.Add("qa_TradeMerchantNo", payParams.QATradeMerchantNo)
  121. hmac = hmac + payParams.QATradeMerchantNo
  122. }
  123. if payParams.QBbuyerId != "" {
  124. postData.Add("qb_buyerId", payParams.QBbuyerId)
  125. hmac = hmac + payParams.QBbuyerId
  126. }
  127. if payParams.QJDJPlan != "" {
  128. postData.Add("qj_DJPlan", payParams.QJDJPlan)
  129. hmac = hmac + payParams.QJDJPlan
  130. }
  131. if payParams.QKDisablePayModel != "" {
  132. postData.Add("qk_DisablePayModel", payParams.QKDisablePayModel)
  133. hmac = hmac + payParams.QKDisablePayModel
  134. }
  135. if payParams.QLTerminalIp != "" {
  136. postData.Add("ql_TerminalIp", payParams.QLTerminalIp)
  137. hmac = hmac + payParams.QLTerminalIp
  138. }
  139. payParams.Hmac = zhios_pay_utils.Md5(hmac + paySecretKey)
  140. postData.Add("hmac", payParams.Hmac)
  141. }