diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..e574bd95 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +## v2.0.1 / 2021-07-21 + +- 增加卡商网服务 + +## v2.0.0 / 2021-07-20 + +- 发布v2版本 diff --git a/kashangwl/kashangwl.go b/kashangwl/kashangwl.go new file mode 100644 index 00000000..1b2e597d --- /dev/null +++ b/kashangwl/kashangwl.go @@ -0,0 +1,72 @@ +package kashangwl + +import ( + "encoding/json" + "fmt" + "github.com/bitly/go-simplejson" + md52 "gopkg.in/dtapps/go-library.v2/md5" + params2 "gopkg.in/dtapps/go-library.v2/params" + string2 "gopkg.in/dtapps/go-library.v2/string" + "io/ioutil" + "net/http" + "sort" + "strings" + "time" +) + +const api = "http://www.kashangwl.com/api/" + +// KaShangWl 每次请求需传入以下参数 +type KaShangWl struct { + customerId int // 商家编号 + customerKey string // 商家密钥 +} + +// Send 发送 http://cha.kashangwl.com/api-doc/ +func (w *KaShangWl) Send(msg interface{}, url string) (*simplejson.Json, error) { + // 当前时间戳(单位:秒) + timestamp := time.Now().UnixNano() / 1e6 + // 处理数据 + marshal, _ := json.Marshal(msg) + newJson, _ := simplejson.NewJson(marshal) + newJson.Set("customer_id", w.customerId) + newJson.Set("timestamp", timestamp) + signStr := sign(newJson, w.customerKey) + newJson.Set("sign", signStr) + j, e := newJson.MarshalJSON() + if e != nil { + return nil, e + } + resp, e := http.Post(api+url, "application/json", strings.NewReader(string(j))) + if e != nil { + return nil, e + } + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + respJson, err := simplejson.NewJson(body) + if err != nil { + return nil, err + } + return respJson, nil +} + +// md5(key + 参数1名称 + 参数1值 + 参数2名称 + 参数2值...) 加密源串应为{key}customer_id1192442order_id827669582783timestamp1626845767 +func sign(params *simplejson.Json, key string) string { + var dataParams string + // 参数按照参数名的字典升序排列 + var keys []string + for k := range params.MustMap() { + keys = append(keys, k) + } + sort.Strings(keys) + // 拼接 + dataParams = fmt.Sprintf("%s%s", dataParams, key) + for _, k := range keys { + dataParams = fmt.Sprintf("%s%s%s", dataParams, k, params2.GetParamsString(params.Get(k))) + } + // MD5加密 + md5Str := md52.GetMD5Encode(dataParams) + str := string2.ToLower(md5Str) + return str +} diff --git a/kashangwl/kashangwl_test.go b/kashangwl/kashangwl_test.go new file mode 100644 index 00000000..7e5e5ae7 --- /dev/null +++ b/kashangwl/kashangwl_test.go @@ -0,0 +1,24 @@ +package kashangwl + +import ( + "gopkg.in/dtapps/go-library.v2/kashangwl/message" + "gopkg.in/dtapps/go-library.v2/kashangwl/url" + "log" + "testing" +) + +func TestName(t *testing.T) { + wl := KaShangWl{ + customerId: 0000000, + customerKey: "xxx", + } + msg := message.Order{ + OrderId: 827669582783, + } + send, err := wl.Send(msg, url.Order) + if err != nil { + log.Printf("错误:%v\n", err) + return + } + log.Printf("返回:%s\n", send) +} diff --git a/kashangwl/message/message.go b/kashangwl/message/message.go new file mode 100644 index 00000000..f1b8c0f1 --- /dev/null +++ b/kashangwl/message/message.go @@ -0,0 +1,30 @@ +package message + +type Product struct { + ProductId int `json:"product_id"` //true 商品编号 +} + +// ProductRechargeParams 获取商品的充值参数。 +type ProductRechargeParams struct { + ProductId int `json:"product_id"` //true 商品编号 +} + +// Buy 购买商品(不支持购买选号类型的商品) +type Buy struct { + ProductId int `json:"product_id"` //true 商品编号 + RechargeAccount string `json:"recharge_account"` //false 充值账号 + RechargeTemplateInputItems interface{} `json:"recharge_template_input_items"` //false 模板充值参数([1]见下方说明) + Quantity int `json:"quantity"` //true 购买数量 + NotifyUrl string `json:"notify_url"` //false 异步通知地址 + OuterOrderId string `json:"outer_order_id"` //false 外部订单号 +} + +// Order 获取单个订单信息 +type Order struct { + OrderId int64 `json:"order_id"` //true 订单号 +} + +// Customer 获取商家信息 +type Customer struct { + CustomerId int `json:"customer_id"` //true 商家编号 +} diff --git a/kashangwl/url/url.go b/kashangwl/url/url.go new file mode 100644 index 00000000..77be40c5 --- /dev/null +++ b/kashangwl/url/url.go @@ -0,0 +1,9 @@ +package url + +const ( + Product = "product" // 获取单个商品信息 + ProductRechargeParams = "product/recharge-params" // 获取商品的充值参数 + Buy = "buy" // 购买商品(不支持购买选号类型的商品) + Order = "order" // 获取单个订单信息 + Customer = "customer" // 获取商家信息 +) diff --git a/library/go.go b/library/go.go index 2945f60e..b90040c3 100644 --- a/library/go.go +++ b/library/go.go @@ -1,5 +1,5 @@ package library func Version() string { - return "2.0.0" + return "2.0.1" } diff --git a/md5/md5.go b/md5/md5.go index b8fd6e63..3013168a 100644 --- a/md5/md5.go +++ b/md5/md5.go @@ -18,3 +18,15 @@ func Md5ToUpper(str string) string { cipherStr := h.Sum(nil) return strings.ToUpper(hex.EncodeToString(cipherStr)) } + +// GetMD5Encode 返回一个32位md5加密后的字符串 +func GetMD5Encode(data string) string { + h := md5.New() + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum(nil)) +} + +// Get16MD5Encode 返回一个16位md5加密后的字符串 +func Get16MD5Encode(data string) string { + return GetMD5Encode(data)[8:24] +} diff --git a/string/string.go b/string/string.go index 36bb5d9d..714c7d2a 100644 --- a/string/string.go +++ b/string/string.go @@ -4,6 +4,7 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" + "strings" ) func HmacSha256Hex(key, strToSign string) string { @@ -11,3 +12,7 @@ func HmacSha256Hex(key, strToSign string) string { hasher.Write([]byte(strToSign)) return hex.EncodeToString(hasher.Sum(nil)) } + +func ToLower(str string) string { + return strings.ToLower(str) +}