Skip to content

Commit

Permalink
add support for LinkPoint's Items entity
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Fauser <codyfauser@gmail.com>
  • Loading branch information
Tony Primerano authored and Cody Fauser committed Oct 3, 2009
1 parent f096f09 commit ac47ae5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
= ActiveMerchant CHANGELOG

* Add line item support for LinkpointGateway. [Tony Primerano]
* Add support for SallieMae gateway [iamjwc]
* Add support for the JetPay gateway [Phil Ripperger, Peter Williams, cody]
* Add support for advanced SkipJack processors. Pass :advanced => true when constructing gateway [cody]
Expand Down
75 changes: 64 additions & 11 deletions lib/active_merchant/billing/gateways/linkpoint.rb
Expand Up @@ -82,12 +82,41 @@ module Billing #:nodoc:
# :transactiondetails => [:transactionorigin, :oid, :ponumber, :taxexempt, :terminaltype, :ip, :reference_number, :recurring, :tdate],
# :periodic => [:action, :installments, :threshold, :startdate, :periodicity, :comments],
# :notes => [:comments, :referred]
# :items => [:item => [:price, :quantity, :description, :id, :options => [:option => [:name, :value]]]]
# }
#
#
# IMPORTANT NOTICE:
#
# LinkPoint's Items entity is not yet supported in this module.
# LinkPoint's Items entity is an optional entity that can be attached to orders.
# It is entered as :line_items to be consistent with the CyberSource implementation
#
# The line_item hash goes in the options hash and should look like
#
# :line_items => [
# {
# :id => '123456',
# :description => 'Logo T-Shirt',
# :price => '12.00',
# :quantity => '1',
# :options => [
# {
# :name => 'Color',
# :value => 'Red'
# },
# {
# :name => 'Size',
# :value => 'XL'
# }
# ]
# },
# {
# :id => '111',
# :description => 'keychain',
# :price => '3.00',
# :quantity => '1'
# }
# ]
# This functionality is only supported by this particular gateway may
# be changed at any time
#
class LinkpointGateway < Gateway
# Your global PEM file. This will be assigned to you by linkpoint
Expand Down Expand Up @@ -247,16 +276,38 @@ def post_data(money, creditcard, options)
# Loop over the params hash to construct the XML string
for key, value in params
elem = order.add_element(key.to_s)
for k, v in params[key]
elem.add_element(k.to_s).text = params[key][k].to_s if params[key][k]
if key == :items
build_items(elem, value)
else
for k, v in params[key]
elem.add_element(k.to_s).text = params[key][k].to_s if params[key][k]
end
end
# Linkpoint doesn't understand empty elements:
order.delete(elem) if elem.size == 0
end

return xml.to_s
end


# adds LinkPoint's Items entity to the XML. Called from post_data
def build_items(element, items)
for item in items
item_element = element.add_element("item")
for key, value in item
if key == :options
options_element = item_element.add_element("options")
for option in value
opt_element = options_element.add_element("option")
opt_element.add_element("name").text = option[:name] unless option[:name].blank?
opt_element.add_element("value").text = option[:value] unless option[:value].blank?
end
else
item_element.add_element(key.to_s).text = item[key].to_s unless item[key].blank?
end
end
end
end

# Set up the parameters hash just once so we don't have to do it
# for every action.
def parameters(money, creditcard, options = {})
Expand Down Expand Up @@ -307,10 +358,10 @@ def parameters(money, creditcard, options = {})

if creditcard
params[:creditcard] = {
:cardnumber => creditcard.number,
:cardexpmonth => creditcard.month,
:cardexpyear => format_creditcard_expiry_year(creditcard.year),
:track => nil
:cardnumber => creditcard.number,
:cardexpmonth => creditcard.month,
:cardexpyear => format_creditcard_expiry_year(creditcard.year),
:track => nil
}

if creditcard.verification_value?
Expand Down Expand Up @@ -348,6 +399,8 @@ def parameters(money, creditcard, options = {})
params[:shipping][:country] = shipping_address[:country] unless shipping_address[:country].blank?
end

params[:items] = options[:line_items] if options[:line_items]

return params
end

Expand Down
11 changes: 10 additions & 1 deletion test/remote/gateways/remote_linkpoint_test.rb
Expand Up @@ -89,7 +89,16 @@ def test_successfull_purchase_and_credit
assert_success credit
end


def test_successfull_purchase_with_item_entity
@options.merge!({:line_items => [
{:id => '123456', :description => "Logo T-Shirt", :price => "12.00", :quantity => '1', :options =>
[{:name => "Color", :value => "Red"}, {:name => "Size", :value => "XL"}]},
{:id => '111', :description => "keychain", :price => "3.00", :quantity => '1'}]})
assert purchase = @gateway.purchase(1500, @credit_card, @options)
assert_success purchase

end

def test_successful_recurring_payment
assert response = @gateway.recurring(2400, @credit_card,
:order_id => generate_unique_id,
Expand Down
29 changes: 23 additions & 6 deletions test/unit/gateways/linkpoint_test.rb
Expand Up @@ -53,11 +53,11 @@ def test_recurring
end

def test_amount_style
assert_equal '10.34', @gateway.send(:amount, 1034)
assert_equal '10.34', @gateway.send(:amount, 1034)

assert_raise(ArgumentError) do
@gateway.send(:amount, '10.34')
end
assert_raise(ArgumentError) do
@gateway.send(:amount, '10.34')
end
end

def test_purchase_is_valid_xml
Expand All @@ -69,11 +69,11 @@ def test_purchase_is_valid_xml
:zip => '90210'
}
)

assert data = @gateway.send(:post_data, @amount, @credit_card, @options)
assert REXML::Document.new(data)
end

def test_recurring_is_valid_xml
parameters = @gateway.send(:parameters, 1000, @credit_card, :ordertype => "SALE", :action => "SUBMIT", :installments => 12, :startdate => "immediate", :periodicity => "monthly", :order_id => 1006,
:billing_address => {
Expand All @@ -87,6 +87,23 @@ def test_recurring_is_valid_xml
assert REXML::Document.new(data)
end

def test_line_items_are_valid_xml
options = {:ordertype => "SALE", :action => "SUBMIT", :installments => 12, :startdate => "immediate", :periodicity => "monthly", :order_id => 1006,
:billing_address => {
:address1 => '1313 lucky lane',
:city => 'Lost Angeles',
:state => 'CA',
:zip => '90210'
},
:line_items => [{:id => '123456', :description => "Logo T-Shirt", :price =>
"12.00", :quantity => '1', :options => [{:name => "Color", :value =>
"Red"}, {:name => "Size", :value => "XL"}]},{:id => '111', :description => "keychain", :price => "3.00", :quantity => '1'}]}


assert data = @gateway.send(:post_data, @amount, @credit_card, options)
assert REXML::Document.new(data)
end

def test_declined_purchase_is_valid_xml
@gateway = LinkpointGateway.new(:login => 123123, :pem => 'PEM')

Expand Down

0 comments on commit ac47ae5

Please sign in to comment.