Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Valitor integration and tests #360

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 85 additions & 41 deletions lib/offsite_payments/integrations/valitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module OffsitePayments #:nodoc:
module Integrations #:nodoc:
module Valitor
mattr_accessor :test_url
self.test_url = 'https://testvefverslun.valitor.is/1_1/'
self.test_url = 'https://paymentweb.uat.valitor.is/'

mattr_accessor :production_url
self.production_url = 'https://vefverslun.valitor.is/1_1/'
self.production_url = 'https://paymentweb.valitor.is/'

def self.test?
(OffsitePayments.mode == :test)
Expand All @@ -31,72 +31,115 @@ class Helper < OffsitePayments::Helper
def initialize(order, account, options={})
options[:currency] ||= 'ISK'
super
add_field 'Adeinsheimild', '0'
add_field 'KaupandaUpplysingar', '0'
add_field 'SlokkvaHaus', '0'
add_field 'AuthorizationOnly', '0'
add_field 'DisplayBuyerInfo', '0'

add_field 'Language', 'IS'

@security_number = options[:credential2]
@amount = options[:amount]
@order = order
end

mapping :account, 'VefverslunID'
mapping :currency, 'Gjaldmidill'
mapping :account, 'MerchantID'
mapping :currency, 'Currency'

mapping :order, 'ReferenceNumber'

mapping :order, 'Tilvisunarnumer'
mapping :notify_url, 'PaymentSuccessfulServerSideURL'
mapping :return_url, 'PaymentSuccessfulURL'
mapping :cancel_return_url, 'PaymentCancelledURL'

mapping :notify_url, 'SlodTokstAdGjaldfaeraServerSide'
mapping :return_url, 'SlodTokstAdGjaldfaera'
mapping :cancel_return_url, 'SlodNotandiHaettirVid'
mapping :session_expired_timeout_in_seconds, 'SessionExpiredTimeoutInSeconds'
mapping :session_expired_redirect_url, 'SessionExpiredRedirectURL'
mapping :card_loan_user_name, 'MerchantName'

mapping :success_text, 'SlodTokstAdGjaldfaeraTexti'
mapping :success_text, 'PaymentSuccessfulURLText'

mapping :language, 'Lang'
mapping :language, 'Language'

def language=(val)
raise ArgumentError, "Language can only be one of IS, EN, DA or DE" unless %w(IS EN DA DE).include?(val)
add_field 'Language', val
end

def authorize_only
add_field 'Adeinsheimild', '1'
add_field 'AuthorizationOnly', '1'
end

def collect_customer_info
add_field 'KaupandaUpplysingar', '1'
add_field 'DisplayBuyerInfo', '1'
end

def hide_header
add_field 'SlokkvaHaus', '1'
def payment_successful_automatic_redirect
add_field 'PaymentSuccessfulAutomaticRedirect', '1'
end

def is_card_loan
add_field 'IsCardLoan', '1'
end

def card_loan_is_interest_free
add_field 'IsInterestFree', '1'
end

def require_postal_code
add_field 'RequirePostalCode', '1'
end

def hide_postal_code
add_field 'HidePostalCode', '1'
end

%w(SSN Name Address City Country Phone Email Comments).each do |name|
define_method :"require_#{name.downcase}" do
add_field "Require#{name}", '1'
end

define_method :"hide_#{name.downcase}" do
add_field "Hide#{name}", '1'
end
end


def product(id, options={})
raise ArgumentError, "Product id #{id} is not an integer between 1 and 500" unless id.to_i > 0 && id.to_i <= 500
requires!(options, :amount, :description)
options.assert_valid_keys([:description, :quantity, :amount, :discount])

add_field("Vara_#{id}_Verd", format_amount(options[:amount], @fields[mappings[:currency]]))
add_field("Vara_#{id}_Fjoldi", options[:quantity] || "1")
add_field("Product_#{id}_Price", options[:amount])
add_field("Product_#{id}_Quantity", options[:quantity] || "1")

add_field("Vara_#{id}_Lysing", options[:description]) if options[:description]
add_field("Vara_#{id}_Afslattur", options[:discount] || '0')
add_field("Product_#{id}_Description", options[:description]) if options[:description]
add_field("Product_#{id}_Discount", options[:discount] || '0')

@products ||= []
@products << id.to_i
end

def signature
raise ArgumentError, "Security number not set" unless @security_number
parts = [@security_number, @fields['Adeinsheimild']]
parts = [@security_number, @fields['AuthorizationOnly']]
@products.sort.uniq.each do |id|
parts.concat(["Vara_#{id}_Fjoldi", "Vara_#{id}_Verd", "Vara_#{id}_Afslattur"].collect{|e| @fields[e]})
parts.concat(["Product_#{id}_Quantity", "Product_#{id}_Price", "Product_#{id}_Discount"].collect{|e| @fields[e]})
end if @products
parts.concat(%w(VefverslunID Tilvisunarnumer SlodTokstAdGjaldfaera SlodTokstAdGjaldfaeraServerSide Gjaldmidill).collect{|e| @fields[e]})
parts.concat(%w(MerchantID ReferenceNumber PaymentSuccessfulURL PaymentSuccessfulServerSideURL Currency).collect{|e| @fields[e]})
Digest::MD5.hexdigest(parts.compact.join(''))
end

def form_fields
product(1, :amount => @amount, :description => @order) if Array(@products).empty?

@products.each do |id|
@fields["Product_#{id}_Price"] = format_amount(@fields["Product_#{id}_Price"], @fields[mappings[:currency]])
end

@fields[mappings[:success_text]] ||= DEFAULT_SUCCESS_TEXT
@fields.merge('RafraenUndirskrift' => signature)
@fields.merge('DigitalSignature' => signature)
end

def format_amount(amount, currency)
OffsitePayments::CURRENCIES_WITHOUT_FRACTIONS.include?(currency) ? amount.to_f.round : sprintf("%.2f", amount)
OffsitePayments::CURRENCIES_WITHOUT_FRACTIONS.include?(currency) ? amount.to_f.round : sprintf("%.2f", amount).sub('.', ',')
end
end

Expand All @@ -111,12 +154,12 @@ def test?
end

def item_id
params['Tilvisunarnumer']
params['ReferenceNumber']
end
alias :order :item_id

def transaction_id
params['VefverslunSalaID']
params['SaleID']
end

def currency
Expand All @@ -128,63 +171,64 @@ def status
end

def received_at
Time.parse(params['Dagsetning'].to_s)
Time.parse(params['Date'].to_s)
end

def gross
return "0" if !params['Upphaed']
"%0.2f" % params['Upphaed'].to_s.sub(',', '.')
end

def card_type
params['Kortategund']
params['CardType']
end

def card_last_four
params['KortnumerSidustu']
params['CardNumberMasked'][-4..]
end

def authorization_number
params['Heimildarnumer']
params['AuthorizationNumber']
end

def transaction_number
params['Faerslunumer']
params['TransactionNumber']
end

def customer_name
params['Nafn']
params['Name']
end

def customer_address
params['Heimilisfang']
params['Address']
end

def customer_zip
params['Postnumer']
params['PostalCode']
end

def customer_city
params['Stadur']
params['City']
end

def customer_country
params['Land']
params['Country']
end

def customer_email
params['Tolvupostfang']
params['Email']
end

def customer_comment
params['Athugasemdir']
params['Comments']
end

def password
@options[:credential2]
end

def acknowledge(authcode = nil)
password ? Digest::MD5.hexdigest("#{password}#{order}") == params['RafraenUndirskriftSvar'] : true
password ? Digest::MD5.hexdigest("#{password}#{order}") == params['DigitalSignatureResponse'] : true
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ usa_epay_advanced:
software_id: Z

valitor:
login: WebsiteID
password: SecurityNumber
login: 1
password: 12345

# Working credentials, no need to replace
verify:
Expand Down
Loading