forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alipay_server_api.go
146 lines (134 loc) · 3.91 KB
/
alipay_server_api.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
//==================================
// * Name:Jerry
// * DateTime:2019/6/18 19:24
// * Desc:
//==================================
package gopay
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
//解析支付宝支付完成后的Notify信息
func ParseAliPayNotifyResult(req *http.Request) (notifyRsp *AliPayNotifyRequest, err error) {
notifyRsp = new(AliPayNotifyRequest)
defer req.Body.Close()
err = json.NewDecoder(req.Body).Decode(notifyRsp)
if err != nil {
return nil, err
}
return
}
//支付通知的签名验证和参数签名后的Sign
// aliPayPublicKey:支付宝公钥
// notifyRsp:利用 gopay.ParseAliPayNotifyResult() 得到的结构体
// 返回参数ok:是否验证通过
// 返回参数sign:根据参数计算的sign值,非支付宝返回参数中的Sign
func VerifyAliPayResultSign(aliPayPublicKey string, notifyRsp *AliPayNotifyRequest) (ok bool, sign string) {
body := make(BodyMap)
body.Set("notify_time", notifyRsp.NotifyTime)
body.Set("notify_type", notifyRsp.NotifyType)
body.Set("notify_id", notifyRsp.NotifyId)
body.Set("app_id", notifyRsp.AppId)
body.Set("charset", notifyRsp.Charset)
body.Set("version", notifyRsp.Version)
body.Set("trade_no", notifyRsp.TradeNo)
body.Set("out_trade_no", notifyRsp.OutTradeNo)
body.Set("out_biz_no", notifyRsp.OutBizNo)
body.Set("buyer_id", notifyRsp.BuyerId)
body.Set("buyer_logon_id", notifyRsp.BuyerLogonId)
body.Set("seller_id", notifyRsp.SellerId)
body.Set("seller_email", notifyRsp.SellerEmail)
body.Set("trade_status", notifyRsp.TradeStatus)
body.Set("total_amount", notifyRsp.TotalAmount)
body.Set("receipt_amount", notifyRsp.ReceiptAmount)
body.Set("invoice_amount", notifyRsp.InvoiceAmount)
body.Set("buyer_pay_amount", notifyRsp.BuyerPayAmount)
body.Set("point_amount", notifyRsp.PointAmount)
body.Set("refund_fee", notifyRsp.RefundFee)
body.Set("subject", notifyRsp.Subject)
body.Set("body", notifyRsp.Body)
body.Set("gmt_create", notifyRsp.GmtCreate)
body.Set("gmt_payment", notifyRsp.GmtPayment)
body.Set("gmt_refund", notifyRsp.GmtRefund)
body.Set("gmt_close", notifyRsp.GmtClose)
body.Set("fund_bill_list", jsonToString(notifyRsp.FundBillList))
body.Set("passback_params", notifyRsp.PassbackParams)
body.Set("voucher_detail_list", jsonToString(notifyRsp.VoucherDetailList))
newBody := make(BodyMap)
for k, v := range body {
if v != null {
newBody.Set(k, v)
}
}
sign, err := getRsaSign(newBody, aliPayPublicKey)
if err != nil {
return false, ""
}
ok = sign == notifyRsp.Sign
return
}
func jsonToString(v interface{}) (str string) {
bs, err := json.Marshal(v)
if err != nil {
fmt.Println("err:", err)
return ""
}
//log.Println("string:", string(bs))
return string(bs)
}
//格式化秘钥
func FormatPrivateKey(privateKey string) (pKey string) {
buffer := new(bytes.Buffer)
buffer.WriteString("-----BEGIN RSA PRIVATE KEY-----\n")
rawLen := 64
keyLen := len(privateKey)
raws := keyLen / rawLen
temp := keyLen % rawLen
if temp > 0 {
raws++
}
start := 0
end := start + rawLen
for i := 0; i < raws; i++ {
if i == raws-1 {
buffer.WriteString(privateKey[start:])
} else {
buffer.WriteString(privateKey[start:end])
}
buffer.WriteString("\n")
start += rawLen
end = start + rawLen
}
buffer.WriteString("-----END RSA PRIVATE KEY-----\n")
pKey = buffer.String()
return
}
//格式化秘钥
func FormatAliPayPublicKey(publickKey string) (pKey string) {
buffer := new(bytes.Buffer)
buffer.WriteString("-----BEGIN PUBLIC KEY-----\n")
rawLen := 64
keyLen := len(publickKey)
raws := keyLen / rawLen
temp := keyLen % rawLen
if temp > 0 {
raws++
}
start := 0
end := start + rawLen
for i := 0; i < raws; i++ {
if i == raws-1 {
buffer.WriteString(publickKey[start:])
} else {
buffer.WriteString(publickKey[start:end])
}
buffer.WriteString("\n")
start += rawLen
end = start + rawLen
}
buffer.WriteString("-----END PUBLIC KEY-----\n")
pKey = buffer.String()
return
}