diff --git a/lib/mangopay/common/json_tag_converter.rb b/lib/mangopay/common/json_tag_converter.rb index 4c67a6e..e601eb1 100644 --- a/lib/mangopay/common/json_tag_converter.rb +++ b/lib/mangopay/common/json_tag_converter.rb @@ -8,39 +8,42 @@ class << self # its API UpperCamelCase counterpart. def to_json_tag(field) field.split('_').collect do |word| - apply_capitalization word + apply_capitalization! word end.join end - # Applies necessary capitalization to a word - # in order to match API conventions. - def apply_capitalization(word) - word.sub!('kyc', 'KYC') - word.sub!('url', 'URL') - word.sub!('iban', 'IBAN') - word.sub!('bic', 'BIC') - word.sub!('aba', 'ABA') - word.sub!('ubo', 'UBO') - word[0] = word[0].upcase - word - end - # Converts an API-returned UpperCamelCase-named JSON tag # to its Ruby-standard snake_case counterpart. def from_json_tag(tag) - tag = tag.sub('UBO', 'Ubo') + tag = tag.sub('UBO', 'Ubo').sub('AVS', 'Avs') parts = tag.split(/(?=[A-Z])/) parts = compress_upcase_strings(parts) field = '' parts.each.with_index do |part, index| - decapitalize part + decapitalize! part field << '_' if !field.empty? && (part.length > 1\ - || (part == 'e' && parts[index + 1] == 'Money')) + || (part == 'e' && parts[index + 1] == 'Money')) field << part end field end + private + + # Applies necessary capitalization to a word + # in order to match API conventions. + def apply_capitalization!(word) + word.sub!('kyc', 'KYC') + word.sub!('url', 'URL') + word.sub!('iban', 'IBAN') + word.sub!('bic', 'BIC') + word.sub!('aba', 'ABA') + word.sub!('ubo', 'UBO') + word.sub!('avs', 'AVS') + word[0] = word[0].upcase + word + end + # Takes an array of strings and sticks together those # which are single uppercase letters in order to form # the actual words they compose. @@ -59,7 +62,7 @@ def compress_upcase_strings(strings) current.empty? ? result : result << current end - def decapitalize(word) + def decapitalize!(word) word[0] = word[0].downcase word end diff --git a/lib/mangopay/common/jsonifier.rb b/lib/mangopay/common/jsonifier.rb index 7ac8ac6..d547b50 100644 --- a/lib/mangopay/common/jsonifier.rb +++ b/lib/mangopay/common/jsonifier.rb @@ -49,6 +49,7 @@ def jsonify! # @param +hash+ [Hash] hash converted from an API-returned JSON string # @return [Object] corresponding object (typed) def dejsonify(hash) + return nil unless hash hash.each do |tag, value| field = JsonTagConverter.from_json_tag tag field_value = nil @@ -130,8 +131,7 @@ def dejsonify(hash) raise 'Unrecognized bank account type: ' + value['Type'] end end - unless field_value - field_value = case field + field_value ||= case field when *MangoModel.fields_of_type(MangoModel::Address) MangoModel::Address.new.dejsonify value when *MangoModel.fields_of_type(MangoModel::Money) @@ -142,6 +142,10 @@ def dejsonify(hash) 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::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) @@ -206,12 +210,13 @@ def dejsonify(hash) 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 else value end - end instance_variable_set "@#{field}", field_value end LOG.debug 'DE-JSONIFIED {}', hash diff --git a/lib/mangopay/model/billing.rb b/lib/mangopay/model/billing.rb new file mode 100644 index 0000000..07c6fe6 --- /dev/null +++ b/lib/mangopay/model/billing.rb @@ -0,0 +1,13 @@ +require_relative '../common/jsonifier' + +module MangoModel + + # Billing information + class Billing + include MangoPay::Jsonifier + + # [Address] The billing address + attr_accessor :address + + end +end \ No newline at end of file diff --git a/lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb b/lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb index 0ee3194..175e6bd 100644 --- a/lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb +++ b/lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb @@ -29,5 +29,11 @@ class CardDirectPayIn < PayIn # [String] The URL where to redirect users to proceed to # 3D secure validation attr_accessor :secure_mode_redirect_url + + # [Billing] Billing information + attr_accessor :billing + + # [SecurityInfo] Security & Validation information + attr_accessor :security_info end end \ No newline at end of file diff --git a/lib/mangopay/model/entity/pre_authorization.rb b/lib/mangopay/model/entity/pre_authorization.rb index 60f540d..9bcdc55 100644 --- a/lib/mangopay/model/entity/pre_authorization.rb +++ b/lib/mangopay/model/entity/pre_authorization.rb @@ -64,5 +64,12 @@ class PreAuthorization < EntityBase # [String] ID of the associated Pay-in attr_accessor :pay_in_id + + # [Billing] Billing information + attr_accessor :billing + + # [SecurityInfo] Security & validation information + attr_accessor :security_info + end end \ No newline at end of file diff --git a/lib/mangopay/model/enum/avs_result.rb b/lib/mangopay/model/enum/avs_result.rb new file mode 100644 index 0000000..caedafe --- /dev/null +++ b/lib/mangopay/model/enum/avs_result.rb @@ -0,0 +1,19 @@ +require_relative '../../util/enum' + +module MangoModel + + # Result of an AVS verification + class AvsResult + extend Enum + + FULL_MATCH = value 'FULL_MATCH' + + ADDRESS_MATCH_ONLY = value 'ADDRESS_MATCH_ONLY' + + POSTAL_CODE_MATCH_ONLY = value 'POSTAL_CODE_MATCH_ONLY' + + NO_MATCH = value 'NO_MATCH' + + NO_CHECK = value 'NO_CHECK' + end +end \ No newline at end of file diff --git a/lib/mangopay/model/model.rb b/lib/mangopay/model/model.rb index b2d681b..ef44585 100644 --- a/lib/mangopay/model/model.rb +++ b/lib/mangopay/model/model.rb @@ -49,6 +49,7 @@ require_relative 'enum/report_status' require_relative 'enum/business_type' require_relative 'enum/sector' +require_relative 'enum/avs_result' require_relative 'entity/user/user' require_relative 'entity/user/natural_user' require_relative 'entity/user/legal_user' @@ -68,6 +69,8 @@ require_relative 'document_page_consult' require_relative 'dispute_reason' require_relative 'platform_categorization' +require_relative 'billing' +require_relative 'security_info' # Module for model classes. module MangoModel @@ -126,7 +129,10 @@ module MangoModel ReportType => %w[report_type], Sector => %w[sector], BusinessType => %w[business_type], - PlatformCategorization => %w[platform_categorization] + PlatformCategorization => %w[platform_categorization], + Billing => %w[billing], + SecurityInfo => %w[security_info], + AvsResult => %w[avs_result] } class << self diff --git a/lib/mangopay/model/security_info.rb b/lib/mangopay/model/security_info.rb new file mode 100644 index 0000000..5974af5 --- /dev/null +++ b/lib/mangopay/model/security_info.rb @@ -0,0 +1,13 @@ +require_relative '../common/jsonifier' + +module MangoModel + + # Security & validation information + class SecurityInfo + include MangoPay::Jsonifier + + # [AVSResult] Result of the AVS verification + attr_accessor :avs_result + + end +end \ No newline at end of file diff --git a/spec/context/pay_in_context.rb b/spec/context/pay_in_context.rb index f4d7d4e..4bd9e55 100644 --- a/spec/context/pay_in_context.rb +++ b/spec/context/pay_in_context.rb @@ -93,6 +93,10 @@ def build_card_direct_pay_in pay_in.card_id = CARD.id pay_in.secure_mode = MangoModel::SecureMode::DEFAULT pay_in.statement_descriptor = 'Mar2016' + billing = MangoModel::Billing.new + billing.address = build_address + billing.address.postal_code = '68400' + pay_in.billing = billing pay_in end diff --git a/spec/context/pre_authorization_context.rb b/spec/context/pre_authorization_context.rb index 5d5b33c..af19500 100644 --- a/spec/context/pre_authorization_context.rb +++ b/spec/context/pre_authorization_context.rb @@ -25,6 +25,10 @@ def build_pre_authorization pre_auth.secure_mode = MangoModel::SecureMode::DEFAULT pre_auth.card_id = CARD.id pre_auth.secure_mode_return_url = 'http://www.my-site.com/returnURL' + billing = MangoModel::Billing.new + billing.address = build_address + billing.address.postal_code = '68400' + pre_auth.billing = billing pre_auth end diff --git a/spec/mangopay/pay_ins_spec.rb b/spec/mangopay/pay_ins_spec.rb index 9090933..2142452 100644 --- a/spec/mangopay/pay_ins_spec.rb +++ b/spec/mangopay/pay_ins_spec.rb @@ -35,6 +35,7 @@ expect(created.status).to be MangoModel::TransactionStatus::SUCCEEDED expect(created.payment_type).to be MangoModel::PayInPaymentType::CARD expect(created.execution_type).to be MangoModel::PayInExecutionType::DIRECT + expect(created.security_info.avs_result).to be MangoModel::AvsResult::FULL_MATCH expect(its_the_same_card_direct(pay_in, created)).to be_truthy end end diff --git a/spec/mangopay/pre_authorizations_spec.rb b/spec/mangopay/pre_authorizations_spec.rb index 7be7d27..11a887a 100644 --- a/spec/mangopay/pre_authorizations_spec.rb +++ b/spec/mangopay/pre_authorizations_spec.rb @@ -2,6 +2,7 @@ require_relative '../context/card_context' require_relative '../../lib/mangopay/api/service/pre_authorizations' require_relative '../../lib/mangopay/model/enum/payment_status' +require_relative '../../lib/mangopay/model/enum/avs_result' require_relative '../../lib/mangopay/common/sort_field' require_relative '../../lib/mangopay/common/sort_direction' @@ -20,6 +21,7 @@ expect(created).to be_kind_of MangoModel::PreAuthorization expect(created.id).not_to be_nil expect(created.payment_status).to be MangoModel::PaymentStatus::WAITING + expect(created.security_info.avs_result).to be MangoModel::AvsResult::FULL_MATCH expect(its_the_same_pre_auth(pre_auth, created)).to be_truthy end end