mp_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b7b0169d1530136f6b19479bb0b7aaa23d787a3e771d145c1b8613c8e7f96a5b
4
+ data.tar.gz: 5ebe7055efab9262284a5ee4561f766c321123634a8a829d6d4535ff41103602
5
+ SHA512:
6
+ metadata.gz: 62e14852365b35f0f2f30aa249d1f6662cb4f32066c9b986568482e33ae27b0db902c8c9faa6e3474d01dda96aec66355219de238264c9fd6c370534146dfcd7
7
+ data.tar.gz: 89df57f8ee749ca1e7186cc631f94d71ed848cdad531523c8c6c9f1ac0a1fa564822714d8caee93eb42e788e210ce6c654738cf41cf1bd2705baeb9690605958
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 caio
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,35 @@
1
+ # MercadoPagoApi
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mercado_pago_api`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mercado_pago_api.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,10 @@
1
+ module MercadoPagoApi
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("templates", __dir__)
5
+ def copy_initializer
6
+ template "initializer.rb", "config/initializers/mercado_pago_api.rb"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ MercadoPagoApi.configure do |config|
2
+ # config.access_token = nil
3
+ end
@@ -0,0 +1,40 @@
1
+ module MercadoPagoApi
2
+ class Client < Ac::Base
3
+ BASE_URL = "https://api.mercadopago.com/v1/"
4
+
5
+ attr_reader :access_token
6
+ def initialize(access_token=MercadoPagoApi.configuration.access_token)
7
+ @headers = {
8
+ "Content-Type": "application/json",
9
+ 'x-idempotency-key' => SecureRandom.uuid
10
+ }
11
+ super access_token
12
+ end
13
+
14
+ def create_payment(body)
15
+ response = post("/payments", body: body, headers: @headers) {_1.json['id']}
16
+ response.json
17
+ end
18
+
19
+ def get_payment(payment_id)
20
+ response = get("/payments/#{payment_id}") {_1.json['id']}
21
+ response.json
22
+ end
23
+
24
+ def create_token(card_token_data)
25
+ response = post("/card_tokens", body: card_token_data, headers: @headers){_1.json['id']}
26
+ response.json
27
+ end
28
+
29
+ def search_payment_methods(query)
30
+ response = get("/payment_methods/search", params: query) {_1.json['results'][0]}
31
+ response.json
32
+ end
33
+
34
+ def get_payment_methods
35
+ response = get("/payment_methods") {_1.json['id']}
36
+ response.json
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,94 @@
1
+ module MercadoPagoApi
2
+ class Payment
3
+
4
+ def self.find_by_id(payment_id)
5
+ response = Client.new.get_payment(payment_id)
6
+ new(**build_hash(response))
7
+ end
8
+
9
+ attr_reader :status_detail, :three_ds_info_creq, :three_ds_info_external_resource_url, :three_d_secure_mode, :id, :description, :date_approved, :date_created, :money_release_date, :payer_email, :payer_identification_type, :payer_identification_number, :payment_method, :payment_type, :qr_code, :qr_code_base_64, :transaction_id, :ticket_url, :status, :amount, :token, :issuer_id, :installments
10
+ def initialize(status_detail:nil, three_ds_info_creq:nil, three_ds_info_external_resource_url:nil, three_d_secure_mode:nil, id:nil, description:nil, date_approved:nil, date_created:nil, money_release_date:nil, payer_email:, payer_identification_type:, payer_identification_number:, payment_method:, payment_type:nil, qr_code:nil, qr_code_base_64:nil, transaction_id:nil, ticket_url:nil, status:nil, amount:, token:nil, issuer_id:nil, installments:nil)
11
+ @id = id
12
+ @description = description
13
+ @date_approved = date_approved
14
+ @date_created = date_created
15
+ @money_release_date = money_release_date
16
+ @payer_email = payer_email
17
+ @payer_identification_type = payer_identification_type
18
+ @payer_identification_number = payer_identification_number
19
+ @payment_method = payment_method
20
+ @payment_type = payment_type
21
+ @qr_code = qr_code
22
+ @qr_code_base_64 = qr_code_base_64
23
+ @transaction_id = transaction_id
24
+ @ticket_url = ticket_url
25
+ @status = status
26
+ @amount = amount
27
+ @token = token
28
+ @issuer_id = issuer_id
29
+ @installments = installments
30
+ @three_d_secure_mode = three_d_secure_mode
31
+ @three_ds_info_external_resource_url = three_ds_info_external_resource_url
32
+ @three_ds_info_creq = three_ds_info_creq
33
+ @status_detail = status_detail
34
+ end
35
+
36
+ def build_json
37
+ send("build_json_#{payment_method == 'pix' ? 'pix' : 'credit_card'}").except(:description)
38
+ end
39
+
40
+ def build_json_pix
41
+ {
42
+ transaction_amount: amount,
43
+ description: description,
44
+ payment_method_id: payment_method,
45
+ payer: {
46
+ email: payer_email,
47
+ identification: {
48
+ type: payer_identification_type,
49
+ number: payer_identification_number
50
+ }
51
+ }
52
+ }
53
+ end
54
+
55
+ def build_json_credit_card
56
+ build_json_pix.merge({
57
+ token: token,
58
+ issuer_id: issuer_id,
59
+ installments: installments,
60
+ three_d_secure_mode: three_d_secure_mode ? 'optional' : 'not_supported'
61
+ })
62
+ end
63
+
64
+ def create
65
+ response = Client.new.create_payment(JSON.dump(build_json))
66
+ self.class.new(**self.class.build_hash(response))
67
+ end
68
+
69
+ def self.build_hash json_response
70
+ {
71
+ id: json_response.dig('id'),
72
+ description: json_response.dig('description'),
73
+ date_approved: json_response.dig('date_approved'),
74
+ date_created: json_response.dig('date_created'),
75
+ money_release_date: json_response.dig('money_release_date'),
76
+ payer_email: json_response.dig('payer', 'email'),
77
+ payer_identification_type: json_response.dig('payer', 'identification', 'type'),
78
+ payer_identification_number: json_response.dig('payer', 'identification', 'number'),
79
+ payment_method: json_response.dig('payment_method_id'),
80
+ payment_type: json_response.dig('payment_type_id'),
81
+ qr_code: json_response.dig('point_of_interaction', 'transaction_data', 'qr_code'),
82
+ qr_code_base_64: json_response.dig('point_of_interaction', 'transaction_data', 'qr_code_base64'),
83
+ three_ds_info_external_resource_url: json_response.dig('three_ds_info', 'external_resource_url'),
84
+ three_ds_info_creq: json_response.dig('three_ds_info', 'creq'),
85
+ transaction_id: json_response.dig('point_of_interaction', 'transaction_data', 'transaction_id'),
86
+ ticket_url: json_response.dig('point_of_interaction', 'transaction_data', 'ticket_url'),
87
+ status: json_response.dig('status'),
88
+ status_detail: json_response.dig('status_detail'),
89
+ amount: json_response.dig('transaction_amount')
90
+ }
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,37 @@
1
+ module MercadoPagoApi
2
+
3
+ class PaymentMethod
4
+
5
+ def self.find_by_first_six_digits(first_six_digits, credit: true)
6
+ response = Client.new.search_payment_methods(build_query(first_six_digits))
7
+ new(**build_hash(response, credit))
8
+ end
9
+
10
+ def self.build_query(first_six_digits)
11
+ {
12
+ marketplace: 'NONE',
13
+ status: 'active',
14
+ bins: first_six_digits
15
+ }
16
+ end
17
+
18
+ def self.build_hash(response, credit)
19
+ payment_type_id = credit ? 'credit_card' : 'debit_card'
20
+ payment_method = response.dig('results').find {|pm| pm['payment_type_id'] == payment_type_id}
21
+ {
22
+ payment_method_id: payment_method.dig('id'),
23
+ issuer_id: payment_method.dig('issuer', 'id'),
24
+ installments: payment_method.dig('payer_costs')
25
+ }
26
+ end
27
+
28
+ attr_reader :payment_method_id, :issuer_id, :installments
29
+ def initialize(payment_method_id: , issuer_id: , installments:)
30
+ @payment_method_id = payment_method_id
31
+ @issuer_id = issuer_id
32
+ @installments = installments
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,58 @@
1
+ module MercadoPagoApi
2
+
3
+ class Token
4
+
5
+ attr_reader :card_number, :expiration_year, :expiration_month, :security_code, :cardholder_name, :id
6
+ def initialize(card_number:nil, expiration_year:nil, expiration_month:nil, security_code:nil, cardholder_name:nil, id:nil)
7
+ @card_number = card_number
8
+ @expiration_year = expiration_year
9
+ @expiration_month = expiration_month
10
+ @security_code = security_code
11
+ @cardholder_name = cardholder_name
12
+ @id = id
13
+ end
14
+
15
+ def build_json
16
+ {
17
+ card_number: card_number,
18
+ expiration_year: expiration_year,
19
+ expiration_month: expiration_month,
20
+ security_code: security_code,
21
+ cardholder: {
22
+ name: cardholder_name
23
+ }
24
+ }
25
+ end
26
+
27
+ def create
28
+ response = Client.new.create_token(JSON.dump(build_json))
29
+ self.class.new(**self.class.build_hash(response))
30
+ end
31
+
32
+ def self.build_hash(response)
33
+ {
34
+ id: response['id'],
35
+ expiration_year: response['expiration_year'],
36
+ expiration_month: response['expiration_month'],
37
+ cardholder_name: response['cardholder']['name']
38
+ }
39
+ end
40
+
41
+ end
42
+ end
43
+
44
+ # {"id"=>"78558e7bbb73b8939df27269fb6dcbf5",
45
+ # "first_six_digits"=>"503143",
46
+ # "expiration_month"=>11,
47
+ # "expiration_year"=>2025,
48
+ # "last_four_digits"=>"6351",
49
+ # "cardholder"=>{"identification"=>{}, "name"=>"APRO"},
50
+ # "status"=>"active",
51
+ # "date_created"=>"2023-11-27T09:42:13.887-04:00",
52
+ # "date_last_updated"=>"2023-11-27T09:42:13.887-04:00",
53
+ # "date_due"=>"2023-12-05T09:42:13.887-04:00",
54
+ # "luhn_validation"=>true,
55
+ # "live_mode"=>false,
56
+ # "require_esc"=>false,
57
+ # "card_number_length"=>16,
58
+ # "security_code_length"=>3}
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MercadoPagoApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ac"
4
+ require_relative "mercado_pago_api/client"
5
+ require_relative "mercado_pago_api/payment"
6
+ require_relative "mercado_pago_api/token"
7
+ require_relative "mercado_pago_api/payment_method"
8
+ require_relative "mercado_pago_api/version"
9
+ module MercadoPagoApi
10
+ class Error < StandardError; end
11
+
12
+ class Configuration
13
+ attr_accessor :access_token, :public_key
14
+
15
+ def initialize
16
+ end
17
+ end
18
+
19
+ class << self
20
+ attr_accessor :configuration
21
+ end
22
+
23
+ def self.configure
24
+ self.configuration ||= Configuration.new
25
+ yield configuration
26
+ end
27
+
28
+ end
29
+
30
+
31
+ # require "json"
32
+ # require "mercadopago"
33
+
34
+ # public_key = 'TEST-a083c930-a19c-4b7f-b5b8-69710fdab075'
35
+ # access_token_test = 'TEST-928288737990873-111314-2de12dbb8938464bdce453b10e86929c-269641183'
36
+ # access_token = 'APP_USR-928288737990873-111314-a89b6befd776eb2b0a3266a3eb34156e-269641183'
37
+
38
+ # sdk = Mercadopago::SDK.new(access_token_test)
39
+
40
+ # payment_data = {
41
+ # transaction_amount: 1,
42
+ # description: "pagamento",
43
+ # payment_method_id: "pix",
44
+ # payer: {
45
+ # email: 'caioif4@gmail.com',
46
+ # identification: {
47
+ # type: "CPF",
48
+ # number: "34473469816"
49
+ # }
50
+ # }
51
+ # }
52
+
53
+ # # result = sdk.payment.create(payment_data)
54
+ # # ayment = result[:response]
55
+
56
+ # result = sdk.payment.get("1319503347")
57
+ # payment = result[:response]
58
+ # puts payment
59
+ # @@API_BASE_URL = 'https://api.mercadopago.com'
@@ -0,0 +1,4 @@
1
+ module MercadoPagoApi
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mp_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - caio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ac
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - caioif4@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/generators/mercado_pago_api/install/install_generator.rb
38
+ - lib/generators/mercado_pago_api/install/templates/initializer.rb
39
+ - lib/mercado_pago_api.rb
40
+ - lib/mercado_pago_api/client.rb
41
+ - lib/mercado_pago_api/payment.rb
42
+ - lib/mercado_pago_api/payment_method.rb
43
+ - lib/mercado_pago_api/token.rb
44
+ - lib/mercado_pago_api/version.rb
45
+ - sig/mercado_pago_api.rbs
46
+ homepage: https://github.com/CaioGarcia1/mercado_pago_api
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/CaioGarcia1/mercado_pago_api
51
+ source_code_uri: https://github.com/CaioGarcia1/mercado_pago_api
52
+ changelog_uri: https://github.com/CaioGarcia1/mercado_pago_api
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.6
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: ''
72
+ test_files: []