Searching for Transactions
If you want to look up a single transaction using its ID, use the find method.
transaction = Braintree::Transaction.find("the_transaction_token")
Otherwise, you can search for transactions using a variety of criteria. For operators available on search fields, see the search fields page. For conciseness, we’re going to show searching on several fields at once, but you can search on as many or as few as you would like in a single search.
Credit Card
search_results = Braintree::Transaction.search do |search|
search.credit_card_cardholder_name.is "Patrick Smith"
search.credit_card_expiration_date.is "05/2012"
search.credit_card_number.is "5105105105105100"
end
Searching on credit card number has a few restrictions. If you search using “starts with” you can only enter up to the first 6 digits. If you search using “ends with” you can only enter the last 4 digits. And you can’t search on “contains.”
Expiration date also has restrictions. “is” and “is not” work, while “starts with,” “ends with,” and “contains” do not.
search_results = Braintree::Transaction.search do |search|
search.credit_card_number.starts_with "510510"
end
search_results = Braintree::Transaction.search do |search|
search.credit_card_number.ends_with "5100"
end
Customer
search_results = Braintree::Transaction.search do |search|
search.customer_company.is "Braintree"
search.customer_email.is "smith@example.com"
search.customer_fax.is "5551231234"
search.customer_first_name.is "Tom"
search.customer_last_name.is "Smith"
search.customer_phone.is "5551231234"
search.customer_website.is "http://example.com"
end
See search fields for a list of available operators. They allow you to do nice things like this:
search_results = Braintree::Transaction.search do |search|
search.customer_email.ends_with "example.com"
end
Billing Address
search_results = Braintree::Transaction.search do |search|
search.billing_first_name.is "Paul"
search.billing_last_name.is "Smith"
search.billing_company.is "Braintree"
search.billing_street_address.is "123 Main St"
search.billing_extended_address.is "Suite 123"
search.billing_locality.is "Chicago"
search.billing_region.is "IL"
search.billing_postal_code.is "12345"
search.billing_country_name.is "United States of America"
end
Shipping Address
search_results = Braintree::Transaction.search do |search|
search.shipping_first_name.is "Paul"
search.shipping_last_name.is "Smith"
search.shipping_company.is "Braintree"
search.shipping_street_address.is "123 Main St"
search.shipping_extended_address.is "Suite 123"
search.shipping_locality.is "Chicago"
search.shipping_region.is "IL"
search.shipping_postal_code.is "12345"
search.shipping_country_name.is "United States of America"
end
Other Top-level Attributes
search_results = Braintree::Transaction.search do |search|
search.order_id.is "myorder"
search.processor_authorization_code.is "123456"
end
Vault Associations
You can search for transactions associated to a payment method token.
search_results = Braintree::Transaction.search do |search|
search.payment_method_token.is "the_token"
end
Or a customer ID.
search_results = Braintree::Transaction.search do |search|
search.customer_id.is "the_customer_id"
end
How the transaction was created
You can search for transactions that were created using a token.
search_results = Braintree::Transaction.search do |search|
search.created_using.is Braintree::Transaction::CreatedUsing::Token
end
Or transactions that were created using full credit card information.
search_results = Braintree::Transaction.search do |search|
search.created_using.is Braintree::Transaction::CreatedUsing::FullInformation
end
Those are the only two choices for created_using.
Customer Location
Customers in the US.
search_results = Braintree::Transaction.search do |search|
search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::US
end
Or international customers.
search_results = Braintree::Transaction.search do |search|
search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::International
end
Merchant Account
A specific one.
search_results = Braintree::Transaction.search do |search|
search.merchant_account_id.is "my_merchant_account"
end
Or any of several.
search_results = Braintree::Transaction.search do |search|
search.merchant_account_id.in "account_1", "account_2"
end
Credit Card Type
A specific one.
search_results = Braintree::Transaction.search do |search|
search.credit_card_card_type.is Braintree::CreditCard::CardType::Visa
end
Or any of several.
search_results = Braintree::Transaction.search do |search|
search.credit_card_card_type.in(
Braintree::CreditCard::CardType::Visa,
Braintree::CreditCard::CardType::MasterCard
)
end
Transaction Status
Another one or many search field.
search_results = Braintree::Transaction.search do |search|
search.status.in(
Braintree::Transaction::Status::ProcessorDeclined,
Braintree::Transaction::Status::GatewayRejected
)
end
Transaction Source
API, Control Panel, or Recurring Billing.
search_results = Braintree::Transaction.search do |search|
search.source.in(
Braintree::Transaction::Source::Api,
Braintree::Transaction::Source::ControlPanel,
Braintree::Transaction::Source::Recurring
)
end
Transaction Type
The two types of transactions are sale and credit. Refunds are a special kind of credit, so there are some extra options around them.
search_results = Braintree::Transaction.search do |search|
search.type.is Braintree::Transaction::Type::Sale
end
This will return all credits, regardless of whether they’re refunds, or standalone credits.
search_results = Braintree::Transaction.search do |search|
search.type.is Braintree::Transaction::Type::Credit
end
If you only want the refunds:
search_results = Braintree::Transaction.search do |search|
search.type.is Braintree::Transaction::Type::Credit
search.refund.is true
end
And if you only want the credits that aren’t refunds:
search_results = Braintree::Transaction.search do |search|
search.type.is Braintree::Transaction::Type::Credit
search.refund.is false
end
Amount
It’s a range field.
search_results = Braintree::Transaction.search do |search|
search.amount.between "100.00", "200.00"
end
search_results = Braintree::Transaction.search do |search|
search.amount >= "100.00"
end
search_results = Braintree::Transaction.search do |search|
search.amount <= "100.00"
end
Status Changes
You can search for transactions that entered a given status at a certain time. For instance, you can find all transactions which were submitted for settlement in the past 3 days.
You can search on these statuses:
created_atauthorized_atsubmitted_for_settlement_atsettled_atvoided_atprocessor_declined_atgateway_rejected_atfailed_at
A few examples:
search_results = Braintree::Transaction.search do |search|
search.created_at >= Time.now - 60*60*24 # a day ago
end
search_results = Braintree::Transaction.search do |search|
search.settled_at >= Time.now - 60*60*24 # a day ago
end
search_results = Braintree::Transaction.search do |search|
search.processor_declined_at >= Time.now - 60*60*24 # a day ago
end
Note: Timezones specified in the time value will be respected in the search.
Results will always be returned with time values in UTC.