Jelajahi Sumber

update and fix

Jerry 7 tahun lalu
induk
melakukan
f8fc12283c
5 mengubah file dengan 96 tambahan dan 27 penghapusan
  1. 21 7
      README.md
  2. 12 13
      alipay_params.go
  3. 7 6
      message.go
  4. 43 1
      wechat_client.go
  5. 13 0
      wechat_rsp.go

+ 21 - 7
README.md

@@ -16,7 +16,7 @@
     * APP - app支付
     * MWEB - H5支付
 * 查询订单
-* 关闭订单(开发中)
+* 关闭订单
 * 申请退款(开发中)
 * 查询退款(开发中)
 * 下载对账单(开发中)
@@ -90,11 +90,6 @@ fmt.Println("MwebUrl:", wxRsp.MwebUrl)
 
 ### 查询订单
 ```go
-//初始化微信客户端
-//    appId:应用ID
-//    mchID:商户ID
-//    secretKey:Key值
-//    isProd:是否是正式环境
 client := gopay.NewWeChatClient("wxd678efh567hg6787", "1230000109", "192006250b4c09247ec02edce69f6a2d", true)
 
 //初始化参数结构体
@@ -103,7 +98,7 @@ body.Set("out_trade_no", "CC68aTofMIwVKkVR5UruoBLFFXTAqBfv")
 body.Set("nonce_str", gopay.GetRandomString(32))
 body.Set("sign_type", gopay.SignType_MD5)
 
-//请求订单查询
+//请求查询订单
 wxRsp, err := client.QueryOrder(body)
 if err != nil {
 	fmt.Println("Error:", err)
@@ -112,6 +107,25 @@ if err != nil {
 fmt.Println("Response:", wxRsp)
 ```
 
+### 关闭订单
+```go
+client := gopay.NewWeChatClient("wxd678efh567hg6787", "1230000109", "192006250b4c09247ec02edce69f6a2d", true)
+
+//初始化参数结构体
+body := make(gopay.BodyMap)
+body.Set("out_trade_no", "CC68aTofMIwVKkVR5UruoBLFFXTAqBfv")
+body.Set("nonce_str", gopay.GetRandomString(32))
+body.Set("sign_type", gopay.SignType_MD5)
+
+//请求关闭订单
+wxRsp, err := client.CloseOrder(body)
+if err != nil {
+	fmt.Println("Error:", err)
+	return
+}
+fmt.Println("Response:", wxRsp)
+```
+
 ## 支付宝支付 example
 
 * Coming soon.

+ 12 - 13
alipay_params.go

@@ -14,17 +14,16 @@ type AliPayParams struct {
 	NotifyUrl      string `xml:"notify_url"`
 	TradeType      string `xml:"trade_type"`
 
-	DeviceInfo string     `xml:"device_info"`
-	SignType   string     `xml:"sign_type"`
-	Detail     string     `xml:"detail"`
-	Attach     string     `xml:"attach"`
-	FeeType    string     `xml:"fee_type"`
-	TimeStart  string     `xml:"time_start"`
-	TimeExpire string     `xml:"time_expire"`
-	GoodsTag   string     `xml:"goods_tag"`
-	ProductId  string     `xml:"product_id"`
-	LimitPay   string     `xml:"limit_pay"`
-	Openid     string     `xml:"openid"`
-	Receipt    string     `xml:"receipt"`
-	SceneInfo  *StoreInfo `xml:"scene_info"`
+	DeviceInfo string `xml:"device_info"`
+	SignType   string `xml:"sign_type"`
+	Detail     string `xml:"detail"`
+	Attach     string `xml:"attach"`
+	FeeType    string `xml:"fee_type"`
+	TimeStart  string `xml:"time_start"`
+	TimeExpire string `xml:"time_expire"`
+	GoodsTag   string `xml:"goods_tag"`
+	ProductId  string `xml:"product_id"`
+	LimitPay   string `xml:"limit_pay"`
+	Openid     string `xml:"openid"`
+	Receipt    string `xml:"receipt"`
 }

+ 7 - 6
message.go

@@ -1,10 +1,11 @@
 package gopay
 
-type errorMessage struct {
-	ErrorCode int    `json:"error_code"`
-	ErrorDesc string `json:"error_desc"`
-}
+const (
+	SUCCESS = "SUCCESS"
+	FAIL    = "FAIL"
+)
 
-type successMessage struct {
-	Msg string `json:"msg"`
+type ReturnMessage struct {
+	ReturnCode string `json:"return_code"`
+	ReturnMsg  string `json:"return_msg"`
 }

+ 43 - 1
wechat_client.go

@@ -124,8 +124,50 @@ func (this *weChatClient) QueryOrder(body BodyMap) (wxRsp *weChatQueryOrderRespo
 }
 
 //关闭订单
-func (this *weChatClient) CloseOrder() {
+func (this *weChatClient) CloseOrder(body BodyMap) (wxRsp *weChatCloseOrderResponse, err error) {
+	var sign string
+	body.Set("appid", this.AppId)
+	body.Set("mch_id", this.MchId)
+	//===============生成参数===================
+	if !this.isProd {
+		//沙箱环境
+		body.Set("sign_type", SignType_MD5)
+		//从微信接口获取SanBoxSignKey
+		key, err := getSanBoxSign(this.MchId, body.Get("nonce_str"), this.secretKey, body.Get("sign_type"))
+		if err != nil {
+			return nil, err
+		}
+		sign = getLocalSign(key, body.Get("sign_type"), body)
+	} else {
+		//正式环境
+		//本地计算Sign
+		sign = getLocalSign(this.secretKey, body.Get("sign_type"), body)
+	}
+	body.Set("sign", sign)
 
+	reqXML := generateXml(body)
+	//fmt.Println("req:::", reqXML)
+	//===============发起请求===================
+	agent := gorequest.New()
+	if this.isProd {
+		agent.Post(wxURL_CloseOrder)
+	} else {
+		agent.Post(wxURL_SanBox_CloseOrder)
+	}
+	agent.Type("xml")
+	agent.SendString(reqXML)
+	response, bytes, errs := agent.EndBytes()
+	defer response.Body.Close()
+	if len(errs) > 0 {
+		return nil, errs[0]
+	}
+	//fmt.Println("bytes:", string(bytes))
+	wxRsp = new(weChatCloseOrderResponse)
+	err = xml.Unmarshal(bytes, wxRsp)
+	if err != nil {
+		return nil, err
+	}
+	return wxRsp, nil
 }
 
 //申请退款

+ 13 - 0
wechat_rsp.go

@@ -55,6 +55,19 @@ type weChatQueryOrderResponse struct {
 	TradeStateDesc     string `xml:"trade_state_desc"`
 }
 
+type weChatCloseOrderResponse struct {
+	ReturnCode string `xml:"return_code"`
+	ReturnMsg  string `xml:"return_msg"`
+	Appid      string `xml:"appid"`
+	MchId      string `xml:"mch_id"`
+	DeviceInfo string `xml:"device_info"`
+	NonceStr   string `xml:"nonce_str"`
+	Sign       string `xml:"sign"`
+	ResultCode string `xml:"result_code"`
+	ErrCode    string `xml:"err_code"`
+	ErrCodeDes string `xml:"err_code_des"`
+}
+
 type getSignKeyResponse struct {
 	ReturnCode     string `xml:"return_code"`
 	ReturnMsg      string `xml:"return_msg"`