Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

1.2.1

  • Loading branch information...
commit 8229c6f87edb3e5c033a840ba0d3669d7cb37bac 1 parent 168d742
@braintreeps braintreeps authored
View
7 CHANGELOG.rdoc
@@ -1,3 +1,10 @@
+== 1.2.1
+
+* Added ValidationErrorCollection#shallow_errors to get all of the ValidationErrors at a given level in the error hierarchy
+* Added the ability to make a credit card the default card for a customer
+* Added constants for transaction statuses
+* Updated Quick Start in README.rdoc to show a workflow with error checking
+
== 1.2.0
* Added Subscription search
View
18 README.rdoc
@@ -17,15 +17,25 @@ The Braintree gem provides integration access to the Braintree Gateway.
Braintree::Configuration.public_key = "a_public_key"
Braintree::Configuration.private_key = "a_private_key"
- transaction = Braintree::Transaction.sale!(
- :amount => "100.00",
+ result = Braintree::Transaction.sale(
+ :amount => "1000.00",
:credit_card => {
:number => "5105105105105100",
:expiration_date => "05/12"
}
)
- puts "Transaction ID: #{transaction.id}"
- puts "Status: #{transaction.status}"
+
+ if result.success?
+ if result.transaction.status == Braintree::Transaction::Status::Authorized
+ puts "sucess!: #{result.transaction.id}"
+ else
+ puts "Error processing transaction:"
+ puts " code: #{result.transaction.processor_response_code}"
+ puts " text: #{result.transaction.processor_response_text}"
+ end
+ else
+ p result.errors
+ end
== Bang Methods
View
2  lib/braintree/credit_card.rb
@@ -224,7 +224,7 @@ def self._do_update(http_verb, url, params) # :nodoc:
def self._update_signature # :nodoc:
[
:cardholder_name, :cvv, :expiration_date, :expiration_month, :expiration_year, :number, :token,
- {:options => [:verify_card]},
+ {:options => [:make_default, :verify_card]},
{:billing_address => [:company, :country_name, :extended_address, :first_name, :last_name, :locality, :postal_code, :region, :street_address]}
]
end
View
13 lib/braintree/transaction.rb
@@ -119,6 +119,19 @@ module Braintree
class Transaction
include BaseModule
+ module Status
+ Authorizing = 'authorizing'
+ Authorized = 'authorized'
+ GatewayRejected = 'gateway_rejected'
+ Failed = 'failed'
+ ProcessorDeclined = 'processor_declined'
+ Settled = 'settled'
+ SettlementFailed = 'settlement_failed'
+ SubmittedForSettlement = 'submitted_for_settlement'
+ Unknown = 'unknown'
+ Voided = 'voided'
+ end
+
module Type # :nodoc:
Credit = "credit" # :nodoc:
Sale = "sale" # :nodoc:
View
7 lib/braintree/validation_error_collection.rb
@@ -35,6 +35,8 @@ def [](index)
@errors[index]
end
+ # Returns an array of ValidationError objects at this level and all nested levels in the error
+ # hierarchy
def deep_errors
([@errors] + @nested.values.map { |error_collection| error_collection.deep_errors }).flatten
end
@@ -63,6 +65,11 @@ def on(attribute)
@errors.select { |error| error.attribute == attribute.to_s }
end
+ # Returns an array of ValidationError objects at the given level in the error hierarchy
+ def shallow_errors
+ @errors.dup
+ end
+
# The number of errors at this level. This does not include nested errors.
def size
@errors.size
View
2  lib/braintree/version.rb
@@ -2,7 +2,7 @@ module Braintree
module Version
Major = 1
Minor = 2
- Tiny = 0
+ Tiny = 1
String = "#{Major}.#{Minor}.#{Tiny}"
end
View
118 spec/integration/braintree/credit_card_spec.rb
@@ -67,7 +67,7 @@
:options => {:verify_card => true}
)
result.success?.should == false
- result.credit_card_verification.status.should == "processor_declined"
+ result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
result.credit_card_verification.processor_response_code.should == "2000"
result.credit_card_verification.processor_response_text.should == "Do Not Honor"
result.credit_card_verification.cvv_response_code.should == "I"
@@ -116,6 +116,28 @@
result.success?.should == false
result.errors.for(:credit_card).on(:expiration_date)[0].message.should == "Expiration date is invalid."
end
+
+ it "can set the default flag" do
+ customer = Braintree::Customer.create!
+ card1 = Braintree::CreditCard.create(
+ :customer_id => customer.id,
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2009"
+ ).credit_card
+ card1.should be_default
+
+ card2 = Braintree::CreditCard.create(
+ :customer_id => customer.id,
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2009",
+ :options => {
+ :make_default => true
+ }
+ ).credit_card
+ card2.should be_default
+
+ Braintree::CreditCard.find(card1.token).should_not be_default
+ end
end
describe "self.create!" do
@@ -221,6 +243,39 @@
credit_card.customer_id.should == customer.id
end
+ it "create card as default" do
+ customer = Braintree::Customer.create!(
+ :credit_card => {
+ :cardholder_name => "Old Cardholder Name",
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2012"
+ }
+ )
+ card1 = customer.credit_cards[0]
+
+ params = {
+ :credit_card => {
+ :cardholder_name => "Card Holder",
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2012",
+ :options => {:make_default => true}
+ }
+ }
+ tr_data_params = {
+ :credit_card => {
+ :customer_id => customer.id
+ }
+ }
+ tr_data = Braintree::TransparentRedirect.create_credit_card_data({:redirect_url => "http://example.com"}.merge(tr_data_params))
+ query_string_response = SpecHelper.simulate_form_post_for_tr(Braintree::CreditCard.create_credit_card_url, tr_data, params)
+ result = Braintree::CreditCard.create_from_transparent_redirect(query_string_response)
+ result.success?.should == true
+ card2 = result.credit_card
+
+ Braintree::CreditCard.find(card1.token).should_not be_default
+ card2.should be_default
+ end
+
it "returns xml with nested errors if validation errors" do
customer = Braintree::Customer.create.customer
params = {
@@ -307,7 +362,7 @@
:options => {:verify_card => true}
)
update_result.success?.should == false
- update_result.credit_card_verification.status.should == "processor_declined"
+ update_result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
end
it "can update the billing address" do
@@ -373,6 +428,28 @@
update_result.success?.should == false
update_result.errors.for(:credit_card).on(:number)[0].message.should == "Credit card number must be 12-19 digits."
end
+
+ it "can update the default" do
+ customer = Braintree::Customer.create!
+ card1 = Braintree::CreditCard.create(
+ :customer_id => customer.id,
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2009"
+ ).credit_card
+ card2 = Braintree::CreditCard.create(
+ :customer_id => customer.id,
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2009"
+ ).credit_card
+
+ card1.should be_default
+ card2.should_not be_default
+
+ Braintree::CreditCard.update(card2.token, :options => {:make_default => true})
+
+ Braintree::CreditCard.find(card1.token).should_not be_default
+ Braintree::CreditCard.find(card2.token).should be_default
+ end
end
describe "self.update!" do
@@ -451,6 +528,41 @@
credit_card.expiration_date.should == "05/2014"
credit_card.token.should == new_token
end
+
+ it "updates the default credit card" do
+ customer = Braintree::Customer.create!(
+ :credit_card => {
+ :cardholder_name => "Old Cardholder Name",
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2012"
+ }
+ )
+ card1 = customer.credit_cards[0]
+
+ card2 = Braintree::CreditCard.create(
+ :customer_id => customer.id,
+ :number => Braintree::Test::CreditCardNumbers::Visa,
+ :expiration_date => "05/2009"
+ ).credit_card
+
+ card1.should be_default
+ card2.should_not be_default
+
+ params = {
+ :credit_card => {
+ :options => {:make_default => true}
+ }
+ }
+ tr_data_params = {
+ :payment_method_token => card2.token
+ }
+ tr_data = Braintree::TransparentRedirect.update_credit_card_data({:redirect_url => "http://example.com"}.merge(tr_data_params))
+ query_string_response = SpecHelper.simulate_form_post_for_tr(Braintree::CreditCard.update_credit_card_url, tr_data, params)
+ result = Braintree::CreditCard.update_from_transparent_redirect(query_string_response)
+
+ Braintree::CreditCard.find(card1.token).should_not be_default
+ Braintree::CreditCard.find(card2.token).should be_default
+ end
end
describe "credit" do
@@ -744,7 +856,7 @@
:options => {:verify_card => true}
)
update_result.success?.should == false
- update_result.credit_card_verification.status.should == "processor_declined"
+ update_result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
end
it "can update the billing address" do
View
2  spec/integration/braintree/customer_spec.rb
@@ -110,7 +110,7 @@
}
)
result.success?.should == false
- result.credit_card_verification.status.should == "processor_declined"
+ result.credit_card_verification.status.should == Braintree::Transaction::Status::ProcessorDeclined
end
it "can create a customer, payment method, and billing address at the same time" do
View
4 spec/integration/braintree/test/transaction_amounts_spec.rb
@@ -10,7 +10,7 @@
:expiration_date => "12/2012"
}
)
- transaction.status.should == "authorized"
+ transaction.status.should == Braintree::Transaction::Status::Authorized
end
end
@@ -23,7 +23,7 @@
:expiration_date => "12/2012"
}
)
- transaction.status.should == "processor_declined"
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
end
end
end
View
34 spec/integration/braintree/transaction_spec.rb
@@ -34,7 +34,7 @@
result.success?.should == true
result.transaction.id.should =~ /^\w{6}$/
result.transaction.type.should == "sale"
- result.transaction.status.should == "processor_declined"
+ result.transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
result.transaction.processor_response_code.should == "2000"
result.transaction.processor_response_text.should == "Do Not Honor"
end
@@ -296,7 +296,7 @@
transaction = result.transaction
transaction.id.should =~ /\A\w{6}\z/
transaction.type.should == "sale"
- transaction.status.should == "authorized"
+ transaction.status.should == Braintree::Transaction::Status::Authorized
transaction.amount.should == BigDecimal.new("100.00")
transaction.order_id.should == "123"
transaction.processor_response_code.should == "1000"
@@ -461,7 +461,7 @@
}
)
result.success?.should == true
- result.transaction.status.should == "submitted_for_settlement"
+ result.transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
end
it "can specify the customer id and payment method token" do
@@ -563,7 +563,7 @@
result = Braintree::Transaction.submit_for_settlement(transaction.id, "999.99")
result.success?.should == true
result.transaction.amount.should == BigDecimal.new("999.99")
- result.transaction.status.should == "submitted_for_settlement"
+ result.transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
result.transaction.updated_at.between?(Time.now - 5, Time.now).should == true
end
@@ -606,7 +606,7 @@
}
)
transaction = Braintree::Transaction.submit_for_settlement!(original_transaction.id)
- transaction.status.should == "submitted_for_settlement"
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
transaction.id.should == original_transaction.id
end
@@ -773,7 +773,7 @@
transaction = result.transaction
transaction.id.should =~ /\A\w{6}\z/
transaction.type.should == "sale"
- transaction.status.should == "authorized"
+ transaction.status.should == Braintree::Transaction::Status::Authorized
transaction.amount.should == BigDecimal.new("100.00")
transaction.order_id.should == "123"
transaction.processor_response_code.should == "1000"
@@ -874,7 +874,7 @@
result = Braintree::Transaction.void(transaction.id)
result.success?.should == true
result.transaction.id.should == transaction.id
- result.transaction.status.should == "voided"
+ result.transaction.status.should == Braintree::Transaction::Status::Voided
end
it "returns an error result if unsuccessful" do
@@ -902,7 +902,7 @@
)
returned_transaction = Braintree::Transaction.void!(transaction.id)
returned_transaction.should == transaction
- returned_transaction.status.should == "voided"
+ returned_transaction.status.should == Braintree::Transaction::Status::Voided
end
it "raises a ValidationsFailed if unsuccessful" do
@@ -922,7 +922,7 @@
describe "refund" do
it "returns a successful result if successful" do
transaction = create_transaction_to_refund
- transaction.status.should == "settled"
+ transaction.status.should == Braintree::Transaction::Status::Settled
result = transaction.refund
result.success?.should == true
result.new_transaction.type.should == "credit"
@@ -991,7 +991,7 @@
result = transaction.submit_for_settlement("999.99")
result.success?.should == true
transaction.amount.should == BigDecimal.new("999.99")
- transaction.status.should == "submitted_for_settlement"
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
transaction.updated_at.between?(Time.now - 5, Time.now).should == true
end
@@ -1021,7 +1021,7 @@
}
)
transaction = original_transaction.submit_for_settlement!
- transaction.status.should == "submitted_for_settlement"
+ transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
transaction.id.should == original_transaction.id
end
@@ -1149,8 +1149,8 @@
)
transaction.submit_for_settlement!
transaction.status_history.size.should == 2
- transaction.status_history[0].status.should == "authorized"
- transaction.status_history[1].status.should == "submitted_for_settlement"
+ transaction.status_history[0].status.should == Braintree::Transaction::Status::Authorized
+ transaction.status_history[1].status.should == Braintree::Transaction::Status::SubmittedForSettlement
end
end
@@ -1216,7 +1216,7 @@
)
result.success?.should == true
transaction = result.transaction
- transaction.status.should == "authorized"
+ transaction.status.should == Braintree::Transaction::Status::Authorized
void_result = transaction.void
void_result.success?.should == true
void_result.transaction.should == transaction
@@ -1231,7 +1231,7 @@
:expiration_date => "05/2009"
}
)
- transaction.status.should == "processor_declined"
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
result = transaction.void
result.success?.should == false
result.errors.for(:transaction).on(:base)[0].code.should == Braintree::ErrorCodes::Transaction::CannotBeVoided
@@ -1248,7 +1248,7 @@
}
)
transaction.void!.should == transaction
- transaction.status.should == "voided"
+ transaction.status.should == Braintree::Transaction::Status::Voided
end
it "raises a ValidationsFailed if unsuccessful" do
@@ -1259,7 +1259,7 @@
:expiration_date => "05/2009"
}
)
- transaction.status.should == "processor_declined"
+ transaction.status.should == Braintree::Transaction::Status::ProcessorDeclined
expect do
transaction.void!
end.to raise_error(Braintree::ValidationsFailed)
View
6 spec/unit/braintree/transaction_spec.rb
@@ -82,14 +82,14 @@
transaction = Braintree::Transaction._new(
:status_history => [
{ :timestamp => time, :amount => "12.00", :transaction_source => "API",
- :user => "larry", :status => "authorized" },
+ :user => "larry", :status => Braintree::Transaction::Status::Authorized },
{ :timestamp => Time.utc(2010,1,15), :amount => "12.00", :transaction_source => "API",
:user => "curly", :status => "scheduled_for_settlement"}
])
transaction.status_history.size.should == 2
transaction.status_history[0].user.should == "larry"
transaction.status_history[0].amount.should == "12.00"
- transaction.status_history[0].status.should == "authorized"
+ transaction.status_history[0].status.should == Braintree::Transaction::Status::Authorized
transaction.status_history[0].transaction_source.should == "API"
transaction.status_history[0].timestamp.should == time
transaction.status_history[1].user.should == "curly"
@@ -119,7 +119,7 @@
:id => "1234",
:type => "sale",
:amount => "100.00",
- :status => "authorized"
+ :status => Braintree::Transaction::Status::Authorized
)
output = transaction.inspect
output.should include(%Q(#<Braintree::Transaction id: "1234", type: "sale", amount: "100.0", status: "authorized"))
View
27 spec/unit/braintree/validation_error_collection_spec.rb
@@ -145,4 +145,31 @@
end
end
+ describe "shallow_errors" do
+ it "returns errors on one level" do
+ errors = Braintree::ValidationErrorCollection.new(
+ :errors => [
+ { :attribute => "one", :code => 1, :message => "bad juju" },
+ { :attribute => "two", :code => 2, :message => "bad juju" }],
+ :nested => {
+ :errors => [{ :attribute => "three", :code => 3, :message => "badder juju"}],
+ :nested_again => {
+ :errors => [{ :attribute => "four", :code => 4, :message => "badder juju"}]
+ }
+ }
+ )
+ errors.shallow_errors.map {|e| e.code}.should == [1, 2]
+ errors.for(:nested).shallow_errors.map {|e| e.code}.should == [3]
+ end
+
+ it "returns an clone of the real array" do
+ errors = Braintree::ValidationErrorCollection.new(
+ :errors => [
+ { :attribute => "one", :code => 1, :message => "bad juju" },
+ { :attribute => "two", :code => 2, :message => "bad juju" }]
+ )
+ errors.shallow_errors.pop
+ errors.shallow_errors.map {|e| e.code}.should == [1, 2]
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.