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,58 @@
1
+ module EPayCo
2
+ class Client
3
+ # Defines methods related to Customers
4
+ module Customer
5
+ # Returns all the Customers registered
6
+ #
7
+ # @format :json
8
+ # @return [Array]
9
+ # @example All the Customers
10
+ # [
11
+ # {
12
+ # "id_customer": "PKEMb9wfxQjttGeP",
13
+ # "object": "customer",
14
+ # "name": "Juan Fernando",
15
+ # "email": "cliente1@epayco.com",
16
+ # "phone": "3333333",
17
+ # "created": "21-10-2016"
18
+ # },
19
+ # {
20
+ # "id_customer": "qRTGGssNKXZo2Q6pL",
21
+ # "object": "customer",
22
+ # "name": "Pedro Jaramillo",
23
+ # "email": "cliente2@epayco.com",
24
+ # "phone": "3333333",
25
+ # "created": "21-10-2016"
26
+ # }
27
+ # ]
28
+ def customer_all(params={})
29
+ path = "/recurring/v1/customers/#{public_key}"
30
+ response_options = { return_object: true }
31
+ get(path, params, response_options)
32
+ end
33
+
34
+ # Creates a new customer
35
+ #
36
+ # @format :json
37
+ # @param options The Customer Attributes
38
+ # @option options [String] :token_card Token de la tarjeta tokenizada
39
+ # @option options [String] :name Nombre del cliente al que se asocia la tarjeta
40
+ # @option options [String] :email Email del cliente al que se asocia la tarjeta
41
+ # @option options [String] :phone Phone del cliente al que se asocia la tarjeta
42
+ # @option options [Boolean] :default Define la terjeta con la cobra por default
43
+ # @return [Hashie::Mash]
44
+ # @example Success
45
+ # {
46
+ # "status": "Creado",
47
+ # "description": "El cliente ha sido creado con exito para realizar operaciones y enlazarlo use el id: 6eba2u73ZBh49Po7q",
48
+ # "customerId": "6eba2u73ZBh49Po7q",
49
+ # "token": "ZdTo2WFZEH9r3HC7N"
50
+ # }
51
+ def customer_create(options={})
52
+ path = "/recurring/v1/customer/create"
53
+ response_options = { return_object: 'data' }
54
+ post(path, options.merge(public_key: public_key), response_options)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,115 @@
1
+ module EPayCo
2
+ class Client
3
+ # Defines methods related to Plans
4
+ module Plans
5
+ # Returns all the Plans registered
6
+ #
7
+ # @format :json
8
+ # @return [Array]
9
+ # @example All the Plans
10
+ # [
11
+ # {
12
+ # "id_plan": "cursocarpinteria",
13
+ # "object": "plan",
14
+ # "name": "Curso de carpintería",
15
+ # "description": "En este curso aprenderás carpintería",
16
+ # "amount": 30000,
17
+ # "currency": "cop",
18
+ # "interval": "month",
19
+ # "status": "active",
20
+ # "trialDays": 30,
21
+ # "created": "Sep 26, 2016 3:15 PM"
22
+ # },
23
+ # {
24
+ # "id_plan": "cursocarpinteria2",
25
+ # "object": "plan",
26
+ # "name": "Curso de carpintería2",
27
+ # "description": "En este curso aprenderás carpintería2",
28
+ # "amount": 30000,
29
+ # "currency": "cop",
30
+ # "interval": "month",
31
+ # "status": "active",
32
+ # "trialDays": 30,
33
+ # "created": "Sep 26, 2016 3:52 PM"
34
+ # }
35
+ # ]
36
+ def plan_all(params={})
37
+ path = "/recurring/v1/plans/#{public_key}"
38
+ response_options = { return_object: true }
39
+ get(path, params, response_options)
40
+ end
41
+
42
+ # Creates a new plan
43
+ #
44
+ # @format :json
45
+ # @param options The Plan Attributes
46
+ # @option options [String] :id_plan Proporciona un nombre único para identificar el plan
47
+ # @option options [String] :name Nombre del plan que mostraras a tus clientes
48
+ # @option options [String] :description Descripción del plan creado que será mostrada a tus clientes
49
+ # @option options [Integer] :amount Hace referencia al valor que se cobrará en cada periodo
50
+ # @option options [String] :currency Consta de 3 letras que identifica la moneda en la que harás el cobro segun las normas [ISO code](http://www.xe.com/iso4217.php) currency
51
+ # @option options [String] :interval Especifica la recurrencia del cobro. 'day', 'week', 'month', o 'year'
52
+ # @option options [Integer] :interval_count Especifica la cantidad de intervalos por ejemplo si interval = “month” y interval_count es igual a 2 se cobrará cada 2 meses
53
+ # @option options [Integer] :trial_days Numero de dias que se podrán probar tus servicios antes del cobro, su valor inicial es 0
54
+ # @return [Hashie::Mash]
55
+ # @example Success
56
+ # {
57
+ # "status": "Creado",
58
+ # "user": "cdc4654c502f9d320ebet4f6230m669d",
59
+ # "id": "test"
60
+ # }
61
+ # @example Fail
62
+ # {
63
+ # "status": "error",
64
+ # "description": "El id del mensaje es existente o ya no esta en uso id: test"
65
+ # }
66
+ def plan_create(options={})
67
+ path = "/recurring/v1/plan/create"
68
+ response_options = { return_object: 'data' }
69
+ post(path, options.merge(public_key: public_key), response_options)
70
+ end
71
+
72
+ # Updates a new plan
73
+ #
74
+ # @format :json
75
+ # @param plan_id [String] Plan ID (nombre único para identificar el plan)
76
+ # @param fields [Hash] Clave y valor de los campos a editar
77
+ # @return [Hashie::Mash]
78
+ # @example Success
79
+ # {
80
+ # "status": "Actualizado",
81
+ # "userId": "4378g0ebb84536876540407bzb04815n",
82
+ # "planId": "test"
83
+ # }
84
+ def plan_update(plan_id, fields={})
85
+ path = "/recurring/v1/plan/edit/#{public_key}/#{plan_id}"
86
+ response_options = { return_object: 'data' }
87
+ put(path, fields, response_options)
88
+ end
89
+
90
+ # Returns the details of a plan
91
+ #
92
+ # @format :json
93
+ # @param plan_id [String] Plan ID (nombre único para identificar el plan)
94
+ # @return [Hash]
95
+ # @example Plan Details
96
+ # {
97
+ # "id_plan": "test",
98
+ # "object": "plan",
99
+ # "name": "Test",
100
+ # "description": "Plan de prueba",
101
+ # "amount": 30,
102
+ # "currency": "usd",
103
+ # "interval": "year",
104
+ # "status": "active",
105
+ # "trialDays": 0,
106
+ # "created": "19-02-2017"
107
+ # }
108
+ def plan_details(plan_id, params={})
109
+ path = "/recurring/v1/plan/#{public_key}/#{plan_id}"
110
+ response_options = { return_object: true }
111
+ get(path, params, response_options)
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,150 @@
1
+ module EPayCo
2
+ class Client
3
+ # Defines methods related to Subscription
4
+ module Subscription
5
+ # Returns all the Subscriptions registered
6
+ #
7
+ # @format :json
8
+ # @return [Array]
9
+ # @example All the Subscription
10
+ # [
11
+ # {
12
+ # "_id": "wAzyX9Sutm3BaLxM2",
13
+ # "idPlan": "cursocarpinteria2",
14
+ # "data": {
15
+ # "_id": "LS5L9cRaJSWA4ykeM",
16
+ # "idClient": "cursocarpinteria2",
17
+ # "name": "Curso de carpintería2",
18
+ # "description": "En este curso aprenderás carpintería2",
19
+ # "amount": 30000,
20
+ # "currency": "cop",
21
+ # "interval": "month",
22
+ # "clientId": "5c4773856f296c674685209bbfd11f92",
23
+ # "status": "active",
24
+ # "trialDays": 30,
25
+ # "createdAt": "2016-09-27T15:29:38.772Z"
26
+ # },
27
+ # "periodStart": "2016-09-27T15:59:46.339Z",
28
+ # "periodEnd": "2016-11-26T15:59:46.339Z",
29
+ # "status": "active",
30
+ # "idUser": "5c4773856f296c674685209bbfd11f92",
31
+ # "idCustomer": "6eba2u73ZBh49Po7q",
32
+ # "createdAt": "2016-09-27T15:59:46.352Z"
33
+ # },
34
+ # {
35
+ # "_id": "cZHvGKHxJDd8oY72m",
36
+ # "idPlan": "cursocarpinteria2",
37
+ # "data": {
38
+ # "_id": "LS5L9cRaJSWA4ykeM",
39
+ # "idClient": "cursocarpinteria2",
40
+ # "name": "Curso de carpintería2",
41
+ # "description": "En este curso aprenderás carpintería2",
42
+ # "amount": 30000,
43
+ # "currency": "cop",
44
+ # "interval": "month",
45
+ # "clientId": "5c4773856f296c674685209bbfd11f92",
46
+ # "status": "active",
47
+ # "trialDays": 30,
48
+ # "createdAt": "2016-09-27T15:29:38.772Z"
49
+ # },
50
+ # "periodStart": "2016-09-27T16:49:59.758Z",
51
+ # "periodEnd": "2016-11-26T16:49:59.758Z",
52
+ # "status": "active",
53
+ # "idUser": "5c4773856f296c674685209bbfd11f92",
54
+ # "idCustomer": "cus_97dn6u3OFeeeOye",
55
+ # "createdAt": "2016-09-27T16:49:59.767Z"
56
+ # }
57
+ # ]
58
+ def subscription_all(params={})
59
+ path = "/recurring/v1/subscriptions/#{public_key}"
60
+ response_options = { return_object: "plans" }
61
+ get(path, params, response_options)
62
+ end
63
+
64
+ # Creates a new subscription
65
+ #
66
+ # @format :json
67
+ # @param options The Necessary fields to create a Subscription
68
+ # @option options [String] :id_plan Id único del plan que se desea asociar al cliente
69
+ # @option options [String] :token_card Id de la tarjeta tokenizada con la que se efectuara un pago y ha sido asociada a un cliente
70
+ # @option options [String] :customer Customer ID (Optional)
71
+ # @return [Hashie::Mash]
72
+ # @example Success
73
+ # {
74
+ # "success": true,
75
+ # "object": "subscription",
76
+ # "id": "wAzyX9Sutm3BaLxM2",
77
+ # "created": "Sep 27, 2016 10:59 AM",
78
+ # "current_period_start": "Sep 27, 2016 10:59 AM",
79
+ # "current_period_end": "Nov 26, 2016 10:59 AM",
80
+ # "customer": "6eba2u73ZBh49Po7q",
81
+ # "message": "Suscripción creada",
82
+ # "data": {
83
+ # "id_plan": "cursocarpinteria2",
84
+ # "name": "Curso de carpintería2",
85
+ # "description": "En este curso aprenderás carpintería2",
86
+ # "amount": 30000,
87
+ # "currency": "cop",
88
+ # "interval": "month",
89
+ # "status": "active",
90
+ # "trialDays": 30,
91
+ # "createdAt": "Sep 27, 2016 10:59 AM"
92
+ # },
93
+ # "status": "active"
94
+ # }
95
+ def subscription_create(options={})
96
+ path = "/recurring/v1/subscription/create"
97
+ post(path, options.merge(public_key: public_key))
98
+ end
99
+
100
+ # Returns the details of a subscription
101
+ #
102
+ # @format :json
103
+ # @return [Hashie::Mash]
104
+ # @param subscription_id [String] Id único del suscripción que se asocio a un cliente
105
+ # @example Subscription Details
106
+ # {
107
+ # "success": true,
108
+ # "id": "wAzyX9Sutm3BaLxM2",
109
+ # "object": "subscription",
110
+ # "created": "2016-09-27T15:59:46.352Z",
111
+ # "current_period_start": "Sep 27, 2016 10:59 AM",
112
+ # "current_period_end": "Nov 26, 2016 10:59 AM",
113
+ # "customer": "6eba2u73ZBh49Po7q",
114
+ # "plan": {
115
+ # "id_plan": "cursocarpinteria2",
116
+ # "name": "Curso de carpintería2",
117
+ # "description": "En este curso aprenderás carpintería2",
118
+ # "amount": 30000,
119
+ # "currency": "cop",
120
+ # "interval": "month",
121
+ # "status": "active",
122
+ # "trialDays": 30,
123
+ # "createdAt": "Sep 27, 2016 10:59 AM"
124
+ # },
125
+ # "status": "active"
126
+ # }
127
+ def subscription_details(subscription_id, params={})
128
+ path = "/recurring/v1/subscription/#{subscription_id}/#{public_key}"
129
+ get(path, params)
130
+ end
131
+
132
+ # Cancel a subscription
133
+ #
134
+ # @format :json
135
+ # @param subscription_id [String] Id único del suscripción que se asocio a un cliente
136
+ # @return [Hashie::Mash]
137
+ # @example Success
138
+ # {
139
+ # "status": "Actualizado",
140
+ # "description": "La suscripción ha sido inhabilidata para el usuario wAzyX9Sutm3BaLxM2"
141
+ # }
142
+ def subscription_cancel(subscription_id)
143
+ path = "/recurring/v1/subscription/cancel"
144
+ request_options = { public_key: public_key, id: subscription_id }
145
+ response_options = { return_object: 'data' }
146
+ post(path, request_options, response_options)
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,74 @@
1
+ require 'faraday'
2
+ require File.expand_path('../version', __FILE__)
3
+
4
+ module EPayCo
5
+ # Defines constants and methods related to configuration
6
+ module Configuration
7
+ # An array of valid keys in the options hash when configuring a {EPayCo::API}
8
+ VALID_OPTIONS_KEYS = [
9
+ :public_key,
10
+ :private_key,
11
+ :adapter,
12
+ :connection_options,
13
+ :endpoint,
14
+ :proxy,
15
+ :user_agent
16
+ ].freeze
17
+
18
+ # By default, don't set a user public key
19
+ DEFAULT_PUBLIC_KEY = nil
20
+
21
+ # By default, don't set a user private key
22
+ DEFAULT_PRIVATE_KEY = nil
23
+
24
+ # The adapter that will be used to connect if none is set
25
+ #
26
+ # @note The default faraday adapter is Net::HTTP.
27
+ DEFAULT_ADAPTER = Faraday.default_adapter
28
+
29
+ # By default, don't set any connection options
30
+ DEFAULT_CONNECTION_OPTIONS = {}
31
+
32
+ # The endpoint that will be used to connect if none is set
33
+ #
34
+ # @note There is no reason to use any other endpoint at this time
35
+ DEFAULT_ENDPOINT = 'https://api.secure.payco.co/'.freeze
36
+
37
+ # By default, don't use a proxy server
38
+ DEFAULT_PROXY = nil
39
+
40
+ # The user agent that will be sent to the API endpoint if none is set
41
+ DEFAULT_USER_AGENT = "EPayCo Ruby Gem #{EPayCo::VERSION}".freeze
42
+
43
+ # @private
44
+ attr_accessor *VALID_OPTIONS_KEYS
45
+
46
+ # When this module is extended, set all configuration options to their default values
47
+ def self.extended(base)
48
+ base.reset
49
+ end
50
+
51
+ # Convenience method to allow configuration options to be set in a block
52
+ def configure
53
+ yield self
54
+ end
55
+
56
+ # Create a hash of options and their values
57
+ def options
58
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
59
+ option.merge!(key => send(key))
60
+ end
61
+ end
62
+
63
+ # Reset all configuration options to defaults
64
+ def reset
65
+ self.public_key = DEFAULT_PUBLIC_KEY
66
+ self.private_key = DEFAULT_PRIVATE_KEY
67
+ self.adapter = DEFAULT_ADAPTER
68
+ self.connection_options = DEFAULT_CONNECTION_OPTIONS
69
+ self.endpoint = DEFAULT_ENDPOINT
70
+ self.proxy = DEFAULT_PROXY
71
+ self.user_agent = DEFAULT_USER_AGENT
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday_middleware'
2
+ Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
3
+
4
+ module EPayCo
5
+ # @private
6
+ module Connection
7
+ private
8
+
9
+ def connection(raw=false)
10
+ options = {
11
+ :headers => {
12
+ 'Accept' => "application/json; charset=utf-8;",
13
+ 'Content-Type' => "application/json",
14
+ 'User-Agent' => user_agent},
15
+ :proxy => proxy,
16
+ :url => endpoint,
17
+ }.merge(connection_options)
18
+
19
+ Faraday::Connection.new(options) do |connection|
20
+ connection.use Faraday::Request::UrlEncoded
21
+ unless raw
22
+ connection.use FaradayMiddleware::Mashify
23
+ connection.use Faraday::Response::ParseJson
24
+ end
25
+ connection.use FaradayMiddleware::RaiseHttpException
26
+ connection.adapter(adapter)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ module EPayCo
2
+ # Custom error class for rescuing from all EPayCo errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when EPayCo returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when EPayCo returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when EPayCo returns the HTTP status code 429
12
+ class TooManyRequests < Error; end
13
+
14
+ # Raised when EPayCo returns the HTTP status code 500
15
+ class InternalServerError < Error; end
16
+
17
+ # Raised when EPayCo returns the HTTP status code 502
18
+ class BadGateway < Error; end
19
+
20
+ # Raised when EPayCo returns the HTTP status code 503
21
+ class ServiceUnavailable < Error; end
22
+
23
+ # Raised when EPayCo returns the HTTP status code 504
24
+ class GatewayTimeout < Error; end
25
+
26
+ # Raised when a subscription payload hash is invalid
27
+ class InvalidSignature < Error; end
28
+
29
+ # Raised when EPayCo returns the HTTP status code 429
30
+ class RateLimitExceeded < Error; end
31
+ end