yaotian 8 роки тому
батько
коміт
5a29e2c35e
11 змінених файлів з 191 додано та 138 видалено
  1. 1 1
      docs/doc.adoc
  2. 3 3
      docs/doc.html
  3. 1 1
      docs/index.md
  4. 16 0
      mch.go
  5. 96 0
      mch/base/base.go
  6. 1 1
      mch/pay/helper.go
  7. 1 1
      mch/pay/tools.go
  8. 2 90
      mch/pay/pay.go
  9. 23 0
      mch/paytool/cash_coupon.go
  10. 47 0
      mp.go
  11. 0 41
      wechat.go

+ 1 - 1
docs/doc.adoc

@@ -1,4 +1,4 @@
-= GoWechat开发文档
+= GoWechat使用文档
 :toc: left
 
 [[install,安装]]

+ 3 - 3
docs/doc.html

@@ -5,7 +5,7 @@
 <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta name="generator" content="Asciidoctor 1.5.6.1">
-<title>GoWechat开发文档</title>
+<title>GoWechat使用文档</title>
 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
 <style>
 /* Asciidoctor default stylesheet | MIT License | http://asciidoctor.org */
@@ -427,7 +427,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
 </head>
 <body class="article toc2 toc-left">
 <div id="header">
-<h1>GoWechat开发文档</h1>
+<h1>GoWechat使用文档</h1>
 <div id="toc" class="toc2">
 <div id="toctitle">Table of Contents</div>
 <ul class="sectlevel1">
@@ -494,7 +494,7 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
 </div>
 <div id="footer">
 <div id="footer-text">
-Last updated 2017-12-13 17:30:40 CST
+Last updated 2017-12-13 18:05:40 CST
 </div>
 </div>
 </body>

+ 1 - 1
docs/index.md

@@ -10,4 +10,4 @@
 /open| 微信开放平台|   [文档](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN) |
 /corp | 微信企业号  | [文档](http://qydev.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5) |
 
-## [开发文档](./doc.html)
+## [使用文档](./doc.html)

+ 16 - 0
mch.go

@@ -0,0 +1,16 @@
+package gowechat
+
+import (
+	"github.com/yaotian/gowechat/mch/pay"
+	"github.com/yaotian/gowechat/mch/paytool"
+)
+
+// GetPay 基本支付api
+func (wc *Wechat) GetPay() *pay.Pay {
+	return pay.NewPay(wc.Context)
+}
+
+// GetPayTool 支付工具,发红包等
+func (wc *Wechat) GetPayTool() *paytool.PayTool {
+	return paytool.NewPayTool(wc.Context)
+}

+ 96 - 0
mch/base/base.go

@@ -0,0 +1,96 @@
+package base
+
+import (
+	"bytes"
+	"errors"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+
+	"github.com/yaotian/gowechat/server/context"
+)
+
+//MchBase base mch
+type MchBase struct {
+	*context.Context
+}
+
+//PostXML postXML
+func (c *MchBase) PostXML(url string, req map[string]string, needSSL bool) (resp map[string]string, err error) {
+	bodyBuf := textBufferPool.Get().(*bytes.Buffer)
+	bodyBuf.Reset()
+	defer textBufferPool.Put(bodyBuf)
+
+	if err = FormatMapToXML(bodyBuf, req); err != nil {
+		return
+	}
+
+	//需要ssl,就需要ssl client
+	client := c.HTTPClient
+	if needSSL {
+		client = c.SHTTPClient
+	}
+
+	httpResp, err := client.Post(url, "text/xml; charset=utf-8", bodyBuf)
+	if err != nil {
+		return resp, err
+	}
+	defer httpResp.Body.Close()
+
+	if httpResp.StatusCode != http.StatusOK {
+		err = fmt.Errorf("http.Status: %s", httpResp.Status)
+		return
+	}
+
+	respBody, err := ioutil.ReadAll(httpResp.Body)
+	if err != nil {
+		return resp, err
+	}
+
+	if resp, err = ParseXMLToMap(bytes.NewReader(respBody)); err != nil {
+		return
+	}
+
+	// 判断协议状态
+	ReturnCode, ok := resp["return_code"]
+	if !ok {
+		err = errors.New("no return_code parameter")
+		return
+	}
+	if ReturnCode != ReturnCodeSuccess {
+		err = &Error{
+			ReturnCode: ReturnCode,
+			ReturnMsg:  resp["return_msg"],
+		}
+		return
+	}
+
+	// 安全考虑, 做下验证
+	mchId, ok := resp["mch_id"]
+	if ok && mchId != c.MchID {
+		err = fmt.Errorf("mch_id mismatch, have: %q, want: %q", mchId, c.MchID)
+		return
+	}
+
+	//发送红包的情况,不需要验证这些,因为有的信息没有
+	if !needSSL {
+		appId, ok := resp["appid"]
+		if ok && appId != c.AppID {
+			err = fmt.Errorf("appid mismatch, have: %q, want: %q", appId, c.AppID)
+			return
+		}
+
+		// 认证签名
+		signature1, ok := resp["sign"]
+		if !ok {
+			err = errors.New("no sign parameter")
+			return
+		}
+		signature2 := Sign(resp, c.MchAPIKey, nil)
+		if signature1 != signature2 {
+			err = fmt.Errorf("check signature failed, \r\ninput: %q, \r\nlocal: %q", signature1, signature2)
+			return
+		}
+	}
+	return
+}

+ 1 - 1
mch/pay/helper.go

@@ -1,4 +1,4 @@
-package pay
+package base
 
 import "fmt"
 

+ 1 - 1
mch/pay/tools.go

@@ -1,4 +1,4 @@
-package pay
+package base
 
 import (
 	"bytes"

+ 2 - 90
mch/pay/pay.go

@@ -1,19 +1,13 @@
 package pay
 
 import (
-	"bytes"
-	"errors"
-	"fmt"
-	"io/ioutil"
-	"net/http"
-
-	"github.com/astaxie/beego"
+	"github.com/yaotian/gowechat/mch/base"
 	"github.com/yaotian/gowechat/server/context"
 )
 
 //Pay pay
 type Pay struct {
-	*context.Context
+	base.MchBase
 }
 
 //NewPay 实例化
@@ -48,85 +42,3 @@ func (c *Pay) Refund(req map[string]string) (resp map[string]string, err error)
 func (c *Pay) RefundQuery(req map[string]string) (resp map[string]string, err error) {
 	return c.PostXML("https://api.mch.weixin.qq.com/pay/refundquery", req, false)
 }
-
-//PostXML postXML
-func (c *Pay) PostXML(url string, req map[string]string, needSSL bool) (resp map[string]string, err error) {
-	bodyBuf := textBufferPool.Get().(*bytes.Buffer)
-	bodyBuf.Reset()
-	defer textBufferPool.Put(bodyBuf)
-
-	if err = FormatMapToXML(bodyBuf, req); err != nil {
-		return
-	}
-
-	//需要ssl,就需要ssl client
-	client := c.HTTPClient
-	if needSSL {
-		client = c.SHTTPClient
-	}
-
-	httpResp, err := client.Post(url, "text/xml; charset=utf-8", bodyBuf)
-	if err != nil {
-		return resp, err
-	}
-	defer httpResp.Body.Close()
-
-	if httpResp.StatusCode != http.StatusOK {
-		err = fmt.Errorf("http.Status: %s", httpResp.Status)
-		return
-	}
-
-	respBody, err := ioutil.ReadAll(httpResp.Body)
-	if err != nil {
-		return resp, err
-	}
-
-	if resp, err = ParseXMLToMap(bytes.NewReader(respBody)); err != nil {
-		return
-	}
-
-	beego.Debug(resp)
-
-	// 判断协议状态
-	ReturnCode, ok := resp["return_code"]
-	if !ok {
-		err = errors.New("no return_code parameter")
-		return
-	}
-	if ReturnCode != ReturnCodeSuccess {
-		err = &Error{
-			ReturnCode: ReturnCode,
-			ReturnMsg:  resp["return_msg"],
-		}
-		return
-	}
-
-	// 安全考虑, 做下验证
-	mchId, ok := resp["mch_id"]
-	if ok && mchId != c.MchID {
-		err = fmt.Errorf("mch_id mismatch, have: %q, want: %q", mchId, c.MchID)
-		return
-	}
-
-	//发送红包的情况,不需要验证这些,因为有的信息没有
-	if !needSSL {
-		appId, ok := resp["appid"]
-		if ok && appId != c.AppID {
-			err = fmt.Errorf("appid mismatch, have: %q, want: %q", appId, c.AppID)
-			return
-		}
-
-		// 认证签名
-		signature1, ok := resp["sign"]
-		if !ok {
-			err = errors.New("no sign parameter")
-			return
-		}
-		signature2 := Sign(resp, c.MchAPIKey, nil)
-		if signature1 != signature2 {
-			err = fmt.Errorf("check signature failed, \r\ninput: %q, \r\nlocal: %q", signature1, signature2)
-			return
-		}
-	}
-	return
-}

+ 23 - 0
mch/paytool/cash_coupon.go

@@ -0,0 +1,23 @@
+package paytool
+
+import (
+	"github.com/yaotian/gowechat/mch/base"
+	"github.com/yaotian/gowechat/server/context"
+)
+
+//PayTool pay tool
+type PayTool struct {
+	base.MchBase
+}
+
+//NewPayTool 实例化
+func NewPayTool(context *context.Context) *PayTool {
+	payT := new(PayTool)
+	payT.Context = context
+	return payT
+}
+
+//SendRedPack 发现金红包
+func (c *PayTool) SendRedPack(req map[string]string) (resp map[string]string, err error) {
+	return c.PostXML("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", req, true)
+}

+ 47 - 0
mp.go

@@ -0,0 +1,47 @@
+// @description  微信公共平台的接口
+
+package gowechat
+
+import (
+	"github.com/yaotian/gowechat/mp/jssdk"
+	"github.com/yaotian/gowechat/mp/material"
+	"github.com/yaotian/gowechat/mp/menu"
+	"github.com/yaotian/gowechat/mp/template"
+	"github.com/yaotian/gowechat/mp/user"
+	"github.com/yaotian/gowechat/server/oauth"
+)
+
+//GetAccessToken 获取access_token
+func (wc *Wechat) GetAccessToken() (string, error) {
+	return wc.Context.GetAccessToken()
+}
+
+// GetOauth oauth2网页授权
+func (wc *Wechat) GetOauth() *oauth.Oauth {
+	return oauth.NewOauth(wc.Context)
+}
+
+// GetMaterial 素材管理
+func (wc *Wechat) GetMaterial() *material.Material {
+	return material.NewMaterial(wc.Context)
+}
+
+// GetJs js-sdk配置
+func (wc *Wechat) GetJs() *jssdk.Js {
+	return jssdk.NewJs(wc.Context)
+}
+
+// GetMenu 菜单管理接口
+func (wc *Wechat) GetMenu() *menu.Menu {
+	return menu.NewMenu(wc.Context)
+}
+
+// GetUser 用户管理接口
+func (wc *Wechat) GetUser() *user.User {
+	return user.NewUser(wc.Context)
+}
+
+// GetTemplate 模板消息接口
+func (wc *Wechat) GetTemplate() *template.Template {
+	return template.NewTemplate(wc.Context)
+}

+ 0 - 41
wechat.go

@@ -6,14 +6,8 @@ import (
 
 	"github.com/astaxie/beego/cache"
 	"github.com/yaotian/gowechat/mch/pay"
-	"github.com/yaotian/gowechat/mp/jssdk"
-	"github.com/yaotian/gowechat/mp/material"
-	"github.com/yaotian/gowechat/mp/menu"
-	"github.com/yaotian/gowechat/mp/template"
-	"github.com/yaotian/gowechat/mp/user"
 	"github.com/yaotian/gowechat/server"
 	"github.com/yaotian/gowechat/server/context"
-	"github.com/yaotian/gowechat/server/oauth"
 	"github.com/yaotian/gowechat/util"
 )
 
@@ -84,41 +78,6 @@ func (wc *Wechat) GetServer(req *http.Request, writer http.ResponseWriter) *serv
 	return server.NewServer(wc.Context)
 }
 
-//GetAccessToken 获取access_token
-func (wc *Wechat) GetAccessToken() (string, error) {
-	return wc.Context.GetAccessToken()
-}
-
-// GetOauth oauth2网页授权
-func (wc *Wechat) GetOauth() *oauth.Oauth {
-	return oauth.NewOauth(wc.Context)
-}
-
-// GetMaterial 素材管理
-func (wc *Wechat) GetMaterial() *material.Material {
-	return material.NewMaterial(wc.Context)
-}
-
-// GetJs js-sdk配置
-func (wc *Wechat) GetJs() *jssdk.Js {
-	return jssdk.NewJs(wc.Context)
-}
-
-// GetMenu 菜单管理接口
-func (wc *Wechat) GetMenu() *menu.Menu {
-	return menu.NewMenu(wc.Context)
-}
-
-// GetUser 用户管理接口
-func (wc *Wechat) GetUser() *user.User {
-	return user.NewUser(wc.Context)
-}
-
-// GetTemplate 模板消息接口
-func (wc *Wechat) GetTemplate() *template.Template {
-	return template.NewTemplate(wc.Context)
-}
-
 //GetPay get pay
 func (wc *Wechat) GetPay() *pay.Pay {
 	return pay.NewPay(wc.Context)