mp_api 1.0.0 → 1.2.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: f698c10976e005ed5fb524e6ff6a92005ae477ce7218a54b29783849fbe85048
4
- data.tar.gz: cdce89a4463b792079923cb701ebe92a1b2b4c32e8a9a6aab67cd5660114921a
3
+ metadata.gz: acb1327c5e7bdeb10e34f09b348fff419d38451506f7f4e18f75bdeeceec5294
4
+ data.tar.gz: 170333900c1568e4937e0dbd5b333ea764f1f548871acaab5dd1825e238c6605
5
5
  SHA512:
6
- metadata.gz: c7f8e4fa1fb084aa51134a39451d0b644ac00a821bbfc367c76bbe491e9ce2ce31e23ba1f4f535b5879a16186bc4d691d2c3ae90f8bd7b1f1dbb71620fc66c6f
7
- data.tar.gz: f3e034f371edd0f4203798d31c7879e52f9393b5165eb65fa26f44a2383849f1317b2d0971d2b730da471315b077e9e629837805097a2b1049cdc7c8d56f5731
6
+ metadata.gz: 77a6f9bc3cdce5dfddf47c90dc2de04af4b3f0da903cbd502c41325cdf910f9573a25757f22ef5dd99bcd822841cfd9cd23aa88950fac49315539f45ae4569cc
7
+ data.tar.gz: de19a66ddb638012c114f1df58b679affe37be5345ab418253c37bb8376f99b0634181353a26ce3ca61c5dca093c5fa0eec926a39fc8fc32beccb6208b63234c
data/README.md CHANGED
@@ -1,19 +1,86 @@
1
1
  # MpApi
2
2
 
3
- ## Installation
3
+ Wrapper de integração com a API do Mercado Pago.
4
4
 
5
- Add solidus_bling to your Gemfile:
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
12
+
13
+ ## Instalação
14
+
15
+ Adicionar mp_api no Gemfile:
6
16
 
7
17
  ```ruby
8
18
  gem 'mp_api'
9
19
  ```
20
+ ou
21
+ ```shell
22
+ bundle add mp_api
23
+ ```
10
24
 
11
- Bundle your dependencies and run the installation generator:
25
+ Instalar a gem:
12
26
 
13
27
  ```shell
14
28
  bin/rails generate mp_api:install
15
29
  ```
16
30
 
17
- ## License
31
+ ## Uso
32
+
33
+ Configurar access_token no initializer ou direto no código:
34
+ ```ruby
35
+ MpApi.configure do |config|
36
+ config.access_token = "ACCESS_TOKEN"
37
+ end
38
+ ```
18
39
 
19
- Copyright (c) 2023 CaioGarcia1, released under the New BSD License.
40
+ ### Exemplos de criação de pagamento
41
+ #### Cartão de crédito
42
+ ```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",
62
+ payer_identification_type: "CPF",
63
+ payer_identification_number: "12345678909",
64
+ token: token.id,
65
+ issuer_id: payment_method.issuer_id,
66
+ installments: 1,
67
+ three_d_secure_mode: true
68
+ ).create
69
+
70
+ puts payment.id # 1318474186
71
+ puts payment.status # "approved"
72
+ ```
73
+
74
+ #### Pix
75
+ ```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
+ ```
@@ -0,0 +1,46 @@
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
data/lib/mp_api/client.rb CHANGED
@@ -35,6 +35,14 @@ module MpApi
35
35
  get("/payment_methods") { |response| validate_response(response, "id") }
36
36
  end
37
37
 
38
+ def create_customer(body)
39
+ post("/customers", body: body, headers: @headers) { |response| validate_response(response, "id") }
40
+ end
41
+
42
+ def create_card(customer_id, body)
43
+ post("/customers/#{customer_id}/cards", body: body, headers: @headers) { |response| validate_response(response, "id") }
44
+ end
45
+
38
46
  private
39
47
 
40
48
  def validate_response(response, required_response_key)
@@ -0,0 +1,54 @@
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,7 +1,7 @@
1
1
  module MpApi
2
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
4
- def initialize(payer_email:, payer_identification_type:, payer_identification_number:, payment_method:, last_four_digits: nil, amount:, 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)
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:, last_four_digits: nil, amount:, 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
5
  @id = id
6
6
  @description = description
7
7
  @date_approved = date_approved
@@ -32,6 +32,8 @@ module MpApi
32
32
  @internal_error = internal_error
33
33
  @capture = capture
34
34
  @date_of_expiration = date_of_expiration
35
+ @saved_card = saved_card
36
+ @customer_id = customer_id
35
37
  end
36
38
 
37
39
  def build_json
@@ -68,13 +70,25 @@ module MpApi
68
70
  end
69
71
 
70
72
  def build_json_credit_card
71
- build_json_pix.merge({
72
- capture: capture,
73
- token: token,
74
- issuer_id: issuer_id,
75
- installments: installments,
76
- three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported"
77
- }.compact)
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
78
92
  end
79
93
 
80
94
  def create
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MpApi
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/mp_api.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "ac"
4
+ require "securerandom"
4
5
  require_relative "mp_api/client"
5
6
  require_relative "mp_api/payment"
6
7
  require_relative "mp_api/payment_error"
7
8
  require_relative "mp_api/token"
8
9
  require_relative "mp_api/payment_method"
10
+ require_relative "mp_api/customer"
11
+ require_relative "mp_api/card"
9
12
  require_relative "mp_api/version"
10
13
 
11
14
  module MpApi
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mp_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - caio
7
+ - Todas Essas Coisas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-10 00:00:00.000000000 Z
11
+ date: 2024-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ac
@@ -37,19 +37,21 @@ files:
37
37
  - lib/generators/mp_api/install/install_generator.rb
38
38
  - lib/generators/mp_api/install/templates/initializer.rb
39
39
  - lib/mp_api.rb
40
+ - lib/mp_api/card.rb
40
41
  - lib/mp_api/client.rb
42
+ - lib/mp_api/customer.rb
41
43
  - lib/mp_api/payment.rb
42
44
  - lib/mp_api/payment_error.rb
43
45
  - lib/mp_api/payment_method.rb
44
46
  - lib/mp_api/token.rb
45
47
  - lib/mp_api/version.rb
46
- homepage: https://github.com/CaioGarcia1/mp_api
48
+ homepage: https://github.com/todasessascoisas/mp_api
47
49
  licenses:
48
50
  - MIT
49
51
  metadata:
50
- homepage_uri: https://github.com/CaioGarcia1/mp_api
51
- source_code_uri: https://github.com/CaioGarcia1/mp_api
52
- changelog_uri: https://github.com/CaioGarcia1/mp_api
52
+ homepage_uri: https://github.com/todasessascoisas/mp_api
53
+ source_code_uri: https://github.com/todasessascoisas/mp_api
54
+ changelog_uri: https://github.com/todasessascoisas/mp_api
53
55
  post_install_message:
54
56
  rdoc_options: []
55
57
  require_paths:
@@ -65,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
67
  - !ruby/object:Gem::Version
66
68
  version: '0'
67
69
  requirements: []
68
- rubygems_version: 3.5.10
70
+ rubygems_version: 3.5.11
69
71
  signing_key:
70
72
  specification_version: 4
71
73
  summary: ''