/
PayQr.go
84 lines (71 loc) · 2.14 KB
/
PayQr.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
package wechat
import (
"fmt"
"github.com/bennya8/go-union-payment/payloads"
"github.com/bennya8/go-union-payment/utils/date"
"strconv"
"time"
)
type PayQr struct {
Base *Base
}
func (w PayQr) Request(params map[string]string) *payloads.UnionPaymentResult {
uri := w.Base.GetFullGatewayUrl("pay/unifiedorder")
//api
resp, err := w.Base.Request(uri, w.BuildParams(params))
return payloads.NewUnionPaymentResult(err == nil, fmt.Sprintf("%s", err), resp)
}
func (w PayQr) BuildParams(params map[string]string) map[string]string {
var notifyUrl = ""
if len(w.Base.Config.NotifyUrl) > 0 {
notifyUrl = w.Base.Config.NotifyUrl
}
var limitPay = ""
if len(w.Base.Config.LimitPay) > 0 {
limitPay = w.Base.Config.LimitPay[0]
}
var feeType = "CNY"
if len(w.Base.Config.FeeType) > 0 {
feeType = w.Base.Config.FeeType
}
amount, err := strconv.ParseFloat(params["amount"], 64)
if err != nil {
panic("union.payment.error:" + err.Error())
}
totalFee := amount * 100
timeStart := date.TimeFormat(time.Now(), "YYYYMMDDhhiiss")
timeExpire := date.TimeFormat(time.Now().Add(30*time.Minute), "YYYYMMDDhhiiss")
if len(params["time_expire"]) > 0 {
expire, _ := strconv.ParseInt(params["time_expire"], 10, 64)
expireTime := time.Unix(expire, 0)
timeExpire = date.TimeFormat(expireTime, "YYYYMMDDhhiiss")
}
receipt, err := strconv.ParseBool(params["receipt"])
if err != nil {
receipt = false
}
receiptStr := ""
if receipt {
receiptStr = "Y"
}
ret := map[string]string{
"device_info": params["device_info"],
"body": params["subject"],
"detail": params["body"],
"attach": params["return_param"],
"out_trade_no": params["trade_no"],
"fee_type": feeType,
"total_fee": fmt.Sprintf("%f", totalFee),
"spbill_create_ip": params["client_ip"],
"time_start": timeStart,
"time_expire": timeExpire,
"goods_tag": params["goods_tag"],
"notify_url": notifyUrl,
"trade_type": "NATIVE",
"limit_pay": limitPay,
"receipt": receiptStr,
"scene_info": params["scene_info"],
"openid": params["openid"],
}
return ret
}