344 lines
9.2 KiB
Go
344 lines
9.2 KiB
Go
package weipinshang_api
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type WeiPinShangClient struct {
|
|
Host string
|
|
ChannelType string
|
|
Key string
|
|
}
|
|
|
|
func NewWeiPinShangClient(host, channelType, key string) *WeiPinShangClient {
|
|
return &WeiPinShangClient{
|
|
Host: host,
|
|
ChannelType: channelType,
|
|
Key: key,
|
|
}
|
|
}
|
|
|
|
// pay
|
|
|
|
func (w *WeiPinShangClient) GetManyPostage(getManyPostageReq *GetManyPostageReq) (res *GetManyPostageRes, err error) {
|
|
fmt.Println("getManyPostageReq", getManyPostageReq)
|
|
paramMap := make(map[string]any)
|
|
|
|
paramMap["goodInfo"] = getManyPostageReq.GoodsInfo
|
|
paramMap["address"] = getManyPostageReq.Address
|
|
paramMap["province"] = getManyPostageReq.Province
|
|
paramMap["county"] = getManyPostageReq.County
|
|
paramMap["city"] = getManyPostageReq.City
|
|
|
|
postRes, err := w.WPSPost("mcang/Mcang/getManyPostage", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换GetManyPostageRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) PreOrder(preOrderReq *PreOrderReq) (res *PreOrderRes, err error) {
|
|
fmt.Println("preOrderReq", preOrderReq)
|
|
paramMap := make(map[string]any)
|
|
paramMap["lockCode"] = preOrderReq.LockCode
|
|
paramMap["consigneeContacts"] = preOrderReq.ConsigneeContacts
|
|
paramMap["consigneePhone"] = preOrderReq.ConsigneePhone
|
|
|
|
paramMap["address"] = preOrderReq.Address
|
|
paramMap["province"] = preOrderReq.Province
|
|
paramMap["area"] = preOrderReq.Area
|
|
paramMap["city"] = preOrderReq.City
|
|
|
|
paramMap["goodInfo"] = preOrderReq.GoodsInfo
|
|
paramMap["source"] = preOrderReq.Source
|
|
postRes, err := w.WPSPost("mcang/Order/preOrder", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换PreOrderRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) CreateOrder(createOrderReq *CreateOrderReq) (res *CreateOrderRes, err error) {
|
|
fmt.Println("createOrderReq", createOrderReq)
|
|
paramMap := make(map[string]any)
|
|
paramMap["lockCode"] = createOrderReq.LockCode
|
|
paramMap["orderNo"] = createOrderReq.OrderNo
|
|
paramMap["noticeUrl"] = createOrderReq.NoticeUrl
|
|
postRes, err := w.WPSPost("mcang/Order/createOrder", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换CreateOrderRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetOrderInfo 1
|
|
|
|
func (w *WeiPinShangClient) GetOrderInfo(orderNo string) (res *GetOrderInfoRes, err error) {
|
|
fmt.Println("orderNo", orderNo)
|
|
paramMap := make(map[string]any)
|
|
paramMap["orderNo"] = orderNo
|
|
postRes, err := w.WPSPost("mcang/Mcang/getOrderInfo", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换GetOrderInfoRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetOrderInfo n
|
|
|
|
func (w *WeiPinShangClient) GetOrderInfoByThirdNO(orderNo string) (res *GetOrderInfoByThirdNORes, err error) {
|
|
fmt.Println("orderNo", orderNo)
|
|
paramMap := make(map[string]any)
|
|
paramMap["orderNo"] = orderNo
|
|
postRes, err := w.WPSPost("mcang/Order/getOrderInfoByThirdNO", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换GetOrderInfoByThirdNORes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// refund
|
|
|
|
func (w *WeiPinShangClient) GetOrderInfoByItemNO(mcOrderNo string) (res *GetOrderInfoByItemNORes, err error) {
|
|
fmt.Println("mcOrderNo", mcOrderNo)
|
|
paramMap := make(map[string]any)
|
|
paramMap["mcOrderNo"] = mcOrderNo
|
|
|
|
postRes, err := w.WPSPost("mcang/Order/getOrderInfoByItemNO", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换GetOrderInfoByItemNORes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) IsRefund(mcOrderNo string) (res *IsRefundRes, err error) {
|
|
fmt.Println("mcOrderNo", mcOrderNo)
|
|
paramMap := make(map[string]any)
|
|
paramMap["mcOrderNo"] = mcOrderNo
|
|
postRes, err := w.WPSPost("mcang/Refunds/IsRefund", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换IsRefundRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) CreateAfsApply(createAfsApplyReq *CreateAfsApplyReq) (res *CreateAfsApplyRes, err error) {
|
|
fmt.Println("createAfsApplyReq", createAfsApplyReq)
|
|
paramMap := make(map[string]any)
|
|
|
|
paramMap["mcOrderNo"] = createAfsApplyReq.McOrderNo
|
|
|
|
paramMap["customerExpect"] = createAfsApplyReq.CustomerExpect
|
|
paramMap["questionDesc"] = createAfsApplyReq.QuestionDesc
|
|
paramMap["questionPic"] = createAfsApplyReq.QuestionPic
|
|
paramMap["customerContactName"] = createAfsApplyReq.CustomerContactName
|
|
paramMap["customerMobilePhone"] = createAfsApplyReq.CustomerMobilePhone
|
|
paramMap["customerTel"] = createAfsApplyReq.CustomerTel
|
|
|
|
paramMap["pickwareProvince"] = createAfsApplyReq.PickwareProvince
|
|
paramMap["pickwareCity"] = createAfsApplyReq.PickwareCity
|
|
paramMap["pickwareCounty"] = createAfsApplyReq.PickwareCounty
|
|
paramMap["pickwareAddress"] = createAfsApplyReq.PickwareAddress
|
|
|
|
postRes, err := w.WPSPost("mcang/Refunds/createAfsApply", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换CreateAfsApplyRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) OrderCancel(mcOrderNo string) (res *OrderCancelRes, err error) {
|
|
fmt.Println("mcOrderNo", mcOrderNo)
|
|
paramMap := make(map[string]any)
|
|
paramMap["mcOrderNo"] = mcOrderNo
|
|
postRes, err := w.WPSPost("mcang/Refunds/orderCancel", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换OrderCancelRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) RefundDelivery(refundDeliveryReq RefundDeliveryReq) (res *RefundDeliveryRes, err error) {
|
|
fmt.Println("refundDeliveryReq", refundDeliveryReq)
|
|
paramMap := make(map[string]any)
|
|
paramMap["mcOrderNo"] = refundDeliveryReq.McOrderNo
|
|
paramMap["deliveryName"] = refundDeliveryReq.DeliveryName
|
|
paramMap["deliveryNo"] = refundDeliveryReq.DeliveryNo
|
|
paramMap["freight"] = refundDeliveryReq.Freight
|
|
paramMap["freightImg"] = refundDeliveryReq.FreightImg
|
|
postRes, err := w.WPSPost("mcang/Refunds/refundDelivery", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换RefundDeliveryRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) Sign(paramMap map[string]any) (res string, err error) {
|
|
var sumParamString string
|
|
if len(paramMap) != 0 {
|
|
sumParamString = SortedString(paramMap) + w.ChannelType + w.Key
|
|
} else {
|
|
sumParamString = w.ChannelType + w.Key
|
|
}
|
|
md5Sum := md5.Sum([]byte(sumParamString))
|
|
md5Str := hex.EncodeToString(md5Sum[:])
|
|
md5Str = strings.ToLower(md5Str)
|
|
return md5Str, nil
|
|
}
|
|
|
|
func (w *WeiPinShangClient) WPSPost(url string, paramMap map[string]any) (res []byte, err error) {
|
|
|
|
bodyByte, _ := json.Marshal(paramMap)
|
|
|
|
req, err := http.NewRequest("POST", w.Host+url, bytes.NewBuffer(bodyByte))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("channelType", w.ChannelType)
|
|
sign, err := w.Sign(paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
req.Header.Set("md5", sign)
|
|
|
|
// 创建 HTTP 客户端
|
|
client := &http.Client{}
|
|
|
|
// 发送请求
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func(Body io.ReadCloser) {
|
|
_ = Body.Close()
|
|
}(resp.Body)
|
|
|
|
// 读取响应体
|
|
res, err = io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
fmt.Printf("res: %s\n", string(res))
|
|
|
|
if !json.Valid(res) {
|
|
return nil, fmt.Errorf("响应不是有效的JSON格式: %s", string(res))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Test ----------------------------------------------------------------------------------------------
|
|
|
|
func (w *WeiPinShangClient) DeliverGoods(deliverGoodsReq *DeliverGoodsReq) (res *TestRes, err error) {
|
|
fmt.Println("deliverGoodsReq", deliverGoodsReq)
|
|
paramMap := make(map[string]any)
|
|
paramMap["c_order_item_no"] = deliverGoodsReq.COrderItemNo
|
|
paramMap["c_delivery_name"] = deliverGoodsReq.CDeliveryName
|
|
paramMap["c_delivery_no"] = deliverGoodsReq.CDeliveryNo
|
|
postRes, err := w.WPSPost("mcang/Cycle/deliverGoods", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换TestRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (w *WeiPinShangClient) UpdateService(updateServiceReq *UpdateServiceReq) (res *TestRes, err error) {
|
|
fmt.Println("updateServiceReq", updateServiceReq)
|
|
paramMap := make(map[string]any)
|
|
paramMap["c_order_item_no"] = updateServiceReq.COrderItemNo
|
|
paramMap["c_type"] = updateServiceReq.CType
|
|
postRes, err := w.WPSPost("mcang/Cycle/updateService", paramMap)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(postRes, &res)
|
|
if err != nil || res == nil {
|
|
err = fmt.Errorf("转换TestRes结构体失败: %s", string(postRes))
|
|
return
|
|
}
|
|
return
|
|
}
|