forked from DoersGuild/go-appengine-paypal
-
Notifications
You must be signed in to change notification settings - Fork 9
/
paypal.go
181 lines (153 loc) · 5.72 KB
/
paypal.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package paypal
import (
"net/http"
"net/url"
"fmt"
"io/ioutil"
"strings"
)
const (
NVP_SANDBOX_URL = "https://api-3t.sandbox.paypal.com/nvp"
NVP_PRODUCTION_URL = "https://api-3t.paypal.com/nvp"
CHECKOUT_SANDBOX_URL = "https://www.sandbox.paypal.com/cgi-bin/webscr"
CHECKOUT_PRODUCTION_URL = "https://www.paypal.com/cgi-bin/webscr"
NVP_VERSION = "84"
)
type PayPalClient struct {
username string
password string
signature string
usesSandbox bool
client *http.Client
}
type PayPalDigitalGood struct {
Name string
Amount float64
Quantity int16
}
type PayPalResponse struct {
Ack string
CorrelationId string
Timestamp string
Version string
Build string
Values url.Values
usedSandbox bool
}
type PayPalError struct {
Ack string
ErrorCode string
ShortMessage string
LongMessage string
SeverityCode string
}
func (e *PayPalError) Error() string {
var message string
if len(e.ErrorCode) != 0 && len(e.ShortMessage) != 0 {
message = "PayPal Error " + e.ErrorCode + ": " + e.ShortMessage
} else if len(e.Ack) != 0 {
message = e.Ack
} else {
message = "PayPal is undergoing maintenance.\nPlease try again later."
}
return message
}
func (r *PayPalResponse) CheckoutUrl() string {
query := url.Values{}
query.Set("cmd", "_express-checkout")
query.Add("token", r.Values["TOKEN"][0])
checkoutUrl := CHECKOUT_PRODUCTION_URL
if r.usedSandbox {
checkoutUrl = CHECKOUT_SANDBOX_URL
}
return fmt.Sprintf("%s?%s", checkoutUrl, query.Encode())
}
func SumPayPalDigitalGoodAmounts(goods *[]PayPalDigitalGood) (sum float64) {
for _, dg := range *goods {
sum += dg.Amount * float64(dg.Quantity)
}
return
}
func NewDefaultClient(username, password, signature string, usesSandbox bool) *PayPalClient {
return &PayPalClient{username, password, signature, usesSandbox, new(http.Client)}
}
func NewClient(username, password, signature string, usesSandbox bool, client *http.Client) *PayPalClient {
return &PayPalClient{username, password, signature, usesSandbox, client}
}
func (pClient *PayPalClient) PerformRequest(values url.Values) (*PayPalResponse, error) {
values.Add("USER", pClient.username);
values.Add("PWD", pClient.password);
values.Add("SIGNATURE", pClient.signature);
values.Add("VERSION", NVP_VERSION);
endpoint := NVP_PRODUCTION_URL
if pClient.usesSandbox {
endpoint = NVP_SANDBOX_URL
}
formResponse, err := pClient.client.PostForm(endpoint, values)
if err != nil { return nil, err }
defer formResponse.Body.Close()
body, err := ioutil.ReadAll(formResponse.Body)
if err != nil { return nil, err }
responseValues, err := url.ParseQuery(string(body))
response := &PayPalResponse{usedSandbox: pClient.usesSandbox}
if err == nil {
response.Ack = responseValues.Get("ACK")
response.CorrelationId = responseValues.Get("CORRELATIONID")
response.Timestamp = responseValues.Get("TIMESTAMP")
response.Version = responseValues.Get("VERSION")
response.Build = responseValues.Get("2975009")
response.Values = responseValues
errorCode := responseValues.Get("L_ERRORCODE0")
if len(errorCode) != 0 || strings.ToLower(response.Ack) == "failure" || strings.ToLower(response.Ack) == "failurewithwarning" {
pError := new(PayPalError)
pError.Ack = response.Ack
pError.ErrorCode = errorCode
pError.ShortMessage = responseValues.Get("L_SHORTMESSAGE0")
pError.LongMessage = responseValues.Get("L_LONGMESSAGE0")
pError.SeverityCode = responseValues.Get("L_SEVERITYCODE0")
err = pError
}
}
return response, err
}
func (pClient *PayPalClient) SetExpressCheckoutDigitalGoods(paymentAmount float64, currencyCode string, returnURL, cancelURL string, goods[]PayPalDigitalGood) (*PayPalResponse, error) {
values := url.Values{}
values.Set("METHOD", "SetExpressCheckout")
values.Add("PAYMENTREQUEST_0_AMT", fmt.Sprintf("%.2f", paymentAmount))
values.Add("PAYMENTREQUEST_0_PAYMENTACTION", "Sale");
values.Add("PAYMENTREQUEST_0_CURRENCYCODE", currencyCode);
values.Add("RETURNURL", returnURL);
values.Add("CANCELURL", cancelURL);
values.Add("REQCONFIRMSHIPPING", "0");
values.Add("NOSHIPPING", "1");
values.Add("SOLUTIONTYPE", "Sole");
for i := 0; i < len(goods); i++ {
good := goods[i]
values.Add(fmt.Sprintf("%s%d", "L_PAYMENTREQUEST_0_NAME", i), good.Name)
values.Add(fmt.Sprintf("%s%d", "L_PAYMENTREQUEST_0_AMT", i), fmt.Sprintf("%.2f", good.Amount))
values.Add(fmt.Sprintf("%s%d", "L_PAYMENTREQUEST_0_QTY", i), fmt.Sprintf("%d", good.Quantity))
values.Add(fmt.Sprintf("%s%d", "L_PAYMENTREQUEST_0_ITEMCATEGORY", i), "Digital")
}
return pClient.PerformRequest(values)
}
// Convenience function for Sale (Charge)
func (pClient *PayPalClient) DoExpressCheckoutSale(token, payerId, currencyCode string, finalPaymentAmount float64) (*PayPalResponse, error) {
return pClient.DoExpressCheckoutPayment(token, payerId, "Sale", currencyCode, finalPaymentAmount)
}
// paymentType can be "Sale" or "Authorization" or "Order" (ship later)
func (pClient *PayPalClient) DoExpressCheckoutPayment(token, payerId, paymentType, currencyCode string, finalPaymentAmount float64) (*PayPalResponse, error) {
values := url.Values{}
values.Set("METHOD", "DoExpressCheckoutPayment")
values.Add("TOKEN", token)
values.Add("PAYERID", payerId)
values.Add("PAYMENTREQUEST_0_PAYMENTACTION", paymentType)
values.Add("PAYMENTREQUEST_0_CURRENCYCODE", currencyCode);
values.Add("PAYMENTREQUEST_0_AMT", fmt.Sprintf("%.2f", finalPaymentAmount))
return pClient.PerformRequest(values)
}
func (pClient *PayPalClient) GetExpressCheckoutDetails(token string) (*PayPalResponse, error) {
values := url.Values{}
values.Add("TOKEN", token)
values.Set("METHOD", "GetExpressCheckoutDetails")
return pClient.PerformRequest(values)
}