forked from netlify/gocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paypal.go
108 lines (91 loc) · 2.7 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
package api
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/guregu/kami"
paypalsdk "github.com/logpacker/PayPal-Go-SDK"
)
type Experience struct {
profile *paypalsdk.WebProfile
mutex sync.Mutex
}
var paypalExperience Experience
// PaypalCreatePayment creates a new payment that can be authorized in the browser
func (a *API) PaypalCreatePayment(ctx context.Context, w http.ResponseWriter, r *http.Request) {
profile, err := a.getExperience()
if err != nil {
internalServerError(w, fmt.Sprintf("Error creating paypal experience: %v", err))
return
}
amount := &paypalsdk.Amount{
Total: r.FormValue("amount"),
Currency: r.FormValue("currency"),
}
a.log.Infof("Creating paypal payment with profile %v: %v", profile, amount)
redirectURI := a.config.SiteURL + "/netlify-commerce/paypal"
cancelURI := a.config.SiteURL + "/netlify-commerce/paypal/cancel"
paymentResult, err := a.paypal.CreatePayment(paypalsdk.Payment{
Intent: "sale",
Payer: &paypalsdk.Payer{
PaymentMethod: "paypal",
},
ExperienceProfileID: profile.ID,
Transactions: []paypalsdk.Transaction{paypalsdk.Transaction{
Amount: amount,
Description: r.FormValue("description"),
}},
RedirectURLs: &paypalsdk.RedirectURLs{
ReturnURL: redirectURI,
CancelURL: cancelURI,
},
})
if err != nil {
internalServerError(w, fmt.Sprintf("Error creating paypal payment: %v", err))
return
}
sendJSON(w, 200, paymentResult)
}
// PaypalGetPayment retrieves information on an authorized paypal payment, including
// the shipping address
func (a *API) PaypalGetPayment(ctx context.Context, w http.ResponseWriter, r *http.Request) {
payment, err := a.paypal.GetPayment(kami.Param(ctx, "payment_id"))
if err != nil {
internalServerError(w, fmt.Sprintf("Error fetching paypal payment: %v", err))
return
}
sendJSON(w, 200, payment)
}
func (a *API) getExperience() (*paypalsdk.WebProfile, error) {
if paypalExperience.profile != nil {
return paypalExperience.profile, nil
}
experiences, err := a.paypal.GetWebProfiles()
if err != nil {
a.log.Errorf("Error getting web profiles: %v", err)
return nil, err
}
for _, profile := range experiences {
if profile.Name == "netlify-commerce" {
paypalExperience.mutex.Lock()
paypalExperience.profile = &profile
paypalExperience.mutex.Unlock()
return paypalExperience.profile, nil
}
}
profile, err := a.paypal.CreateWebProfile(paypalsdk.WebProfile{
Name: "netlify-commerce",
InputFields: paypalsdk.InputFields{
NoShipping: 1,
},
})
if err != nil {
a.log.Errorf("Error creating web profile: %v", err)
return nil, err
}
paypalExperience.mutex.Lock()
paypalExperience.profile = profile
paypalExperience.mutex.Unlock()
return profile, nil
}