Browse Source

wechat refund check & random

ls 5 years ago
parent
commit
c9f520d429
2 changed files with 34 additions and 0 deletions
  1. 18 0
      wechat/refund.go
  2. 16 0
      wechat/wechat.go

+ 18 - 0
wechat/refund.go

@@ -3,6 +3,7 @@ package wechat
 import (
 	"bytes"
 	"encoding/xml"
+	"errors"
 	"fmt"
 
 	"git.chuangxin1.com/cx/util"
@@ -61,6 +62,23 @@ type WePayRefundReply struct {
 	CashFee       int      `xml:"cash_fee"`   // 现金支付金额
 }
 
+// PayRefundCheck check refund reply
+func PayRefundCheck(reply WePayRefundReply) (ok bool, err error) {
+	//  通信是否正确
+	if reply.ReturnCode == "FAIL" {
+		err = errors.New(reply.ReturnMsg)
+		return
+	}
+	// 交易是否成功
+	if reply.ResultCode == "FAIL" {
+		err = errors.New(reply.ErrCodeDes)
+		return
+	}
+
+	ok = true
+	return
+}
+
 // PayRefund 退款
 func PayRefund(config WePayConfig, order WePayRefund) (reply WePayRefundReply, err error) {
 	var data []byte

+ 16 - 0
wechat/wechat.go

@@ -6,9 +6,11 @@ import (
 	"encoding/xml"
 	"errors"
 	"fmt"
+	"math/rand"
 	"sort"
 	"strings"
 	"sync"
+	"time"
 
 	"git.chuangxin1.com/cx/util"
 )
@@ -120,6 +122,20 @@ func Sign(req map[string]interface{}, key string) (sign string) {
 	return
 }
 
+// Random 指定长度的随机字符串(字母或数字)
+func Random(n int) string {
+	str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+	bytes := []byte(str)
+	result := []byte{}
+
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	n1 := len(bytes)
+	for i := 0; i < n; i++ {
+		result = append(result, bytes[r.Intn(n1)])
+	}
+	return string(result)
+}
+
 func makeSignature(token, signature, timestamp, nonce string) bool {
 	sl := []string{token, timestamp, nonce}
 	sort.Strings(sl)