Class: MpApi::Client

Inherits:
Ac::Base
  • Object
show all
Defined in:
lib/mp_api/client.rb

Constant Summary collapse

BASE_URL =
"https://api.mercadopago.com/v1/"
MAX_RETRIES =
1
SAVE_RESPONSES =
true

Instance Method Summary collapse

Instance Method Details

#create_card(customer_id:, token:) ⇒ Object



128
129
130
131
132
133
# File 'lib/mp_api/client.rb', line 128

def create_card(customer_id:, token:)
  body = {
    token: token
  }
  post("/customers/#{customer_id}/cards", body:)
end

#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, external_reference: nil, token: nil, issuer_id: nil, installments: nil, three_d_secure_mode: false, items: []) ⇒ Object

Parameters:

  • items (Array<MpApi::Item>) (defaults to: [])

    Array de objetos Item contendo id, title, description, category_id, quantity, unit_price



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mp_api/client.rb', line 31

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, external_reference: nil, token: nil, issuer_id: nil, installments: nil, three_d_secure_mode: false, items: [])
  body = {
    date_of_expiration: date_of_expiration,
    transaction_amount: amount,
    description: description,
    statement_descriptor: statement_descriptor,
    payment_method_id: payment_method,
    payer: {
      email: payer_email,
      identification: {
        type: payer_identification_type,
        number: payer_identification_number
      }
    },
    capture: capture,
    external_reference: external_reference,
    token: token,
    issuer_id: issuer_id,
    installments: installments,
    three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported",
    additional_info: {
      items: items.map(&:to_api_hash)
    }
  }.compact
  post("/payments", body:)
end

#create_customer(email:, first_name: nil, identification_type:, identification_number:, phone_area_code: "55", phone_number: nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mp_api/client.rb', line 112

def create_customer(email:, first_name: nil, identification_type:, identification_number:, phone_area_code: "55", phone_number: nil)
  body = {
    email: email,
    first_name: first_name&.gsub(/[^a-z A-Z]/, ""),
    identification: {
      type: identification_type,
      number: identification_number
    },
    phone: {
      area_code: phone_area_code,
      number: phone_number
    }
  }
  post("/customers", body:)
end

#create_pix_payment(date_of_expiration: nil, amount:, description: nil, statement_descriptor: nil, payment_method:, payer_email:, payer_identification_type:, payer_identification_number:, external_reference: nil, items: []) ⇒ Object

Parameters:

  • items (Array<MpApi::Item>) (defaults to: [])

    Array de objetos Item contendo id, title, description, category_id, quantity, unit_price



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mp_api/client.rb', line 8

def create_pix_payment(date_of_expiration: nil, amount:, description: nil, statement_descriptor: nil, payment_method:, payer_email:, payer_identification_type:, payer_identification_number:, external_reference: nil, items: [])
  body = {
    date_of_expiration: date_of_expiration,
    transaction_amount: amount,
    description: description,
    statement_descriptor: statement_descriptor,
    payment_method_id: payment_method,
    payer: {
      email: payer_email,
      identification: {
        type: payer_identification_type,
        number: payer_identification_number
      }
    },
    external_reference: external_reference,
    additional_info: {
      items: items.map(&:to_api_hash)
    }
  }.compact
  post("/payments", body:)
end

#create_refund(id:, amount:) ⇒ Object



139
140
141
142
143
144
# File 'lib/mp_api/client.rb', line 139

def create_refund(id:, amount:)
  body = {
    amount: amount
  }
  post("/payments/#{id}/refunds", body:)
end

#create_saved_credit_card_payment(amount:, token: nil, installments: nil, customer_id: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mp_api/client.rb', line 58

def create_saved_credit_card_payment(amount:, token: nil, installments: nil, customer_id: nil)
  body = {
    transaction_amount: amount,
    token: token,
    installments: installments,
    payer: {
      type: "customer",
      id: customer_id
    }
  }
  post("/payments", body:)
end

#create_token(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mp_api/client.rb', line 82

def create_token(card_number:, expiration_year:, expiration_month:, security_code:, cardholder_name:)
  body = {
    card_number: card_number,
    expiration_year: expiration_year,
    expiration_month: expiration_month,
    security_code: security_code,
    cardholder: {
      name: cardholder_name.gsub(/[^a-z A-Z]/, "")
    }
  }
  post("/card_tokens", body:)
end

#delete_card(customer_id:, card_id:) ⇒ Object



135
136
137
# File 'lib/mp_api/client.rb', line 135

def delete_card(customer_id:, card_id:)
  delete("/customers/#{customer_id}/cards/#{card_id}")
end

#get_customer(email) ⇒ Object



108
109
110
# File 'lib/mp_api/client.rb', line 108

def get_customer(email)
   get("/customers/search?email=#{email}")
end

#get_payment(payment_id) ⇒ Object



71
72
73
# File 'lib/mp_api/client.rb', line 71

def get_payment(payment_id)
  get("/payments/#{payment_id}")
end

#get_payment_methodsObject



104
105
106
# File 'lib/mp_api/client.rb', line 104

def get_payment_methods
  get("/payment_methods")
end

#get_refund(payment_id:, refund_id:) ⇒ Object



146
147
148
# File 'lib/mp_api/client.rb', line 146

def get_refund(payment_id:, refund_id:)
  get("/payments/#{payment_id}/refunds/#{refund_id}")
end

#search_payment_methods(first_six_digits:) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/mp_api/client.rb', line 95

def search_payment_methods(first_six_digits:)
  body = {
    marketplace: "NONE",
    status: "active",
    bins: first_six_digits
  }
  get("/payment_methods/search", params: body)
end

#update_payment(payment_id:, status:) ⇒ Object



75
76
77
78
79
80
# File 'lib/mp_api/client.rb', line 75

def update_payment(payment_id:, status:)
  body = {
    status: status
  }
  put("/payments/#{payment_id}", body:)
end