-
Notifications
You must be signed in to change notification settings - Fork 3
/
pay.go
122 lines (105 loc) · 2.53 KB
/
pay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package weixin_shop_pay
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
// Pay 普通支付
type Pay struct {
client *Client
}
// Order 下单
func (t *Pay) Order(p *PayOrder) (*PayOrderResp, error) {
// 请求参数
dataJsonByte, err := json.Marshal(p)
if err != nil {
return nil, err
}
// 发起请求
urlPath := "/v3/pay/partner/transactions/jsapi"
resp, err := tool.PostRequest(t.client.config, urlPath, dataJsonByte)
if err != nil {
return nil, err
}
// 解析返回参数
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// 验证接口是否错误
log.Println("响应结果", string(respData))
log.Println("响应头信息", resp.StatusCode, resp.Status)
if resp.StatusCode != 200 {
err := t.client.setErrorResponse(respData)
if err != nil {
return nil, err
}
}
log.Println(string(respData))
var output PayOrderResp
err = json.Unmarshal(respData, &output)
if err != nil {
return nil, err
}
return &output, nil
}
// QueryOrderTransaction 微信订单查询
func (t *Pay) QueryOrderTransaction(p *PayQueryOrderTransaction) (*PayQueryOrderResp, error) {
// 请求参数
dataJsonByte, err := json.Marshal(p)
if err != nil {
return nil, err
}
// 发起请求
urlPath := "/v3/pay/partner/transactions/id/" + p.TransactionID
resp, err := tool.PostRequest(t.client.config, urlPath, dataJsonByte)
if err != nil {
return nil, err
}
// 解析返回参数
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err := t.client.setErrorResponse(respData)
if err != nil {
return nil, err
}
}
log.Println(string(respData))
var output PayQueryOrderResp
err = json.Unmarshal(respData, &output)
if err != nil {
return nil, err
}
return &output, nil
}
// QueryOrderOutTradeNo 商户订单查询
func (t *Pay) QueryOrderOutTradeNo(p *PayQueryOrderOutTradeNo) (*PayQueryOrderResp, error) {
// 发起请求
urlPath := "/v3/pay/partner/transactions/out-trade-no/" + p.OutTradeNo + fmt.Sprintf("?sp_mchid=%s&sub_mchid=%s", p.SpMchID, p.SubMchID)
resp, err := tool.GetRequest(t.client.config, urlPath)
if err != nil {
return nil, err
}
// 解析返回参数
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err := t.client.setErrorResponse(respData)
if err != nil {
return nil, err
}
}
log.Println(string(respData))
var output PayQueryOrderResp
err = json.Unmarshal(respData, &output)
if err != nil {
return nil, err
}
return &output, nil
}