Class: MpApi::Payment

Inherits:
Object
  • Object
show all
Defined in:
lib/mp_api/payment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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) ⇒ Payment

Returns a new instance of Payment.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mp_api/payment.rb', line 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)
  @id = id
  @description = description
  @date_approved = date_approved
  @date_created = date_created
  @money_release_date = money_release_date
  @payer_email = payer_email
  @payer_identification_type = payer_identification_type
  @payer_identification_number = payer_identification_number
  @payment_method = payment_method
  @last_four_digits = last_four_digits
  @payment_type = payment_type
  @qr_code = qr_code
  @qr_code_base_64 = qr_code_base_64
  @transaction_id = transaction_id
  @ticket_url = ticket_url
  @status = status
  @amount = amount
  @token = token
  @issuer_id = issuer_id
  @installments = installments
  @three_d_secure_mode = three_d_secure_mode
  @three_ds_info_external_resource_url = three_ds_info_external_resource_url
  @three_ds_info_creq = three_ds_info_creq
  @status_detail = status_detail
  @statement_descriptor = statement_descriptor
  @items = items
  @error = error
  @internal_error = internal_error
  @capture = capture
  @date_of_expiration = date_of_expiration
  @saved_card = saved_card
  @customer_id = customer_id
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def amount
  @amount
end

#captureObject (readonly)

Returns the value of attribute capture.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def capture
  @capture
end

#customer_idObject (readonly)

Returns the value of attribute customer_id.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def customer_id
  @customer_id
end

#date_approvedObject (readonly)

Returns the value of attribute date_approved.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def date_approved
  @date_approved
end

#date_createdObject (readonly)

Returns the value of attribute date_created.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def date_created
  @date_created
end

#date_of_expirationObject (readonly)

Returns the value of attribute date_of_expiration.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def date_of_expiration
  @date_of_expiration
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def description
  @description
end

#errorObject (readonly)

Returns the value of attribute error.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def id
  @id
end

#installmentsObject (readonly)

Returns the value of attribute installments.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def installments
  @installments
end

#internal_errorObject (readonly)

Returns the value of attribute internal_error.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def internal_error
  @internal_error
end

#issuer_idObject (readonly)

Returns the value of attribute issuer_id.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def issuer_id
  @issuer_id
end

#itemsObject (readonly)

Returns the value of attribute items.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def items
  @items
end

#last_four_digitsObject (readonly)

Returns the value of attribute last_four_digits.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def last_four_digits
  @last_four_digits
end

#money_release_dateObject (readonly)

Returns the value of attribute money_release_date.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def money_release_date
  @money_release_date
end

#payer_emailObject (readonly)

Returns the value of attribute payer_email.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def payer_email
  @payer_email
end

#payer_identification_numberObject (readonly)

Returns the value of attribute payer_identification_number.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def payer_identification_number
  @payer_identification_number
end

#payer_identification_typeObject (readonly)

Returns the value of attribute payer_identification_type.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def payer_identification_type
  @payer_identification_type
end

#payment_methodObject (readonly)

Returns the value of attribute payment_method.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def payment_method
  @payment_method
end

#payment_typeObject (readonly)

Returns the value of attribute payment_type.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def payment_type
  @payment_type
end

#qr_codeObject (readonly)

Returns the value of attribute qr_code.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def qr_code
  @qr_code
end

#qr_code_base_64Object (readonly)

Returns the value of attribute qr_code_base_64.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def qr_code_base_64
  @qr_code_base_64
end

#saved_cardObject (readonly)

Returns the value of attribute saved_card.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def saved_card
  @saved_card
end

#statement_descriptorObject (readonly)

Returns the value of attribute statement_descriptor.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def statement_descriptor
  @statement_descriptor
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def status
  @status
end

#status_detailObject (readonly)

Returns the value of attribute status_detail.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def status_detail
  @status_detail
end

#three_d_secure_modeObject (readonly)

Returns the value of attribute three_d_secure_mode.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def three_d_secure_mode
  @three_d_secure_mode
end

#three_ds_info_creqObject (readonly)

Returns the value of attribute three_ds_info_creq.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def three_ds_info_creq
  @three_ds_info_creq
end

#three_ds_info_external_resource_urlObject (readonly)

Returns the value of attribute three_ds_info_external_resource_url.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def three_ds_info_external_resource_url
  @three_ds_info_external_resource_url
end

#ticket_urlObject (readonly)

Returns the value of attribute ticket_url.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def ticket_url
  @ticket_url
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def token
  @token
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.



3
4
5
# File 'lib/mp_api/payment.rb', line 3

def transaction_id
  @transaction_id
end

Class Method Details

.build_hash(json_response) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/mp_api/payment.rb', line 121

def self.build_hash json_response
  payment_error = PaymentError.new(json_response: json_response)
  {
    id: json_response.dig("id"),
    description: json_response.dig("description"),
    date_approved: json_response.dig("date_approved"),
    date_created: json_response.dig("date_created"),
    money_release_date: json_response.dig("money_release_date"),
    payer_email: json_response.dig("payer", "email"),
    payer_identification_type: json_response.dig("payer", "identification", "type"),
    payer_identification_number: json_response.dig("payer", "identification", "number"),
    payment_method: json_response.dig("payment_method_id"),
    last_four_digits: json_response.dig("card", "last_four_digits"),
    payment_type: json_response.dig("payment_type_id"),
    qr_code: json_response.dig("point_of_interaction", "transaction_data", "qr_code"),
    qr_code_base_64: json_response.dig("point_of_interaction", "transaction_data", "qr_code_base64"),
    three_ds_info_external_resource_url: json_response.dig("three_ds_info", "external_resource_url"),
    three_ds_info_creq: json_response.dig("three_ds_info", "creq"),
    transaction_id: json_response.dig("point_of_interaction", "transaction_data", "transaction_id"),
    ticket_url: json_response.dig("point_of_interaction", "transaction_data", "ticket_url"),
    status: json_response.dig("status"),
    status_detail: json_response.dig("status_detail"),
    amount: json_response.dig("transaction_amount"),
    error: payment_error.error,
    internal_error: payment_error.internal_error
  }
end

.find_by_id(payment_id) ⇒ Object



116
117
118
119
# File 'lib/mp_api/payment.rb', line 116

def self.find_by_id(payment_id)
  response = Client.new.get_payment(payment_id)
  new(**build_hash(response.json))
end

Instance Method Details

#build_jsonObject



39
40
41
# File 'lib/mp_api/payment.rb', line 39

def build_json
  send("build_json_#{(payment_method == "pix") ? "pix" : "credit_card"}").except(:description)
end

#build_json_credit_cardObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mp_api/payment.rb', line 72

def build_json_credit_card
  if saved_card
    {
      transaction_amount: amount,
      token: token,
      installments: installments,
      payer: {
        type: "customer",
        id: customer_id
      }
    }
  else
    build_json_pix.merge({
      capture: capture,
      token: token,
      issuer_id: issuer_id,
      installments: installments,
      three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported"
    }.compact)
  end
end

#build_json_pixObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mp_api/payment.rb', line 52

def build_json_pix
  {
    date_of_expiration: date_of_expiration,
    transaction_amount: amount,
    description: description,
    statement_descriptor: statement_descriptor,
    additional_info: {
      items: items
    },
    payment_method_id: payment_method,
    payer: {
      email: payer_email,
      identification: {
        type: payer_identification_type,
        number: payer_identification_number
      }
    }
  }.compact
end

#build_update_jsonObject



43
44
45
46
47
48
49
50
# File 'lib/mp_api/payment.rb', line 43

def build_update_json
  {
    # capture: capture,
    status: status
    # date_of_expiration: date_of_expiration,
    # transaction_amount: amount
  }.compact
end

#createObject



94
95
96
97
# File 'lib/mp_api/payment.rb', line 94

def create
  response = Client.new.create_payment(JSON.dump(build_json))
  self.class.new(**self.class.build_hash(response.json))
end

#invalidate_pix!Object



112
113
114
# File 'lib/mp_api/payment.rb', line 112

def invalidate_pix!
  update(status: "cancelled")
end

#pix_paid?(external_value = nil) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/mp_api/payment.rb', line 99

def pix_paid? external_value = nil
  external_value = @amount if external_value.nil?
  @status == "approved" && external_value.to_d == @amount.to_d
end

#update(attributes) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/mp_api/payment.rb', line 104

def update attributes
  attributes.each do |key, value|
    instance_variable_set(:"@#{key}", value)
  end
  Client.new.update_payment(id, JSON.dump(build_update_json))
  self.class.find_by_id(id)
end