Skip to content

Commit

Permalink
2.81.0
Browse files Browse the repository at this point in the history
  • Loading branch information
braintreeps committed Jan 4, 2018
1 parent f1d264a commit 91f6faf
Show file tree
Hide file tree
Showing 13 changed files with 1,260 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.81.0
* Add support for line_items
* Update README to use instance methods

# 2.80.1
* Fix spec to expect PayPal transactions to move to settling rather than settled
* Fix permissions issue where SDK could not be loaded in some environments
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ gem 'braintree'
require "rubygems"
require "braintree"

Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = "your_merchant_id"
Braintree::Configuration.public_key = "your_public_key"
Braintree::Configuration.private_key = "your_private_key"
gateway = Braintree::Gateway.new(
:environment => :sandbox,
:merchant_id => "your_merchant_id",
:public_key => "your_public_key",
:private_key => "your_private_key",
)

result = Braintree::Transaction.sale(
result = gateway.transaction.sale(
:amount => "1000.00",
:payment_method_nonce => nonce_from_the_client,
:options => {
Expand All @@ -56,14 +58,14 @@ You retrieve your `merchant_id`, `public_key`, and `private_key` when [signing u

## Bang Methods

Most methods have a bang and a non-bang version (e.g. `Braintree::Customer.create` and `Braintree::Customer.create!`).
Most methods have a bang and a non-bang version (e.g. `gateway.customer.create` and `gateway.customer.create!`).
The non-bang version will either return a `SuccessfulResult` or an `ErrorResult`. The bang version will either return
the created or updated resource, or it will raise a `ValidationsFailed` exception.

Example of using non-bang method:

```ruby
result = Braintree::Customer.create(:first_name => "Josh")
result = gateway.customer.create(:first_name => "Josh")
if result.success?
puts "Created customer #{result.customer.id}"
else
Expand All @@ -78,7 +80,7 @@ Example of using bang method:

```ruby
begin
customer = Braintree::Customer.create!(:first_name => "Josh")
customer = gateway.customer.create!(:first_name => "Josh")
puts "Created customer #{customer.id}"
rescue Braintree::ValidationsFailed
puts "Validations failed"
Expand Down Expand Up @@ -106,7 +108,7 @@ To suppress logs from Braintree on environments where they are considered noise
```ruby
logger = Logger.new("/dev/null")
logger.level = Logger::INFO
Braintree::Configuration.logger = logger
gateway.config.logger = logger
```

## License
Expand Down
2 changes: 2 additions & 0 deletions lib/braintree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
require "braintree/test/transaction_amounts"
require "braintree/testing_gateway"
require "braintree/transaction"
require "braintree/transaction_line_item"
require "braintree/test_transaction"
require "braintree/transaction/address_details"
require "braintree/transaction/apple_pay_details"
Expand All @@ -133,6 +134,7 @@
require "braintree/transaction/paypal_details"
require "braintree/transaction/subscription_details"
require "braintree/transaction_gateway"
require "braintree/transaction_line_item_gateway"
require "braintree/transaction_search"
require "braintree/transaction/status_details"
require "braintree/transaction/venmo_account_details"
Expand Down
30 changes: 30 additions & 0 deletions lib/braintree/error_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ module Transaction
FinalAuthSubmitForSettlementForDifferentAmount = "95601"
HasAlreadyBeenRefunded = "91512"
IdealPaymentNotComplete = "815141"
TooManyLineItems = "915157"
LineItemsExpected = "915158"
DiscountAmountFormatIsInvalid = "915159"
DiscountAmountCannotBeNegative = "915160"
DiscountAmountIsTooLarge = "915161"
Expand Down Expand Up @@ -418,6 +420,34 @@ module TravelCruise
end
end

module TransactionLineItem
CommodityCodeIsTooLong = "95801"
DescriptionIsTooLong = "95803"
DiscountAmountFormatIsInvalid = "95804"
DiscountAmountIsTooLarge = "95805"
DiscountAmountMustBeGreaterThanZero = "95806"
KindIsInvalid = "95807"
KindIsRequired = "95808"
NameIsRequired = "95822"
NameIsTooLong = "95823"
ProductCodeIsTooLong = "95809"
QuantityFormatIsInvalid = "95810"
QuantityIsRequired = "95811"
QuantityIsTooLarge = "95812"
TotalAmountFormatIsInvalid = "95813"
TotalAmountIsRequired = "95814"
TotalAmountIsTooLarge = "95815"
TotalAmountMustBeGreaterThanZero = "95816"
UnitAmountFormatIsInvalid = "95817"
UnitAmountIsRequired = "95818"
UnitAmountIsTooLarge = "95819"
UnitAmountMustBeGreaterThanZero = "95820"
UnitOfMeasureIsTooLong = "95821"
UnitTaxAmountFormatIsInvalid = "95824"
UnitTaxAmountIsTooLarge = "95825"
UnitTaxAmountMustBeGreaterThanZero = "95826"
end

module Merchant
CountryCannotBeBlank = "83603"
CountryCodeAlpha2IsInvalid = "93607"
Expand Down
4 changes: 4 additions & 0 deletions lib/braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def transaction
TransactionGateway.new(self)
end

def transaction_line_item
TransactionLineItemGateway.new(self)
end

def testing
TestingGateway.new(self)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/braintree/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ def self.find(id)
Configuration.gateway.transaction.find(id)
end

def self.line_items(id)
Configuration.gateway.transaction_line_item.find_all(id)
end

def self.hold_in_escrow(id)
Configuration.gateway.transaction.hold_in_escrow(id)
end
Expand Down Expand Up @@ -301,6 +305,10 @@ def inspect # :nodoc:
"#<#{self.class} #{nice_attributes.join(', ')}>"
end

def line_items
@gateway.transaction_line_item.find_all(id)
end

# Deprecated. Use Braintree::Transaction.refund
def refund(amount = nil)
warn "[DEPRECATED] refund as an instance method is deprecated. Please use Transaction.refund"
Expand Down
1 change: 1 addition & 0 deletions lib/braintree/transaction_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def self._create_signature # :nodoc:
:shipping_amount, :discount_amount, :ships_from_postal_code,
:billing_address_id, :payment_method_nonce, :three_d_secure_token,
:shared_payment_method_token, :shared_billing_address_id, :shared_customer_id, :shared_shipping_address_id, :shared_payment_method_nonce,
{:line_items => [:quantity, :name, :description, :kind, :unit_amount, :unit_tax_amount, :total_amount, :discount_amount, :unit_of_measure, :product_code, :commodity_code, :url]},
{:risk_data => [:customer_browser, :customer_ip]},
{:credit_card => [:token, :cardholder_name, :cvv, :expiration_date, :expiration_month, :expiration_year, :number]},
{:customer => [:id, :company, :email, :fax, :first_name, :last_name, :phone, :website]},
Expand Down
34 changes: 34 additions & 0 deletions lib/braintree/transaction_line_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Braintree
class TransactionLineItem
include BaseModule
attr_reader :quantity
attr_reader :name
attr_reader :description
attr_reader :kind
attr_reader :unit_amount
attr_reader :unit_tax_amount
attr_reader :unit_of_measure
attr_reader :discount_amount
attr_reader :total_amount
attr_reader :product_code
attr_reader :commodity_code
attr_reader :url

def initialize(gateway, attributes) # :nodoc:
@gateway = gateway
set_instance_variables_from_hash(attributes)
@quantity = Util.to_big_decimal(quantity)
@unit_amount = Util.to_big_decimal(unit_amount)
@unit_tax_amount = Util.to_big_decimal(unit_tax_amount)
@discount_amount = Util.to_big_decimal(discount_amount)
@total_amount = Util.to_big_decimal(total_amount)
end

class << self
protected :new
def _new(*args) # :nodoc:
self.new *args
end
end
end
end
19 changes: 19 additions & 0 deletions lib/braintree/transaction_line_item_gateway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Braintree
class TransactionLineItemGateway # :nodoc:
def initialize(gateway)
@gateway = gateway
@config = gateway.config
@config.assert_has_access_token_or_keys
end

def find_all(transaction_id)
raise ArgumentError, "transaction_id cannot be blank" if transaction_id.nil? || transaction_id.strip.to_s == ""
response = @config.http.get("#{@config.base_merchant_path}/transactions/#{transaction_id}/line_items")
response[:line_items].map do |line_item_params|
TransactionLineItem._new(@gateway, line_item_params)
end
rescue NotFoundError
raise NotFoundError, "transaction with id #{transaction_id.inspect} not found"
end
end
end
4 changes: 2 additions & 2 deletions lib/braintree/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Braintree
module Version
Major = 2
Minor = 80
Tiny = 1
Minor = 81
Tiny = 0

String = "#{Major}.#{Minor}.#{Tiny}"
end
Expand Down
33 changes: 2 additions & 31 deletions spec/integration/braintree/apple_pay_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,57 +36,28 @@
describe "unregister_domain" do
it "unregisters an apple pay domain" do
domain = "example.org"
result = @gateway.apple_pay.register_domain(domain)
result.should be_success

result = @gateway.apple_pay.unregister_domain(domain)
result.should be_success
@gateway.apple_pay.registered_domains.apple_pay_options.domains.should be_empty
end

it "unregisters an apple pay domain with scheme in url" do
domain = "http://example.org"
result = @gateway.apple_pay.register_domain(domain)
result.should be_success

result = @gateway.apple_pay.unregister_domain(domain)
result.should be_success
@gateway.apple_pay.registered_domains.apple_pay_options.domains.should be_empty
end

it "does not fail when unregistering a non-registered domain" do
result = @gateway.apple_pay.unregister_domain("unregistered.com")
result.should be_success
end

it "escapes the unregistered domain query parameter" do
domain = "ex&mple.org"
result = @gateway.apple_pay.register_domain(domain)
result.should be_success
@gateway.apple_pay.registered_domains.apple_pay_options.domains.should_not be_empty

result = @gateway.apple_pay.unregister_domain(domain)
result.should be_success
@gateway.apple_pay.registered_domains.apple_pay_options.domains.should be_empty
end
end

describe "registered_domains" do
it "returns registered domains" do
result = @gateway.apple_pay.register_domain("www.example.com")
result.should be_success
result = @gateway.apple_pay.register_domain("www.example.org")
result.should be_success

result = @gateway.apple_pay.registered_domains
result.should be_success
result.apple_pay_options.domains.should =~ ["www.example.org", "www.example.com"]
end

it "returns an empty list if no domains are registered" do
it "returns stubbed registered domains" do
result = @gateway.apple_pay.registered_domains
result.should be_success
result.apple_pay_options.domains.should == []
result.apple_pay_options.domains.should == ["www.example.com"]
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/braintree/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
it "raises an AuthorizationError if authorization fails" do
expect do
config = Braintree::Configuration.instantiate
config.http.get("#{config.base_merchant_path}/users")
config.http.get("#{config.base_merchant_path}/downloads")
end.to raise_error(Braintree::AuthorizationError)
end
end
Expand Down
Loading

0 comments on commit 91f6faf

Please sign in to comment.