Module: MangoPay::Jsonifier

Included in:
CancelRequest, CompleteRegistrationRequest, CurrencyRequest, DeactivationRequest, MangoModel::Address, MangoModel::ApplePayPayIn, MangoModel::BankWireDirectPayIn, MangoModel::BankWireExternalInstructionPayIn, MangoModel::BankingAlias, MangoModel::Billing, MangoModel::Birthplace, MangoModel::BrowserInfo, MangoModel::CaBankAccount, MangoModel::Card, MangoModel::CardDirectPayIn, MangoModel::CardPreAuthorizedPayIn, MangoModel::CardRegistration, MangoModel::CardWebPayIn, MangoModel::Client, MangoModel::ClientWallet, MangoModel::DebitedBankAccount, MangoModel::DeclaredUbo, MangoModel::DirectDebitDirectPayIn, MangoModel::DirectDebitWebPayIn, MangoModel::Dispute, MangoModel::DisputeDocument, MangoModel::DisputeReason, MangoModel::DocumentPageConsult, MangoModel::EMoney, MangoModel::Event, MangoModel::GbBankAccount, MangoModel::GooglePayPayIn, MangoModel::Hook, MangoModel::IbanBankAccount, MangoModel::KycDocument, MangoModel::LegalUser, MangoModel::Mandate, MangoModel::Money, MangoModel::NaturalUser, MangoModel::OtherBankAccount, MangoModel::PayInWebExtendedView, MangoModel::PayOut, MangoModel::PaypalWebPayIn, MangoModel::PlatformCategorization, MangoModel::PreAuthorization, MangoModel::Refund, MangoModel::RefundReason, MangoModel::Report, MangoModel::Repudiation, MangoModel::ResponseReplica, MangoModel::ScopeBlocked, MangoModel::SecurityInfo, MangoModel::SettlementTransfer, MangoModel::Shipping, MangoModel::Transaction, MangoModel::Transfer, MangoModel::Ubo, MangoModel::UboDeclaration, MangoModel::UsBankAccount, MangoModel::UserBlockStatus, MangoModel::Wallet, ReportFilter, SubmitDocumentRequest, SubmitUboDeclarationRequest, TemplateUrlOptions, UploadFileRequest
Defined in:
lib/mangopay/common/jsonifier.rb

Overview

To be included in order to apply JSON de/serialization methods

Constant Summary collapse

LOG =
LogProvider.provide(self)

Instance Method Summary collapse

Instance Method Details

#dejsonify(hash) ⇒ Object

De-serializes an object from a JSON-originating hash.

Parameters:

  • +hash+ (Hash)

    hash converted from an API-returned JSON string

Returns:

  • (Object)

    corresponding object (typed)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/mangopay/common/jsonifier.rb', line 60

def dejsonify(hash)
  return nil unless hash
  hash.each do |tag, value|
    field = JsonTagConverter.from_json_tag tag
    field_value = nil
    if field == 'type'
      if is_a? MangoModel::BankAccount
        field_value = MangoModel::AccountType.value_of value
      elsif is_a? MangoModel::KycDocument
        field_value = MangoModel::KycDocumentType.value_of value
      elsif is_a? MangoModel::DisputeDocument
        field_value = MangoModel::DisputeDocumentType.value_of value
      end
    end
    if field == 'status'
      if is_a? MangoModel::CardRegistration
        field_value = MangoModel::CardStatus.value_of value
      elsif is_a? MangoModel::PreAuthorization
        field_value = MangoModel::PreAuthorizationStatus.value_of value
      elsif is_a?(MangoModel::KycDocument) || is_a?(MangoModel::DisputeDocument)
        field_value = MangoModel::DocumentStatus.value_of value
      elsif is_a? MangoModel::UboDeclaration
        field_value = MangoModel::UboDeclarationStatus.value_of value
      elsif is_a? MangoModel::DeclaredUbo
        field_value = MangoModel::DeclaredUboStatus.value_of value
      elsif is_a? MangoModel::Hook
        field_value = MangoModel::HookStatus.value_of value
      elsif is_a? MangoModel::Dispute
        field_value = MangoModel::DisputeStatus.value_of value
      elsif is_a? MangoModel::Report
        field_value = MangoModel::ReportStatus.value_of value
      end
    end
    if is_a? MangoModel::Mandate
      if field == 'culture'
        field_value = MangoModel::MandateCultureCode.value_of value
      elsif field == 'status'
        field_value = MangoModel::MandateStatus.value_of value
      elsif field == 'execution_type'
        field_value = MangoModel::MandateExecutionType.value_of value
      end
    end
    if field == 'execution_type' && is_a?(MangoModel::PreAuthorization)
      field_value = MangoModel::PreAuthorizationExecutionType.value_of value
    end
    if field == 'payment_type' && is_a?(MangoModel::PayOut)
      field_value = MangoModel::PayOutPaymentType.value_of value
    end
    if field == 'refused_reason_type'
      if is_a? MangoModel::DeclaredUbo
        field_value = MangoModel::DeclaredUboRefusedReasonType.value_of value
      elsif is_a? MangoModel::DisputeDocument
        field_value = MangoModel::DisputeDocRefusedReasonType.value_of value
      end
    end
    if field == 'validity' && is_a?(MangoModel::Hook)
      field_value = MangoModel::HookValidity.value_of value
    end
    if field == 'date' && is_a?(MangoModel::Event)
      field_value = value
    end
    if field == 'declared_ubos' && is_a?(MangoModel::UboDeclaration)
      field_value = []
      value.each do |declared_ubo|
        field_value << MangoModel::DeclaredUbo.new.dejsonify(declared_ubo)
      end
    end
    if field == 'bank_account' && is_a?(MangoModel::BankWireDirectPayIn)
      field_value = case value['Type']
                    when MangoModel::AccountType::IBAN.to_s
                      MangoModel::IbanBankAccount.new.dejsonify value
                    when MangoModel::AccountType::US.to_s
                      MangoModel::UsBankAccount.new.dejsonify value
                    when MangoModel::AccountType::CA.to_s
                      MangoModel::CaBankAccount.new.dejsonify value
                    when MangoModel::AccountType::GB.to_s
                      MangoModel::GbBankAccount.new.dejsonify value
                    when MangoModel::AccountType::OTHER.to_s
                      MangoModel::OtherBankAccount.new.dejsonify value
                    else
                      raise 'Unrecognized bank account type: ' + value['Type']
                    end
    end
    if field == 'debited_bank_account' && is_a?(MangoModel::BankWireExternalInstructionPayIn)
      field_value = MangoModel::DebitedBankAccount.new.dejsonify value
    end
    field_value ||= case field
                    when *MangoModel.fields_of_type(MangoModel::Address)
                      MangoModel::Address.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::Money)
                      MangoModel::Money.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::RefundReason)
                      MangoModel::RefundReason.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::DisputeReason)
                      MangoModel::DisputeReason.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::PlatformCategorization)
                      MangoModel::PlatformCategorization.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::Billing)
                      MangoModel::Billing.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::Shipping)
                      MangoModel::Shipping.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::BrowserInfo)
                      MangoModel::BrowserInfo.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::SecurityInfo)
                      MangoModel::SecurityInfo.new.dejsonify value
                    when *MangoModel.fields_of_type(MangoModel::PersonType)
                      MangoModel::PersonType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::KycLevel)
                      MangoModel::KycLevel.value_of value
                    when *MangoModel.fields_of_type(MangoModel::LegalPersonType)
                      MangoModel::LegalPersonType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::CountryIso)
                      MangoModel::CountryIso.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DepositType)
                      MangoModel::DepositType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::CurrencyIso)
                      MangoModel::CurrencyIso.value_of value
                    when *MangoModel.fields_of_type(MangoModel::FundsType)
                      MangoModel::FundsType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::PlatformType)
                      MangoModel::PlatformType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::TransactionType)
                      MangoModel::TransactionType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::TransactionNature)
                      MangoModel::TransactionNature.value_of value
                    when *MangoModel.fields_of_type(MangoModel::TransactionStatus)
                      MangoModel::TransactionStatus.value_of value
                    when *MangoModel.fields_of_type(MangoModel::PayInPaymentType)
                      MangoModel::PayInPaymentType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::PayInExecutionType)
                      MangoModel::PayInExecutionType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::CardType)
                      MangoModel::CardType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::SecureMode)
                      MangoModel::SecureMode.value_of value
                    when *MangoModel.fields_of_type(MangoModel::CultureCode)
                      MangoModel::CultureCode.value_of value
                    when *MangoModel.fields_of_type(MangoModel::CardValidity)
                      MangoModel::CardValidity.value_of value
                    when *MangoModel.fields_of_type(MangoModel::PaymentStatus)
                      MangoModel::PaymentStatus.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DirectDebitType)
                      MangoModel::DirectDebitType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::MandateScheme)
                      MangoModel::MandateScheme.value_of value
                    when *MangoModel.fields_of_type(MangoModel::MandateType)
                      MangoModel::MandateType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::KycDocRefusedReasonType)
                      MangoModel::KycDocRefusedReasonType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::NaturalUserCapacity)
                      MangoModel::NaturalUserCapacity.value_of value
                    when *MangoModel.fields_of_type(MangoModel::EventType)
                      MangoModel::EventType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::RefundReasonType)
                      MangoModel::RefundReasonType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DisputeType)
                      MangoModel::DisputeType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DisputeReasonType)
                      MangoModel::DisputeReasonType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DisputeStatus)
                      MangoModel::DisputeStatus.value_of value
                    when *MangoModel.fields_of_type(MangoModel::DownloadFormat)
                      MangoModel::DownloadFormat.value_of value
                    when *MangoModel.fields_of_type(MangoModel::ReportType)
                      MangoModel::ReportType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::BusinessType)
                      MangoModel::BusinessType.value_of value
                    when *MangoModel.fields_of_type(MangoModel::Sector)
                      MangoModel::Sector.value_of value
                    when *MangoModel.fields_of_type(MangoModel::AvsResult)
                      MangoModel::AvsResult.value_of value
                    when *MangoModel.fields_of_type(DateTime)
                      DateTime.parse value
                    when *MangoModel.fields_of_type(MangoModel::ScopeBlocked)
                      MangoModel::ScopeBlocked.value_of value
                    else
                      value
                    end
    instance_variable_set "@#{field}", field_value
  end
  LOG.debug 'DE-JSONIFIED {}', hash
  LOG.debug 'INTO {}', inspect
  self
end

#jsonify!String

Serializes an object to a JSON string. Read-only fields will be removed

Returns:

  • (String)

    JSON string of non-read-only fields and values



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mangopay/common/jsonifier.rb', line 44

def jsonify!
  json = hashed_variables.to_json
  # hash.to_json() sometimes adds
  # unnecessary backslashes and double quotes
  json.delete!('\\')
  json.gsub!(/"{/, '{')
  json.gsub!(/}"/, '}')
  LOG.debug 'JSONIFIED {}', inspect
  LOG.debug 'INTO {}', json
  json
end