alipay_server_api.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //==================================
  2. // * Name:Jerry
  3. // * DateTime:2019/6/18 19:24
  4. // * Desc:
  5. //==================================
  6. package gopay
  7. import (
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "net/http"
  12. )
  13. //解析支付宝支付完成后的Notify信息
  14. func ParseAliPayNotifyResult(req *http.Request) (notifyRsp *AliPayNotifyRequest, err error) {
  15. notifyRsp = new(AliPayNotifyRequest)
  16. notifyRsp.NotifyTime = req.FormValue("notify_time")
  17. notifyRsp.NotifyType = req.FormValue("notify_type")
  18. notifyRsp.NotifyId = req.FormValue("notify_id")
  19. notifyRsp.AppId = req.FormValue("app_id")
  20. notifyRsp.Charset = req.FormValue("charset")
  21. notifyRsp.Version = req.FormValue("version")
  22. notifyRsp.SignType = req.FormValue("sign_type")
  23. notifyRsp.Sign = req.FormValue("sign")
  24. notifyRsp.AuthAppId = req.FormValue("auth_app_id")
  25. notifyRsp.TradeNo = req.FormValue("trade_no")
  26. notifyRsp.OutTradeNo = req.FormValue("out_trade_no")
  27. notifyRsp.OutBizNo = req.FormValue("out_biz_no")
  28. notifyRsp.BuyerId = req.FormValue("buyer_id")
  29. notifyRsp.BuyerLogonId = req.FormValue("buyer_logon_id")
  30. notifyRsp.SellerId = req.FormValue("seller_id")
  31. notifyRsp.SellerEmail = req.FormValue("seller_email")
  32. notifyRsp.TradeStatus = req.FormValue("trade_status")
  33. notifyRsp.TotalAmount = req.FormValue("total_amount")
  34. notifyRsp.ReceiptAmount = req.FormValue("receipt_amount")
  35. notifyRsp.InvoiceAmount = req.FormValue("invoice_amount")
  36. notifyRsp.BuyerPayAmount = req.FormValue("buyer_pay_amount")
  37. notifyRsp.PointAmount = req.FormValue("point_amount")
  38. notifyRsp.RefundFee = req.FormValue("refund_fee")
  39. notifyRsp.Subject = req.FormValue("subject")
  40. notifyRsp.Body = req.FormValue("body")
  41. notifyRsp.GmtCreate = req.FormValue("gmt_create")
  42. notifyRsp.GmtPayment = req.FormValue("gmt_payment")
  43. notifyRsp.GmtRefund = req.FormValue("gmt_refund")
  44. notifyRsp.GmtClose = req.FormValue("gmt_close")
  45. billList := req.FormValue("fund_bill_list")
  46. //log.Println("billList:", billList)
  47. if billList != null {
  48. bills := make([]FundBillListInfo, 0)
  49. err = json.Unmarshal([]byte(billList), &bills)
  50. if err != nil {
  51. return nil, err
  52. }
  53. notifyRsp.FundBillList = bills
  54. } else {
  55. notifyRsp.FundBillList = nil
  56. }
  57. notifyRsp.PassbackParams = req.FormValue("passback_params")
  58. detailList := req.FormValue("voucher_detail_list")
  59. //log.Println("detailList:", detailList)
  60. if detailList != null {
  61. details := make([]VoucherDetailListInfo, 0)
  62. err = json.Unmarshal([]byte(detailList), &details)
  63. if err != nil {
  64. return nil, err
  65. }
  66. notifyRsp.VoucherDetailList = details
  67. } else {
  68. notifyRsp.VoucherDetailList = nil
  69. }
  70. return notifyRsp, err
  71. }
  72. //支付通知的签名验证和参数签名后的Sign
  73. // aliPayPublicKey:支付宝公钥
  74. // notifyRsp:利用 gopay.ParseAliPayNotifyResult() 得到的结构体
  75. // 返回参数ok:是否验证通过
  76. // 返回参数sign:根据参数计算的sign值,非支付宝返回参数中的Sign
  77. func VerifyAliPayResultSign(aliPayPublicKey string, notifyRsp *AliPayNotifyRequest) (ok bool, sign string) {
  78. body := make(BodyMap)
  79. body.Set("notify_time", notifyRsp.NotifyTime)
  80. body.Set("notify_type", notifyRsp.NotifyType)
  81. body.Set("notify_id", notifyRsp.NotifyId)
  82. body.Set("app_id", notifyRsp.AppId)
  83. body.Set("charset", notifyRsp.Charset)
  84. body.Set("version", notifyRsp.Version)
  85. body.Set("auth_app_id", notifyRsp.AuthAppId)
  86. body.Set("trade_no", notifyRsp.TradeNo)
  87. body.Set("out_trade_no", notifyRsp.OutTradeNo)
  88. body.Set("out_biz_no", notifyRsp.OutBizNo)
  89. body.Set("buyer_id", notifyRsp.BuyerId)
  90. body.Set("buyer_logon_id", notifyRsp.BuyerLogonId)
  91. body.Set("seller_id", notifyRsp.SellerId)
  92. body.Set("seller_email", notifyRsp.SellerEmail)
  93. body.Set("trade_status", notifyRsp.TradeStatus)
  94. body.Set("total_amount", notifyRsp.TotalAmount)
  95. body.Set("receipt_amount", notifyRsp.ReceiptAmount)
  96. body.Set("invoice_amount", notifyRsp.InvoiceAmount)
  97. body.Set("buyer_pay_amount", notifyRsp.BuyerPayAmount)
  98. body.Set("point_amount", notifyRsp.PointAmount)
  99. body.Set("refund_fee", notifyRsp.RefundFee)
  100. body.Set("subject", notifyRsp.Subject)
  101. body.Set("body", notifyRsp.Body)
  102. body.Set("gmt_create", notifyRsp.GmtCreate)
  103. body.Set("gmt_payment", notifyRsp.GmtPayment)
  104. body.Set("gmt_refund", notifyRsp.GmtRefund)
  105. body.Set("gmt_close", notifyRsp.GmtClose)
  106. body.Set("fund_bill_list", jsonToString(notifyRsp.FundBillList))
  107. body.Set("passback_params", notifyRsp.PassbackParams)
  108. body.Set("voucher_detail_list", jsonToString(notifyRsp.VoucherDetailList))
  109. newBody := make(BodyMap)
  110. for k, v := range body {
  111. if v != null {
  112. newBody.Set(k, v)
  113. }
  114. }
  115. pKey := FormatAliPayPublicKey(aliPayPublicKey)
  116. sign, err := getRsaSign(newBody, pKey)
  117. if err != nil {
  118. return false, ""
  119. }
  120. ok = sign == notifyRsp.Sign
  121. return
  122. }
  123. func jsonToString(v interface{}) (str string) {
  124. if v == nil {
  125. return ""
  126. }
  127. bs, err := json.Marshal(v)
  128. if err != nil {
  129. fmt.Println("err:", err)
  130. return ""
  131. }
  132. //log.Println("string:", string(bs))
  133. return string(bs)
  134. }
  135. //格式化秘钥
  136. func FormatPrivateKey(privateKey string) (pKey string) {
  137. buffer := new(bytes.Buffer)
  138. buffer.WriteString("-----BEGIN RSA PRIVATE KEY-----\n")
  139. rawLen := 64
  140. keyLen := len(privateKey)
  141. raws := keyLen / rawLen
  142. temp := keyLen % rawLen
  143. if temp > 0 {
  144. raws++
  145. }
  146. start := 0
  147. end := start + rawLen
  148. for i := 0; i < raws; i++ {
  149. if i == raws-1 {
  150. buffer.WriteString(privateKey[start:])
  151. } else {
  152. buffer.WriteString(privateKey[start:end])
  153. }
  154. buffer.WriteString("\n")
  155. start += rawLen
  156. end = start + rawLen
  157. }
  158. buffer.WriteString("-----END RSA PRIVATE KEY-----\n")
  159. pKey = buffer.String()
  160. return
  161. }
  162. //格式化秘钥
  163. func FormatAliPayPublicKey(publicKey string) (pKey string) {
  164. buffer := new(bytes.Buffer)
  165. buffer.WriteString("-----BEGIN PUBLIC KEY-----\n")
  166. rawLen := 64
  167. keyLen := len(publicKey)
  168. raws := keyLen / rawLen
  169. temp := keyLen % rawLen
  170. if temp > 0 {
  171. raws++
  172. }
  173. start := 0
  174. end := start + rawLen
  175. for i := 0; i < raws; i++ {
  176. if i == raws-1 {
  177. buffer.WriteString(publicKey[start:])
  178. } else {
  179. buffer.WriteString(publicKey[start:end])
  180. }
  181. buffer.WriteString("\n")
  182. start += rawLen
  183. end = start + rawLen
  184. }
  185. buffer.WriteString("-----END PUBLIC KEY-----\n")
  186. pKey = buffer.String()
  187. return
  188. }