tpaga_service 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1188de672e27642b768dbd6903e48a69cb10881b4c92e85d14cf59fa8cd2056e
4
+ data.tar.gz: 11cb086a4a5680dc8f26241f5368a8a08c1ab175c3071c194b111bbb40e3229b
5
+ SHA512:
6
+ metadata.gz: c1c90d5c4bab8e23b03f0674f0bc7e729614c40e5909a7a399e187a8431e432d4418ce021e675540ac198d99c897235c181c00a1deaecd42181f17559f860d28
7
+ data.tar.gz: e83ceebc285ef9385df0e98f8624c0fedece0d4173d6a5a420b096c2eb0c7944cb277467f026174b986a037ac08cbd96d0f55453b7119f99f968dea36a1b71b8
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at juan.contreras@packen.co. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tpaga_service.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tpaga_service (0.1.0)
5
+ faraday
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ faraday (1.0.1)
12
+ multipart-post (>= 1.2, < 3)
13
+ multipart-post (2.1.1)
14
+ rake (13.0.1)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.2)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.4)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.2)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.17)
34
+ faraday
35
+ rake (>= 12.3.3)
36
+ rspec (~> 3.0)
37
+ tpaga_service!
38
+
39
+ BUNDLED WITH
40
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 juanpacken
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,152 @@
1
+ TPaga Service
2
+ ===
3
+
4
+ TPaga Service gem provides the functionality to call the multiple services of the TPaga platform.
5
+
6
+ Installation
7
+ ---
8
+
9
+ To install as a gem in your environment type in your console:
10
+
11
+ ```sh
12
+ gem install tpaga-service
13
+ ```
14
+
15
+ If you want to use in your rails application, add to your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'tpaga_service'
19
+ ```
20
+
21
+ And run from your application directory:
22
+
23
+ ```sh
24
+ bundle install
25
+ ```
26
+
27
+ Configuration
28
+ ---
29
+
30
+ If you are going to run tpaga in a rails environment add to the environment file (ex: `environments/development.rb`) the following lines:
31
+
32
+ ```ruby
33
+ config.schema = 'https'
34
+ config.host = 'sandbox.tpaga_service.co'
35
+ config.base_path = '/api'
36
+ config.private_api_key = 'd13fr8n7vhvkuch3lq2ds5qhjnd2pdd2'
37
+ config.public_api_key = 'd13fr8n7vhvkuch3lq2ds5qhjnd2pdd2'
38
+ ```
39
+
40
+ using the respective api key for your TPaga user account.
41
+
42
+ Next add the the file `tpaga-service.rb` to the rails initializers (ex: `initializers/tpaga-service.rb`) with he following lines:
43
+
44
+ ```ruby
45
+ require 'tpaga_service'
46
+
47
+ TpagaService::Swagger.configure do |config|
48
+ config.scheme = Rails.application.config.tpaga_schema
49
+ config.host = Rails.application.config.tpaga_host
50
+ config.base_path = Rails.application.config.tpaga_base_path
51
+ config.private_api_key = Rails.application.config.tpaga_private_api_key
52
+ config.public_api_key = Rails.application.config.tpaga_public_api_key
53
+ config.inject_format = false
54
+ end
55
+ ```
56
+
57
+ if you are not using rails, just simply initialize the service with the lines in the tpaga initializer before you call any service of the api.
58
+
59
+ Managing customers
60
+ ---
61
+
62
+ You can create a customer:
63
+
64
+ ```ruby
65
+ # if outside from rails
66
+ require 'tpaga_service'
67
+
68
+ # creating a tpaga_service customer object
69
+ customer = TpagaService::Customer.new(
70
+ firstName: 'foo',
71
+ lastName: 'bar',
72
+ email: 'foo@bar.com',
73
+ phone: '0000000000',
74
+ gender: 'M'
75
+ )
76
+
77
+ # call of the api to create the customer in the TPaga account
78
+ customer = TpagaService::CustomerApi.create_customer customer
79
+ customer.id # The unique identifier of the customer in the TPaga account
80
+ # if the request cannot be completed, raise a generic error
81
+ ```
82
+
83
+ Get a customer by it's id:
84
+
85
+ ```ruby
86
+ customer = TpagaService::CustomerApi.get_customer_by_id 'id'
87
+ # returns a TpagaService::Customer object, raise error if not
88
+ ```
89
+
90
+ Delete a customer by it's id:
91
+
92
+ ```ruby
93
+ response = TpagaService::CustomerApi.delete_customer_by_id 'customer_id'
94
+ # return nil if success, raise an error if not
95
+ ```
96
+ Managing credit cards
97
+ ---
98
+
99
+ You can add a credit card to existing customers:
100
+
101
+ ```ruby
102
+ # build the credit card object to create
103
+ credit_card_create = TpagaService::CreditCardCreate.new(
104
+ primaryAccountNumber: '4111111111111111',
105
+ expirationMonth: '08',
106
+ expirationYear: '2019',
107
+ cardVerificationCode: '789',
108
+ cardHolderName: 'Jon Snow',
109
+ billingAddress: TpagaService::BillingAddress.new
110
+ )
111
+
112
+ credit_card = TpagaService::CreditCardApi.add_credit_card 'customer_id', credit_card_create
113
+ # returns a TpagaService::CreditCard object, raise error if not
114
+ ```
115
+
116
+ you can get the credit card of customers:
117
+
118
+ ```ruby
119
+ credit_card = TpagaService::CreditCardApi.get_credit_card_by_id 'customer_id', 'credit_card_id'
120
+ # return a TpagaService::CreditCard object, raise error if not
121
+ ```
122
+
123
+ You can delete the credit card of customers:
124
+
125
+ ```ruby
126
+ TpagaService::CreditCardApi.delete_credit_card_by_id 'customer_id', 'credit_card_id'
127
+ # return nil if success, raise error if not
128
+ ```
129
+
130
+ You can charge a credit card:
131
+
132
+ ```ruby
133
+ # build the charge object
134
+ charge = TpagaService::CreditCardCharge.new(
135
+ amount: 10000,
136
+ taxAmount: 10000 * 0.1,
137
+ currency: 'COP',
138
+ orderId: 'Your identifier for the order',
139
+ installments: 1,
140
+ description: 'A new leash for ghost',
141
+ creditCard: 'credit_card_id'
142
+ )
143
+
144
+ charge = TpagaService::CreditCardApi.add_credit_card_charge charge
145
+ # return a TpagaService::CreditCardCharge object, raise error if not
146
+ ```
147
+
148
+ Retrieve a charge by it's id:
149
+
150
+ ```ruby
151
+ charge = TpagaService::CreditCardApi.get_credit_card_charge_by_id 'charge_id'
152
+ # return a TpagaService::CreditCardCharge object, raise error if not
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tpaga_service/api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,64 @@
1
+ module TpagaService
2
+ module ChargeApi
3
+ extend self
4
+ # ===== Parameters:
5
+ # *+data+ : Hash -
6
+ # {
7
+ # "creditCard": "string",
8
+ # "amount": 0,
9
+ # "currency": "string",
10
+ # "installments": 0,
11
+ # "orderId": "string",
12
+ # "taxAmount": 0, (optional)
13
+ # "iacAmount": 0, (optional)
14
+ # "tipAmount": 0, (optional)
15
+ # "description": "string",
16
+ # "thirdPartyId": "string", (optional)
17
+ # "childMerchantId": "string" (optional)
18
+ # }
19
+ # ===== Return:
20
+ # +Hash+ - {
21
+ # "id": "string",
22
+ # "amount": 0,
23
+ # "taxAmount": 0,
24
+ # "childMerchantId": "string",
25
+ # "currency": "string",
26
+ # "creditCard": "string",
27
+ # "installments": 0,
28
+ # "orderId": "string",
29
+ # "iacAmount": 0,
30
+ # "tipAmount": 0,
31
+ # "description": "string",
32
+ # "dateCreated": "2020-04-30T00:51:06.192Z",
33
+ # "thirdPartyId": "string",
34
+ # "paid": true,
35
+ # "customer": "string",
36
+ # "paymentTransaction": "string",
37
+ # "errorCode": "string",
38
+ # "errorMessage": "string",
39
+ # "reteRentaAmount": "string",
40
+ # "reteIcaAmount": "string",
41
+ # "reteIvaAmount": "string",
42
+ # "tpagaFeeAmount": "string",
43
+ # "transactionInfo": {
44
+ # "authorizationCode": "string",
45
+ # "status": "created"
46
+ # }
47
+ # }
48
+ def add_credit_card_charge(data)
49
+ host = Swagger.configuration.host
50
+ api_key = Swagger.configuration.private_api_key
51
+
52
+ conn = Faraday.new
53
+ resp = conn.post do |req|
54
+ req.url "https://#{host}/api/charge/credit_card"
55
+ req.headers['Content-Type'] = 'application/json'
56
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
57
+ req.body = data.to_json
58
+ end
59
+ body = JSON.parse(resp.body)
60
+ Swagger::Response.new(resp.status, body)
61
+ return body
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,110 @@
1
+ module TpagaService
2
+ module CreditCardApi
3
+ extend self
4
+
5
+ def add_credit_card(data, customer_id=nil)
6
+ customer_id = customer_id || data[:customer_id]
7
+ return create_credit_card(get_created_credit_card(data), customer_id)
8
+ end
9
+
10
+ def delete_credit_card_by_id(customer_id, credit_card_id)
11
+ host = Swagger.configuration.host
12
+ api_key = Swagger.configuration.private_api_key
13
+
14
+ conn = Faraday.new
15
+ resp = conn.delete do |req|
16
+ req.url "https://#{host}/api/customer/#{customer_id}"
17
+ req.headers['Content-Type'] = 'application/json'
18
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
19
+ end
20
+ body = JSON.parse(resp.body)
21
+ Swagger::Response.new(resp.status, body)
22
+ return true
23
+ end
24
+
25
+ private
26
+
27
+ def create_credit_card(credit_card_create, customer_id)
28
+ host = Swagger.configuration.host
29
+ api_key = Swagger.configuration.public_api_key
30
+
31
+ conn = Faraday.new
32
+ resp = conn.post do |req|
33
+ req.url "https://#{host}/api/tokenize/credit_card"
34
+ req.headers['Content-Type'] = 'application/json'
35
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
36
+ req.body = credit_card_create.to_json
37
+ end
38
+ body = JSON.parse(resp.body)
39
+ Swagger::Response.new(resp.status, body)
40
+ unless body["token"].nil?
41
+ unless body["used"]
42
+ body = _add_credit_card(body["token"], customer_id)
43
+ else
44
+ raise 'Credit Card in Use'
45
+ end
46
+ end
47
+ return body
48
+ end
49
+
50
+ # ===== Parameters:
51
+ # *+credit_card_token+ : String
52
+ # *+customer_id+ : String
53
+ # ===== Return
54
+ # +Hash+ - {
55
+ # "id": "o8shhbmi9hanlcvk976lbq1p3ubavmmq",
56
+ # "bin": "411111",
57
+ # "type": "VISA",
58
+ # "expirationMonth": "12",
59
+ # "expirationYear": "2030",
60
+ # "lastFour": "1111",
61
+ # "customer": "i0ih8d8hcddv984kl7ou4iehrl7akroj",
62
+ # "cardHolderName": "Luis S.",
63
+ # "cardHolderLegalIdNumber": null,
64
+ # "cardHolderLegalIdType": "CC",
65
+ # "addressLine1": null,
66
+ # "addressLine2": null,
67
+ # "addressCity": null,
68
+ # "addressState": null,
69
+ # "addressPostalCode": null,
70
+ # "addressCountry": null,
71
+ # "fingerprint": "0e3fc45a1ce4414f442e0c50186db85b8dc53dda916909918eff3f7e811ad9ca",
72
+ # "validationCharge": {
73
+ # "successful": true,
74
+ # "errorCode": "00"
75
+ # }
76
+ # }
77
+ def _add_credit_card(credit_card_token, customer_id)
78
+ host = Swagger.configuration.host
79
+ api_key = Swagger.configuration.private_api_key
80
+
81
+ conn = Faraday.new
82
+ resp = conn.post do |req|
83
+ req.url "https://#{host}/api/customer/#{customer_id}/credit_card_token"
84
+ req.headers['Content-Type'] = 'application/json'
85
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
86
+ req.body = {
87
+ skipLegalIdCheck: false,
88
+ token: credit_card_token
89
+ }.to_json
90
+ end
91
+ body = JSON.parse(resp.body)
92
+ Swagger::Response.new(resp.status, body)
93
+ return body
94
+ end
95
+
96
+ # ===== Parameters:
97
+ # *+data+ : Hash
98
+ # ===== Return:
99
+ # +Hash+
100
+ def get_created_credit_card(data)
101
+ {
102
+ primaryAccountNumber: (data['primary_number'] || data[:primary_number]),
103
+ expirationMonth: (data['expiration_month'] || data[:expiration_month]),
104
+ expirationYear: (data['expiration_year'] || data[:expiration_year]),
105
+ cardVerificationCode: (data['security_code'] || data[:security_code]),
106
+ cardHolderName: (data['holder_name'] || data[:holder_name])
107
+ }
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,80 @@
1
+ module TpagaService
2
+ module CustomerApi
3
+ extend self
4
+
5
+ # ===== Parameters:
6
+ # *+data+ Hash - { firstName: '', lastName: '', email: '', phone: '' }
7
+ # ===== Return:
8
+ # +Hash+ - {\"id\"=>\"cus-ljcvjfcc4mwzh4j2qmv40gqqu2x2\", \"firstName\"=>\"Sta. Elisa Melgar Arteaga\", \"lastName\"=>\"Sta. Elisa Melgar Arteaga\", \"gender\"=>nil, \"email\"=>\"luciokling@wintheiser.org\", \"phone\"=>\"9521559\", \"legalIdNumber\"=>nil, \"merchantCustomerId\"=>nil}
9
+ def create_customer(data)
10
+ host = Swagger.configuration.host
11
+ api_key = Swagger.configuration.private_api_key
12
+
13
+ conn = Faraday.new
14
+ resp = conn.post do |req|
15
+ req.url "https://#{host}/api/customer"
16
+ req.headers['Content-Type'] = 'application/json'
17
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
18
+ req.body = data.to_json
19
+ end
20
+ body = JSON.parse(resp.body)
21
+ Swagger::Response.new(resp.status, body)
22
+ return body
23
+ end
24
+
25
+ # ===== Parameters:
26
+ # *+customer_id+ : String
27
+ # ===== Return:
28
+ # +Hash+ - {
29
+ # "id": "string",
30
+ # "firstName": "string",
31
+ # "lastName": "string",
32
+ # "email": "string",
33
+ # "gender": "M",
34
+ # "phone": "string",
35
+ # "legalIdNumber": "string",
36
+ # "merchantCustomerId": "string",
37
+ # "address": {
38
+ # "addressLine1": "string",
39
+ # "addressLine2": "string",
40
+ # "postalCode": "string",
41
+ # "city": {
42
+ # "name": "Bogotá",
43
+ # "state": "DC",
44
+ # "country": "CO"
45
+ # }
46
+ # }
47
+ # }
48
+ def get_customer_by_id(customer_id)
49
+ host = Swagger.configuration.host
50
+ api_key = Swagger.configuration.private_api_key
51
+
52
+ conn = Faraday.new
53
+ resp = conn.get do |req|
54
+ req.url "https://#{host}/api/customer/#{customer_id}"
55
+ req.headers['Content-Type'] = 'application/json'
56
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
57
+ end
58
+ body = JSON.parse(resp.body)
59
+ Swagger::Response.new(resp.status, body)
60
+ return body
61
+ end
62
+
63
+ # ===== Parameters:
64
+ # *+customer_id+ : String
65
+ def delete_customer_by_id(customer_id)
66
+ host = Swagger.configuration.host
67
+ api_key = Swagger.configuration.private_api_key
68
+
69
+ conn = Faraday.new
70
+ resp = conn.delete do |req|
71
+ req.url "https://#{host}/api/customer/#{customer_id}"
72
+ req.headers['Content-Type'] = 'application/json'
73
+ req.headers['Authorization'] = 'Basic ' + ["#{api_key}:"].pack('m').delete("\r\n")
74
+ end
75
+ body = JSON.parse(resp.body)
76
+ Swagger::Response.new(resp.status, body)
77
+ return body
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,14 @@
1
+ module TpagaService
2
+ extend self
3
+ module RefundApi
4
+ extend self
5
+ def get_refund_credit_card_charge(transaction_id)
6
+ # @refund_req = Tpaga::CreditCardRefund.new(id: transaction_id)
7
+ # return Tpaga::RefundApi.refund_credit_card_charge(@refund_req)
8
+ # rescue Tpaga::ClientError => e
9
+ # data = JSON.parse(e.message, object_class: OpenStruct)
10
+ # TpagaService.raise_errors(e) unless data.status == 404
11
+ # raise Packen::ApiError.new(data.status, data.status, I18n.translate("errors.tpaga_service.#{data.status}"), {source: e.class})
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ require 'faraday'
2
+
3
+ module TpagaService
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+ module TpagaService
2
+ module Swagger
3
+ class Configuration
4
+ #attr_accessor :format, :api_key, :api_key_prefix, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent, :verify_ssl
5
+ attr_accessor :format, :scheme, :host, :base_path, :private_api_key, :public_api_key, :inject_format
6
+
7
+ # Defaults go in here..
8
+ def initialize
9
+ @format = 'json'
10
+ @scheme = 'https'
11
+ @host = 'sandbox.tpaga.co'
12
+ @base_path = '/api'
13
+ #@user_agent = "ruby-swagger-#{Swagger::VERSION}"
14
+ @inject_format = false
15
+ #@force_ending_format = false
16
+ #@camelize_params = true
17
+
18
+ # keys for API key authentication (param-name => api-key)
19
+ @private_api_key = 'njba5fp5v3lserbeg9nikible8mstn8s'
20
+ @public_api_key = 'e8tbtu6bdi1bae34h9nkipschq9heq0a'
21
+ # api-key prefix for API key authentication, e.g. "Bearer" (param-name => api-key-prefix)
22
+ #@api_key_prefix = {}
23
+
24
+ # whether to verify SSL certificate, default to true
25
+ # Note: do NOT set it to false in production code, otherwise you would
26
+ # face multiple types of cryptographic attacks
27
+ @verify_ssl = true
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,276 @@
1
+ module TpagaService
2
+ module Swagger
3
+ class Request
4
+ # require 'uri'
5
+ # require 'addressable/uri'
6
+ # require 'typhoeus'
7
+
8
+ attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params, :auth_names
9
+
10
+ # All requests must have an HTTP method and a path
11
+ # Optionals parameters are :params, :headers, :body, :format, :host
12
+ def initialize(http_method, path, attributes={})
13
+ # attributes[:format] ||= Swagger.configuration.format
14
+ # attributes[:params] ||= {}
15
+ #
16
+ # # Set default headers
17
+ # default_headers = {
18
+ # 'Content-Type' => "application/#{attributes[:format].downcase}",
19
+ # 'User-Agent' => Swagger.configuration.user_agent
20
+ # }
21
+ #
22
+ # # Merge argument headers into defaults
23
+ # attributes[:headers] = default_headers.merge(attributes[:headers] || {})
24
+ #
25
+ # # Stick in the auth token if there is one
26
+ # if Swagger.authenticated?
27
+ # attributes[:headers].merge!({:auth_token => Swagger.configuration.auth_token})
28
+ # end
29
+ #
30
+ # self.http_method = http_method.to_sym
31
+ # self.path = path
32
+ # attributes.each do |name, value|
33
+ # send("#{name.to_s.underscore.to_sym}=", value)
34
+ # end
35
+ #
36
+ # update_params_for_auth!
37
+ end
38
+
39
+
40
+
41
+
42
+ =begin
43
+
44
+ # Update hearder and query params based on authentication settings.
45
+ def update_params_for_auth!
46
+ (@auth_names || []).each do |auth_name|
47
+ case auth_name
48
+ when 'api_key'
49
+ @headers ||= {}
50
+ http_auth_header = 'Basic ' + ["#{Swagger.configuration.api_key}:"].pack('m').delete("\r\n")
51
+ @headers['Authorization'] = http_auth_header
52
+
53
+ end
54
+ end
55
+ end
56
+
57
+ # Get API key (with prefix if set).
58
+ # @param [String] param_name the parameter name of API key auth
59
+ def get_api_key_with_prefix(param_name)
60
+ if Swagger.configuration.api_key_prefix[param_name].present?
61
+ "#{Swagger.configuration.api_key_prefix[param_name]} #{Swagger.configuration.api_key[param_name]}"
62
+ else
63
+ Swagger.configuration.api_key[param_name]
64
+ end
65
+ end
66
+
67
+ # Construct a base URL
68
+ def url(options = {})
69
+ u = Addressable::URI.new(
70
+ :scheme => Swagger.configuration.scheme,
71
+ :host => Swagger.configuration.host,
72
+ :path => self.interpreted_path,
73
+ :query => self.query_string.sub(/\?/, '')
74
+ ).to_s
75
+
76
+ # Drop trailing question mark, if present
77
+ u.sub! /\?$/, ''
78
+
79
+ u
80
+ end
81
+
82
+ # Iterate over the params hash, injecting any path values into the path string
83
+ # e.g. /word.{format}/{word}/entries => /word.json/cat/entries
84
+ def interpreted_path
85
+ p = self.path.dup
86
+
87
+ # Stick a .{format} placeholder into the path if there isn't
88
+ # one already or an actual format like json or xml
89
+ # e.g. /words/blah => /words.{format}/blah
90
+ if Swagger.configuration.inject_format
91
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
92
+ p = p.sub(/^(\/?\w+)/, "\\1.#{format}")
93
+ end
94
+ end
95
+
96
+ # Stick a .{format} placeholder on the end of the path if there isn't
97
+ # one already or an actual format like json or xml
98
+ # e.g. /words/blah => /words/blah.{format}
99
+ if Swagger.configuration.force_ending_format
100
+ unless ['.json', '.xml', '{format}'].any? {|s| p.downcase.include? s }
101
+ p = "#{p}.#{format}"
102
+ end
103
+ end
104
+
105
+ p = p.sub("{format}", self.format.to_s)
106
+
107
+ URI.encode [Swagger.configuration.base_path, p].join("/").gsub(/\/+/, '/')
108
+ end
109
+
110
+ # Massage the request body into a state of readiness
111
+ # If body is a hash, camelize all keys then convert to a json string
112
+ def body=(value)
113
+ if value.is_a?(Hash)
114
+ value = value.inject({}) do |memo, (k,v)|
115
+ memo[k.to_s.camelize(:lower).to_sym] = v
116
+ memo
117
+ end
118
+ end
119
+ @body = value
120
+ end
121
+
122
+ # If body is an object, JSONify it before making the actual request.
123
+ # For form parameters, remove empty value
124
+ def outgoing_body
125
+ # http form
126
+ if headers['Content-Type'] == 'application/x-www-form-urlencoded'
127
+ data = form_params.dup
128
+ data.each do |key, value|
129
+ data[key] = value.to_s if value && !value.is_a?(File) # remove emtpy form parameter
130
+ end
131
+ data
132
+ elsif @body # http body is JSON
133
+ @body.is_a?(String) ? @body : @body.to_json
134
+ else
135
+ nil
136
+ end
137
+ end
138
+
139
+ # Construct a query string from the query-string-type params
140
+ def query_string
141
+ # Iterate over all params,
142
+ # .. removing the ones that are part of the path itself.
143
+ # .. stringifying values so Addressable doesn't blow up.
144
+ query_values = {}
145
+ self.params.each_pair do |key, value|
146
+ next if self.path.include? "{#{key}}" # skip path params
147
+ next if value.blank? && value.class != FalseClass # skip empties
148
+ if Swagger.configuration.camelize_params
149
+ key = key.to_s.camelize(:lower).to_sym
150
+ end
151
+ query_values[key] = value.to_s
152
+ end
153
+
154
+ # We don't want to end up with '?' as our query string
155
+ # if there aren't really any params
156
+ return "" if query_values.blank?
157
+
158
+ # Addressable requires query_values to be set after initialization..
159
+ qs = Addressable::URI.new
160
+ qs.query_values = query_values
161
+ qs.to_s
162
+ end
163
+
164
+ def make
165
+ #TODO use configuration setting to determine if debugging
166
+ #logger = Logger.new STDOUT
167
+ #logger.debug self.url
168
+
169
+ request_options = {
170
+ :ssl_verifypeer => Swagger.configuration.verify_ssl,
171
+ :headers => self.headers.stringify_keys
172
+ }
173
+ response = case self.http_method.to_sym
174
+ when :get,:GET
175
+ Typhoeus::Request.get(
176
+ self.url,
177
+ request_options
178
+ )
179
+
180
+ when :post,:POST
181
+ Typhoeus::Request.post(
182
+ self.url,
183
+ request_options.merge(:body => self.outgoing_body)
184
+ )
185
+
186
+ when :patch,:PATCH
187
+ Typhoeus::Request.patch(
188
+ self.url,
189
+ request_options.merge(:body => self.outgoing_body)
190
+ )
191
+
192
+ when :put,:PUT
193
+ Typhoeus::Request.put(
194
+ self.url,
195
+ request_options.merge(:body => self.outgoing_body)
196
+ )
197
+
198
+ when :delete,:DELETE
199
+ Typhoeus::Request.delete(
200
+ self.url,
201
+ request_options.merge(:body => self.outgoing_body)
202
+ )
203
+ end
204
+ Response.new(response)
205
+ end
206
+
207
+ def response
208
+ self.make
209
+ end
210
+
211
+ def response_code_pretty
212
+ return unless @response.present?
213
+ @response.code.to_s
214
+ end
215
+
216
+ def response_headers_pretty
217
+ return unless @response.present?
218
+ # JSON.pretty_generate(@response.headers).gsub(/\n/, '<br/>') # <- This was for RestClient
219
+ @response.headers.gsub(/\n/, '<br/>') # <- This is for Typhoeus
220
+ end
221
+
222
+ # return 'Accept' based on an array of accept provided
223
+ # @param [Array] header_accept_array Array fo 'Accept'
224
+ # @return String Accept (e.g. application/json)
225
+ def self.select_header_accept header_accept_array
226
+ if header_accept_array.empty?
227
+ return
228
+ elsif header_accept_array.any?{ |s| s.casecmp('application/json')==0 }
229
+ 'application/json' # look for json data by default
230
+ else
231
+ header_accept_array.join(',')
232
+ end
233
+ end
234
+
235
+ # return the content type based on an array of content-type provided
236
+ # @param [Array] content_type_array Array fo content-type
237
+ # @return String Content-Type (e.g. application/json)
238
+ def self.select_header_content_type content_type_array
239
+ if content_type_array.empty?
240
+ 'application/json' # use application/json by default
241
+ elsif content_type_array.any?{ |s| s.casecmp('application/json')==0 }
242
+ 'application/json' # use application/json if it's included
243
+ else
244
+ content_type_array[0]; # otherwise, use the first one
245
+ end
246
+ end
247
+
248
+ # static method to convert object (array, hash, object, etc) to JSON string
249
+ # @param model object to be converted into JSON string
250
+ # @return string JSON string representation of the object
251
+ def self.object_to_http_body model
252
+ return if model.nil?
253
+ _body = nil
254
+ if model.is_a?(Array)
255
+ _body = model.map{|m| object_to_hash(m) }
256
+ else
257
+ _body = object_to_hash(model)
258
+ end
259
+ _body.to_json
260
+ end
261
+
262
+ # static method to convert object(non-array) to hash
263
+ # @param obj object to be converted into JSON string
264
+ # @return string JSON string representation of the object
265
+ def self.object_to_hash obj
266
+ if obj.respond_to?(:to_hash)
267
+ obj.to_hash
268
+ else
269
+ obj
270
+ end
271
+ end
272
+ =end
273
+
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,17 @@
1
+ module TpagaService
2
+ module Swagger
3
+ class Response
4
+
5
+ # ===== Parameters:
6
+ # *+code_response+ : Integer
7
+ # *+message_error+ : Hash
8
+ def initialize(code_response, body_response)
9
+ case code_response
10
+ when 500..510 then raise(ServerError, body_response)
11
+ when 299..426 then raise(ClientError, body_response)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,75 @@
1
+ #require 'logger'
2
+ require 'json'
3
+
4
+ module TpagaService
5
+ module Swagger
6
+ class << self
7
+ # attr_accessor :logger
8
+
9
+ # A Swagger configuration object. Must act like a hash and return sensible
10
+ # values for all Swagger configuration options. See Swagger::Configuration.
11
+ attr_accessor :configuration
12
+
13
+ # Call this method to modify defaults in your initializers.
14
+ #
15
+ # @example
16
+ # Swagger.configure do |config|
17
+ # config.private_api_key = '1234567890abcdef' # private api key authentication
18
+ # config.public_api_key = '9876543210fedcba' # public api key authentication
19
+ # config.format = 'json' # optional, defaults to 'json'
20
+ # end
21
+ #
22
+ def configure
23
+ yield(configuration) if block_given?
24
+
25
+ # Configure logger. Default to use Rails
26
+ # self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
27
+
28
+ # remove :// from scheme
29
+ # configuration.scheme.sub!(/:\/\//, '')
30
+
31
+ # remove http(s):// and anything after a slash
32
+ configuration.host.sub!(/https?:\/\//, '')
33
+ configuration.host = configuration.host.split('/').first
34
+
35
+ # Add leading and trailing slashes to base_path
36
+ configuration.base_path = "/#{configuration.base_path}".gsub(/\/+/, '/')
37
+ configuration.base_path = "" if configuration.base_path == "/"
38
+ end
39
+
40
+ # def authenticated?
41
+ # Swagger.configuration.auth_token.present?
42
+ # end
43
+ #
44
+ # def de_authenticate
45
+ # Swagger.configuration.auth_token = nil
46
+ # end
47
+ #
48
+ # def authenticate
49
+ # return if Swagger.authenticated?
50
+ #
51
+ # if Swagger.configuration.username.blank? || Swagger.configuration.password.blank?
52
+ # raise ClientError, "Username and password are required to authenticate."
53
+ # end
54
+ #
55
+ # request = Swagger::Request.new(
56
+ # :get,
57
+ # "account/authenticate/{username}",
58
+ # :params => {
59
+ # :username => Swagger.configuration.username,
60
+ # :password => Swagger.configuration.password
61
+ # }
62
+ # )
63
+ #
64
+ # response_body = request.response.body
65
+ # Swagger.configuration.auth_token = response_body['token']
66
+ # end
67
+ end
68
+ end
69
+
70
+ class ServerError < StandardError
71
+ end
72
+
73
+ class ClientError < StandardError
74
+ end
75
+ end
@@ -0,0 +1,17 @@
1
+ require 'tpaga_service/swagger'
2
+ require 'tpaga_service/swagger/configuration'
3
+ require 'tpaga_service/swagger/request'
4
+ require 'tpaga_service/swagger/response'
5
+
6
+ # APIs
7
+ require 'tpaga_service/api/version'
8
+ require 'tpaga_service/api/charge_api'
9
+ require 'tpaga_service/api/credit_card_api'
10
+ require 'tpaga_service/api/customer_api'
11
+ require 'tpaga_service/api/refund_api'
12
+
13
+
14
+ module TpagaService
15
+ # Initialize the default configuration
16
+ Swagger.configuration ||= Swagger::Configuration.new
17
+ end
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tpaga_service/api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tpaga_service"
8
+ spec.version = TpagaService::VERSION
9
+ spec.authors = ["Juan Contreras"]
10
+ spec.email = ["juan.contreras@packen.co"]
11
+
12
+ spec.summary = %q{TPaga API Ruby to Integreate with the payment gateway }
13
+ spec.description = %q{TPaga Payment Gateway API
14
+ [Learn about TPaga](https://tpaga_service.co)
15
+ }
16
+ spec.homepage = "https://rubygems.org/gems/tpaga_service"
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
23
+
24
+ spec.metadata["homepage_uri"] = spec.homepage
25
+ spec.metadata["source_code_uri"] = "https://github.com/jpcontreras/tpaga_service"
26
+ spec.metadata["changelog_uri"] = "https://github.com/jpcontreras/tpaga_service/CODE_OF_CONDUCT.md"
27
+ else
28
+ raise "RubyGems 2.0 or newer is required to protect against " \
29
+ "public gem pushes."
30
+ end
31
+
32
+ # Specify which files should be added to the gem when it is released.
33
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
34
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
35
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
36
+ end
37
+ spec.bindir = "exe"
38
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
+ spec.require_paths = ["lib"]
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.17"
42
+ spec.add_development_dependency "faraday"
43
+ spec.add_development_dependency "rake", ">= 12.3.3"
44
+ spec.add_development_dependency "rspec", "~> 3.0"
45
+
46
+ spec.add_runtime_dependency 'faraday'
47
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpaga_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Contreras
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 12.3.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: |
84
+ TPaga Payment Gateway API
85
+ [Learn about TPaga](https://tpaga_service.co)
86
+ email:
87
+ - juan.contreras@packen.co
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/tpaga_service.rb
104
+ - lib/tpaga_service/api/charge_api.rb
105
+ - lib/tpaga_service/api/credit_card_api.rb
106
+ - lib/tpaga_service/api/customer_api.rb
107
+ - lib/tpaga_service/api/refund_api.rb
108
+ - lib/tpaga_service/api/version.rb
109
+ - lib/tpaga_service/swagger.rb
110
+ - lib/tpaga_service/swagger/configuration.rb
111
+ - lib/tpaga_service/swagger/request.rb
112
+ - lib/tpaga_service/swagger/response.rb
113
+ - tpaga_service.gemspec
114
+ homepage: https://rubygems.org/gems/tpaga_service
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ homepage_uri: https://rubygems.org/gems/tpaga_service
120
+ source_code_uri: https://github.com/jpcontreras/tpaga_service
121
+ changelog_uri: https://github.com/jpcontreras/tpaga_service/CODE_OF_CONDUCT.md
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.0.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: TPaga API Ruby to Integreate with the payment gateway
141
+ test_files: []