增加卡商网服务

master
李光春 3 years ago
parent 5fbb50b31e
commit 199fca58e3

@ -0,0 +1,7 @@
## v2.0.1 / 2021-07-21
- 增加卡商网服务
## v2.0.0 / 2021-07-20
- 发布v2版本

@ -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
}

@ -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)
}

@ -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 商家编号
}

@ -0,0 +1,9 @@
package url
const (
Product = "product" // 获取单个商品信息
ProductRechargeParams = "product/recharge-params" // 获取商品的充值参数
Buy = "buy" // 购买商品(不支持购买选号类型的商品)
Order = "order" // 获取单个订单信息
Customer = "customer" // 获取商家信息
)

@ -1,5 +1,5 @@
package library
func Version() string {
return "2.0.0"
return "2.0.1"
}

@ -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]
}

@ -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)
}

Loading…
Cancel
Save