spec/integration/braintree/transaction_spec.rb in braintree-1.0.1 vs spec/integration/braintree/transaction_spec.rb in braintree-1.1.0

- old
+ new

@@ -12,16 +12,34 @@ } ) result.success?.should == true result.transaction.id.should =~ /^\w{6}$/ result.transaction.type.should == "sale" - result.transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + result.transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) result.transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] result.transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] result.transaction.credit_card_details.expiration_date.should == "05/2009" + result.transaction.credit_card_details.customer_location.should == "US" end - + + it "returns processor response code and text if declined" do + result = Braintree::Transaction.create( + :type => "sale", + :amount => Braintree::Test::TransactionAmounts::Decline, + :credit_card => { + :number => Braintree::Test::CreditCardNumbers::Visa, + :expiration_date => "05/2009" + } + ) + result.success?.should == true + result.transaction.id.should =~ /^\w{6}$/ + result.transaction.type.should == "sale" + result.transaction.status.should == "processor_declined" + result.transaction.processor_response_code.should == "2000" + result.transaction.processor_response_text.should == "Do Not Honor" + end + it "can create custom fields" do result = Braintree::Transaction.create( :type => "sale", :amount => Braintree::Test::TransactionAmounts::Authorize, :credit_card => { @@ -65,11 +83,11 @@ } result = Braintree::Transaction.create(params[:transaction]) result.success?.should == false result.params.should == {:transaction => {:type => 'sale', :amount => nil, :credit_card => {:expiration_date => "05/2009"}}} end - + it "returns errors if validations fail (tests many errors at once for spec speed)" do params = { :transaction => { :type => "pants", :amount => nil, @@ -89,13 +107,12 @@ result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::AmountIsRequired result.errors.for(:transaction).on(:customer_id)[0].code.should == Braintree::ErrorCodes::Transaction::CustomerIdIsInvalid result.errors.for(:transaction).on(:order_id)[0].code.should == Braintree::ErrorCodes::Transaction::OrderIdIsTooLong result.errors.for(:transaction).on(:payment_method_token)[0].code.should == Braintree::ErrorCodes::Transaction::PaymentMethodTokenIsInvalid result.errors.for(:transaction).on(:type)[0].code.should == Braintree::ErrorCodes::Transaction::TypeIsInvalid - end - + it "returns an error if amount is negative" do params = { :transaction => { :type => "credit", :amount => "-1" @@ -115,32 +132,32 @@ } result = Braintree::Transaction.create(params[:transaction]) result.success?.should == false result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::AmountIsInvalid end - + it "returns an error if type is not given" do params = { :transaction => { :type => nil } } result = Braintree::Transaction.create(params[:transaction]) result.success?.should == false result.errors.for(:transaction).on(:type)[0].code.should == Braintree::ErrorCodes::Transaction::TypeIsRequired end - + it "returns an error if no credit card is given" do params = { :transaction => { } } result = Braintree::Transaction.create(params[:transaction]) result.success?.should == false result.errors.for(:transaction).on(:base)[0].code.should == Braintree::ErrorCodes::Transaction::CreditCardIsRequired end - + it "returns an error if the given payment method token doesn't belong to the customer" do customer = Braintree::Customer.create!( :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "05/2010" @@ -167,16 +184,16 @@ :expiration_date => "05/2009" } ) transaction.id.should =~ /^\w{6}$/ transaction.type.should == "sale" - transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] transaction.credit_card_details.expiration_date.should == "05/2009" end - + it "raises a ValidationsFailed if invalid" do expect do Braintree::Transaction.create!( :type => "sale", :amount => nil, @@ -199,11 +216,11 @@ } ) result.success?.should == true result.transaction.id.should =~ /^\w{6}$/ result.transaction.type.should == "sale" - result.transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + result.transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) result.transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] result.transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] result.transaction.credit_card_details.expiration_date.should == "05/2009" end @@ -251,11 +268,11 @@ result.success?.should == true transaction = result.transaction transaction.id.should =~ /\A\w{6}\z/ transaction.type.should == "sale" transaction.status.should == "authorized" - transaction.amount.should == "100.00" + transaction.amount.should == BigDecimal.new("100.00") transaction.order_id.should == "123" transaction.processor_response_code.should == "1000" transaction.created_at.between?(Time.now - 5, Time.now).should == true transaction.updated_at.between?(Time.now - 5, Time.now).should == true transaction.credit_card_details.bin.should == "510510" @@ -289,11 +306,11 @@ transaction.shipping_details.locality.should == "Bartlett" transaction.shipping_details.region.should == "IL" transaction.shipping_details.postal_code.should == "60103" transaction.shipping_details.country_name.should == "United States of America" end - + it "can store customer and credit card in the vault" do result = Braintree::Transaction.sale( :amount => "100", :customer => { :first_name => "Adam", @@ -345,10 +362,12 @@ result.success?.should == true transaction = result.transaction transaction.customer_details.id.should =~ /\A\d{6,7}\z/ transaction.vault_customer.id.should == transaction.customer_details.id credit_card = Braintree::CreditCard.find(transaction.vault_credit_card.token) + transaction.billing_details.id.should == credit_card.billing_address.id + transaction.vault_billing_address.id.should == credit_card.billing_address.id credit_card.billing_address.first_name.should == "Carl" credit_card.billing_address.last_name.should == "Jones" credit_card.billing_address.company.should == "Braintree" credit_card.billing_address.street_address.should == "123 E Main St" credit_card.billing_address.extended_address.should == "Suite 403" @@ -356,10 +375,54 @@ credit_card.billing_address.region.should == "IL" credit_card.billing_address.postal_code.should == "60622" credit_card.billing_address.country_name.should == "United States of America" end + it "can store the shipping address in the vault" do + result = Braintree::Transaction.sale( + :amount => "100", + :customer => { + :first_name => "Adam", + :last_name => "Williams" + }, + :credit_card => { + :number => "5105105105105100", + :expiration_date => "05/2012" + }, + :shipping => { + :first_name => "Carl", + :last_name => "Jones", + :company => "Braintree", + :street_address => "123 E Main St", + :extended_address => "Suite 403", + :locality => "Chicago", + :region => "IL", + :postal_code => "60622", + :country_name => "United States of America" + }, + :options => { + :store_in_vault => true, + :store_shipping_address_in_vault => true, + } + ) + result.success?.should == true + transaction = result.transaction + transaction.customer_details.id.should =~ /\A\d{6,7}\z/ + transaction.vault_customer.id.should == transaction.customer_details.id + transaction.vault_shipping_address.id.should == transaction.vault_customer.addresses[0].id + shipping_address = transaction.vault_customer.addresses[0] + shipping_address.first_name.should == "Carl" + shipping_address.last_name.should == "Jones" + shipping_address.company.should == "Braintree" + shipping_address.street_address.should == "123 E Main St" + shipping_address.extended_address.should == "Suite 403" + shipping_address.locality.should == "Chicago" + shipping_address.region.should == "IL" + shipping_address.postal_code.should == "60622" + shipping_address.country_name.should == "United States of America" + end + it "submits for settlement if given transaction[options][submit_for_settlement]" do result = Braintree::Transaction.sale( :amount => "100", :credit_card => { :number => "5105105105105100", @@ -370,11 +433,11 @@ } ) result.success?.should == true result.transaction.status.should == "submitted_for_settlement" end - + it "can specify the customer id and payment method token" do customer_id = "customer_#{rand(1000000)}" payment_mehtod_token = "credit_card_#{rand(1000000)}" result = Braintree::Transaction.sale( :amount => "100", @@ -397,11 +460,11 @@ transaction.customer_details.id.should == customer_id transaction.vault_customer.id.should == customer_id transaction.credit_card_details.token.should == payment_mehtod_token transaction.vault_credit_card.token.should == payment_mehtod_token end - + it "returns an error result if validations fail" do params = { :transaction => { :amount => nil, :credit_card => { @@ -426,16 +489,16 @@ :expiration_date => "05/2009" } ) transaction.id.should =~ /^\w{6}$/ transaction.type.should == "sale" - transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] transaction.credit_card_details.expiration_date.should == "05/2009" end - + it "raises a ValidationsFailed if invalid" do expect do Braintree::Transaction.sale!( :amount => nil, :credit_card => { @@ -466,14 +529,14 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") result = Braintree::Transaction.submit_for_settlement(transaction.id, "999.99") result.success?.should == true - result.transaction.amount.should == "999.99" + result.transaction.amount.should == BigDecimal.new("999.99") result.transaction.status.should == "submitted_for_settlement" result.transaction.updated_at.between?(Time.now - 5, Time.now).should == true end it "returns an error result if settlement is too large" do @@ -482,11 +545,11 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") result = Braintree::Transaction.submit_for_settlement(transaction.id, "1000.01") result.success?.should == false result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::SettlementAmountIsTooLarge result.params[:transaction][:amount].should == "1000.01" end @@ -525,17 +588,17 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") expect do Braintree::Transaction.submit_for_settlement!(transaction.id, "1000.01") end.to raise_error(Braintree::ValidationsFailed) end end - + describe "self.credit" do it "returns a successful result with type=credit if successful" do result = Braintree::Transaction.credit( :amount => Braintree::Test::TransactionAmounts::Authorize, :credit_card => { @@ -544,16 +607,16 @@ } ) result.success?.should == true result.transaction.id.should =~ /^\w{6}$/ result.transaction.type.should == "credit" - result.transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + result.transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) result.transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] result.transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] result.transaction.credit_card_details.expiration_date.should == "05/2009" end - + it "returns an error result if validations fail" do params = { :transaction => { :amount => nil, :credit_card => { @@ -566,11 +629,11 @@ result.success?.should == false result.params.should == {:transaction => {:type => 'credit', :amount => nil, :credit_card => {:expiration_date => "05/2009"}}} result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::AmountIsRequired end end - + describe "self.credit!" do it "returns the transaction if valid" do transaction = Braintree::Transaction.credit!( :amount => Braintree::Test::TransactionAmounts::Authorize, :credit_card => { @@ -578,16 +641,16 @@ :expiration_date => "05/2009" } ) transaction.id.should =~ /^\w{6}$/ transaction.type.should == "credit" - transaction.amount.should == Braintree::Test::TransactionAmounts::Authorize + transaction.amount.should == BigDecimal.new(Braintree::Test::TransactionAmounts::Authorize) transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] transaction.credit_card_details.expiration_date.should == "05/2009" end - + it "raises a ValidationsFailed if invalid" do expect do Braintree::Transaction.credit!( :amount => nil, :credit_card => { @@ -596,11 +659,11 @@ } ) end.to raise_error(Braintree::ValidationsFailed) end end - + describe "self.create_from_transparent_redirect" do it "returns a successful result if successful" do params = { :transaction => { :amount => Braintree::Test::TransactionAmounts::Authorize, @@ -618,19 +681,19 @@ query_string_response = create_transaction_via_tr(params, tr_data_params) result = Braintree::Transaction.create_from_transparent_redirect(query_string_response) result.success?.should == true transaction = result.transaction transaction.type.should == "sale" - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") transaction.credit_card_details.bin.should == Braintree::Test::CreditCardNumbers::Visa[0, 6] transaction.credit_card_details.last_4.should == Braintree::Test::CreditCardNumbers::Visa[-4..-1] transaction.credit_card_details.expiration_date.should == "05/2009" end - + it "can put any param in tr_data" do params = { - + } tr_data_params = { :transaction => { :amount => "100.00", :order_id => "123", @@ -677,11 +740,11 @@ result = Braintree::Transaction.create_from_transparent_redirect(query_string_response) transaction = result.transaction transaction.id.should =~ /\A\w{6}\z/ transaction.type.should == "sale" transaction.status.should == "authorized" - transaction.amount.should == "100.00" + transaction.amount.should == BigDecimal.new("100.00") transaction.order_id.should == "123" transaction.processor_response_code.should == "1000" transaction.created_at.between?(Time.now - 5, Time.now).should == true transaction.updated_at.between?(Time.now - 5, Time.now).should == true transaction.credit_card_details.bin.should == "510510" @@ -715,11 +778,11 @@ transaction.shipping_details.locality.should == "Bartlett" transaction.shipping_details.region.should == "IL" transaction.shipping_details.postal_code.should == "60103" transaction.shipping_details.country_name.should == "United States of America" end - + it "returns an error result if validations fail" do params = { :transaction => { :amount => "", :credit_card => { @@ -738,11 +801,11 @@ result.success?.should == false result.params[:transaction].should == {:amount => "", :type => "sale", :credit_card => {:expiration_date => "05/2009"}} result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::AmountIsRequired end end - + describe "self.find" do it "finds the transaction with the given id" do result = Braintree::Transaction.create( :type => "sale", :amount => Braintree::Test::TransactionAmounts::Authorize, @@ -791,11 +854,11 @@ result.success?.should == false result.errors.for(:transaction).on(:base)[0].code.should == Braintree::ErrorCodes::Transaction::CannotBeVoided end end - describe "void!" do + describe "self.void!" do it "returns the transaction if successful" do transaction = Braintree::Transaction.sale!( :amount => Braintree::Test::TransactionAmounts::Authorize, :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, @@ -818,21 +881,22 @@ expect do Braintree::Transaction.void!(transaction.id) end.to raise_error(Braintree::ValidationsFailed) end end - + describe "refund" do it "returns a successful result if successful" do - transaction = find_transaction_to_refund + transaction = create_transaction_to_refund + transaction.status.should == "settled" result = transaction.refund result.success?.should == true result.new_transaction.type.should == "credit" end it "returns an error if already refunded" do - transaction = find_transaction_to_refund + transaction = create_transaction_to_refund result = transaction.refund result.success?.should == true result = transaction.refund result.success?.should == false result.errors.for(:transaction).on(:base)[0].code.should == Braintree::ErrorCodes::Transaction::HasAlreadyBeenRefunded @@ -872,28 +936,28 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") result = transaction.submit_for_settlement("999.99") result.success?.should == true - transaction.amount.should == "999.99" + transaction.amount.should == BigDecimal.new("999.99") end it "updates the transaction attributes" do transaction = Braintree::Transaction.sale!( :amount => Braintree::Test::TransactionAmounts::Authorize, :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") result = transaction.submit_for_settlement("999.99") result.success?.should == true - transaction.amount.should == "999.99" + transaction.amount.should == BigDecimal.new("999.99") transaction.status.should == "submitted_for_settlement" transaction.updated_at.between?(Time.now - 5, Time.now).should == true end it "returns an error result if unsuccessful" do @@ -902,11 +966,11 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") result = transaction.submit_for_settlement("1000.01") result.success?.should == false result.errors.for(:transaction).on(:amount)[0].code.should == Braintree::ErrorCodes::Transaction::SettlementAmountIsTooLarge result.params[:transaction][:amount].should == "1000.01" end @@ -932,17 +996,17 @@ :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, :expiration_date => "06/2009" } ) - transaction.amount.should == "1000.00" + transaction.amount.should == BigDecimal.new("1000.00") expect do transaction.submit_for_settlement!("1000.01") end.to raise_error(Braintree::ValidationsFailed) end end - + describe "search" do describe "advanced" do it "pretty much works in one big fell swoop/hash" do result = Braintree::Transaction.create( :type => "sale", @@ -1024,16 +1088,16 @@ describe "basic" do it "returns paged transactions matching the given search terms" do transactions = Braintree::Transaction.search "1111" transactions.total_items.should > 0 end - + it "is paged" do transactions = Braintree::Transaction.search "1111", :page => 2 transactions.current_page_number.should == 2 end - + it "can traverse pages" do transactions = Braintree::Transaction.search "1111", :page => 1 transactions.next_page.current_page_number.should == 2 end end @@ -1052,11 +1116,11 @@ transaction.status_history.size.should == 2 transaction.status_history[0].status.should == "authorized" transaction.status_history[1].status.should == "submitted_for_settlement" end end - + describe "vault_credit_card" do it "returns the Braintree::CreditCard if the transaction credit card is stored in the vault" do customer = Braintree::Customer.create!( :credit_card => { :number => Braintree::Test::CreditCardNumbers::Visa, @@ -1064,11 +1128,11 @@ } ) transaction = customer.credit_cards[0].sale(:amount => "100.00").transaction transaction.vault_credit_card.should == customer.credit_cards[0] end - + it "returns nil if the transaction credit card is not stored in the vault" do transaction = Braintree::Transaction.create!( :amount => "100.00", :type => "sale", :credit_card => { @@ -1089,11 +1153,11 @@ } ) transaction = customer.credit_cards[0].sale(:amount => "100.00").transaction transaction.vault_customer.should == customer end - + it "returns nil if the transaction customer is not stored in the vault" do transaction = Braintree::Transaction.create!( :amount => "100.00", :type => "sale", :credit_card => { @@ -1102,11 +1166,11 @@ } ) transaction.vault_customer.should == nil end end - + describe "void" do it "returns a successful result if successful" do result = Braintree::Transaction.create( :type => "sale", :amount => Braintree::Test::TransactionAmounts::Authorize, @@ -1176,17 +1240,25 @@ :tr_data => Braintree::TransparentRedirect.transaction_data({:redirect_url => "http://testing.com"}.merge(tr_data_params)) }.merge(params) request.body = Braintree::Util.hash_to_query_string(params) response = http.request(request) end - query_string = response["Location"].split("?", 2).last + query_string = response["Location"].split("?", 2).last query_string end - def find_transaction_to_refund - settled_transactions = Braintree::Transaction.search "settled" - found = settled_transactions.detect do |t| - t.status == "settled" && !t.refunded? - end - found || raise("unfortunately your test is not as you intended because we could not find a settled transaction that hasn't already been refunded") + def create_transaction_to_refund + transaction = Braintree::Transaction.sale!( + :amount => Braintree::Test::TransactionAmounts::Authorize, + :credit_card => { + :number => Braintree::Test::CreditCardNumbers::Visa, + :expiration_date => "05/2009" + }, + :options => { + :submit_for_settlement => true + } + ) + + response = Braintree::Http.put "/transactions/#{transaction.id}/settle" + Braintree::Transaction.find(transaction.id) end end