alipay_server_api.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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("sign", notifyRsp.Sign) //验签时去掉
  86. //body.Set("sign_type", notifyRsp.SignType) //验签时去掉
  87. body.Set("auth_app_id", notifyRsp.AuthAppId)
  88. body.Set("trade_no", notifyRsp.TradeNo)
  89. body.Set("out_trade_no", notifyRsp.OutTradeNo)
  90. body.Set("out_biz_no", notifyRsp.OutBizNo)
  91. body.Set("buyer_id", notifyRsp.BuyerId)
  92. body.Set("buyer_logon_id", notifyRsp.BuyerLogonId)
  93. body.Set("seller_id", notifyRsp.SellerId)
  94. body.Set("seller_email", notifyRsp.SellerEmail)
  95. body.Set("trade_status", notifyRsp.TradeStatus)
  96. body.Set("total_amount", notifyRsp.TotalAmount)
  97. body.Set("receipt_amount", notifyRsp.ReceiptAmount)
  98. body.Set("invoice_amount", notifyRsp.InvoiceAmount)
  99. body.Set("buyer_pay_amount", notifyRsp.BuyerPayAmount)
  100. body.Set("point_amount", notifyRsp.PointAmount)
  101. body.Set("refund_fee", notifyRsp.RefundFee)
  102. body.Set("subject", notifyRsp.Subject)
  103. body.Set("body", notifyRsp.Body)
  104. body.Set("gmt_create", notifyRsp.GmtCreate)
  105. body.Set("gmt_payment", notifyRsp.GmtPayment)
  106. body.Set("gmt_refund", notifyRsp.GmtRefund)
  107. body.Set("gmt_close", notifyRsp.GmtClose)
  108. body.Set("fund_bill_list", jsonToString(notifyRsp.FundBillList))
  109. body.Set("passback_params", notifyRsp.PassbackParams)
  110. body.Set("voucher_detail_list", jsonToString(notifyRsp.VoucherDetailList))
  111. newBody := make(BodyMap)
  112. for k, v := range body {
  113. if v != null {
  114. newBody.Set(k, v)
  115. }
  116. }
  117. pKey := FormatAliPayPublicKey(aliPayPublicKey)
  118. sign, err := getRsaSign(newBody, "RSA", pKey)
  119. if err != nil {
  120. return false, err.Error()
  121. }
  122. ok = sign == notifyRsp.Sign
  123. return
  124. }
  125. func jsonToString(v interface{}) (str string) {
  126. if v == nil {
  127. return ""
  128. }
  129. bs, err := json.Marshal(v)
  130. if err != nil {
  131. fmt.Println("err:", err)
  132. return ""
  133. }
  134. //log.Println("string:", string(bs))
  135. return string(bs)
  136. }
  137. //格式化秘钥
  138. func FormatPrivateKey(privateKey string) (pKey string) {
  139. buffer := new(bytes.Buffer)
  140. buffer.WriteString("-----BEGIN RSA PRIVATE KEY-----\n")
  141. rawLen := 64
  142. keyLen := len(privateKey)
  143. raws := keyLen / rawLen
  144. temp := keyLen % rawLen
  145. if temp > 0 {
  146. raws++
  147. }
  148. start := 0
  149. end := start + rawLen
  150. for i := 0; i < raws; i++ {
  151. if i == raws-1 {
  152. buffer.WriteString(privateKey[start:])
  153. } else {
  154. buffer.WriteString(privateKey[start:end])
  155. }
  156. buffer.WriteString("\n")
  157. start += rawLen
  158. end = start + rawLen
  159. }
  160. buffer.WriteString("-----END RSA PRIVATE KEY-----\n")
  161. pKey = buffer.String()
  162. return
  163. }
  164. //格式化秘钥
  165. func FormatAliPayPublicKey(publicKey string) (pKey string) {
  166. buffer := new(bytes.Buffer)
  167. buffer.WriteString("-----BEGIN PUBLIC KEY-----\n")
  168. rawLen := 64
  169. keyLen := len(publicKey)
  170. raws := keyLen / rawLen
  171. temp := keyLen % rawLen
  172. if temp > 0 {
  173. raws++
  174. }
  175. start := 0
  176. end := start + rawLen
  177. for i := 0; i < raws; i++ {
  178. if i == raws-1 {
  179. buffer.WriteString(publicKey[start:])
  180. } else {
  181. buffer.WriteString(publicKey[start:end])
  182. }
  183. buffer.WriteString("\n")
  184. start += rawLen
  185. end = start + rawLen
  186. }
  187. buffer.WriteString("-----END PUBLIC KEY-----\n")
  188. pKey = buffer.String()
  189. return
  190. }