Skip to content

Commit c06b16e

Browse files
authored
Merge pull request #47 from conekta/feature/checkout
Adding Additional Checkout Objects
2 parents e9c3916 + d8b7318 commit c06b16e

File tree

9 files changed

+348
-4
lines changed

9 files changed

+348
-4
lines changed

checkout.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package conekta
2+
3+
// CheckoutRefundParams returns api response object filled
4+
type CheckoutRefundParams struct {
5+
Reason string `json:"reason,omitempty"`
6+
Amount int64 `json:"amount,omitempty"`
7+
}
8+
9+
//CheckoutParams returns api response object filled
10+
type CheckoutParams struct {
11+
AllowedPaymentMethods []string `json:"allowed_payment_methods,omitempty"`
12+
ExpiredAt int64 `json:"expired_at,omitempty"`
13+
FailureUrl string `json:"failure_url,omitempty"`
14+
PaymentsLimitCount int64 `json:"payments_limit_count"`
15+
MultifactorAuthentication bool `json:"multifactor_autentication"`
16+
MonthlyInstallmentsEnabled bool `json:"monthly_installments_enabled"`
17+
MonthlyInstallmentsOptions []int64 `json:"monthly_installments_options,omitempty"`
18+
Name string `json:"name,omitempty"`
19+
NeedsShippingContact bool `json:"needs_shipping_contact"`
20+
OrderTemplate *OrderParams `json:"order_template,omitempty"`
21+
OnDemandEnabled bool `json:"on_demand_enabled"`
22+
Recurrent bool `json:"recurrent"`
23+
SuccessUrl string `json:"success_url,omitempty"`
24+
Type string `json:"type,omitempty"`
25+
}
26+
27+
// Checkout should be a struct of the api response
28+
type Checkout struct {
29+
AllowedInstallmentOptions []int64 `json:"allowed_installment_options,omitempty"`
30+
AllowedPaymentMethods []string `json:"allowed_payment_methods,omitempty"`
31+
EmailsSent int64 `json:"emails_sent,omitempty"`
32+
ExpiredAt int64 `json:"expired_at,omitempty"`
33+
ExpiresAt int64 `json:"expires_at,omitempty"`
34+
Force3dsFlow bool `json:"force_3ds_flow,omitempty"`
35+
ID string `json:"id,omitempty"`
36+
Livemode bool `json:"livemode,omitempty"`
37+
MonthlyInstallmentsEnabled bool `json:"monthl_installments_enabled,omitempty"`
38+
Name string `json:"name,omitempty"`
39+
NeedsShippingContact bool `json:"needs_shipping_contact,omitempty"`
40+
Object string `json:"object,omitempty"`
41+
PaidPaymentsCount int64 `json:"paid_payments_count,omitempty"`
42+
Recurrent bool `json:"recurrent,omitempty"`
43+
Slug string `json:"slug,omitempty"`
44+
SmsSent int64 `json:"sms_sent,omitempty"`
45+
Status string `json:"status,omitempty"`
46+
Type string `json:"type,omitempty"`
47+
Url string `json:"url,omitempty"`
48+
}
49+
50+
// CheckoutList is a list of shippingLines
51+
type CheckoutList struct {
52+
ListMeta
53+
Data []*Checkout `json:"data,omitempty"`
54+
}
55+
56+
// Bytes converts a ChargeParams to []byte
57+
func (c *CheckoutParams) Bytes() []byte {
58+
return paramsToBytes(c)
59+
}

checkout/checkout_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package checkout
2+
3+
import (
4+
"testing"
5+
6+
conekta "github.com/conekta/conekta-go"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func init() {
11+
conekta.APIKey = conekta.TestKey
12+
}
13+
14+
type Query struct {
15+
Currency string `url:"currency,omitempty"`
16+
}
17+
18+
func TestCreate(t *testing.T) {
19+
op := (&conekta.CheckoutParams{}).Mock()
20+
checkoutResponse, err := Create(op)
21+
22+
assert.Equal(t, op.AllowedPaymentMethods, checkoutResponse.AllowedPaymentMethods)
23+
assert.Equal(t, op.ExpiredAt, checkoutResponse.ExpiredAt)
24+
assert.Equal(t, op.MonthlyInstallmentsEnabled, checkoutResponse.MonthlyInstallmentsEnabled)
25+
assert.Equal(t, []int64(nil), checkoutResponse.AllowedInstallmentOptions)
26+
assert.Equal(t, op.Name, checkoutResponse.Name)
27+
assert.Equal(t, op.NeedsShippingContact, checkoutResponse.NeedsShippingContact)
28+
assert.Equal(t, op.Recurrent, checkoutResponse.Recurrent)
29+
assert.Equal(t, op.Type, checkoutResponse.Type)
30+
assert.Equal(t, int64(0), checkoutResponse.EmailsSent)
31+
assert.Equal(t, int64(0), checkoutResponse.SmsSent)
32+
assert.Nil(t, err)
33+
}
34+
35+
func TestCreateValidationError(t *testing.T) {
36+
op := (&conekta.CheckoutParams{}).Mock()
37+
op.ExpiredAt = 0
38+
_, err := Create(op)
39+
assert.NotNil(t, err)
40+
assert.NotNil(t, err.(conekta.Error).ErrorType, "parameter_validation_error")
41+
}
42+
43+
func TestWhere(t *testing.T) {
44+
op := &Query{}
45+
46+
res, err := Where(op)
47+
48+
assert.Nil(t, err)
49+
assert.NotZero(t, len(res.Data))
50+
assert.True(t, res.HasMore)
51+
assert.Equal(t, "list", res.Object)
52+
53+
}
54+
55+
func TestFind(t *testing.T) {
56+
op := (&conekta.CheckoutParams{}).Mock()
57+
checkout, err := Create(op)
58+
res, err := Find(checkout.ID)
59+
assert.Equal(t, checkout.ID, res.ID)
60+
assert.Nil(t, err)
61+
}
62+
63+
func TestCancel(t *testing.T) {
64+
op := (&conekta.CheckoutParams{}).Mock()
65+
checkout, _ := Create(op)
66+
res, err := Cancel(checkout.ID)
67+
68+
assert.NotNil(t, res.ID)
69+
assert.Equal(t, res.ID, checkout.ID)
70+
assert.Equal(t, res.Status, "Cancelled")
71+
assert.Nil(t, err)
72+
73+
}
74+
75+
func TestSendEmail(t *testing.T) {
76+
op := (&conekta.CheckoutParams{}).Mock()
77+
checkout, _ := Create(op)
78+
res, err := SendEmail(checkout.ID, "[email protected]")
79+
80+
assert.Equal(t, res.ID, checkout.ID)
81+
assert.Equal(t, res.EmailsSent, int64(1))
82+
assert.Nil(t, err)
83+
}
84+
func TestSendSms(t *testing.T) {
85+
op := (&conekta.CheckoutParams{}).Mock()
86+
87+
checkout, _ := Create(op)
88+
res, err := SendSms(checkout.ID, "[email protected]")
89+
90+
assert.Equal(t, res.ID, checkout.ID)
91+
assert.Equal(t, res.SmsSent, int64(1))
92+
assert.Nil(t, err)
93+
}

checkout/client.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package checkout
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
conekta "github.com/conekta/conekta-go"
8+
"github.com/google/go-querystring/query"
9+
)
10+
11+
type sendEmailParams struct {
12+
Email string `json:"email,omitempty"`
13+
}
14+
15+
// Bytes converts a ChargeParams to []byte
16+
func (c *sendEmailParams) Bytes() []byte {
17+
r, err := json.Marshal(c)
18+
if err != nil {
19+
return []byte{}
20+
}
21+
return r
22+
}
23+
24+
type sendSmsParams struct {
25+
Phone string `json:"sms,omitempty"`
26+
}
27+
28+
// Bytes converts a ChargeParams to []byte
29+
func (c *sendSmsParams) Bytes() []byte {
30+
r, err := json.Marshal(c)
31+
if err != nil {
32+
return []byte{}
33+
}
34+
return r
35+
}
36+
37+
// Create creates a new checkout
38+
func Create(p *conekta.CheckoutParams, customHeaders ...interface{}) (*conekta.Checkout, error) {
39+
checkout := &conekta.Checkout{}
40+
err := conekta.MakeRequest("POST", "/checkouts", p, checkout, customHeaders...)
41+
return checkout, err
42+
}
43+
44+
// Cancel cancels only a oxxo Checkout
45+
func Cancel(checkoutID string) (*conekta.Checkout, error) {
46+
checkout := &conekta.Checkout{}
47+
err := conekta.MakeRequest("PUT", fmt.Sprintf("/checkouts/%v/cancel", checkoutID), &conekta.EmptyParams{}, checkout)
48+
return checkout, err
49+
}
50+
51+
// Find gets a checkout by id
52+
func Find(id string) (*conekta.Checkout, error) {
53+
checkout := &conekta.Checkout{}
54+
err := conekta.MakeRequest("GET", fmt.Sprintf("/checkouts/%v", id), &conekta.EmptyParams{}, checkout)
55+
return checkout, err
56+
}
57+
58+
// Where gets a checkout by querying parameter
59+
func Where(params interface{}) (*conekta.CheckoutList, error) {
60+
checkout := &conekta.CheckoutList{}
61+
v, _ := query.Values(params)
62+
err := conekta.MakeRequest("GET", fmt.Sprintf("/checkouts?%v", v.Encode()), &conekta.EmptyParams{}, checkout)
63+
return checkout, err
64+
}
65+
66+
// Sends an email
67+
func SendEmail(checkoutID string, email string) (*conekta.Checkout, error) {
68+
checkout := &conekta.Checkout{}
69+
70+
err := conekta.MakeRequest("POST", fmt.Sprintf("/checkouts/%v/email", checkoutID), &sendEmailParams{Email: email}, checkout)
71+
return checkout, err
72+
}
73+
74+
// Sends an SMS
75+
func SendSms(checkoutID string, phone string) (*conekta.Checkout, error) {
76+
checkout := &conekta.Checkout{}
77+
err := conekta.MakeRequest("POST", fmt.Sprintf("/checkouts/%v/sms", checkoutID), &sendSmsParams{Phone: phone}, checkout)
78+
return checkout, err
79+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/conekta/conekta-go
2+
3+
go 1.15
4+
5+
require (
6+
github.com/google/go-querystring v1.0.0
7+
github.com/stretchr/testify v1.7.0
8+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
4+
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

mocks.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package conekta
22

33
import (
4+
"math/rand"
45
"time"
56
)
67

@@ -119,6 +120,33 @@ func (p *ShippingLinesParams) Mock() *ShippingLinesParams {
119120
return p
120121
}
121122

123+
func (p *CheckoutParams) Mock() *CheckoutParams {
124+
p.Name = "Payment Link Name"
125+
p.Type = "PaymentLink"
126+
p.Recurrent = false
127+
p.ExpiredAt = time.Now().Unix() + int64(259200) + int64(rand.Float64()*3600)
128+
p.AllowedPaymentMethods = []string{"cash", "card", "bank_transfer"}
129+
p.NeedsShippingContact = true
130+
p.MonthlyInstallmentsEnabled = false
131+
p.MonthlyInstallmentsOptions = []int64{3, 6, 9, 12}
132+
p.OrderTemplate = &OrderParams{}
133+
p.OrderTemplate.Currency = "MXN"
134+
p.OrderTemplate.LineItems = []*LineItemsParams{
135+
{
136+
Name: "Red Wine",
137+
UnitPrice: 1000,
138+
Quantity: 10,
139+
},
140+
}
141+
p.OrderTemplate.CustomerInfo = &CustomerParams{
142+
Name: "Juan Perez",
143+
144+
Phone: "5566982090",
145+
}
146+
147+
return p
148+
}
149+
122150
// Mock fills OrderParams with dummy data
123151
func (p *OrderParams) Mock() *OrderParams {
124152
cp := &CustomerParams{}
@@ -140,6 +168,26 @@ func (p *OrderParams) Mock() *OrderParams {
140168
return p
141169
}
142170

171+
// Mock fills OrderParams with dummy data
172+
func (p *OrderParams) MockWithCheckout(customerID string) *OrderParams {
173+
p.Currency = "MXN"
174+
p.CustomerInfo = &CustomerParams{
175+
ID: customerID,
176+
}
177+
p.LineItems = []*LineItemsParams{
178+
{
179+
Name: "Box of Cohiba S1s",
180+
UnitPrice: 35000,
181+
Quantity: 1,
182+
},
183+
}
184+
p.Checkout = &OrderCheckoutParams{
185+
ExpiresAt: time.Now().Unix() + int64(259200) + int64(rand.Float64()*3600),
186+
AllowedPaymentMethods: []string{"cash", "card", "bank_transfer"},
187+
}
188+
return p
189+
}
190+
143191
// MockWithoutCharges fills OrderParams with dummy data
144192
func (p *OrderParams) MockWithoutCharges() *OrderParams {
145193
cp := &CustomerParams{}
@@ -314,4 +362,4 @@ func (p *SubscriptionParams) MockSubCreate() *SubscriptionParams {
314362
p.CardID = c.DefaultPaymentSourceID
315363
return p
316364
}
317-
*/
365+
*/

order.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type OrderParams struct {
3131
ShippingLines []*ShippingLinesParams `json:"shipping_lines,omitempty"`
3232
Metadata struct{} `json:"metadata,omitempty"`
3333
Charges []*ChargeParams `json:"charges,omitempty"`
34+
Checkout *OrderCheckoutParams `json:"checkout,omitempty"`
3435
}
3536

3637
// Order should be a struct of the api response
@@ -53,6 +54,27 @@ type Order struct {
5354
ShippingLines *ShippingLinesList `json:"shipping_lines,omitempty"`
5455
DiscountLines *DiscountLinesList `json:"discount_lines,omitempty"`
5556
Charges *ChargesList `json:"charges,omitempty"`
57+
Checkout *Checkout `json:"checkout,omitempty"`
58+
}
59+
60+
type OrderCheckoutParams struct {
61+
Name string `json:"name,omitempty"`
62+
Type string `json:"type,omitempty"`
63+
Recurrent bool `json:"recurrent,omitempty"`
64+
ExpiresAt int64 `json:"expires_at,omitempty"`
65+
AllowedPaymentMethods []string `json:"allowed_payment_methods,omitempty"`
66+
NeedsShippingContact bool `json:"needs_shipping_contact,omitempty"`
67+
MonthlyInstallmentsEnabled bool `json:"monthly_installments_enabled,omitempty"`
68+
MonthlyInstallmentsOptions []int64 `json:"monthly_installments_options,omitempty"`
69+
}
70+
71+
// CustomerInfo describes customer info
72+
type OrderCustomerInfoParams struct {
73+
CustomerID string `json:"customer_id,omitempty"`
74+
Name string `json:"name,omitempty"`
75+
Phone string `json:"phone,omitempty"`
76+
Email string `json:"email,omitempty"`
77+
Corporate bool `json:"corporate,omitempty"`
5678
}
5779

5880
// CustomerInfo describes customer info

order/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package order
22

33
import (
4+
"encoding/json"
5+
46
conekta "github.com/conekta/conekta-go"
57
"github.com/google/go-querystring/query"
6-
"encoding/json"
78
)
89

910
// Create creates a new order
1011
func Create(p *conekta.OrderParams, customHeaders ...interface{}) (*conekta.Order, error) {
1112
ord := &conekta.Order{}
1213
err := conekta.MakeRequest("POST", "/orders", p, ord, customHeaders...)
14+
1315
if err != nil && err.(conekta.Error).ErrorType == "processing_error" {
1416
json.Unmarshal(err.(conekta.Error).Data, ord)
1517
return ord, nil

0 commit comments

Comments
 (0)