conekta 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTE4YzAyYjRjODYyNDg4ZTFmMDM0YmU4ZGI3ZWJhYmQ3MTBjNjI2ZQ==
4
+ ZDNkOGU4MDA2NzIwODAzMGI0ZjE1NDY5ZmVjNGI3MDVmZjBiYmZjZQ==
5
5
  data.tar.gz: !binary |-
6
- ZWQzMzY2NjY1MDg0ZTc1ZjE0MzQ2ZDVjY2I4MzEzY2YxZTFmMzkyMg==
6
+ Y2FhMjIxMzYxNjM0Y2I4MzI1YjA1ZDk2NjE0OWE4OThlYjc3ZGEzZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDk1MjEzZTZmNDkzNTQ4NjgxZjQ4ZDE3ZGExZTYxNzY4NzA1ZjVmN2MzYTQy
10
- OGFiMjlhNmY0MTgyOGIzMWU1OWZkY2JmODE0ZmFjZWIwNDU1ZjA0MWEwZWZk
11
- MWY2NTEzMjFmZTQzMWUxMjM1MmZlNGJkZjc4YWJkZjJlMTNjNjA=
9
+ MThjMjQ0OGIyODNmYWUxMzUxZTczNjQ3N2ExYmYxMWYyYzIyMmUwNDUzY2Ux
10
+ ZmM3NjBjMWVhYjRjNjYzYzYzOGMzNjAyMmNmMzVhZjFkNjE2NWRlMTAzNGFh
11
+ NTkwZjBhMTcwYmU4MDNkZmY2M2RkN2ZlOTk5ZmIxOTc0MjQyN2I=
12
12
  data.tar.gz: !binary |-
13
- YTI4ODIxNzYyNzQ2YWJjNWNkYjU5ZWU3NjY3NmYxMDlkNGFmZmE1MTAzZGY4
14
- NjcwMWNhY2RlY2M5MDFhNGYzN2NmNTkzYzM2MmYwYzc4YWU3ZmMzZDEzZGQw
15
- YmJmNTNlY2EwYWY0NjkyOTQ0ZDkyZmUzMzVmODZiYzI5YjFiZTY=
13
+ ODc1MTU3ZWViM2RiOWExMjBkZjYxMzIzYmFkNjEzNzRhYWI2ZDM2ZDIzMWNh
14
+ OWY3ZmViMTYyZDNmMDA4MThlMDM0ZGZkOGYwNmQ3ZDBjM2M1NGZhOGE3NTM4
15
+ YjM0OTgxZGI4MGEyNDAxNzljNzhmYzJhNzMwYzFkOTg2NWJlMTY=
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG CHANGED
@@ -51,4 +51,8 @@
51
51
 
52
52
  === 0.5.2 2015-01-20
53
53
 
54
- * Change rspec dependency to development.
54
+ * Change rspec dependency to development.
55
+
56
+ == 0.5.3 2015-08-13
57
+
58
+ * Refactorization.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ![alt tag](https://raw.github.com/conekta/conekta-ruby/master/readme_files/cover.png)
2
2
 
3
- # Conekta Ruby v.0.4.6
3
+ # Conekta Ruby v.0.5.2
4
4
 
5
5
  This is a ruby library that allows interaction with https://api.conekta.io API.
6
6
 
@@ -20,11 +20,20 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
  ```ruby
23
+ # Set your configuration variables
24
+
23
25
  # This change the Accept-Language Header to the locale specified
24
26
  Conekta.locale = :es
25
-
27
+
26
28
  Conekta.api_key = '1tv5yJp3xnVZ7eK67m4h'
27
-
29
+
30
+ # Or via an initializer in config/initializers/conekta.rb
31
+ Conekta.config do |c|
32
+ c.locale = :es
33
+ c.api_key = '1tv5yJp3xnVZ7eK67m4h'
34
+ c.api_version = '1.0.0'
35
+ end
36
+
28
37
  begin
29
38
  charge = Conekta::Charge.create({
30
39
  amount: 51000,
@@ -40,7 +49,7 @@ begin
40
49
  rescue Conekta::ProcessingError => e
41
50
  puts e.message
42
51
  #la tarjeta no pudo ser procesada
43
- rescue Conekta::Error
52
+ rescue Conekta::Error => e
44
53
  puts e.message
45
54
  #un error ocurrió que no sucede en el flujo normal de cobros como por ejemplo un auth_key incorrecto
46
55
  end
@@ -20,8 +20,10 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "bundler", "~> 1.3"
22
22
  spec.add_dependency "rake"
23
- spec.add_development_dependency "rspec"
24
23
  spec.add_dependency "faraday"
25
24
  spec.add_dependency "json"
26
25
  spec.add_dependency "sys-uname"
26
+
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry"
27
29
  end
@@ -38,27 +38,38 @@ module Conekta
38
38
  @api_version = '1.0.0'
39
39
  @locale = 'es'
40
40
 
41
+ def self.config
42
+ yield self
43
+ end
44
+
41
45
  def self.api_base
42
46
  @api_base
43
47
  end
48
+
44
49
  def self.api_base=(api_base)
45
50
  @api_base = api_base
46
51
  end
52
+
47
53
  def self.api_version
48
54
  @api_version
49
55
  end
56
+
50
57
  def self.api_version=(api_version)
51
58
  @api_version = api_version
52
59
  end
60
+
53
61
  def self.api_key
54
62
  @api_key
55
63
  end
64
+
56
65
  def self.api_key=(api_key)
57
66
  @api_key = api_key
58
67
  end
68
+
59
69
  def self.locale
60
70
  @locale
61
71
  end
72
+
62
73
  def self.locale=(locale)
63
74
  @locale = locale
64
75
  end
@@ -31,40 +31,40 @@ module Conekta
31
31
  end
32
32
  case code
33
33
  when 400
34
- raise MalformedRequestError.new(@message, @message_to_purchaser, @type, @code, @params)
35
- when 401
36
- raise AuthenticationError.new(@message, @message_to_purchaser, @type, @code, @params)
37
- when 402
38
- raise ProcessingError.new(@message, @message_to_purchaser, @type, @code, @params)
39
- when 404
40
- raise ResourceNotFoundError.new(@message, @message_to_purchaser, @type, @code, @params)
41
- when 422
42
- raise ParameterValidationError.new(@message, @message_to_purchaser, @type, @code, @params)
43
- when 500
44
- raise ApiError.new(@message, @message_to_purchaser, @type, @code, @params)
45
- else
46
- raise Error.new(@message, @message_to_purchaser, @type, @code, @params)
34
+ raise MalformedRequestError.new(@message, @message_to_purchaser, @type, @code, @params)
35
+ when 401
36
+ raise AuthenticationError.new(@message, @message_to_purchaser, @type, @code, @params)
37
+ when 402
38
+ raise ProcessingError.new(@message, @message_to_purchaser, @type, @code, @params)
39
+ when 404
40
+ raise ResourceNotFoundError.new(@message, @message_to_purchaser, @type, @code, @params)
41
+ when 422
42
+ raise ParameterValidationError.new(@message, @message_to_purchaser, @type, @code, @params)
43
+ when 500
44
+ raise ApiError.new(@message, @message_to_purchaser, @type, @code, @params)
45
+ else
46
+ raise Error.new(@message, @message_to_purchaser, @type, @code, @params)
47
47
  end
48
48
  end
49
49
  end
50
- class ApiError < Error
51
- end
52
-
53
- class NoConnectionError < Error
54
- end
50
+ class ApiError < Error
51
+ end
52
+
53
+ class NoConnectionError < Error
54
+ end
55
55
 
56
- class AuthenticationError < Error
57
- end
56
+ class AuthenticationError < Error
57
+ end
58
58
 
59
- class ParameterValidationError < Error
60
- end
59
+ class ParameterValidationError < Error
60
+ end
61
61
 
62
- class ProcessingError < Error
63
- end
62
+ class ProcessingError < Error
63
+ end
64
64
 
65
- class ResourceNotFoundError < Error
66
- end
65
+ class ResourceNotFoundError < Error
66
+ end
67
67
 
68
- class MalformedRequestError < Error
69
- end
70
- end
68
+ class MalformedRequestError < Error
69
+ end
70
+ end
@@ -1,53 +1,69 @@
1
1
  require 'faraday'
2
2
  require 'base64'
3
+
3
4
  module Conekta
4
5
  class Requestor
5
6
  require 'sys/uname'
6
7
  include Sys
8
+
7
9
  attr_reader :api_key
8
- def initialize()
10
+
11
+ def initialize
9
12
  @api_key = Conekta.api_key
10
13
  end
14
+
11
15
  def api_url(url='')
12
16
  api_base = Conekta.api_base
13
17
  api_base + url
14
18
  end
19
+
15
20
  def request(meth, url, params=nil)
16
21
  url = self.api_url(url)
17
22
  meth = meth.downcase
23
+
18
24
  begin
19
- conn = Faraday.new(
20
- :url => url,
21
- :ssl => { :ca_file=> File.dirname(__FILE__) + '/../ssl_data/ca_bundle.crt'}
22
- ) do |faraday|
23
- faraday.adapter Faraday.default_adapter
24
- end
25
- conn.headers['X-Conekta-Client-User-Agent'] = set_headers.to_json
26
- conn.headers['User-Agent'] = 'Conekta/v1 RubyBindings/' + Conekta::VERSION
27
- conn.headers['Accept'] = "application/vnd.conekta-v#{Conekta.api_version}+json"
28
- conn.headers['Accept-Language'] = Conekta.locale.to_s
29
- conn.headers['Authorization'] = "Basic #{ Base64.encode64("#{self.api_key}" + ':')}"
30
- if params
31
- conn.params = params
32
- end
33
- response = conn.method(meth).call
25
+ connection = build_connection(url, params)
26
+ response = connection.method(meth).call
34
27
  rescue Exception => e
35
28
  Error.error_handler(e, "")
36
29
  end
37
- if response.status != 200
38
- Error.error_handler(JSON.parse(response.body), response.status)
39
- end
40
- return JSON.parse(response.body)
30
+
31
+ return Error.error_handler(JSON.parse(response.body), response.status) if response.status != 200
32
+ JSON.parse(response.body)
41
33
  end
34
+
42
35
  private
43
- def set_headers
44
- headers = {}
45
- headers[:bindings_version] = Conekta::VERSION
46
- headers[:lang] = 'ruby'
47
- headers[:lang_version] = RUBY_VERSION
48
- headers[:publisher] = 'conekta'
49
- headers[:uname] = Uname.uname
50
- return headers
36
+
37
+ def build_connection(url, params = nil)
38
+ connection = Faraday.new(
39
+ :url => url,
40
+ :ssl => { :ca_file=> File.dirname(__FILE__) + '/../ssl_data/ca_bundle.crt'}
41
+ ) do |faraday|
42
+ faraday.adapter Faraday.default_adapter
43
+ end
44
+
45
+ set_headers_for(connection)
46
+ connection.params = params if params
47
+ return connection
48
+ end
49
+
50
+ def set_headers_for(connection)
51
+ connection.headers['X-Conekta-Client-User-Agent'] = conekta_headers.to_json
52
+ connection.headers['User-Agent'] = 'Conekta/v1 RubyBindings/' + Conekta::VERSION
53
+ connection.headers['Accept'] = "application/vnd.conekta-v#{Conekta.api_version}+json"
54
+ connection.headers['Accept-Language'] = Conekta.locale.to_s
55
+ connection.headers['Authorization'] = "Basic #{ Base64.encode64("#{self.api_key}" + ':')}"
56
+ return connection
57
+ end
58
+
59
+ def conekta_headers
60
+ @conekta_headers ||= {
61
+ bindings_version: Conekta::VERSION,
62
+ lang: 'ruby',
63
+ lang_version: RUBY_VERSION,
64
+ publisher: 'conekta',
65
+ uname: Uname.uname
66
+ }
51
67
  end
52
68
  end
53
- end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module Conekta
2
- VERSION = '0.5.2'.freeze
2
+ VERSION = '0.5.3'.freeze
3
3
  end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conekta::Charge do
4
+ let(:payment_method) do
5
+ {
6
+ amount: 2000, currency: 'mxn', description: 'Some desc'
7
+ }
8
+ end
9
+ let(:invalid_payment_method) do
10
+ {
11
+ amount: 10, currency: 'mxn', description: 'Some desc'
12
+ }
13
+ end
14
+ let(:card) { { card: 'tok_test_visa_4242' } }
15
+
16
+ context "get" do
17
+ it "succesfully gets charge" do
18
+ transaction = Conekta::Charge.create(payment_method.merge(card))
19
+ expect(transaction.status).to eq("paid")
20
+ charge = Conekta::Charge.find(transaction.id)
21
+ expect(charge).to be_a(Conekta::Charge)
22
+ end
23
+
24
+ it "test succesful where" do
25
+ charges = Conekta::Charge.where
26
+ charge = charges.first
27
+ expect(charge).to be_a(Conekta::Charge)
28
+ end
29
+ end
30
+
31
+ context "creating charges" do
32
+ it "tests succesful bank payment" do
33
+ bank = { bank: { 'type' => 'banorte' } }
34
+ bank_payment = Conekta::Charge.create(payment_method.merge(bank))
35
+ expect(bank_payment.status).to eq("pending_payment")
36
+ end
37
+
38
+ it "tests succesful card payment" do
39
+ card_payment = Conekta::Charge.create(payment_method.merge(card))
40
+ expect(card_payment.status).to eq("paid")
41
+ end
42
+
43
+ it "tests succesful oxxo payment" do
44
+ oxxo = { cash: { 'type' => 'oxxo' } }
45
+ cash_payment = Conekta::Charge.create(payment_method.merge(oxxo))
46
+ expect(cash_payment.status).to eq("pending_payment")
47
+ end
48
+
49
+ it "tests succesful card payment capture" do
50
+ # pm = @valid_payment_method
51
+ # card = @valid_visa_card
52
+ capture = { capture: false }
53
+ transaction = Conekta::Charge.create(
54
+ payment_method.merge(card).merge(capture)
55
+ )
56
+ expect(transaction.status).to eq("pre_authorized")
57
+ transaction.capture
58
+ expect(transaction.status).to eq("paid")
59
+ end
60
+
61
+ it "test unsuccesful create" do
62
+ expect {
63
+ Conekta::Charge.create(invalid_payment_method.merge(card))
64
+ }.to raise_error(
65
+ Conekta::ParameterValidationError,
66
+ "The minimum for card payments is 3 pesos. Check that the amount " +
67
+ "is in cents as explained in the documentation."
68
+ )
69
+ end
70
+ end
71
+
72
+ context "refunding charges" do
73
+ it "test susccesful refund" do
74
+ transaction = Conekta::Charge.create(payment_method.merge(card))
75
+ expect(transaction.status).to eq("paid")
76
+ transaction.refund
77
+ expect(transaction.status).to eq("refunded")
78
+ end
79
+
80
+ it "test unsusccesful refund" do
81
+ transaction = Conekta::Charge.create(payment_method.merge(card))
82
+ expect(transaction.status).to eq("paid")
83
+
84
+ expect { transaction.refund(3000) }.to raise_error(
85
+ Conekta::ProcessingError,
86
+ "The amount to refund exceeds the charge total."
87
+ )
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe Conekta::Customer do
4
+ let(:customer_data) { { :cards => ["tok_test_visa_4242"] } }
5
+
6
+ context "creating customers" do
7
+ it "successful customer create" do
8
+ customer = Conekta::Customer.create(customer_data)
9
+ expect(customer).to be_a(Conekta::Customer)
10
+ end
11
+
12
+ it "successful customer create" do
13
+ expect { Conekta::Customer.create(
14
+ :cards => ["tok_test_visa_4241"]
15
+ ) }.to raise_error(
16
+ Conekta::ResourceNotFoundError,
17
+ "Object tok_test_visa_4241 could not be found."
18
+ )
19
+ end
20
+ end
21
+
22
+ context "getting customers" do
23
+ it "successful customer get" do
24
+ transaction = Conekta::Customer.create(customer_data)
25
+ customer = Conekta::Customer.find(transaction.id)
26
+ expect(customer).to be_a(Conekta::Customer)
27
+ end
28
+
29
+ it "successful customer where" do
30
+ customers = Conekta::Customer.where
31
+ expect(customers.class.class_name).to eq("ConektaObject")
32
+ expect(customers.first).to be_a(Conekta::Customer)
33
+ end
34
+ end
35
+
36
+ context "updating customers" do
37
+ it "successful customer update" do
38
+ customer = Conekta::Customer.create({
39
+ :cards => ["tok_test_visa_4242"],
40
+ })
41
+ customer.update({name: 'Logan', email: 'logan@x-men.org'})
42
+ expect(customer.name).to eq('Logan')
43
+ end
44
+ end
45
+
46
+ context "deleting customers" do
47
+ it "successful customer delete" do
48
+ customer = Conekta::Customer.create(customer_data)
49
+ customer.delete
50
+ expect(customer.deleted).to eq(true)
51
+ end
52
+ end
53
+
54
+ context "adding cards" do
55
+ let!(:customer) { Conekta::Customer.create(customer_data) }
56
+
57
+ it "add card to customer" do
58
+ card = customer.create_card(:token => 'tok_test_visa_1881')
59
+ expect(customer.cards.count).to eq(2)
60
+ expect(customer.cards.last.last4).to eq('1881')
61
+ end
62
+
63
+ it "test delete card" do
64
+ card = customer.cards[0].delete
65
+ expect(card.deleted).to eq(true)
66
+ end
67
+
68
+ it "test update card" do
69
+ customer.cards[0].update(token: 'tok_test_mastercard_4444', active: false)
70
+ expect(customer.cards[0].last4).to eq('4444')
71
+ end
72
+ end
73
+
74
+ context "managing subscriptions" do
75
+ let!(:customer) { Conekta::Customer.create(customer_data) }
76
+
77
+ context "create" do
78
+ it "test succesful create subscription" do
79
+ subscription = customer.create_subscription({plan: 'gold-plan'})
80
+ expect(subscription.class.class_name).to eq('Subscription')
81
+ end
82
+
83
+ it "test unsuccesful create subscription" do
84
+ expect { customer.create_subscription({plan: 'unexistent-plan'}) }.to \
85
+ raise_error(
86
+ Conekta::ResourceNotFoundError,
87
+ "Object Plan unexistent-plan could not be found."
88
+ )
89
+ end
90
+ end
91
+
92
+ context "update" do
93
+ it "test succesful update subscription" do
94
+ subscription = customer.create_subscription({plan: 'gold-plan'})
95
+ plan = find_or_create_plan('gold-plan2')
96
+
97
+ subscription.update({plan: plan.id})
98
+ expect(subscription.plan_id).to eq('gold-plan2')
99
+ end
100
+ end
101
+
102
+ context "pause/resume" do
103
+ let!(:subscription) { customer.create_subscription({plan: 'gold-plan'}) }
104
+
105
+ it "test succesful pause subscription" do
106
+ subscription.pause
107
+ expect(subscription.status).to eq('paused')
108
+ end
109
+
110
+ it "test succesful resume subscription" do
111
+ subscription.pause
112
+ expect(subscription.status).to eq('paused')
113
+ subscription.resume
114
+ expect(subscription.status).to_not eq('paused')
115
+ end
116
+ end
117
+
118
+ context "cancel" do
119
+ it "test succesful cancel subscription" do
120
+ subscription = customer.create_subscription({plan: 'gold-plan'})
121
+ subscription.cancel
122
+ expect(subscription.status).to eq('canceled')
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def find_or_create_plan(plan_id)
129
+ plan = Conekta::Plan.find(plan_id)
130
+ rescue Conekta::Error => e
131
+ plan = Conekta::Plan.create({
132
+ id: plan_id,
133
+ name: "Gold Plan",
134
+ amount: 10000,
135
+ currency: "MXN",
136
+ interval: "month",
137
+ frequency: 1,
138
+ trial_period_days: 15,
139
+ expiry_count: 12
140
+ })
141
+ return plan
142
+ end