Skip to content

Commit 99ac897

Browse files
author
Dathan Vance Pattishall
committed
support for stripe skel
1 parent f31c3f0 commit 99ac897

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module github.com/dathan/go-project-template
22

33
go 1.20
4+
5+
require (
6+
github.com/stretchr/testify v1.10.0
7+
github.com/stripe/stripe-go/v72 v72.122.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
8+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
9+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
10+
github.com/stripe/stripe-go/v72 v72.122.0 h1:eRXWqnEwGny6dneQ5BsxGzUCED5n180u8n665JHlut8=
11+
github.com/stripe/stripe-go/v72 v72.122.0/go.mod h1:QwqJQtduHubZht9mek5sds9CtQcKFdsykV9ZepRWwo0=
12+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
13+
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
14+
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
15+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
16+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
17+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
18+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/stripe/stripe.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package stripe
2+
3+
import (
4+
"github.com/stripe/stripe-go/v72"
5+
"github.com/stripe/stripe-go/v72/paymentintent"
6+
)
7+
8+
// IStripeClient defines the interface for the Stripe client.
9+
type IStripeClient interface {
10+
CreatePaymentIntent(amount int64, currency string) (*stripe.PaymentIntent, error)
11+
ConfirmPaymentIntent(id string) (*stripe.PaymentIntent, error)
12+
}
13+
14+
// Client is a client for interacting with the Stripe API.
15+
type Client struct {
16+
APIKey string
17+
}
18+
19+
// NewClient creates a new Stripe client.
20+
func NewClient(apiKey string) *Client {
21+
stripe.Key = apiKey
22+
return &Client{APIKey: apiKey}
23+
}
24+
25+
// CreatePaymentIntent creates a new payment intent.
26+
func (c *Client) CreatePaymentIntent(amount int64, currency string) (*stripe.PaymentIntent, error) {
27+
params := &stripe.PaymentIntentParams{
28+
Amount: stripe.Int64(amount),
29+
Currency: stripe.String(currency),
30+
}
31+
32+
pi, err := paymentintent.New(params)
33+
if err != nil {
34+
return nil, err
35+
}
36+
return pi, nil
37+
}
38+
39+
// ConfirmPaymentIntent confirms a payment intent.
40+
func (c *Client) ConfirmPaymentIntent(id string) (*stripe.PaymentIntent, error) {
41+
pi, err := paymentintent.Confirm(id, nil)
42+
if err != nil {
43+
return nil, err
44+
}
45+
return pi, nil
46+
}

internal/stripe/stripe_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package stripe
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stripe/stripe-go/v72"
8+
)
9+
10+
// MockStripeClient is a mock of the Stripe client.
11+
type MockStripeClient struct{}
12+
13+
// CreatePaymentIntent is a mock of the CreatePaymentIntent method.
14+
func (m *MockStripeClient) CreatePaymentIntent(amount int64, currency string) (*stripe.PaymentIntent, error) {
15+
pi := &stripe.PaymentIntent{}
16+
pi.ID = "pi_123"
17+
pi.Amount = 1000
18+
pi.Currency = "usd"
19+
return pi, nil
20+
}
21+
22+
// ConfirmPaymentIntent is a mock of the ConfirmPaymentIntent method.
23+
func (m *MockStripeClient) ConfirmPaymentIntent(id string) (*stripe.PaymentIntent, error) {
24+
return &stripe.PaymentIntent{
25+
ID: id,
26+
Status: stripe.PaymentIntentStatusSucceeded,
27+
}, nil
28+
}
29+
30+
func TestCreatePaymentIntent(t *testing.T) {
31+
client := &MockStripeClient{}
32+
amount := int64(1000)
33+
currency := "usd"
34+
35+
pi, err := client.CreatePaymentIntent(amount, currency)
36+
37+
assert.NoError(t, err)
38+
assert.NotNil(t, pi)
39+
assert.Equal(t, "pi_123", pi.ID)
40+
assert.Equal(t, int64(1000), pi.Amount)
41+
assert.Equal(t, "usd", pi.Currency)
42+
}
43+
44+
func TestConfirmPaymentIntent(t *testing.T) {
45+
client := &MockStripeClient{}
46+
id := "pi_123"
47+
48+
pi, err := client.ConfirmPaymentIntent(id)
49+
50+
assert.NoError(t, err)
51+
assert.NotNil(t, pi)
52+
assert.Equal(t, id, pi.ID)
53+
assert.Equal(t, stripe.PaymentIntentStatusSucceeded, pi.Status)
54+
}

0 commit comments

Comments
 (0)