-
Notifications
You must be signed in to change notification settings - Fork 4
/
i_payment_method-rest.go
212 lines (170 loc) · 5.89 KB
/
i_payment_method-rest.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package paypal
/*
import (
"bytes"
"fmt"
"text/template"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/app/models/checkout"
"github.com/ottemo/commerce/utils"
)
func (it *RestAPI) GetName() string {
return PAYMENT_NAME_REST
}
func (it *RestAPI) GetCode() string {
return PAYMENT_CODE_REST
}
func (it *RestAPI) GetType() string {
return checkout.ConstPaymentTypeCreditCard
}
func (it *RestAPI) IsAllowed(checkoutInstance checkout.InterfaceCheckout) bool {
return true
}
func (it *RestAPI) Authorize(checkoutInstance checkout.InterfaceCheckout) error {
ccInfo := utils.InterfaceToMap(checkoutInstance.GetInfo("cc"))
if !utils.StrKeysInMap(ccInfo, "type", "number", "expire_month", "expire_year", "cvv") {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "ce41da6b-6051-4b3a-883a-4dd37c1c7a1a", "credit card info was not specified")
}
billingAddress := checkoutInstance.GetBillingAddress()
if billingAddress == nil {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "9c57fbf9-cb29-4472-901e-f77a9399dda6", "no billing address information")
}
order := checkoutInstance.GetOrder()
if order == nil {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "6219944f-d62b-40d7-8495-f844be0e562d", "no created order")
}
templateValues := map[string]interface{}{
"intent": "sale",
"payment_method": "credit_card",
"number": utils.InterfaceToString(ccInfo["number"]),
"type": utils.InterfaceToString(ccInfo["type"]),
"expire_month": utils.InterfaceToString(ccInfo["expire_month"]),
"expire_year": utils.InterfaceToString(ccInfo["expire_year"]),
"cvv2": utils.InterfaceToString(ccInfo["cvv"]),
"first_name": billingAddress.GetFirstName(),
"last_name": billingAddress.GetLastName(),
"line1": billingAddress.GetAddressLine1(),
"city": billingAddress.GetCity(),
"state": billingAddress.GetState(),
"postal_code": billingAddress.GetZipCode(),
"country_code": billingAddress.GetCountry(),
"total": fmt.Sprintf("%.2f", order.GetGrandTotal()),
"currency": "USD",
"description": "order id - " + order.GetID(),
}
bodyTemplate := `{
"intent":"{{.intent}}",
"payer":{
"payment_method":"{{.payment_method}}",
"funding_instruments":[
{
"credit_card":{
"number":"{{.number}}",
"type":"{{.type}}",
"expire_month":{{.expire_month}},
"expire_year":{{.expire_year}},
"cvv2":"{{.cvv2}}",
"first_name":"{{.first_name}}",
"last_name":"{{.last_name}}",
"billing_address":{
"line1":"{{.line1}}",
"city":"{{.city}}",
"state":"{{.state}}",
"postal_code":"{{.postal_code}}",
"country_code":"{{.country_code}}"
}
}
}
]
},
"transactions":[
{
"amount":{
"total":"{{.total}}",
"currency":"{{.currency}}"
},
"description":"{{.description}}"
}
]
}`
var body bytes.Buffer
parsedTemplate, _ := template.New("paypal_payment").Parse(bodyTemplate)
parsedTemplate.Execute(&body, templateValues)
fmt.Println(body.String())
request, err := http.NewRequest("POST", "https://api.sandbox.paypal.com/v1/payments/payment", &body)
if err != nil {
return env.ErrorDispatch(err)
}
accessToken, err := it.GetAccessToken(checkoutInstance)
if err != nil {
return env.ErrorDispatch(err)
}
fmt.Println(accessToken)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
request.Header.Add("Authorization", "Bearer "+accessToken)
response, err := http.DefaultClient.Do(request)
if err != nil {
return env.ErrorDispatch(err)
}
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
return env.ErrorDispatch(err)
}
fmt.Println(response)
fmt.Println(string(buf))
result := make(map[string]interface{})
err = json.Unmarshal(buf, &result)
if err != nil {
return env.ErrorDispatch(err)
}
if response.StatusCode != 201 {
if response.StatusCode == 400 {
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "b022da83-61ac-4198-a72d-70f58f65acf0", utils.InterfaceToString(result["details"]))
}
return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "741e875d-0ab5-4e8f-81f9-8abbff10cf78", "payment was not processed")
}
//TODO: should store information to order
return nil
}
func (it *RestAPI) Capture(checkoutInstance checkout.InterfaceCheckout) error {
return nil
}
func (it *RestAPI) Refund(checkoutInstance checkout.InterfaceCheckout) error {
return nil
}
func (it *RestAPI) Void(checkoutInstance checkout.InterfaceCheckout) error {
return nil
}
// returns application access token needed for all other requests
func (it *RestAPI) GetAccessToken(checkoutInstance checkout.InterfaceCheckout) (string, error) {
body := "grant_type=client_credentials"
req, err := http.NewRequest("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", bytes.NewBufferString(body))
if err != nil {
return "", env.ErrorDispatch(err)
}
req.SetBasicAuth("AbrcnhDi238ke9aG2NIQqVkW90oMJVg3B1QsjC68d2xRBLDq8boIrCaigPli", "EPcLWBCmfM_AwSOO1jC6TEDLCg-xZhFrUmXQnvTQ9yZV5_786xc5OkQ4Gx2-")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Language", "en_US")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", env.ErrorDispatch(err)
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", env.ErrorDispatch(err)
}
result := make(map[string]interface{})
err = json.Unmarshal(buf, &result)
if err != nil {
return "", env.ErrorDispatch(err)
}
if token, present := result["access_token"]; present {
return utils.InterfaceToString(token), nil
}
return "", env.ErrorNew(ConstErrorModule, ConstErrorLevel, "96d95546-1595-4fe1-9156-ef8d6e43f172", "unexpected response - without access_token")
}*/