epayco 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gitignore +13 -0
  5. data/.rspec +2 -0
  6. data/.travis.yml +10 -0
  7. data/.yardopts +6 -0
  8. data/Gemfile +18 -0
  9. data/README.md +258 -0
  10. data/Rakefile +11 -0
  11. data/epayco.gemspec +27 -0
  12. data/lib/epayco.rb +29 -0
  13. data/lib/epayco/api.rb +29 -0
  14. data/lib/epayco/client.rb +15 -0
  15. data/lib/epayco/client/charge.rb +36 -0
  16. data/lib/epayco/client/customer.rb +58 -0
  17. data/lib/epayco/client/plans.rb +115 -0
  18. data/lib/epayco/client/subscription.rb +150 -0
  19. data/lib/epayco/configuration.rb +74 -0
  20. data/lib/epayco/connection.rb +30 -0
  21. data/lib/epayco/error.rb +31 -0
  22. data/lib/epayco/request.rb +44 -0
  23. data/lib/epayco/response.rb +17 -0
  24. data/lib/epayco/version.rb +3 -0
  25. data/lib/faraday/raise_http_exception.rb +59 -0
  26. data/spec/epay_spec.rb +65 -0
  27. data/spec/epayco/client/charge_spec.rb +40 -0
  28. data/spec/epayco/client/customer_spec.rb +43 -0
  29. data/spec/epayco/client/plans_spec.rb +71 -0
  30. data/spec/epayco/client/subscription_spec.rb +68 -0
  31. data/spec/epayco/client_spec.rb +10 -0
  32. data/spec/fixtures/charge_create.json +10 -0
  33. data/spec/fixtures/customer_all.json +22 -0
  34. data/spec/fixtures/customer_create.json +10 -0
  35. data/spec/fixtures/plan_all.json +18 -0
  36. data/spec/fixtures/plan_create.json +9 -0
  37. data/spec/fixtures/plan_details.json +18 -0
  38. data/spec/fixtures/plan_update.json +9 -0
  39. data/spec/fixtures/subscription_all.json +52 -0
  40. data/spec/fixtures/subscription_cancel.json +8 -0
  41. data/spec/fixtures/subscription_create.json +22 -0
  42. data/spec/spec_helper.rb +74 -0
  43. metadata +177 -0
  44. metadata.gz.sig +0 -0
@@ -0,0 +1,44 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module EPayCo
5
+ # Defines HTTP request methods
6
+ module Request
7
+ # Perform an HTTP GET request
8
+ def get(path, params={}, response_options={})
9
+ request(:get, path, params, response_options)
10
+ end
11
+
12
+ # Perform an HTTP POST request
13
+ def post(path, params={}, response_options={})
14
+ request(:post, path, params, response_options)
15
+ end
16
+
17
+ # Perform an HTTP PUT request
18
+ def put(path, params={}, response_options={})
19
+ request(:put, path, params, response_options)
20
+ end
21
+
22
+ # Perform an HTTP DELETE request
23
+ def delete(path, params={}, response_options={})
24
+ request(:delete, path, params, response_options)
25
+ end
26
+
27
+ private
28
+
29
+ # Perform an HTTP request
30
+ def request(method, path, params, response_options)
31
+ response = connection(response_options[:raw]).send(method) do |request|
32
+ case method
33
+ when :get, :delete
34
+ request.url(URI.encode(path), params)
35
+ when :post, :put
36
+ request.path = URI.encode(path)
37
+ request.body = params.to_json unless params.empty?
38
+ end
39
+ end
40
+
41
+ return Response.create( response, response_options )
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,17 @@
1
+ module EPayCo
2
+ module Response
3
+ def self.create( response, response_options)
4
+ return response if response_options[:raw]
5
+ return response.body if response_options[:no_response_wrapper]
6
+ if response_options[:return_object]
7
+ begin
8
+ object_key = response_options[:return_object].is_a?(String) ? response_options[:return_object] : response.body.object
9
+ return response.body[object_key]
10
+ end
11
+ end
12
+ info = response.body.dup
13
+ info.extend( self )
14
+ info
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module EPayCo
2
+ VERSION = '0.0.2'.freeze unless defined?(::EPayCo::VERSION)
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'faraday'
2
+
3
+ # @private
4
+ module FaradayMiddleware
5
+ # @private
6
+ class RaiseHttpException < Faraday::Middleware
7
+ def call(env)
8
+ @app.call(env).on_complete do |response|
9
+ case response[:status].to_i
10
+ when 400
11
+ raise EPayCo::BadRequest, error_message_400(response)
12
+ when 404
13
+ raise EPayCo::NotFound, error_message_400(response)
14
+ when 429
15
+ raise EPayCo::TooManyRequests, error_message_400(response)
16
+ when 500
17
+ raise EPayCo::InternalServerError, error_message_500(response, "Something is technically wrong.")
18
+ when 502
19
+ raise EPayCo::BadGateway, error_message_500(response, "The server returned an invalid or incomplete response.")
20
+ when 503
21
+ raise EPayCo::ServiceUnavailable, error_message_500(response, "EPayCo is rate limiting your requests.")
22
+ when 504
23
+ raise EPayCo::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
24
+ end
25
+ end
26
+ end
27
+
28
+ def initialize(app)
29
+ super app
30
+ @parser = nil
31
+ end
32
+
33
+ private
34
+
35
+ def error_message_400(response)
36
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
37
+ end
38
+
39
+ def error_body(body)
40
+ # body gets passed as a string, not sure if it is passed as something else from other spots?
41
+ if not body.nil? and not body.empty? and body.kind_of?(String)
42
+ # removed multi_json thanks to wesnolte's commit
43
+ body = ::JSON.parse(body)
44
+ end
45
+
46
+ if body.nil?
47
+ nil
48
+ elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
49
+ ": #{body['meta']['error_message']}"
50
+ elsif body['error_message'] and not body['error_message'].empty?
51
+ ": #{body['error_type']}: #{body['error_message']}"
52
+ end
53
+ end
54
+
55
+ def error_message_500(response, body=nil)
56
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,65 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe EPayCo do
4
+ after do
5
+ EPayCo.reset
6
+ end
7
+
8
+ describe ".client" do
9
+ it "should be a EPayCo::Client" do
10
+ expect(EPayCo.client).to be_a EPayCo::Client
11
+ end
12
+ end
13
+
14
+ describe ".adapter" do
15
+ it "should return the default adapter" do
16
+ expect(EPayCo.adapter).to eq EPayCo::Configuration::DEFAULT_ADAPTER
17
+ end
18
+ end
19
+
20
+ describe ".adapter=" do
21
+ it "should set the adapter" do
22
+ EPayCo.adapter = :typhoeus
23
+ expect(EPayCo.adapter).to eq :typhoeus
24
+ end
25
+ end
26
+
27
+ describe ".endpoint" do
28
+ it "should return the default endpoint" do
29
+ expect(EPayCo.endpoint).to eq EPayCo::Configuration::DEFAULT_ENDPOINT
30
+ end
31
+ end
32
+
33
+ describe ".endpoint=" do
34
+ it "should set the endpoint" do
35
+ EPayCo.endpoint = 'http://tumblr.com'
36
+ expect(EPayCo.endpoint).to eq 'http://tumblr.com'
37
+ end
38
+ end
39
+
40
+ describe ".user_agent" do
41
+ it "should return the default user agent" do
42
+ expect(EPayCo.user_agent).to eq EPayCo::Configuration::DEFAULT_USER_AGENT
43
+ end
44
+ end
45
+
46
+ describe ".user_agent=" do
47
+ it "should set the user_agent" do
48
+ EPayCo.user_agent = 'Custom User Agent'
49
+ expect(EPayCo.user_agent).to eq 'Custom User Agent'
50
+ end
51
+ end
52
+
53
+ describe ".configure" do
54
+
55
+ EPayCo::Configuration::VALID_OPTIONS_KEYS.each do |key|
56
+
57
+ it "should set the #{key}" do
58
+ EPayCo.configure do |config|
59
+ config.send("#{key}=", key)
60
+ expect(EPayCo.send(key)).to eq key
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe EPayCo::Client do
4
+ let(:public_key){ '111111111111111' }
5
+ let(:private_key){ '222222222222222' }
6
+ let(:client) { EPayCo::Client.new(:public_key => public_key, :private_key => private_key) }
7
+
8
+ # TODO: Make tests for erros
9
+ describe ".charge_create" do
10
+ let(:charge_params) { {
11
+ token_card: "hMsDAjwD7KLsgZQ54",
12
+ customer_id: "6eba2u73ZBh49Po7q",
13
+ plan_id: "cursocarpinteria",
14
+ doc_type: "CC",
15
+ doc_number: "1035851980",
16
+ name: "John",
17
+ last_name: "Doe",
18
+ email: "example@email.com",
19
+ ip: "192.198.2.114",
20
+ bill: "OR-1234",
21
+ description: "Test Payment",
22
+ value: "116000",
23
+ tax: "16000",
24
+ tax_base: "100000",
25
+ currency: "COP",
26
+ dues: "12"
27
+ } }
28
+
29
+ before do
30
+ stub_post("recurring/v1/charge/create").
31
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
32
+ to_return(:status => 200, :body => fixture("charge_create.json"), :headers => {:content_type => "application/json;"})
33
+ @response = client.charge_create(charge_params)
34
+ end
35
+
36
+ it { expect(a_post("recurring/v1/charge/create")).to have_been_made }
37
+ it { expect(@response).to be_a(Hashie::Mash) }
38
+ it { expect(@response.status).to eq "Creado" }
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe EPayCo::Client do
4
+ let(:public_key){ '111111111111111' }
5
+ let(:private_key){ '222222222222222' }
6
+ let(:client) { EPayCo::Client.new(:public_key => public_key, :private_key => private_key) }
7
+
8
+ describe ".customer_all" do
9
+ before do
10
+ stub_get("recurring/v1/customers/#{public_key}").
11
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
12
+ to_return(:status => 200, :body => fixture("customer_all.json"), :headers => {:content_type => "application/json;"})
13
+ @customers = client.customer_all
14
+ end
15
+
16
+ it { expect(a_get("recurring/v1/customers/#{public_key}")).to have_been_made }
17
+ it { expect(@customers).to be_an(Array) }
18
+ it { expect(@customers.size).to eq 2 }
19
+ it { expect(@customers.first.id_customer).to eq "PKEMb9wfxQjttGeP" }
20
+ it { expect(@customers.first.object).to eq "customer" }
21
+ end
22
+
23
+ describe ".customer_create" do
24
+ let(:customer_params) { {
25
+ token_card: "ZdTo2WFZEH9r3HC7N",
26
+ name: "Joe Doe",
27
+ email: "joe@payco.co",
28
+ phone: "3005234321",
29
+ default: true
30
+ } }
31
+
32
+ before do
33
+ stub_post("recurring/v1/customer/create").
34
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
35
+ to_return(:status => 200, :body => fixture("customer_create.json"), :headers => {:content_type => "application/json;"})
36
+ @response = client.customer_create(customer_params)
37
+ end
38
+
39
+ it { expect(a_post("recurring/v1/customer/create")).to have_been_made }
40
+ it { expect(@response).to be_a(Hashie::Mash) }
41
+ it { expect(@response.status).to eq "Creado" }
42
+ end
43
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe EPayCo::Client do
4
+ let(:public_key){ '111111111111111' }
5
+ let(:private_key){ '222222222222222' }
6
+ let(:client) { EPayCo::Client.new(:public_key => public_key, :private_key => private_key) }
7
+
8
+ describe ".plan_all" do
9
+ before do
10
+ stub_get("recurring/v1/plans/#{public_key}").
11
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
12
+ to_return(:status => 200, :body => fixture("plan_all.json"), :headers => {:content_type => "application/json;"})
13
+ @plans = client.plan_all
14
+ end
15
+
16
+ it { expect(a_get("recurring/v1/plans/#{public_key}")).to have_been_made }
17
+ it { expect(@plans).to be_an(Array) }
18
+ it { expect(@plans.size).to eq 1 }
19
+ it { expect(@plans.first.id_plan).to eq "test" }
20
+ end
21
+
22
+ describe ".plan_create" do
23
+ let(:plan_params) { {
24
+ id_plan: "test", name: "Test", description: "Plan de prueba", amount: 30,
25
+ currency: "USD", interval: "year", interval_count: 1, trial_days: 0
26
+ } }
27
+
28
+ before do
29
+ stub_post("recurring/v1/plan/create").
30
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
31
+ to_return(:status => 200, :body => fixture("plan_create.json"), :headers => {:content_type => "application/json;"})
32
+ @response = client.plan_create(plan_params)
33
+ end
34
+
35
+ it { expect(a_post("recurring/v1/plan/create")).to have_been_made }
36
+ it { expect(@response).to be_a(Hashie::Mash) }
37
+ it { expect(@response.status).to eq "Creado" }
38
+ end
39
+
40
+ describe ".plan_details" do
41
+ let(:plan_id) { "test" }
42
+ before do
43
+ stub_get("recurring/v1/plan/#{public_key}/#{plan_id}").
44
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
45
+ to_return(:status => 200, :body => fixture("plan_details.json"), :headers => {:content_type => "application/json;"})
46
+ @plan = client.plan_details(plan_id)
47
+ end
48
+
49
+ it { expect(a_get("recurring/v1/plan/#{public_key}/#{plan_id}")).to have_been_made }
50
+ it { expect(@plan).to be_an(Hash) }
51
+ it { expect(@plan.id_plan).to eq "test" }
52
+ end
53
+
54
+ describe ".plan_update" do
55
+ let(:plan_id) { "test" }
56
+ let(:plan_params) { {
57
+ id_plan: "test", name: "Prueba", description: "Plan de prueba", amount: 30,
58
+ currency: "USD", interval: "year", interval_count: 1, trial_days: 0
59
+ } }
60
+ before do
61
+ stub_put("recurring/v1/plan/edit/#{public_key}/#{plan_id}").
62
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
63
+ to_return(:status => 200, :body => fixture("plan_update.json"), :headers => {:content_type => "application/json;"})
64
+ @response = client.plan_update(plan_id, plan_params)
65
+ end
66
+
67
+ it { expect(a_put("recurring/v1/plan/edit/#{public_key}/#{plan_id}")).to have_been_made }
68
+ it { expect(@response).to be_a(Hashie::Hash) }
69
+ it { expect(@response.status).to eq "Actualizado" }
70
+ end
71
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe EPayCo::Client do
4
+ let(:public_key){ '111111111111111' }
5
+ let(:private_key){ '222222222222222' }
6
+ let(:client) { EPayCo::Client.new(:public_key => public_key, :private_key => private_key) }
7
+
8
+ describe ".subscription_all" do
9
+ before do
10
+ stub_get("recurring/v1/subscriptions/#{public_key}").
11
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
12
+ to_return(:status => 200, :body => fixture("subscription_all.json"), :headers => {:content_type => "application/json;"})
13
+ @subscriptions = client.subscription_all
14
+ end
15
+
16
+ it { expect(a_get("recurring/v1/subscriptions/#{public_key}")).to have_been_made }
17
+ it { expect(@subscriptions).to be_an(Array) }
18
+ it { expect(@subscriptions.size).to eq 2 }
19
+ it { expect(@subscriptions.first._id).to eq "wAzyX9Sutm3BaLxM2" }
20
+ end
21
+
22
+ describe ".subscription_create" do
23
+ let(:subscription_params) { {
24
+ id_plan: "cursocarpinteria",
25
+ customer: "6eba2u73ZBh49Po7q",
26
+ token_card: "ZdTo2WFZEH9r3HC7N"
27
+ } }
28
+
29
+ before do
30
+ stub_post("recurring/v1/subscription/create").
31
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
32
+ to_return(:status => 200, :body => fixture("subscription_create.json"), :headers => {:content_type => "application/json;"})
33
+ @response = client.subscription_create(subscription_params)
34
+ end
35
+
36
+ it { expect(a_post("recurring/v1/subscription/create")).to have_been_made }
37
+ it { expect(@response).to be_a(Hashie::Mash) }
38
+ it { expect(@response.id).to eq "wAzyX9Sutm3BaLxM2" }
39
+ end
40
+
41
+ describe ".subscription_details" do
42
+ let(:subscription_id) { "test" }
43
+ before do
44
+ stub_get("recurring/v1/subscription/#{subscription_id}/#{public_key}").
45
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
46
+ to_return(:status => 200, :body => fixture("subscription_create.json"), :headers => {:content_type => "application/json;"})
47
+ @subscription = client.subscription_details(subscription_id)
48
+ end
49
+
50
+ it { expect(a_get("recurring/v1/subscription/#{subscription_id}/#{public_key}")).to have_been_made }
51
+ it { expect(@subscription).to be_an(Hash) }
52
+ it { expect(@subscription.id).to eq "wAzyX9Sutm3BaLxM2" }
53
+ end
54
+
55
+ describe ".subscription_cancel" do
56
+ let(:subscription_id) { "test" }
57
+ before do
58
+ stub_post("recurring/v1/subscription/cancel").
59
+ with(:headers => {'Accept'=>'application/json; charset=utf-8;', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'EPayCo Ruby Gem 0.0.1'}).
60
+ to_return(:status => 200, :body => fixture("subscription_cancel.json"), :headers => {:content_type => "application/json;"})
61
+ @response = client.subscription_cancel(subscription_id)
62
+ end
63
+
64
+ it { expect(a_post("recurring/v1/subscription/cancel")).to have_been_made }
65
+ it { expect(@response).to be_a(Hashie::Hash) }
66
+ it { expect(@response.status).to eq "Actualizado" }
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe EPayCo::Client do
4
+ it "should connect using the endpoint configuration" do
5
+ client = EPayCo::Client.new
6
+ endpoint = URI.parse(client.endpoint)
7
+ connection = client.send(:connection).build_url(nil).to_s
8
+ expect(connection).to eq endpoint.to_s
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "success": true,
3
+ "message": "El charge ha sido creado con exito",
4
+ "data": {
5
+ "status": "Creado",
6
+ "description": "El charge ha sido creado con exito",
7
+ "customerId": "6eba2u73ZBh49Po7q",
8
+ "token": "ZdTo2WFZEH9r3HC7N"
9
+ }
10
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "status": true,
3
+ "object": "customers",
4
+ "customers": [
5
+ {
6
+ "id_customer": "PKEMb9wfxQjttGeP",
7
+ "object": "customer",
8
+ "name": "Juan Fernando",
9
+ "email": "cliente1@epayco.com",
10
+ "phone": "3333333",
11
+ "created": "21-10-2016"
12
+ },
13
+ {
14
+ "id_customer": "qRTGGssNKXZo2Q6pL",
15
+ "object": "customer",
16
+ "name": "Pedro Jaramillo",
17
+ "email": "cliente2@epayco.com",
18
+ "phone": "3333333",
19
+ "created": "21-10-2016"
20
+ }
21
+ ]
22
+ }