-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe.go
150 lines (127 loc) · 4.05 KB
/
stripe.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
// Package stripe implements GoMerchant payment gateway for Stripe.
package stripe
import (
"fmt"
"time"
"github.com/qor/gomerchant"
stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/charge"
"github.com/stripe/stripe-go/refund"
)
// Stripe implements gomerchant.PaymetGateway interface.
type Stripe struct {
Config *Config
}
var _ gomerchant.PaymentGateway = &Stripe{}
// Config stripe config
type Config struct {
Key string
}
// New creates Stripe struct.
func New(config *Config) *Stripe {
stripe.Key = config.Key
return &Stripe{
Config: config,
}
}
var capture bool = false
func (*Stripe) Authorize(amount uint64, params gomerchant.AuthorizeParams) (gomerchant.AuthorizeResponse, error) {
int64Amount := int64(amount)
chargeParams := &stripe.ChargeParams{
Amount: &int64Amount,
Currency: ¶ms.Currency,
Description: ¶ms.Description,
Capture: &capture,
}
chargeParams.AddMetadata("order_id", params.OrderID)
if params.PaymentMethod != nil {
if params.PaymentMethod.CreditCard != nil {
chargeParams.SetSource(toStripeCC(params.Customer, params.PaymentMethod.CreditCard, params.BillingAddress))
}
if params.PaymentMethod.SavedCreditCard != nil {
if len(params.PaymentMethod.SavedCreditCard.CustomerID) > 0 {
chargeParams.Customer = ¶ms.PaymentMethod.SavedCreditCard.CustomerID
}
chargeParams.SetSource(params.PaymentMethod.SavedCreditCard.CreditCardID)
}
}
charge, err := charge.New(chargeParams)
if charge != nil {
return gomerchant.AuthorizeResponse{TransactionID: charge.ID}, err
}
return gomerchant.AuthorizeResponse{}, err
}
func (*Stripe) CompleteAuthorize(paymentID string, params gomerchant.CompleteAuthorizeParams) (gomerchant.CompleteAuthorizeResponse, error) {
return gomerchant.CompleteAuthorizeResponse{}, nil
}
func (*Stripe) Capture(transactionID string, params gomerchant.CaptureParams) (gomerchant.CaptureResponse, error) {
_, err := charge.Capture(transactionID, nil)
return gomerchant.CaptureResponse{TransactionID: transactionID}, err
}
func (s *Stripe) Refund(transactionID string, amount uint, params gomerchant.RefundParams) (gomerchant.RefundResponse, error) {
transaction, err := s.Query(transactionID)
if err == nil {
if transaction.Captured {
int64Amount := int64(amount)
_, err = refund.New(&stripe.RefundParams{
Charge: &transactionID,
Amount: &int64Amount,
})
} else {
int64Amount := int64(transaction.Amount - int(amount))
_, err = charge.Capture(transactionID, &stripe.CaptureParams{
Amount: &int64Amount,
})
}
}
return gomerchant.RefundResponse{TransactionID: transactionID}, err
}
func (*Stripe) Void(transactionID string, params gomerchant.VoidParams) (gomerchant.VoidResponse, error) {
refundParams := &stripe.RefundParams{
Charge: &transactionID,
}
_, err := refund.New(refundParams)
return gomerchant.VoidResponse{TransactionID: transactionID}, err
}
func (*Stripe) Query(transactionID string) (gomerchant.Transaction, error) {
c, err := charge.Get(transactionID, nil)
created := time.Unix(c.Created, 0)
transaction := gomerchant.Transaction{
ID: c.ID,
Amount: int(c.Amount - c.AmountRefunded),
Currency: string(c.Currency),
Captured: c.Captured,
Paid: c.Paid,
Cancelled: c.Refunded,
Status: c.Status,
CreatedAt: &created,
}
if transaction.Cancelled {
transaction.Paid = false
transaction.Captured = false
}
return transaction, err
}
func toStripeCC(customer string, cc *gomerchant.CreditCard, billingAddress *gomerchant.Address) *stripe.CardParams {
var (
expMonth = fmt.Sprint(cc.ExpMonth)
expYear = fmt.Sprint(cc.ExpYear)
)
cm := stripe.CardParams{
Customer: &customer,
Name: &cc.Name,
Number: &cc.Number,
ExpMonth: &expMonth,
ExpYear: &expYear,
CVC: &cc.CVC,
}
if billingAddress != nil {
cm.AddressLine1 = &billingAddress.Address1
cm.AddressLine1 = &billingAddress.Address2
cm.AddressCity = &billingAddress.City
cm.AddressState = &billingAddress.State
cm.AddressZip = &billingAddress.ZIP
cm.AddressCountry = &billingAddress.Country
}
return &cm
}