-
Notifications
You must be signed in to change notification settings - Fork 4
/
i_payment_method-express.go
164 lines (134 loc) · 5.94 KB
/
i_payment_method-express.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
package paypal
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/ottemo/commerce/api"
"github.com/ottemo/commerce/app"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
"github.com/ottemo/commerce/app/models/checkout"
"github.com/ottemo/commerce/app/models/order"
"github.com/ottemo/commerce/app/models/visitor"
)
// GetInternalName returns the name of the payment method
func (it Express) GetInternalName() string {
return ConstPaymentName
}
// GetName returns the user customized name of the payment method
func (it *Express) GetName() string {
return utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathTitle))
}
// GetCode returns payment method code
func (it *Express) GetCode() string {
return ConstPaymentCode
}
// GetType returns the type of payment method
func (it *Express) GetType() string {
return checkout.ConstPaymentTypeRemote
}
// IsTokenable checks for method applicability
func (it *Express) IsTokenable(checkoutInstance checkout.InterfaceCheckout) bool {
return false
}
// IsAllowed checks for method applicability
func (it *Express) IsAllowed(checkoutInstance checkout.InterfaceCheckout) bool {
return utils.InterfaceToBool(env.ConfigGetValue(ConstConfigPathEnabled))
}
// Authorize makes payment method authorize operation
func (it *Express) Authorize(orderInstance order.InterfaceOrder, paymentInfo map[string]interface{}) (interface{}, error) {
// getting order information
//--------------------------
grandTotal := orderInstance.GetGrandTotal()
shippingPrice := orderInstance.GetShippingAmount()
// getting request param values
//-----------------------------
user := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathUser))
password := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathPass))
signature := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathSignature))
action := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathAction))
amount := fmt.Sprintf("%.2f", grandTotal)
shippingAmount := fmt.Sprintf("%.2f", shippingPrice)
itemAmount := fmt.Sprintf("%.2f", grandTotal-shippingPrice)
description := "Purchase%20for%20%24" + fmt.Sprintf("%.2f", grandTotal)
custom := orderInstance.GetID()
cancelURL := app.GetcommerceURL("paypal/cancel")
returnURL := app.GetcommerceURL("paypal/success")
// making NVP request
//-------------------
requestParams := "USER=" + user +
"&PWD=" + password +
"&SIGNATURE=" + signature +
"&METHOD=SetExpressCheckout" +
"&VERSION=78" +
"&PAYMENTREQUEST_0_PAYMENTACTION=" + action +
"&PAYMENTREQUEST_0_AMT=" + amount +
"&PAYMENTREQUEST_0_SHIPPINGAMT=" + shippingAmount +
"&PAYMENTREQUEST_0_ITEMAMT=" + itemAmount +
"&PAYMENTREQUEST_0_DESC=" + description +
"&PAYMENTREQUEST_0_CUSTOM=" + custom +
"&PAYMENTREQUEST_0_CURRENCYCODE=USD" +
"&cancelURL=" + cancelURL +
"&returnURL=" + returnURL
// println(requestParams)
nvpGateway := paymentPayPalExpress[
ConstPaymentPayPalNvp][
utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathPayPalExpressGateway))]
request, err := http.NewRequest("GET", nvpGateway+"?"+requestParams, nil)
if err != nil {
return nil, env.ErrorDispatch(err)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, env.ErrorDispatch(err)
}
// reading/decoding response from NVP
//-----------------------------------
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, env.ErrorDispatch(err)
}
responseValues, err := url.ParseQuery(string(responseData))
if err != nil {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "7b6a22eb-a7b1-40b5-a47a-5528753135b2", "payment unexpected response")
}
if responseValues.Get("ACK") != "Success" || responseValues.Get("TOKEN") == "" {
if responseValues.Get("L_ERRORCODE0") != "" {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "5ec8dc9b-f72c-4f35-9b1e-f4353753dd9e", "payment error "+responseValues.Get("L_ERRORCODE0")+": "+"L_LONGMESSAGE0")
}
}
waitingTokensMutex.Lock()
waitingTokens[responseValues.Get("TOKEN")] = utils.InterfaceToString(paymentInfo["sessionID"])
waitingTokensMutex.Unlock()
env.Log("paypal.log", env.ConstLogPrefixInfo, "NEW TRANSACTION: "+
"Visitor ID - "+utils.InterfaceToString(orderInstance.Get("visitor_id"))+", "+
"Order ID - "+utils.InterfaceToString(orderInstance.GetID())+", "+
"TOKEN - "+utils.InterfaceToString(responseValues.Get("TOKEN")))
// redirecting user to PayPal server for following checkout
//---------------------------------------------------------
payPalGateway := paymentPayPalExpress[
ConstPaymentPayPalGateway][
utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathPayPalExpressGateway))]
redirectGateway := payPalGateway + "&token=" + responseValues.Get("TOKEN")
return api.StructRestRedirect{
Result: "redirect",
Location: redirectGateway,
}, nil
}
// Delete saved card from the payment system. **This method is for future use**
func (it *Express) DeleteSavedCard(token visitor.InterfaceVisitorCard) (interface{}, error) {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "fd76ec12-99fd-45e9-9c21-be75ac490654", "Not implemented")
}
// Capture payment method capture operation
func (it *Express) Capture(orderInstance order.InterfaceOrder, paymentInfo map[string]interface{}) (interface{}, error) {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "8569a80f-ffc0-4c99-ab79-89d0cbb90ca7", "Not implemented")
}
// Refund makes payment method refund operation
func (it *Express) Refund(orderInstance order.InterfaceOrder, paymentInfo map[string]interface{}) (interface{}, error) {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "bcd6d1c3-12ad-4e62-a2c6-e90bc61badd3", "Not implemented")
}
// Void makes payment method void operation
func (it *Express) Void(orderInstance order.InterfaceOrder, paymentInfo map[string]interface{}) (interface{}, error) {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "78083247-0f7b-4cf1-84a2-09f6162ca350", "Not implemented")
}