jssdk.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package pay
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "unicode/utf8"
  8. "github.com/yaotian/gowechat/mch/base"
  9. "github.com/yaotian/gowechat/util"
  10. )
  11. //OrderInput 下单
  12. //官网文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
  13. type OrderInput struct {
  14. OpenID string //trade_type=JSAPI时(即公众号支付),此参数必传,此参数为微信用户在商户对应appid下的唯一标识
  15. Body string //String(128)
  16. OutTradeNum string //String(32) 20150806125346 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*@ ,且在同一个商户号下唯一。
  17. TotalFee int //分为单位
  18. IP string
  19. NotifyURL string //异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数
  20. ProductID string //trade_type=NATIVE时(即扫码支付),此参数必传
  21. tradeType string //JSAPI,NATIVE,APP
  22. }
  23. //SetTradeType 设置TradeType
  24. func (c *OrderInput) setTradeType(tradeType string) {
  25. c.tradeType = tradeType
  26. }
  27. //WxPayInfo 统一下单后,返回的信息,这些信息是前端jssdk支付时需要的配置
  28. type WxPayInfo struct {
  29. AppID string `json:"appId"`
  30. TimeStamp string `json:"timeStamp"`
  31. NonceStr string `json:"nonceStr"`
  32. Package string `json:"package"`
  33. SignType string `json:"signType"`
  34. PaySign string `json:"paySign"`
  35. resultMap map[string]string
  36. }
  37. //ToString wx.chooseWXPay content
  38. func (c *WxPayInfo) ToString() (str string) {
  39. return fmt.Sprintf(`
  40. timestamp: %s,
  41. nonceStr: '%s',
  42. package: '%s',
  43. signType: '%s',
  44. paySign: '%s',
  45. `, c.TimeStamp, c.NonceStr, c.Package, c.SignType, c.PaySign)
  46. }
  47. //ToJSON WeixinJSBridge json content
  48. func (c *WxPayInfo) ToJSON() (str string) {
  49. js, err := json.Marshal(c)
  50. if err == nil {
  51. return string(js)
  52. }
  53. return
  54. }
  55. //ToMap result map[string]string
  56. func (c *WxPayInfo) ToMap() (m map[string]string) {
  57. return c.resultMap
  58. }
  59. /*GetJsAPIConfig 前端JsAPI支付时,需要提交的信息
  60. */
  61. func (c *Pay) GetJsAPIConfig(order OrderInput) (config *WxPayInfo, err error) {
  62. order.setTradeType("JSAPI")
  63. err = c.checkOrder(order)
  64. if err != nil {
  65. return
  66. }
  67. var prepayID string
  68. prepayID, err = c.getPrepayID(order)
  69. if err != nil {
  70. return
  71. }
  72. nocestr := util.RandomStr(8)
  73. timestamp := fmt.Sprint(time.Now().Unix())
  74. result := make(map[string]string)
  75. result["appId"] = c.AppID
  76. result["timeStamp"] = timestamp
  77. result["nonceStr"] = nocestr
  78. result["package"] = "prepay_id=" + prepayID
  79. result["signType"] = "MD5"
  80. sign := base.Sign(result, c.MchAPIKey, nil)
  81. result["paySign"] = sign
  82. config = new(WxPayInfo)
  83. config.NonceStr = util.RandomStr(8)
  84. config.TimeStamp = fmt.Sprint(time.Now().Unix())
  85. config.AppID = c.AppID
  86. config.Package = "prepay_id=" + prepayID
  87. config.SignType = "MD5"
  88. config.PaySign = sign
  89. config.resultMap = result
  90. return
  91. }
  92. //GetNativePayQrcodePicURL native支付时二维码图片的url
  93. func (c *Pay) GetNativePayQrcodePicURL(order OrderInput) (qrcodeURL string, err error) {
  94. order.setTradeType("NATIVE")
  95. input := c.createUnifiedOrderMap(order)
  96. var result map[string]string
  97. if result, err = c.UnifiedOrder(input); err == nil { //有prepay_id
  98. qrcodeURL = result["code_url"]
  99. if len(qrcodeURL) == 0 {
  100. err = fmt.Errorf("native pay Qrcode url is empty")
  101. }
  102. }
  103. return
  104. }
  105. // 调用 UnifiedOrder 获得 prepayID
  106. func (c *Pay) getPrepayID(order OrderInput) (prepayID string, err error) {
  107. input := c.createUnifiedOrderMap(order)
  108. var result map[string]string
  109. if result, err = c.UnifiedOrder(input); err == nil { //有prepay_id
  110. prepayID := result["prepay_id"]
  111. if prepayID != "" {
  112. return prepayID, nil
  113. }
  114. err = fmt.Errorf("prepayID is empty")
  115. }
  116. return
  117. }
  118. func (c *Pay) createUnifiedOrderMap(order OrderInput) (input map[string]string) {
  119. input = make(map[string]string)
  120. input["appid"] = c.AppID //设置微信分配的公众账号ID
  121. input["mch_id"] = c.MchID //设置微信支付分配的商户号
  122. input["nonce_str"] = util.RandomStr(5) //设置随机字符串,不长于32位。推荐随机数生成算法
  123. input["body"] = order.Body //获取商品或支付单简要描述的值
  124. input["out_trade_no"] = order.OutTradeNum //设置商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
  125. input["total_fee"] = util.ToStr(order.TotalFee) //设置订单总金额,只能为整数,详见支付金额
  126. input["spbill_create_ip"] = order.IP //设置APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
  127. input["notify_url"] = order.NotifyURL //设置接收微信支付异步通知回调地址
  128. input["trade_type"] = order.tradeType
  129. //设置取值如下:JSAPI,NATIVE,APP,详细说明见参数规定
  130. if order.ProductID != "" {
  131. input["product_id"] = order.ProductID //这个
  132. }
  133. input["openid"] = order.OpenID //设置trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。下单前需要调用【网页授权获取用户信息】接口获取到用户的Openid
  134. //sign
  135. sign := base.Sign(input, c.MchAPIKey, nil)
  136. input["sign"] = sign
  137. return
  138. }
  139. func (c *Pay) checkOrder(order OrderInput) (err error) {
  140. tradeType := order.tradeType
  141. if tradeType != "JSAPI" && tradeType != "APP" && tradeType != "NATIVE" {
  142. return fmt.Errorf("tradeType is invalid")
  143. }
  144. if tradeType == "NATIVE" {
  145. if order.ProductID == "" {
  146. err = fmt.Errorf("Native TradeType need ProductID")
  147. return
  148. }
  149. }
  150. if tradeType == "JSAPI" {
  151. if order.OpenID == "" {
  152. err = fmt.Errorf("OpenID can not be empty when pay mode is JSAPI")
  153. return
  154. }
  155. }
  156. if utf8.RuneCountInString(order.Body) > 128 || order.Body == "" {
  157. err = fmt.Errorf("Body is invalid. Size can not exceed 128.")
  158. return
  159. }
  160. if utf8.RuneCountInString(order.OutTradeNum) > 32 || order.OutTradeNum == "" {
  161. err = fmt.Errorf("OutTradeNum is invalid. Size can not exceed 128.")
  162. return
  163. }
  164. if order.TotalFee <= 0 {
  165. err = fmt.Errorf("Order TotalFee is invalid.")
  166. return
  167. }
  168. if order.IP == "" {
  169. err = fmt.Errorf("Order IP is invalid.")
  170. return
  171. }
  172. if order.NotifyURL == "" {
  173. err = fmt.Errorf("Notify URL is invalid.")
  174. return
  175. }
  176. return
  177. }
  178. //CheckPayNotifyData 检查pay notify url收到的消息,是否是返回成功
  179. func (c *Pay) CheckPayNotifyData(data []byte) (isSuccess bool, err error) {
  180. msg, err := base.ParseXMLToMap(bytes.NewReader(data))
  181. if err != nil {
  182. return
  183. }
  184. ReturnCode, ok := msg["return_code"]
  185. if ReturnCode == base.ReturnCodeSuccess || !ok {
  186. haveAppId := msg["appid"]
  187. if haveAppId != c.AppID {
  188. err = fmt.Errorf("get appid is not same as mine. AppID from response is %s. My server AppID is %s,", haveAppId, c.AppID)
  189. return
  190. }
  191. haveMchId := msg["mch_id"]
  192. if haveMchId != c.MchID {
  193. err = fmt.Errorf("get Mch id is not same as mine. MchID from response is %s. My server MchID is %s,", haveMchId, c.MchID)
  194. return
  195. }
  196. signature1, ok := msg["sign"]
  197. if !ok {
  198. err = fmt.Errorf("no sign got")
  199. return
  200. }
  201. signature2 := base.Sign(msg, c.MchAPIKey, nil)
  202. if signature1 != signature2 {
  203. err = fmt.Errorf("Sign is not same. sige_got is %s, sign_mine is %s", signature1, signature2)
  204. return
  205. }
  206. outTradeNum, ok := msg["out_trade_no"]
  207. if !ok || outTradeNum == "" {
  208. err = fmt.Errorf("no out_trade_no")
  209. return
  210. }
  211. result_code, ok := msg["result_code"]
  212. if !ok {
  213. err = fmt.Errorf("no result_code")
  214. return
  215. }
  216. if result_code == base.ResultCodeSuccess {
  217. isSuccess = true
  218. }
  219. }
  220. return
  221. }