Skip to content

Commit

Permalink
Cardstream: Reference purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousepic authored and rwdaigle committed Apr 5, 2016
1 parent fc32c3c commit 03e29e3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Barclaycard Smartpay: Use strict_encode64 [duff]
* VisaNetPeru: Fix error when billing address empty [shasum]
* Vanco: Update live_url [duff]
* Cardstream: Reference purchase [curiousepic]


== Version 1.58.0 (March 1, 2016)
Expand Down
38 changes: 26 additions & 12 deletions lib/active_merchant/billing/gateways/card_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ def initialize(options = {})
super
end

def authorize(money, creditcard, options = {})
def authorize(money, credit_card_or_reference, options = {})
post = {}
add_pair(post, :captureDelay, -1)
add_amount(post, money, options)
add_invoice(post, creditcard, money, options)
add_creditcard(post, creditcard)
add_invoice(post, credit_card_or_reference, money, options)
add_credit_card_or_reference(post, credit_card_or_reference)
add_customer_data(post, options)
commit('SALE', post)
end

def purchase(money, creditcard, options = {})
def purchase(money, credit_card_or_reference, options = {})
post = {}
add_amount(post, money, options)
add_invoice(post, creditcard, money, options)
add_creditcard(post, creditcard)
add_invoice(post, credit_card_or_reference, money, options)
add_credit_card_or_reference(post, credit_card_or_reference)
add_customer_data(post, options)
commit('SALE', post)
end
Expand Down Expand Up @@ -149,20 +149,34 @@ def add_customer_data(post, options)
end
end

def add_invoice(post, credit_card, money, options)
def add_invoice(post, credit_card_or_reference, money, options)
add_pair(post, :transactionUnique, options[:order_id], :required => true)
add_pair(post, :orderRef, options[:description] || options[:order_id], :required => true)
if ['american_express', 'diners_club'].include?(card_brand(credit_card).to_s)
add_pair(post, :item1Quantity, 1)
add_pair(post, :item1Description, (options[:description] || options[:order_id]).slice(0, 15))
add_pair(post, :item1GrossValue, amount(money))
if credit_card_or_reference.respond_to?(:number)
if ['american_express', 'diners_club'].include?(card_brand(credit_card_or_reference).to_s)
add_pair(post, :item1Quantity, 1)
add_pair(post, :item1Description, (options[:description] || options[:order_id]).slice(0, 15))
add_pair(post, :item1GrossValue, amount(money))
end
end

add_pair(post, :type, options[:type] || '1')
add_threeds_required(post, options)
end

def add_creditcard(post, credit_card)
def add_credit_card_or_reference(post, credit_card_or_reference)
if credit_card_or_reference.respond_to?(:number)
add_credit_card(post, credit_card_or_reference)
else
add_reference(post, credit_card_or_reference.to_s)
end
end

def add_reference(post, reference)
add_pair(post, :xref, reference, :required => true)
end

def add_credit_card(post, credit_card)
add_pair(post, :customerName, credit_card.name, :required => true)
add_pair(post, :cardNumber, credit_card.number, :required => true)

Expand Down
23 changes: 23 additions & 0 deletions test/remote/gateways/remote_card_stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def setup
:description => 'AM test purchase'
}

@visacredit_reference_options = {
:order_id => generate_unique_id,
:description => 'AM test purchase'
}

@visadebit_options = {
:billing_address => {
:address1 => 'Unit 5, Pickwick Walk',
Expand Down Expand Up @@ -228,6 +233,24 @@ def test_successful_visacreditcard_purchase
assert !response.authorization.blank?
end

def test_successful_visacreditcard_purchase_via_reference
assert response = @gateway.purchase(142, @visacreditcard, @visacredit_options.merge({:type => '9'}))
assert_equal 'APPROVED', response.message
assert_success response
assert response.test?
assert response = @gateway.purchase(142, response.authorization, @visacredit_reference_options)
assert_equal 'APPROVED', response.message
assert_success response
assert response.test?
end

def test_failed_visacreditcard_purchase_via_reference
assert response = @gateway.purchase(142, 123, @visacredit_reference_options)
assert_equal 'DB ERROR', response.message
assert_failure response
assert response.test?
end

def test_purchase_no_currency_specified_defaults_to_GBP
assert response = @gateway.purchase(142, @visacreditcard, @visacredit_options.merge(currency: nil))
assert_success response
Expand Down
26 changes: 26 additions & 0 deletions test/unit/gateways/card_stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def test_successful_visacreditcard_purchase
assert responseRefund.test?
end

def test_successful_visacreditcard_purchase_via_reference
@gateway.expects(:ssl_post).returns(successful_reference_purchase_response)

assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
assert_equal 'APPROVED', responsePurchase.message
assert_success responsePurchase
assert responsePurchase.test?
end

def test_failed_visacreditcard_purchase_via_reference
@gateway.expects(:ssl_post).returns(failed_reference_purchase_response)

assert responsePurchase = @gateway.purchase(142, 'authorization', @visacredit_options)
assert_equal 'DB ERROR', responsePurchase.message
assert_failure responsePurchase
assert responsePurchase.test?
end

def test_declined_mastercard_purchase
@gateway.expects(:ssl_post).returns(failed_purchase_card_declined_response)

Expand Down Expand Up @@ -245,6 +263,14 @@ def failed_void_response
"merchantID=0000000&threeDSEnabled=Y&merchantDescription=General+test+account+with+AVS%2FCV2+checking&amount=10000&currencyCode=826&transactionUnique=7385df1d9c5484142bb6be1e932cd2df&orderRef=AM+test+purchase&customerName=Longbob+Longsen&customerAddress=25+The+Larches+&customerPostCode=LE10+2RT&action=REFUND&type=1&countryCode=826&merchantAlias=0000992&remoteAddress=80.229.33.63&responseCode=5&responseMessage=CARD+DECLINED&xref=13021914RQ07HK55HG29KPH&threeDSEnrolled=U&threeDSXID=00000000000004717495&transactionID=4717495&transactionPreviousID=0&timestamp=2013-02-19+14%3A08%3A18&amountReceived=0&cardNumberMask=%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A%2A0191&cardTypeCode=MC&cardType=Mastercard&threeDSErrorCode=-1&threeDSErrorDescription=Error+while+attempting+to+send+the+request+to%3A+https%3A%2F%2F3dstest.universalpaymentgateway.com%3A4343%2FAPI%0A%0DPlease+make+sure+that+ActiveMerchant+server+is+running+and+the+URL+is+valid.+ERROR_INTERNET_CANNOT_CONNECT%3A+The+attempt+to+connect+to+the+server+failed.&threeDSMerchantPref=PROCEED&threeDSVETimestamp=2013-02-19+14%3A07%3A55&currencyExponent=2&responseStatus=1&merchantName=CARDSTREAM+TEST&merchantID2=100001"
end

def successful_reference_purchase_response
"merchantID=103191&threeDSEnabled=Y&threeDSCheckPref=authenticated&avscv2CheckEnabled=N&addressCheckPref=not+known%2Cnot+checked%2Cmatched%2Cnot+matched%2Cpartially+matched&postcodeCheckPref=not+known%2Cnot+checked%2Cmatched%2Cnot+matched%2Cpartially+matched&customerReceiptsRequired=N&eReceiptsEnabled=N&eReceiptsStoreID=1&amount=142&currencyCode=826&transactionUnique=1f21b6d2fdf1f13378705707bc7e24bd&orderRef=AM+test+purchase&type=1&threeDSRequired=N&countryCode=826&action=SALE&responseCode=0&responseMessage=AUTHCODE%3A300382&state=captured&remoteAddress=107.15.253.186&requestMerchantID=103191&processMerchantID=103191&cardNumberMask=492942%2A%2A%2A%2A%2A%2A0821&cardExpiryMonth=12&cardExpiryYear=14&customerName=Longbob+Longsen&customerAddress=Flat+6%2C+Primrose+Rise+347+Lavender+Road&customerPostcode=NN17+8YG&previousID=10886746&cardCVVMandatory=N&xref=16040515PS17RG20GT73SBF&cardExpiryDate=1214&authorisationCode=300382&transactionID=10886747&responseStatus=0&timestamp=2016-04-05+15%3A17%3A20&amountReceived=142&avscv2ResponseCode=422100&avscv2ResponseMessage=ADDRESS+MATCH+ONLY&avscv2AuthEntity=merchant+host&cv2Check=not+matched&addressCheck=matched&postcodeCheck=matched&cardType=Visa+Credit&cardTypeCode=VC&cardScheme=Visa+&cardSchemeCode=VC&cardIssuer=BARCLAYS+BANK+PLC&cardIssuerCountry=United+Kingdom&cardIssuerCountryCode=GBR&vcsResponseCode=0&vcsResponseMessage=Success+-+no+velocity+check+rules+applied&currencyExponent=2&signature=9e13f5ffd9cf94215225ab60f80915879a242dcf8c30c7f39a0265acf0f3f7186cbea656d53fcce249e54f522050efd32e677726fc7d5aa1f7b6ee746e040956"
end

def failed_reference_purchase_response
"responseCode=65548&responseMessage=DB+ERROR&responseStatus=2&amount=142&currencyCode=826&transactionUnique=740290de68c10d18acabe9fb0a52bd92&orderRef=AM+test+purchase&type=1&threeDSRequired=N&xref=16040515NM21GM43SF71MMR&countryCode=GB&merchantID=103191&action=SALE&state=finished&remoteAddress=107.15.253.186&requestMerchantID=103191&processMerchantID=103191&transactionID=10886787&timestamp=2016-04-05+15%3A21%3A43&vcsResponseCode=0&vcsResponseMessage=Success+-+no+velocity+check+rules+applied&signature=311f2bfd94c3807fae4fbeff13801c37d81c3e1082c5d3c5893281f18d7152b96420fa7b45285eb692b09b576b3b85d894ee35ff9e31cd06ace54019b806550f"
end

def transcript
<<-eos
POST /direct/ HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nConnection: close\r\nHost: gateway.cardstream.com\r\nContent-Length: 501\r\n\r\n"
Expand Down

0 comments on commit 03e29e3

Please sign in to comment.