This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharge_test.go
149 lines (129 loc) · 3.64 KB
/
charge_test.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
package stripe
import (
"testing"
"time"
)
func init() {
// In order to execute Unit Test, you must set your Stripe API Key as
// environment variable, STRIPE_API_KEY=xxxx
if err := SetKeyEnv(); err != nil {
panic(err)
}
}
// Sample Charges to use when creating, deleting, updating Charge data.
var (
// Charge with only the required fields
charge1 = ChargeParams{
Description: "Calzone",
Amount: 400,
Currency: USD,
Card: &CardParams{
Name: "George Costanza",
Number: "4242424242424242",
ExpYear: time.Now().Year() + 1,
ExpMonth: 5,
},
}
)
// TestCreateCharge will test that we can successfully Charge a credit card,
// parse the JSON reponse from Stripe, and that all values are populated as
// expected.
func TestCreateCharge(t *testing.T) {
// Create the charge
resp, err := Charges.Create(&charge1)
if err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
}
if resp.Description != charge1.Description {
t.Errorf("Expected Charge Desc %s, got %s", charge1.Description, resp.Description)
}
if resp.Amount != charge1.Amount {
t.Errorf("Expected Charge Amount %v, got %v", charge1.Amount, resp.Amount)
}
if resp.Card == nil {
t.Errorf("Expected Charge Response to include the Charged Credit Card")
return
}
if resp.Paid != true {
t.Errorf("Expected Charge was paid, got %v", resp.Paid)
}
}
// TestCreateChargeToken attempts to charge using a Card Token.
func TestCreateChargeToken(t *testing.T) {
// Create a Token for the credit card
token, err := Tokens.Create(&token1)
if err != nil {
t.Errorf("Expected Token Creation, got Error %s", err.Error())
}
// Create a Charge that uses a Token
charge := ChargeParams{
Description: "Calzone",
Amount: 400,
Currency: USD,
Token: token.ID,
}
// Create the charge
_, err = Charges.Create(&charge)
if err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
}
}
// TestCreateChargeCustomer attempts to charge a pre-defined customer, meaning
// we don't specify the credit card or token when Creating the charge.
func TestCreateChargeCustomer(t *testing.T) {
// Create a Customer and defer deletion
// This customer should have a credit card setup
cust, _ := Customers.Create(&cust4)
defer Customers.Delete(cust.ID)
if cust.DefaultCard == "" {
t.Errorf("Cannot test charging a customer with no pre-defined Card")
return
}
// Create a Charge that uses a Token
charge := ChargeParams{
Description: "Calzone",
Amount: 400,
Currency: USD,
Customer: cust.ID,
}
// Create the charge
_, err := Charges.Create(&charge)
if err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
}
}
func TestRetrieveCharge(t *testing.T) {
// Create the charge
resp, err := Charges.Create(&charge1)
if err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
return
}
// Retrieve the charge from the database
_, err = Charges.Get(resp.ID)
if err != nil {
t.Errorf("Expected to retrieve Charge by ID, got Error %s", err.Error())
return
}
}
func TestRefundCharge(t *testing.T) {
// Create the charge
resp, err := Charges.Create(&charge1)
if err != nil {
t.Errorf("Expected Successful Charge, got Error %s", err.Error())
return
}
// Refund the full amount
charge, err := Charges.Refund(resp.ID)
if err != nil {
t.Errorf("Expected Refund, got Error %s", err.Error())
return
}
if charge.Refunded == false {
t.Errorf("Expected Refund, however Refund flag was set to false")
}
if charge.AmountRefunded != charge1.Amount {
t.Errorf("Expected AmountRefunded %v, but got %v", charge1.Amount, charge.AmountRefunded)
return
}
}