Skip to content

Commit

Permalink
Merge pull request #116 from Mangopay/feature/MPSDK-245-add-avs-support
Browse files Browse the repository at this point in the history
Implemented AVS Support
  • Loading branch information
mickaelpois authored Mar 23, 2018
2 parents 617bd34 + 4d26087 commit ecb2406
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 22 deletions.
39 changes: 21 additions & 18 deletions lib/mangopay/common/json_tag_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/mangopay/common/jsonifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/mangopay/model/billing.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/mangopay/model/entity/pay_in/card_direct_pay_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/mangopay/model/entity/pre_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions lib/mangopay/model/enum/avs_result.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion lib/mangopay/model/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/mangopay/model/security_info.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/context/pay_in_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions spec/context/pre_authorization_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/mangopay/pay_ins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/mangopay/pre_authorizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down

0 comments on commit ecb2406

Please sign in to comment.