-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe.go
123 lines (115 loc) · 3.21 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
package stripe
import (
"fmt"
stripe "github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/charge"
"github.com/stripe/stripe-go/v72/customer"
price "github.com/stripe/stripe-go/v72/price"
"github.com/stripe/stripe-go/v72/product"
"github.com/stripe/stripe-go/v72/token"
)
type Stripe interface {
AddProduct(sku, currency string, amount float64) (string, error)
ChargeCustomer(priceID, customerID string) (string, error)
CreateCardToken(cardNmuber, expMonth, expYear, cvc string) (string, error)
CreateCustomer(email, cardToken, userName string) (string, error)
UpdateCustomer(customerID, cardToken string) error
}
func NewStripe(key string) Stripe {
return &Client{APIKey: key}
}
type Client struct {
APIKey string
}
func (c *Client) CreateCardToken(cardNmuber, expMonth, expYear, cvc string) (string, error) {
stripe.Key = c.APIKey
cardParm := &stripe.CardParams{
CVC: stripe.String(cvc),
ExpMonth: stripe.String(expMonth),
ExpYear: stripe.String(expYear),
Number: stripe.String(cardNmuber),
}
tok, err := token.New(&stripe.TokenParams{Card: cardParm})
if err != nil {
return "", err
}
return tok.ID, nil
}
func (c *Client) CreateCustomer(email, cardToken, userName string) (string, error) {
stripe.Key = c.APIKey
parameters := stripe.CustomerParams{
Email: stripe.String(email),
Name: stripe.String(userName),
Source: &stripe.SourceParams{Token: stripe.String(cardToken)},
}
customer, err := customer.New(¶meters)
if err != nil {
return "", err
}
return customer.ID, nil
}
func (c *Client) UpdateCustomer(customerID, cardToken string) error {
stripe.Key = c.APIKey
params := &stripe.CustomerParams{Source: &stripe.SourceParams{Token: stripe.String(cardToken)}}
_, err := customer.Update(customerID, params)
if err != nil {
return err
}
return nil
}
func (c *Client) AddProduct(sku, currency string, amount float64) (string, error) {
stripe.Key = c.APIKey
prod, err := c.addProduct(sku)
if err != nil {
return "", err
}
price, err := c.addPrice(currency, prod, amount)
if err != nil {
return "", err
}
return price, nil
}
func (c *Client) addProduct(sku string) (string, error) {
stripe.Key = c.APIKey
parameters := stripe.ProductParams{
Name: stripe.String(sku),
}
prod, err := product.New(¶meters)
if err != nil {
return "", err
}
return prod.ID, nil
}
func (c *Client) addPrice(currency, product string, amount float64) (string, error) {
stripe.Key = c.APIKey
parameters := stripe.PriceParams{
Currency: stripe.String(currency),
Product: stripe.String(product),
UnitAmountDecimal: stripe.Float64(amount),
}
price, err := price.New(¶meters)
if err != nil {
return "", err
}
return price.ID, nil
}
func (c *Client) ChargeCustomer(priceID, customerID string) (string, error) {
stripe.Key = c.APIKey
item, err := price.Get(priceID, nil)
if err != nil {
return "", err
}
params := stripe.ChargeParams{
Amount: stripe.Int64(item.UnitAmount),
Currency: stripe.String(string(item.Currency)),
Customer: stripe.String(customerID),
}
ch, err := charge.New(¶ms)
if err != nil {
return "", err
}
if ch.Status == "succeeded" {
return ch.ID, nil
}
return "", fmt.Errorf("can't perform the transaction %v", err)
}