require File.dirname(__FILE__) + "/../test_helper" class RestEncodingTest < Test::Unit::TestCase def test_basic_request_encoding r = EWS::Transaction::Request.new(basic_params) rest = EWS::Transaction::Mapping.request_to_rest(r) xml = REXML::Document.new(rest) txn = REXML::XPath.first(xml, "//Transaction") assert_equal "4111111111111111", REXML::XPath.first(txn, "Card_Number").text assert_equal "Simon Brown", REXML::XPath.first(txn, "CardHoldersName").text assert_equal "00", REXML::XPath.first(txn, "Transaction_Type").text assert_equal "1012", REXML::XPath.first(txn, "Expiry_Date").text assert_equal "10.13", REXML::XPath.first(txn, "DollarAmount").text end def test_request_encoding_everything request = EWS::Transaction::Request.new(sample_everything_params) rest = EWS::Transaction::Mapping.request_to_rest(request) xml = REXML::Document.new(rest) txn = REXML::XPath.first(xml, "//Transaction") assert_equal request.amount.to_s, REXML::XPath.first(txn, "DollarAmount").text assert_equal request.authorization_num.to_s, REXML::XPath.first(txn, "Authorization_Num").text assert_equal request.cardholder_name.to_s, REXML::XPath.first(txn, "CardHoldersName").text assert_equal request.cavv.to_s, REXML::XPath.first(txn, "CAVV").text assert_equal request.cavv_algorithm.to_s, REXML::XPath.first(txn, "CAVV_Algorithm").text assert_equal request.cc_expiry.to_s, REXML::XPath.first(txn, "Expiry_Date").text assert_equal request.cc_number.to_s, REXML::XPath.first(txn, "Card_Number").text assert_equal request.cc_verification_str1.to_s, REXML::XPath.first(txn, "VerificationStr1").text assert_equal request.cc_verification_str2.to_s, REXML::XPath.first(txn, "VerificationStr2").text assert_equal request.client_email.to_s, REXML::XPath.first(txn, "Client_Email").text assert_equal request.client_ip.to_s, REXML::XPath.first(txn, "Client_IP").text assert_equal request.customer_ref.to_s, REXML::XPath.first(txn, "Customer_Ref").text assert_equal request.cvd_presence_ind.to_s, REXML::XPath.first(txn, "CVD_Presence_Ind").text assert_equal request.ecommerce_flag.to_s, REXML::XPath.first(txn, "Ecommerce_Flag").text assert_equal request.gateway_id.to_s, REXML::XPath.first(txn, "ExactID").text assert_equal request.language.to_s, REXML::XPath.first(txn, "Language").text assert_equal request.password.to_s, REXML::XPath.first(txn, "Password").text assert_equal request.pan.to_s, REXML::XPath.first(txn, "PAN").text assert_equal request.reference_3.to_s, REXML::XPath.first(txn, "Reference_3").text assert_equal request.reference_no.to_s, REXML::XPath.first(txn, "Reference_No").text assert_equal request.secure_auth_required.to_s, REXML::XPath.first(txn, "Secure_AuthRequired").text assert_equal request.secure_auth_result.to_s, REXML::XPath.first(txn, "Secure_AuthResult").text assert_equal request.surcharge_amount.to_s, REXML::XPath.first(txn, "SurchargeAmount").text assert_equal request.tax1_amount.to_s, REXML::XPath.first(txn, "Tax1Amount").text assert_equal request.tax1_number.to_s, REXML::XPath.first(txn, "Tax1Number").text assert_equal request.tax2_amount.to_s, REXML::XPath.first(txn, "Tax2Amount").text assert_equal request.tax2_number.to_s, REXML::XPath.first(txn, "Tax2Number").text assert_equal request.track1.to_s, REXML::XPath.first(txn, "Track1").text assert_equal request.track2.to_s, REXML::XPath.first(txn, "Track2").text assert_equal request.transaction_tag.to_s, REXML::XPath.first(txn, "Transaction_Tag").text assert_equal request.user_name.to_s, REXML::XPath.first(txn, "User_Name").text assert_equal request.xid.to_s, REXML::XPath.first(txn, "XID").text assert_equal request.zip_code.to_s, REXML::XPath.first(txn, "ZipCode").text end def test_basic_response_decoding filename = File.dirname(__FILE__) + "/../samples/rest.response.xml" payload = IO.read(filename) r = nil assert_nothing_raised do r = EWS::Transaction::Mapping.rest_to_response(payload) end assert_not_nil r assert_equal "Transaction Normal", r.exact_message assert_nil r.error_description assert_equal "APPROVED", r.bank_message assert_equal 2516, r.transaction_tag assert_equal "00", r.transaction_type end def test_dodgy_response_decoding filename = File.dirname(__FILE__) + "/../samples/rest.dodgy.response.xml" payload = IO.read(filename) r = nil assert_nothing_raised do r = EWS::Transaction::Mapping.rest_to_response(payload) end assert_not_nil r assert_equal "Transaction Normal", r.exact_message assert_nil r.error_description assert_equal "APPROVED", r.bank_message assert_equal 2516, r.transaction_tag assert_equal "00", r.transaction_type # the following are all invalid examples of the relevant fields, so ensure we # decode them without complaint. assert_equal "Simon Brown Is Not A Really Long Name But This Is", r.cardholder_name assert_equal "nonsense", r.cc_number assert_equal "This Is A Really LONG Customer Reference", r.customer_ref end def test_response_decoding_everything filename = File.dirname(__FILE__) + "/../samples/rest.everything.response.xml" payload = IO.read(filename) r = nil assert_nothing_raised do r = EWS::Transaction::Mapping.rest_to_response(payload) end assert_not_nil r assert !r.transaction_error? assert r.approved? assert_equal 'Z', r.avs assert_equal "APPROVED", r.bank_message assert_equal "000", r.bank_resp_code assert_equal "12", r.bank_resp_code_2 assert_equal "0", r.cavv_response assert_equal "This is\na CTR", r.ctr assert_equal "U", r.cvv2 assert_equal "Something", r.error_description assert_equal "650", r.error_number assert_equal "Transaction Normal", r.exact_message assert_equal "00", r.exact_resp_code assert_equal "Processed by E-Xact", r.logon_message assert_equal "07203434", r.retrieval_ref_no assert_equal "258", r.sequence_no assert_equal "Paymentech Goods Buy and Sell(Calgary) 5300003", r.merchant_name assert_equal "4567 8th ave SE.", r.merchant_address assert_equal "Calgary", r.merchant_city assert_equal "United States", r.merchant_country assert_equal "V6B 2K4", r.merchant_postal assert_equal "Alabama", r.merchant_province assert_equal "http://somewhere.com/somehow/", r.merchant_url assert_equal "ET3434", r.authorization_num assert_equal "Simon Brown", r.cardholder_name assert_equal "4111111111111111", r.cc_number assert_equal "123", r.cavv assert_equal "8", r.cavv_algorithm assert_equal "simon@example.com", r.client_email assert_equal "127.0.0.1", r.client_ip assert_equal "REF-123", r.customer_ref assert_equal "0", r.cvd_presence_ind assert_equal "4", r.ecommerce_flag assert_equal "1210", r.cc_expiry assert_equal "4", r.language assert_equal "12345678900987654321", r.pan assert_equal "AD0009-01", r.gateway_id assert_equal "passw0rd", r.password assert_equal "REF-456", r.reference_3 assert_equal "987987", r.reference_no assert_equal "1", r.secure_auth_required assert_equal "9", r.secure_auth_result assert_equal "11.12", r.surcharge_amount assert_equal "23.23", r.tax1_amount assert_equal "987654", r.tax1_number assert_equal "34.34", r.tax2_amount assert_equal "123456", r.tax2_number assert_equal "%B4111111111111111^Jones/Simon ^1309101063510010000000306000000?", r.track1 assert_equal ";4111111111111111=1309101420320192611?", r.track2 assert_equal 2516, r.transaction_tag assert_equal "00", r.transaction_type assert_equal "Superman", r.user_name assert_equal "verified", r.cc_verification_str1 assert_equal "6785", r.cc_verification_str2 assert_equal "x678", r.xid assert_equal "10100", r.zip_code end end