-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpay.go
160 lines (141 loc) · 4.54 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright The Wechat Pay Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package wechatpay implements V3 endpoints for wechat pay. It is general
// SDK and provides the featrues, such as pay/query/close/notify transaction,
// refund/download bill.
//
// As a quick start:
// client, err := NewClient(Config{})
// // check error
//
// If you want to apply a pay request, use PayRequest
// // create a pay request
// req := &.PayRequest{
// AppId: appId,
// MchId: mchId,
// Description: "for testing",
// TradeType: .Native,
// }
//
// resp, err := req.Do(r.Context(), client)
// if err != nil {
// // do something
// }
// codeUrl := resp.CodeUrl
package wechatpay
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
)
// PayAmount is total amount paid, have total and currency.
type PayAmount struct {
Total int `json:"total"`
Currency string `json:"currency,omitempty"`
}
// PayDetail is the promotion information about the transaction.
type PayDetail struct {
CostPrice int `json:"cost_price,omitempty"`
InvoiceId string `json:"invoice_id,omitempty"`
GoodsDetail []GoodDetail `json:"goods_detail,omitempty"`
}
// GoodDetail is the good information about the transaction.
type GoodDetail struct {
MerchantGoodsId string `json:"merchant_goods_id"`
WechatpayGoodsId string `json:"wechatpay_goods_id,omitempty"`
GoodsName string `json:"goods_name,omitempty"`
Quantity int `json:"quantity"`
UnitPrice int `json:"unit_price"`
}
// PaySceneInfo is the scene information about the transaction.
type PaySceneInfo struct {
PayerClientIp string `json:"payer_client_ip"`
DeviceId string `json:"device_id,omitempty"`
StoreInfo *StoreInfo `json:"store_info,omitempty"`
}
// StoreInfo the store information about the transaction.
type StoreInfo struct {
Id string `json:"id"`
Name string `json:"name,omitempty"`
AreaCode string `json:"area_code,omitempty"`
Address string `json:"address,omitempty"`
}
// PayRequest is request when send a payment.
type PayRequest struct {
AppId string `json:"appid"`
MchId string `json:"mchid"`
Description string `json:"description"`
OutTradeNo string `json:"out_trade_no"`
TimeExpire time.Time `json:"time_expire,omitempty"`
Attach string `json:"attach,omitempty"`
NotifyUrl string `json:"notify_url"`
GoodsTag string `json:"goods_tag,omitempty"`
Amount PayAmount `json:"amount"`
// Only set up Payer for JSAPI
Payer *Payer `json:"payer,omitempty"`
Detail *PayDetail `json:"detail,omitempty"`
SceneInfo *PaySceneInfo `json:"scene_info,omitempty"`
TradeType TradeType `json:"-"`
}
// TradeType is trade type and defined by wechat pay.
type TradeType string
const (
JSAPI TradeType = "JSAPI"
APP TradeType = "APP"
H5 TradeType = "H5"
Native TradeType = "NATIVE"
)
// PayResponse is response when send a payment.
type PayResponse struct {
// The CodeUrl is returned when the merchant used Native
CodeUrl string `json:"code_url"`
// The CodeUrl is returned when the merchant used JSAPI APP
PrepayId string `json:"prepay_id"`
// The CodeUrl is returned when the merchant used H5
H5Url string `json:"h5_url"`
}
// Do send a transaction and invoke wechat payment.
func (r *PayRequest) Do(ctx context.Context, c Client) (*PayResponse, error) {
if r.AppId == "" {
r.AppId = c.Config().AppId
}
if r.MchId == "" {
r.MchId = c.Config().MchId
}
if r.TradeType == "" {
r.TradeType = Native
}
switch r.TradeType {
case JSAPI:
if r.Payer == nil || r.Payer.OpenId == "" {
return nil, errors.New("payer is required for JSAPI")
}
default:
if r.Payer != nil {
return nil, fmt.Errorf("don't set payer is for %v", r.TradeType)
}
}
url := r.url(c.Config().Options().Domain)
resp := &PayResponse{}
if err := c.Do(ctx, http.MethodPost, url, r).Scan(resp); err != nil {
return nil, err
}
return resp, nil
}
func (r *PayRequest) url(domain string) string {
return domain + "/v3/pay/transactions/" + strings.ToLower(string(r.TradeType))
}