/
stripe.go
executable file
·52 lines (46 loc) · 1.09 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
package stripeexample
import (
"net"
"time"
"github.com/nu7hatch/gouuid"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/charge"
"golang.org/x/net/context"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
func init() {
stripe.Key = "sk_test_oh4N9TLop9gVlVQ1N05IGamg"
}
func chargeAccount(ctx context.Context, stripeToken string) error {
// because we're on app engine, use a custom http client
b := stripe.BackendConfiguration{
stripe.APIBackend,
"https://api.stripe.com/v1",
urlfetch.Client(ctx),
}
id, _ := uuid.NewV4()
for {
chargeParams := &stripe.ChargeParams{
Amount: 100 * 200000,
Currency: "usd",
Desc: "Charge for test@example.com",
}
chargeParams.IdempotencyKey = id.String()
chargeParams.SetSource(stripeToken)
chargeClient := &charge.Client{
Key: stripe.Key,
B: b,
}
ch, err := chargeClient.New(chargeParams)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(time.Second)
continue
}
return err
}
log.Infof(ctx, "CHARGE: %v", ch)
return nil
}
}