2024-06-28 22:07:15 +08:00
|
|
|
package unify
|
2024-01-21 18:12:39 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-07-12 16:26:05 +08:00
|
|
|
"git.u8t.cn/open/gosdk/util"
|
2024-01-21 18:12:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
PayTypeWeixin = "weixin"
|
|
|
|
PayTypeAlipay = "alipay"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pay struct {
|
|
|
|
address string
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPay(address string, token string) *Pay {
|
|
|
|
if address == "" {
|
|
|
|
address = "http://127.0.0.1:9281"
|
|
|
|
}
|
|
|
|
return &Pay{
|
|
|
|
address: address,
|
|
|
|
token: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pay) CreateOrder(order *CreateOrderReq) (map[string]interface{}, error) {
|
|
|
|
if order.PayType == "" {
|
|
|
|
errors.New("payType is nil")
|
|
|
|
}
|
|
|
|
if order.PayPrice <= 0 {
|
|
|
|
return nil, errors.New("payPrice is nil")
|
|
|
|
}
|
|
|
|
reqBody, _ := json.Marshal(order)
|
|
|
|
|
|
|
|
result, err := util.HttpPostJson(p.address+"/api/pay/order", map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
}, reqBody)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Data, nil
|
|
|
|
}
|
|
|
|
|
2024-10-27 20:58:17 +08:00
|
|
|
func (p *Pay) CreatePartnerOrder(order *CreatePartnerOrderReq) (map[string]interface{}, error) {
|
2024-10-27 20:43:20 +08:00
|
|
|
|
2024-10-27 20:58:17 +08:00
|
|
|
if order.PayType == "" {
|
|
|
|
errors.New("payType is nil")
|
|
|
|
}
|
|
|
|
if order.PayPrice <= 0 {
|
|
|
|
return nil, errors.New("payPrice is nil")
|
|
|
|
}
|
|
|
|
reqBody, _ := json.Marshal(order)
|
2024-10-27 14:36:57 +08:00
|
|
|
|
|
|
|
result, err := util.HttpPostJson(p.address+"/api/pay/partner/order", map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
}, reqBody)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Data, nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 18:12:39 +08:00
|
|
|
func (p *Pay) UpdateOrder(data map[string]interface{}) error {
|
|
|
|
reqBody, _ := json.Marshal(data)
|
|
|
|
|
|
|
|
result, err := util.HttpPutJson(p.address+"/api/pay/order", map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
}, reqBody)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pay) GetOrder(outTradeNo string) (map[string]interface{}, error) {
|
|
|
|
if outTradeNo == "" {
|
|
|
|
errors.New("outTradeNo is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqUrl := fmt.Sprintf("%s/api/pay/order?outTradeNo=%s", p.address, outTradeNo)
|
|
|
|
result, err := util.HttpGet(reqUrl, map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Data, nil
|
|
|
|
}
|
|
|
|
|
2024-10-27 14:36:57 +08:00
|
|
|
func (p *Pay) GetPartnerOrder(outTradeNo string) (map[string]interface{}, error) {
|
|
|
|
if outTradeNo == "" {
|
|
|
|
errors.New("outTradeNo is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
reqUrl := fmt.Sprintf("%s/api/pay/partner/order?outTradeNo=%s", p.address, outTradeNo)
|
|
|
|
result, err := util.HttpGet(reqUrl, map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Data, nil
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:18:22 +08:00
|
|
|
func (p *Pay) GetPartner(id int64) (map[string]interface{}, error) {
|
|
|
|
reqUrl := fmt.Sprintf("%s/api/pay/partner?id=%d", p.address, id)
|
|
|
|
result, err := util.HttpGet(reqUrl, map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return nil, fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Data, nil
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:45:46 +08:00
|
|
|
func (p *Pay) RefundOrder(req *RefundOrderReq) error {
|
2024-11-04 14:41:07 +08:00
|
|
|
if req.OutTradeNo == "" {
|
2024-01-21 18:12:39 +08:00
|
|
|
errors.New("outTradeNo is nil")
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:41:07 +08:00
|
|
|
reqUrl := fmt.Sprintf("%s/api/pay/order?outTradeNo=%s&reason=%s&refundFee=%d", p.address, req.OutTradeNo, req.Reason, req.RefundFee)
|
2024-01-21 18:12:39 +08:00
|
|
|
result, err := util.HttpDelete(reqUrl, map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-27 14:36:57 +08:00
|
|
|
|
2024-11-04 14:45:46 +08:00
|
|
|
func (p *Pay) RefundPartnerOrder(req *RefundOrderReq) error {
|
|
|
|
if req.OutTradeNo == "" {
|
2024-10-27 14:36:57 +08:00
|
|
|
errors.New("outTradeNo is nil")
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:45:46 +08:00
|
|
|
reqUrl := fmt.Sprintf("%s/api/pay/partner/order?outTradeNo=%s&reason=%s&refundFee=%d", p.address, req.OutTradeNo, req.Reason, req.RefundFee)
|
2024-10-27 14:36:57 +08:00
|
|
|
result, err := util.HttpDelete(reqUrl, map[string]string{
|
|
|
|
"x-token": p.token,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var rsp CommonResponse
|
|
|
|
if err := json.Unmarshal([]byte(result), &rsp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rsp.Code != 0 {
|
|
|
|
return fmt.Errorf("%d:%s", rsp.Code, rsp.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|