mp_api 1.2.2 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59d843843d099fc02a62263d86caacb6d71655c160680034923a443155c2b1c8
4
- data.tar.gz: 42bf488a1003f5e38d2418125e5d5b29f0de78c7c0aca65bb6a9329f2831f092
3
+ metadata.gz: e7626180e5c5faecffcf4f6f0a29befaf72d9a572549697ef4ebae057e404c85
4
+ data.tar.gz: ffb2373be7426f18b8f2ec3be5b7fd7a1e48e03ac6b19bd92f79b6d6fd574403
5
5
  SHA512:
6
- metadata.gz: 0b769d68eb164df97a1ff8d2e7745b6205472acb3619aa22ee41fd1f869c17099cc1479bee8743dfcc30ada82c6b553169dfc43c55e6121e8ee6e4026f0b7d5b
7
- data.tar.gz: 8d5503eb188b0bdc9bf1be1b9ecc8e2532a33b5474048bda8c46ef897cc8b9d597e63a6b7843f1af48ab8c328d0f1bcf61f8d12021dec506afc254c0eb226613
6
+ metadata.gz: bc745ec3c6f9820bf9ff76ed44485baf7300e1197fa4274d5afe1e8ff275c0d949bdefcfea394be558c800aae6b1007e7316adc95226006e28672ad43999e63f
7
+ data.tar.gz: a8be7e24c71e403b3ff02017d137dfb4ea2c588b6edc697e7f6575e3ce16c23d3a1ea37c2470abe107ea319abe6837238c23eb6a697e5ffb0b0964a186178357
data/README.md CHANGED
@@ -1,86 +1,174 @@
1
1
  # MpApi
2
2
 
3
- Wrapper de integração com a API do Mercado Pago.
4
-
5
- [Documentação API Mercado Pago](https://www.mercadopago.com.br/developers/pt/docs)
6
-
7
- ### Funcionalidades:
8
- - Criar pagamentos Pix ou cartão de crédito
9
- - Gerar token único para um cartão de crédito
10
- - Atualizar e encontrar pagamentos pelo ID
11
- - Buscar bandeira do cartão a partir dos seis primeiros digitos
3
+ Uma gem Ruby para integração com a API do Mercado Pago, oferecendo uma interface simples e elegante para gerenciar pagamentos, clientes e cartões.
12
4
 
13
5
  ## Instalação
14
6
 
15
- Adicionar mp_api no Gemfile:
7
+ Adicione esta linha ao Gemfile da sua aplicação:
16
8
 
17
9
  ```ruby
18
10
  gem 'mp_api'
19
11
  ```
20
- ou
21
- ```shell
22
- bundle add mp_api
12
+
13
+ E então execute:
14
+
15
+ ```bash
16
+ bundle install
23
17
  ```
24
18
 
25
- Instalar a gem:
19
+ Ou instale diretamente:
26
20
 
27
- ```shell
28
- bin/rails generate mp_api:install
21
+ ```bash
22
+ gem install mp_api
29
23
  ```
30
24
 
31
- ## Uso
25
+ ### Criando um Cliente
32
26
 
33
- Configurar access_token no initializer ou direto no código:
34
27
  ```ruby
35
- MpApi.configure do |config|
36
- config.access_token = "ACCESS_TOKEN"
37
- end
28
+ # Inicialize o cliente
29
+ client = MpApi::Client.new("SEU_ACCESS_TOKEN")
38
30
  ```
39
31
 
40
- ### Exemplos de criação de pagamento
41
- #### Cartão de crédito
32
+ ### Pagamentos PIX
33
+
42
34
  ```ruby
43
- # Criação de um token para cartão teste
44
- token = MpApi::Token.new(
45
- card_number: "5031433215406351",
46
- expiration_year: "2025",
47
- expiration_month: "11",
48
- security_code: "123",
49
- cardholder_name: "APRO"
50
- ).create
51
- puts token.id # c8ad2335a9bb58e585a8c35bfd9f29ad
52
-
53
- # Encontra o ID e a bandeira do cartão
54
- payment_method = MpApi::PaymentMethod.find_by_first_six_digits("503143")
55
- puts payment_method.payment_method_id # "master"
56
-
57
- # Criação do pagamento
58
- payment = MpApi::Payment.new(
59
- amount: 140.0,
60
- payment_method: payment_method.payment_method_id,
61
- payer_email: "email@example.com",
35
+ # Criar um pagamento PIX
36
+ response = client.create_pix_payment(
37
+ amount: 100.50,
38
+ description: "Pagamento de teste",
39
+ payment_method: "pix",
40
+ payer_email: "cliente@exemplo.com",
62
41
  payer_identification_type: "CPF",
63
- payer_identification_number: "12345678909",
64
- token: token.id,
65
- issuer_id: payment_method.issuer_id,
66
- installments: 1,
42
+ payer_identification_number: "12345678901",
43
+ date_of_expiration: "2025-12-31T23:59:59.000-04:00"
44
+ )
45
+ ```
46
+
47
+ ### Pagamentos com Cartão de Crédito
48
+
49
+ ```ruby
50
+ # Criar um pagamento com cartão de crédito
51
+ response = client.create_credit_card_payment(
52
+ amount: 250.00,
53
+ description: "Compra online",
54
+ payment_method: "visa",
55
+ payer_email: "cliente@exemplo.com",
56
+ payer_identification_type: "CPF",
57
+ payer_identification_number: "12345678901",
58
+ token: "TOKEN_DO_CARTAO",
59
+ installments: 3,
67
60
  three_d_secure_mode: true
68
- ).create
61
+ )
62
+ ```
63
+
64
+ ### Pagamentos com Cartão Salvo
69
65
 
70
- puts payment.id # 1318474186
71
- puts payment.status # "approved"
66
+ ```ruby
67
+ # Criar pagamento com cartão previamente salvo
68
+ response = client.create_saved_credit_card_payment(
69
+ amount: 150.00,
70
+ token: "TOKEN_DO_CARTAO_SALVO",
71
+ installments: 1,
72
+ customer_id: "CUSTOMER_ID"
73
+ )
72
74
  ```
73
75
 
74
- #### Pix
76
+ ### Gerenciamento de Pagamentos
77
+
75
78
  ```ruby
76
- # Criação do pagamento
77
- payment = MpApi::Payment.new(
78
- payer_email: "email@example.com",
79
- payer_identification_type: "CPF",
80
- payer_identification_number: "12345678909",
81
- payment_method: "pix",
82
- amount: 140.0
83
- ).create
84
- puts payment.id # 1318474264
85
- puts payment.status # "pending"
86
- ```
79
+ # Consultar um pagamento
80
+ payment = client.get_payment("PAYMENT_ID")
81
+
82
+ # Atualizar status de um pagamento
83
+ client.update_payment(
84
+ payment_id: "PAYMENT_ID",
85
+ status: "cancelled"
86
+ )
87
+ ```
88
+
89
+ ### Tokenização de Cartões
90
+
91
+ ```ruby
92
+ # Criar token para um cartão
93
+ token_response = client.create_token(
94
+ card_number: "4111111111111111",
95
+ expiration_year: "2028",
96
+ expiration_month: "12",
97
+ security_code: "123",
98
+ cardholder_name: "João Silva"
99
+ )
100
+ ```
101
+
102
+ ### Métodos de Pagamento
103
+
104
+ ```ruby
105
+ # Buscar métodos de pagamento por BIN
106
+ payment_methods = client.search_payment_methods(
107
+ first_six_digits: "411111"
108
+ )
109
+
110
+ # Listar todos os métodos de pagamento
111
+ all_methods = client.get_payment_methods
112
+ ```
113
+
114
+ ### Gerenciamento de Clientes
115
+
116
+ ```ruby
117
+ # Buscar cliente por email
118
+ customer = client.get_customer("cliente@exemplo.com")
119
+
120
+ # Criar um novo cliente
121
+ new_customer = client.create_customer(
122
+ email: "novo@cliente.com",
123
+ first_name: "João",
124
+ identification_type: "CPF",
125
+ identification_number: "12345678901",
126
+ phone_area_code: "11",
127
+ phone_number: "999999999"
128
+ )
129
+ ```
130
+
131
+ ### Gerenciamento de Cartões
132
+
133
+ ```ruby
134
+ # Adicionar cartão a um cliente
135
+ card = client.create_card(
136
+ customer_id: "CUSTOMER_ID",
137
+ token: "TOKEN_DO_CARTAO"
138
+ )
139
+
140
+ # Remover cartão de um cliente
141
+ client.delete_card(
142
+ customer_id: "CUSTOMER_ID",
143
+ card_id: "CARD_ID"
144
+ )
145
+ ```
146
+
147
+ ## Tratamento de Erros
148
+
149
+ A gem utiliza a classe `MpApi::Error` para tratamento de erros:
150
+
151
+ ```ruby
152
+ begin
153
+ response = client.create_pix_payment(...)
154
+ rescue MpApi::Error => e
155
+ puts "Erro na API: #{e.message}"
156
+ end
157
+ ```
158
+
159
+ ## Funcionalidades
160
+
161
+ - ✅ Pagamentos PIX
162
+ - ✅ Pagamentos com cartão de crédito
163
+ - ✅ Pagamentos com cartões salvos
164
+ - ✅ Tokenização de cartões
165
+ - ✅ Gerenciamento de clientes
166
+ - ✅ Gerenciamento de cartões
167
+ - ✅ Consulta de métodos de pagamento
168
+ - ✅ Atualização de pagamentos
169
+ - ✅ Suporte a parcelamento
170
+ - ✅ Suporte a 3D Secure
171
+
172
+ ## Dependências
173
+
174
+ Esta gem depende da gem `ac` (>= 1.1.0) para funcionalidades de cliente HTTP.
data/lib/mp_api/client.rb CHANGED
@@ -4,52 +4,126 @@ module MpApi
4
4
  MAX_RETRIES = 1
5
5
  SAVE_RESPONSES = true
6
6
 
7
- attr_reader :access_token
8
- def initialize(access_token = MpApi.configuration.access_token)
9
- @headers = {
10
- "Content-Type" => "application/json",
11
- "x-idempotency-key" => SecureRandom.uuid
12
- }
13
- super(access_token)
7
+ def create_pix_payment(date_of_expiration: nil, amount:, description: nil, statement_descriptor: nil, payment_method:, payer_email:, payer_identification_type:, payer_identification_number:)
8
+ body = {
9
+ date_of_expiration: date_of_expiration,
10
+ transaction_amount: amount,
11
+ description: description,
12
+ statement_descriptor: statement_descriptor,
13
+ payment_method_id: payment_method,
14
+ payer: {
15
+ email: payer_email,
16
+ identification: {
17
+ type: payer_identification_type,
18
+ number: payer_identification_number
19
+ }
20
+ }
21
+ }.compact
22
+ post("/payments", body:)
14
23
  end
15
24
 
16
- def create_payment(body)
17
- post("/payments", body: body, headers: @headers) { |response| validate_response(response, "id") }
25
+ def create_credit_card_payment(date_of_expiration: nil, amount:, description: nil, statement_descriptor: nil, payment_method:, payer_email:, payer_identification_type:, payer_identification_number:, capture: nil, token: nil, issuer_id: nil, installments: nil, three_d_secure_mode: false)
26
+ body = {
27
+ date_of_expiration: date_of_expiration,
28
+ transaction_amount: amount,
29
+ description: description,
30
+ statement_descriptor: statement_descriptor,
31
+ payment_method_id: payment_method,
32
+ payer: {
33
+ email: payer_email,
34
+ identification: {
35
+ type: payer_identification_type,
36
+ number: payer_identification_number
37
+ }
38
+ },
39
+ capture: capture,
40
+ token: token,
41
+ issuer_id: issuer_id,
42
+ installments: installments,
43
+ three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported"
44
+ }.compact
45
+ post("/payments", body:)
46
+ end
47
+
48
+ def create_saved_credit_card_payment(amount:, token: nil, installments: nil, customer_id: nil)
49
+ body = {
50
+ transaction_amount: amount,
51
+ token: token,
52
+ installments: installments,
53
+ payer: {
54
+ type: "customer",
55
+ id: customer_id
56
+ }
57
+ }
58
+ post("/payments", body:)
18
59
  end
19
60
 
20
61
  def get_payment(payment_id)
21
- get("/payments/#{payment_id}") { |response| validate_response(response, "id") }
62
+ get("/payments/#{payment_id}")
22
63
  end
23
64
 
24
- def update_payment(payment_id, body)
25
- put("/payments/#{payment_id}", body: body, headers: @headers) { |response| validate_response(response, "id") }
65
+ def update_payment(payment_id:, status:)
66
+ body = {
67
+ status: status
68
+ }
69
+ put("/payments/#{payment_id}", body:)
26
70
  end
27
71
 
28
- def create_token(card_token_data)
29
- post("/card_tokens", body: card_token_data, headers: @headers) { |response| validate_response(response, "id") }
72
+ def create_token(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:)
73
+ body = {
74
+ card_number: card_number,
75
+ expiration_year: expiration_year,
76
+ expiration_month: expiration_month,
77
+ security_code: security_code,
78
+ cardholder: {
79
+ name: cardholder_name
80
+ }
81
+ }
82
+ post("/card_tokens", body:)
30
83
  end
31
84
 
32
- def search_payment_methods(query)
33
- get("/payment_methods/search", params: query) { |response| validate_response(response, ["results", 0]) }
85
+ def search_payment_methods(first_six_digits:)
86
+ body = {
87
+ marketplace: "NONE",
88
+ status: "active",
89
+ bins: first_six_digits
90
+ }
91
+ get("/payment_methods/search", params: body)
34
92
  end
35
93
 
36
94
  def get_payment_methods
37
- get("/payment_methods") { |response| validate_response(response, "id") }
95
+ get("/payment_methods")
38
96
  end
39
97
 
40
- def create_customer(body)
41
- post("/customers", body: body, headers: @headers) { |response| validate_response(response, "id") }
98
+ def get_customer(email)
99
+ get("/customers/search?email=#{email}")
42
100
  end
43
101
 
44
- def create_card(customer_id, body)
45
- post("/customers/#{customer_id}/cards", body: body, headers: @headers) { |response| validate_response(response, "id") }
102
+ def create_customer(email:, first_name: nil, identification_type:, identification_number:, phone_area_code: "55", phone_number: nil)
103
+ body = {
104
+ email: email,
105
+ first_name: first_name,
106
+ identification: {
107
+ type: identification_type,
108
+ number: identification_number
109
+ },
110
+ phone: {
111
+ area_code: phone_area_code,
112
+ number: phone_number
113
+ }
114
+ }
115
+ post("/customers", body:)
46
116
  end
47
117
 
48
- private
118
+ def create_card(customer_id:, token:)
119
+ body = {
120
+ token: token
121
+ }
122
+ post("/customers/#{customer_id}/cards", body:)
123
+ end
49
124
 
50
- def validate_response(response, required_response_key)
51
- puts response.json unless response.success?
52
- ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
125
+ def delete_card(customer_id:, card_id:)
126
+ delete("/customers/#{customer_id}/cards/#{card_id}")
53
127
  end
54
128
  end
55
129
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MpApi
4
- VERSION = "1.2.2"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/mp_api.rb CHANGED
@@ -3,33 +3,8 @@
3
3
  require "ac"
4
4
  require "securerandom"
5
5
  require_relative "mp_api/client"
6
- require_relative "mp_api/payment"
7
- require_relative "mp_api/payment_error"
8
- require_relative "mp_api/token"
9
- require_relative "mp_api/payment_method"
10
- require_relative "mp_api/customer"
11
- require_relative "mp_api/card"
12
6
  require_relative "mp_api/version"
13
7
 
14
8
  module MpApi
15
9
  class Error < StandardError; end
16
-
17
- class RequestError < StandardError
18
- def initialize(message = "Request error")
19
- super(message)
20
- end
21
- end
22
-
23
- class Configuration
24
- attr_accessor :access_token, :public_key
25
- end
26
-
27
- class << self
28
- attr_accessor :configuration
29
- end
30
-
31
- def self.configure
32
- self.configuration ||= Configuration.new
33
- yield configuration
34
- end
35
10
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mp_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todas Essas Coisas
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: ac
@@ -16,15 +15,14 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '0'
18
+ version: 1.1.0
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '0'
27
- description:
25
+ version: 1.1.0
28
26
  email:
29
27
  - caioif4@gmail.com
30
28
  executables: []
@@ -34,16 +32,8 @@ files:
34
32
  - LICENSE.txt
35
33
  - README.md
36
34
  - Rakefile
37
- - lib/generators/mp_api/install/install_generator.rb
38
- - lib/generators/mp_api/install/templates/initializer.rb
39
35
  - lib/mp_api.rb
40
- - lib/mp_api/card.rb
41
36
  - lib/mp_api/client.rb
42
- - lib/mp_api/customer.rb
43
- - lib/mp_api/payment.rb
44
- - lib/mp_api/payment_error.rb
45
- - lib/mp_api/payment_method.rb
46
- - lib/mp_api/token.rb
47
37
  - lib/mp_api/version.rb
48
38
  homepage: https://github.com/todasessascoisas/mp_api
49
39
  licenses:
@@ -52,7 +42,6 @@ metadata:
52
42
  homepage_uri: https://github.com/todasessascoisas/mp_api
53
43
  source_code_uri: https://github.com/todasessascoisas/mp_api
54
44
  changelog_uri: https://github.com/todasessascoisas/mp_api
55
- post_install_message:
56
45
  rdoc_options: []
57
46
  require_paths:
58
47
  - lib
@@ -67,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
56
  - !ruby/object:Gem::Version
68
57
  version: '0'
69
58
  requirements: []
70
- rubygems_version: 3.5.22
71
- signing_key:
59
+ rubygems_version: 3.6.9
72
60
  specification_version: 4
73
61
  summary: ''
74
62
  test_files: []
@@ -1,10 +0,0 @@
1
- module MpApi
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/mp_api.rb"
7
- end
8
- end
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- MpApi.configure do |config|
2
- # config.access_token = nil
3
- end
data/lib/mp_api/card.rb DELETED
@@ -1,46 +0,0 @@
1
- module MpApi
2
- class Card
3
- attr_reader :token, :customer_id, :external_id, :cardholder_name, :expiration_month, :expiration_year, :first_six_digits, :last_four_digits, :issuer_id, :mp_payment_method_id, :error, :error_detail
4
-
5
- def initialize(token: nil, customer_id: nil, external_id: nil, cardholder_name: nil, expiration_month: nil, expiration_year: nil, first_six_digits: nil, last_four_digits: nil, issuer_id: nil, mp_payment_method_id: nil, error: nil, error_detail: nil)
6
- @token = token
7
- @customer_id = customer_id
8
- @external_id = external_id
9
- @cardholder_name = cardholder_name
10
- @expiration_month = expiration_month
11
- @expiration_year = expiration_year
12
- @first_six_digits = first_six_digits
13
- @last_four_digits = last_four_digits
14
- @issuer_id = issuer_id
15
- @mp_payment_method_id = mp_payment_method_id
16
- @error = error
17
- @error_detail = error_detail
18
- end
19
-
20
- def build_json
21
- {
22
- token: token
23
- }
24
- end
25
-
26
- def create
27
- response = Client.new.create_card(customer_id, JSON.dump(build_json))
28
- self.class.new(**self.class.build_hash(response.json))
29
- end
30
-
31
- def self.build_hash(response)
32
- {
33
- external_id: response.dig("id"),
34
- cardholder_name: response.dig("cardholder", "name"),
35
- expiration_month: response.dig("expiration_month"),
36
- expiration_year: response.dig("expiration_year"),
37
- first_six_digits: response.dig("first_six_digits"),
38
- last_four_digits: response.dig("last_four_digits"),
39
- issuer_id: response.dig("issuer", "id"),
40
- mp_payment_method_id: response.dig("payment_method", "id"),
41
- error: response.dig("message"),
42
- error_detail: response.dig("cause")
43
- }
44
- end
45
- end
46
- end
@@ -1,54 +0,0 @@
1
- module MpApi
2
- class Customer
3
-
4
- attr_reader :external_id, :email, :first_name, :identification_type, :identification_number, :phone_area_code, :phone_number, :status, :error, :error_detail
5
- def initialize(external_id: nil, email:, first_name: nil, identification_type:, identification_number:, phone_area_code: "55", phone_number: nil, status: nil, error: nil, error_detail: nil)
6
- @external_id = external_id
7
- @email = email
8
- @first_name = first_name
9
- @identification_type = identification_type
10
- @identification_number = identification_number
11
- @phone_area_code = phone_area_code
12
- @phone_number = phone_number
13
- @status = status
14
- @error = error
15
- @error_detail = error_detail
16
- end
17
-
18
- def build_json
19
- {
20
- email: email,
21
- first_name: first_name,
22
- identification: {
23
- type: identification_type,
24
- number: identification_number
25
- },
26
- phone: {
27
- area_code: phone_area_code,
28
- number: phone_number
29
- }
30
- }
31
- end
32
-
33
- def create
34
- response = Client.new.create_customer(JSON.dump(build_json))
35
- self.class.new(**self.class.build_hash(response.json))
36
- end
37
-
38
- def self.build_hash(response)
39
- {
40
- external_id: response.dig("id"),
41
- email: response.dig("email"),
42
- first_name: response.dig("first_name"),
43
- identification_type: response.dig("identification", "type"),
44
- identification_number: response.dig("identification", "number"),
45
- phone_area_code: response.dig("phone", "area_code"),
46
- phone_number: response.dig("phone", "number"),
47
- status: response.dig("status"),
48
- error: response.dig("message"),
49
- error_detail: response.dig("cause")
50
- }
51
- end
52
-
53
- end
54
- end
@@ -1,149 +0,0 @@
1
- module MpApi
2
- class Payment
3
- 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, :last_four_digits, :payment_type, :qr_code, :qr_code_base_64, :transaction_id, :ticket_url, :status, :amount, :token, :issuer_id, :installments, :statement_descriptor, :items, :error, :internal_error, :capture, :date_of_expiration, :saved_card, :customer_id
4
- def initialize(payer_email:, payer_identification_type:, payer_identification_number:, payment_method:, amount:, last_four_digits: nil, 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, payment_type: nil, qr_code: nil, qr_code_base_64: nil, transaction_id: nil, ticket_url: nil, status: nil, token: nil, issuer_id: nil, installments: nil, items: nil, statement_descriptor: nil, error: nil, internal_error: nil, capture: nil, date_of_expiration: nil, saved_card: false, customer_id: nil)
5
- @id = id
6
- @description = description
7
- @date_approved = date_approved
8
- @date_created = date_created
9
- @money_release_date = money_release_date
10
- @payer_email = payer_email
11
- @payer_identification_type = payer_identification_type
12
- @payer_identification_number = payer_identification_number
13
- @payment_method = payment_method
14
- @last_four_digits = last_four_digits
15
- @payment_type = payment_type
16
- @qr_code = qr_code
17
- @qr_code_base_64 = qr_code_base_64
18
- @transaction_id = transaction_id
19
- @ticket_url = ticket_url
20
- @status = status
21
- @amount = amount
22
- @token = token
23
- @issuer_id = issuer_id
24
- @installments = installments
25
- @three_d_secure_mode = three_d_secure_mode
26
- @three_ds_info_external_resource_url = three_ds_info_external_resource_url
27
- @three_ds_info_creq = three_ds_info_creq
28
- @status_detail = status_detail
29
- @statement_descriptor = statement_descriptor
30
- @items = items
31
- @error = error
32
- @internal_error = internal_error
33
- @capture = capture
34
- @date_of_expiration = date_of_expiration
35
- @saved_card = saved_card
36
- @customer_id = customer_id
37
- end
38
-
39
- def build_json
40
- send("build_json_#{(payment_method == "pix") ? "pix" : "credit_card"}").except(:description)
41
- end
42
-
43
- def build_update_json
44
- {
45
- # capture: capture,
46
- status: status
47
- # date_of_expiration: date_of_expiration,
48
- # transaction_amount: amount
49
- }.compact
50
- end
51
-
52
- def build_json_pix
53
- {
54
- date_of_expiration: date_of_expiration,
55
- transaction_amount: amount,
56
- description: description,
57
- statement_descriptor: statement_descriptor,
58
- additional_info: {
59
- items: items
60
- },
61
- payment_method_id: payment_method,
62
- payer: {
63
- email: payer_email,
64
- identification: {
65
- type: payer_identification_type,
66
- number: payer_identification_number
67
- }
68
- }
69
- }.compact
70
- end
71
-
72
- def build_json_credit_card
73
- if saved_card
74
- {
75
- transaction_amount: amount,
76
- token: token,
77
- installments: installments,
78
- payer: {
79
- type: "customer",
80
- id: customer_id
81
- }
82
- }
83
- else
84
- build_json_pix.merge({
85
- capture: capture,
86
- token: token,
87
- issuer_id: issuer_id,
88
- installments: installments,
89
- three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported"
90
- }.compact)
91
- end
92
- end
93
-
94
- def create
95
- response = Client.new.create_payment(JSON.dump(build_json))
96
- self.class.new(**self.class.build_hash(response.json))
97
- end
98
-
99
- def pix_paid? external_value = nil
100
- external_value = @amount if external_value.nil?
101
- @status == "approved" && external_value.to_d == @amount.to_d
102
- end
103
-
104
- def update attributes
105
- attributes.each do |key, value|
106
- instance_variable_set(:"@#{key}", value)
107
- end
108
- Client.new.update_payment(id, JSON.dump(build_update_json))
109
- self.class.find_by_id(id)
110
- end
111
-
112
- def invalidate_pix!
113
- update(status: "cancelled")
114
- end
115
-
116
- def self.find_by_id(payment_id)
117
- response = Client.new.get_payment(payment_id)
118
- new(**build_hash(response.json))
119
- end
120
-
121
- def self.build_hash json_response
122
- payment_error = PaymentError.new(json_response: json_response)
123
- {
124
- id: json_response.dig("id"),
125
- description: json_response.dig("description"),
126
- date_approved: json_response.dig("date_approved"),
127
- date_created: json_response.dig("date_created"),
128
- money_release_date: json_response.dig("money_release_date"),
129
- payer_email: json_response.dig("payer", "email"),
130
- payer_identification_type: json_response.dig("payer", "identification", "type"),
131
- payer_identification_number: json_response.dig("payer", "identification", "number"),
132
- payment_method: json_response.dig("payment_method_id"),
133
- last_four_digits: json_response.dig("card", "last_four_digits"),
134
- payment_type: json_response.dig("payment_type_id"),
135
- qr_code: json_response.dig("point_of_interaction", "transaction_data", "qr_code"),
136
- qr_code_base_64: json_response.dig("point_of_interaction", "transaction_data", "qr_code_base64"),
137
- three_ds_info_external_resource_url: json_response.dig("three_ds_info", "external_resource_url"),
138
- three_ds_info_creq: json_response.dig("three_ds_info", "creq"),
139
- transaction_id: json_response.dig("point_of_interaction", "transaction_data", "transaction_id"),
140
- ticket_url: json_response.dig("point_of_interaction", "transaction_data", "ticket_url"),
141
- status: json_response.dig("status"),
142
- status_detail: json_response.dig("status_detail"),
143
- amount: json_response.dig("transaction_amount"),
144
- error: payment_error.error,
145
- internal_error: payment_error.internal_error
146
- }
147
- end
148
- end
149
- end
@@ -1,49 +0,0 @@
1
- module MpApi
2
- class PaymentError
3
- attr_reader :json_response
4
- def initialize(json_response:)
5
- @json_response = json_response
6
- end
7
-
8
- def error
9
- return nil unless json_response["error"] || ["rejected"].include?(json_response["status"])
10
- rejected_payment_error || failed_payment_error || "Pagamento recusado."
11
- end
12
-
13
- def internal_error
14
- return nil unless json_response["error"]
15
- json_response["cause"]&.first["description"]
16
- end
17
-
18
- private
19
-
20
- def rejected_payment_error
21
- message_by_status_detail(json_response["status_detail"])
22
- end
23
-
24
- def failed_payment_error
25
- message_by_error_message(json_response.dig("cause", 0, "description"))
26
- end
27
-
28
- def message_by_status_detail(status_detail)
29
- {
30
- "cc_rejected_bad_filled_other" => "Pagamento recusado.",
31
- "cc_rejected_bad_filled_date" => "Pagamento recusado. Confira a data de validade do cartão.",
32
- "cc_rejected_bad_filled_security_code" => "Pagamento recusado. Confira o código de segurança (CVV) do cartão.",
33
- "cc_rejected_insufficient_amount" => "Pagamento recusado. Limite insuficiente.",
34
- "cc_rejected_call_for_authorize" => "Pagamento recusado.",
35
- "cc_rejected_other_reason" => "Pagamento recusado.",
36
- "cc_rejected_high_risk" => "Pagamento recusado."
37
- }[status_detail]
38
- end
39
-
40
- def message_by_error_message(error_message)
41
- {
42
- "Invalid user identification number" => "CPF inválido.",
43
- "payer.email must be a valid email" => "E-mail inválido."
44
- # "Payer email forbidden" => "E-mail inválido.", Caso do erro no email test_user_2147198029@testuser.com (Nao sei se da pra causar com email valido)
45
- }[error_message]
46
- end
47
-
48
- end
49
- end
@@ -1,35 +0,0 @@
1
- module MpApi
2
- class PaymentMethod
3
- attr_reader :payment_method_id, :issuer_id, :installments, :errors
4
- def initialize(payment_method_id:, issuer_id:, installments:, errors: nil)
5
- @payment_method_id = payment_method_id
6
- @issuer_id = issuer_id
7
- @installments = installments
8
- @errors = errors
9
- end
10
-
11
- def self.find_by_first_six_digits(first_six_digits, credit: true)
12
- response = Client.new.search_payment_methods(build_query(first_six_digits))
13
- new(**build_hash(response.json, credit))
14
- end
15
-
16
- def self.build_query(first_six_digits)
17
- {
18
- marketplace: "NONE",
19
- status: "active",
20
- bins: first_six_digits
21
- }
22
- end
23
-
24
- def self.build_hash(response, credit)
25
- payment_type_id = credit ? "credit_card" : "debit_card"
26
- payment_method = response.dig("results")&.find { |pm| pm["payment_type_id"] == payment_type_id }
27
- {
28
- payment_method_id: payment_method&.dig("id"),
29
- issuer_id: payment_method&.dig("issuer", "id"),
30
- installments: payment_method&.dig("payer_costs"),
31
- errors: response.dig("message")
32
- }
33
- end
34
- end
35
- end
data/lib/mp_api/token.rb DELETED
@@ -1,43 +0,0 @@
1
- module MpApi
2
- class Token
3
- attr_reader :card_number, :expiration_year, :expiration_month, :security_code, :cardholder_name, :id, :errors
4
- def initialize(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:, id: nil, errors: nil)
5
- @card_number = card_number
6
- @expiration_year = expiration_year
7
- @expiration_month = expiration_month
8
- @security_code = security_code
9
- @cardholder_name = cardholder_name
10
- @id = id
11
- @errors = errors
12
- end
13
-
14
- def build_json
15
- {
16
- card_number: card_number,
17
- expiration_year: expiration_year,
18
- expiration_month: expiration_month,
19
- security_code: security_code,
20
- cardholder: {
21
- name: cardholder_name
22
- }
23
- }
24
- end
25
-
26
- def create
27
- response = Client.new.create_token(JSON.dump(build_json))
28
- self.class.new(**self.class.build_hash(response.json))
29
- end
30
-
31
- def self.build_hash(response)
32
- {
33
- id: response["id"],
34
- expiration_year: response["expiration_year"],
35
- expiration_month: response["expiration_month"],
36
- cardholder_name: response.dig("cardholder", "name"),
37
- card_number: response["card_number"],
38
- security_code: response["security_code"],
39
- errors: response["error"]
40
- }
41
- end
42
- end
43
- end