Skip to content

Commit b125fb3

Browse files
authored
Merge pull request #45 from conekta/cleanup/catch_subscription_processing_error
Error handling tests for Golang
2 parents 2da4236 + 104c2c6 commit b125fb3

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

subscription/Subscription_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ func CreateCustomerTest() (*conekta.Customer, error){
1818
return c, err
1919
}
2020

21+
func CreateDeclinedCustomerTest() (*conekta.Customer, error){
22+
cus := &conekta.CustomerParams{}
23+
customerParams := cus.MockCustomerPaymentSource()
24+
customerParams.PaymentSources[0].TokenID = "tok_test_card_declined"
25+
c, err := customer.Create(customerParams)
26+
return c, err
27+
}
28+
2129
func CreatePlanTest() (*conekta.Plan, error) {
2230
pl := &conekta.PlanParams{}
23-
p, err := plan.Create(pl.MockPlanCreate())
31+
pl.MockPlanCreate()
32+
pl.TrialPeriodDays = 0
33+
p, err := plan.Create(pl)
2434
return p, err
2535
}
2636

@@ -40,6 +50,23 @@ func TestCreate(t *testing.T){
4050
assert.Nil(t, err)
4151
}
4252

53+
func TestCreateProcessingError(t *testing.T){
54+
c, _ := CreateDeclinedCustomerTest()
55+
p, _ := CreatePlanTest()
56+
57+
sp := &conekta.SubscriptionParams{
58+
PlanID: p.ID,
59+
CardID: c.DefaultPaymentSourceID,
60+
}
61+
sub, err := Create(c.ID, sp)
62+
63+
assert.Equal(t, sp.CardID, sub.CardID)
64+
assert.Equal(t, sp.PlanID, sub.PlanID)
65+
assert.Equal(t, "past_due", sub.Status)
66+
67+
assert.Nil(t, err)
68+
}
69+
4370

4471
func TestCreateError(t *testing.T) {
4572
cus := &conekta.CustomerParams{}
@@ -64,6 +91,22 @@ func TestResume(t *testing.T) {
6491
assert.Nil(t, err)
6592
}
6693

94+
func TestResumeProcessingError(t *testing.T) {
95+
c, _ := CreateDeclinedCustomerTest()
96+
p, _ := CreatePlanTest()
97+
98+
sp := &conekta.SubscriptionParams{
99+
PlanID: p.ID,
100+
CardID: c.DefaultPaymentSourceID,
101+
}
102+
sub, _ := Create(c.ID, sp)
103+
Pause(sub.CustomerID)
104+
105+
pausedSub, err := Resume(sub.CustomerID)
106+
assert.Nil(t, err)
107+
assert.Equal(t, "past_due", pausedSub.Status)
108+
}
109+
67110
func TestPause(t *testing.T) {
68111
c, _ := CreateCustomerTest()
69112
p, _ := CreatePlanTest()
@@ -90,4 +133,4 @@ func TestCancel(t *testing.T) {
90133

91134
_, err := Cancel(sub.CustomerID)
92135
assert.Nil(t, err)
93-
}
136+
}

subscription/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,39 @@ package subscription
22

33
import (
44
conekta "github.com/conekta/conekta-go"
5+
"encoding/json"
56
//"github.com/google/go-querystring/query"
67
)
78

89
// Create creates a new subscription
910
func Create(id string, p *conekta.SubscriptionParams, customHeaders ...interface{}) (*conekta.Subscription, error) {
1011
sub := &conekta.Subscription{}
1112
err := conekta.MakeRequest("POST", "/customers/"+id+"/subscription", p, sub, customHeaders...)
13+
if err != nil && err.(conekta.Error).ErrorType == "processing_error" {
14+
json.Unmarshal(err.(conekta.Error).Data, sub)
15+
return sub, nil
16+
}
1217
return sub, err
1318
}
1419

1520

1621
func Update(id string, p *conekta.SubscriptionParams) (*conekta.Subscription, error) {
1722
sub := &conekta.Subscription{}
1823
err := conekta.MakeRequest("POST", "/customers/"+id+"/subscription", p, sub)
24+
if err != nil && err.(conekta.Error).ErrorType == "processing_error" {
25+
json.Unmarshal(err.(conekta.Error).Data, sub)
26+
return sub, nil
27+
}
1928
return sub, err
2029
}
2130

2231
func Resume(id string) (*conekta.Subscription, error) {
2332
sub := &conekta.Subscription{}
2433
err := conekta.MakeRequest("POST", "/customers/"+id+"/subscription/resume", &conekta.SubscriptionParams{}, sub)
34+
if err != nil && err.(conekta.Error).ErrorType == "processing_error" {
35+
json.Unmarshal(err.(conekta.Error).Data, sub)
36+
return sub, nil
37+
}
2538
return sub, err
2639
}
2740

0 commit comments

Comments
 (0)