braintree-rails 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57414c4ea8df5541d2cd778d119faf881ba79701
4
- data.tar.gz: 0db1cce99d56b85da28f190701fdf0f01203ef55
3
+ metadata.gz: 56202c668de2552e6a850a95602e323bfebff239
4
+ data.tar.gz: 1c0f8bf6594cbfb8550d56cbd7be0589af412694
5
5
  SHA512:
6
- metadata.gz: a6fb3ebeca4008a5ab5262cf3ac6840537c27747099e7b1bbf6acde6b910d1e28d31903459f876cb95a95fab86ace1efe11a303feb62ad2333d8a4ffe5d7784c
7
- data.tar.gz: a667b40488b6593ee9ff5875ebf65b61ef465a7e96c980152ddc01fbbac915dfe3501f4c97e023e0da87b1c299451b762ce0eb5cc9076a9231472d5ba3423f39
6
+ metadata.gz: b11a071290144088a5e2c068957fdfcebace2b616c2e7801735843c324ae43d64f2534100918833233626aa834e003e91451dd12eb14d6f596e9c774ef2b5917
7
+ data.tar.gz: a5f3167d16569e8127f16c9188b212aa7114a831668edd34b164c54b65f0fcf2c7b7eaeb138371da7948bee333fd0a873a80318535b63b2a58450aed39c91978
@@ -4,4 +4,3 @@ rvm:
4
4
  - "1.9.3"
5
5
  - "2.0.0"
6
6
  - "jruby-19mode"
7
- - "rbx-19mode"
@@ -1,4 +1,13 @@
1
1
  ## Unreleased (master)
2
+ ### Enhancements
3
+ * Customer create/update now accepts an optional :credit_card params.
4
+
5
+ ### Bug Fixes
6
+ * Fixed a bug where it failed to update credit card expiry date when only expiration year changed. (Thanks, @vedanova)
7
+
8
+ ## v1.2.2 (370fab4), Oct 14 2013
9
+ ### Bug Fixes
10
+ * Fixed gemspec to only package necessary files to reduce the gem file size. (Thanks, @ivankocienski)
2
11
 
3
12
  ## v1.2.1 (370fab4), Jul 01 2013
4
13
  ### Enhancements
@@ -27,11 +27,11 @@ module BraintreeRails
27
27
 
28
28
  def define_association_reader(name, options)
29
29
  define_method(name) do
30
- value = instance_variable_get("@#{name}")
31
- return value if value.present?
32
- value = self.send(options[:foreign_key])
33
- value &&= options[:class].new(value)
34
- instance_variable_set("@#{name}", value)
30
+ if value = instance_variable_get("@#{name}")
31
+ return value
32
+ elsif options[:foreign_key] && value = send(options[:foreign_key])
33
+ instance_variable_set("@#{name}", options[:class].new(value))
34
+ end
35
35
  end
36
36
  end
37
37
 
@@ -14,11 +14,13 @@ module BraintreeRails
14
14
  has_many :transactions, :class => Transactions
15
15
  has_many :subscriptions, :class => Subscriptions
16
16
  belongs_to :customer, :class => Customer, :foreign_key => :customer_id
17
+ has_one :billing_address, :class => BillingAddress
17
18
 
18
19
  alias_method :id, :token
19
20
  alias_method :id=, :token=
20
21
 
21
22
  around_persist :clear_encryped_attributes
23
+ before_update :normalize_expiration_date, :if => :expiry_date_changed?
22
24
 
23
25
  def ensure_model(model)
24
26
  if Braintree::Transaction::CreditCardDetails === model
@@ -42,10 +44,6 @@ module BraintreeRails
42
44
  "#{bin}******#{last_4}"
43
45
  end
44
46
 
45
- def billing_address=(value)
46
- @billing_address = value && BillingAddress.new(value)
47
- end
48
-
49
47
  def add_errors(validation_errors)
50
48
  billing_address.add_errors(validation_errors.except(:base)) if billing_address
51
49
  super(validation_errors)
@@ -67,5 +65,14 @@ module BraintreeRails
67
65
  self.send(encrypted_attribute, nil)
68
66
  end
69
67
  end
68
+
69
+ def normalize_expiration_date
70
+ self.expiration_date = [self.expiration_month, self.expiration_year].join("/")
71
+ self.expiration_month = self.expiration_year = nil
72
+ end
73
+
74
+ def expiry_date_changed?
75
+ changed.include?(:expiration_month) || changed.include?(:expiration_year)
76
+ end
70
77
  end
71
78
  end
@@ -15,14 +15,14 @@ module BraintreeRails
15
15
  ]
16
16
 
17
17
  def validate(credit_card)
18
- have_valid_billing_address(credit_card) if validate_billing_address?
18
+ has_valid_billing_address(credit_card) if validate_billing_address?
19
19
  end
20
20
 
21
- def have_valid_billing_address(credit_card)
21
+ def has_valid_billing_address(credit_card)
22
22
  credit_card.instance_eval do
23
23
  errors.add(:billing_address, "is empty") and return if billing_address.blank?
24
24
  if billing_address.invalid?
25
- errors.add(:billing_address, "is not valid")
25
+ errors.add(:billing_address, "is invalid")
26
26
  billing_address.errors.full_messages.each do |message|
27
27
  errors.add(:base, message)
28
28
  end
@@ -3,8 +3,8 @@ module BraintreeRails
3
3
  include Model
4
4
 
5
5
  define_attributes(
6
- :create => [:company, :custom_fields, :email, :fax, :first_name, :id, :last_name, :options, :phone, :website],
7
- :update => [:company, :custom_fields, :email, :fax, :first_name, :last_name, :options, :phone, :website],
6
+ :create => [:company, :custom_fields, :email, :fax, :first_name, :id, :last_name, :options, :phone, :website, :credit_card],
7
+ :update => [:company, :custom_fields, :email, :fax, :first_name, :last_name, :options, :phone, :website, :credit_card],
8
8
  :readonly => [:created_at, :updated_at],
9
9
  :as_association => [:id, :company, :email, :fax, :first_name, :last_name, :phone, :website]
10
10
  )
@@ -12,6 +12,7 @@ module BraintreeRails
12
12
  has_many :addresses, :class => Addresses
13
13
  has_many :transactions, :class => Transactions
14
14
  has_many :credit_cards, :class => CreditCards
15
+ has_one :credit_card, :class => CreditCard
15
16
 
16
17
  def ensure_model(model)
17
18
  if Braintree::Transaction::CustomerDetails === model
@@ -27,8 +28,21 @@ module BraintreeRails
27
28
  "#{first_name} #{last_name}".strip
28
29
  end
29
30
 
31
+ def add_errors(validation_errors)
32
+ credit_card.add_errors(validation_errors.except(:base)) if credit_card
33
+ super(validation_errors)
34
+ end
35
+
36
+ def attributes_for(action)
37
+ super.merge(credit_card_attributes(action))
38
+ end
39
+
30
40
  def default_credit_card
31
41
  credit_cards.find(&:default?)
32
42
  end
43
+
44
+ def credit_card_attributes(action)
45
+ credit_card.present? ? {:credit_card => credit_card.attributes_for(action).except(:customer_id, :token)} : {}
46
+ end
33
47
  end
34
48
  end
@@ -4,5 +4,20 @@ module BraintreeRails
4
4
  [:id, :format => {:with => /\A[-_a-z0-9]*\z/i}, :length => {:maximum => 36}, :exclusion => {:in => %w(all new)}],
5
5
  [:first_name, :last_name, :company, :website, :phone, :fax, :length => {:maximum => 255}]
6
6
  ]
7
+
8
+ def validate(customer)
9
+ validate_credit_card(customer) if customer.credit_card.present?
10
+ end
11
+
12
+ def validate_credit_card(customer)
13
+ customer.instance_eval do
14
+ if credit_card.invalid?
15
+ errors.add(:credit_card, "is invalid")
16
+ credit_card.errors.full_messages.each do |message|
17
+ errors.add(:base, message)
18
+ end
19
+ end
20
+ end
21
+ end
7
22
  end
8
23
  end
@@ -1,3 +1,3 @@
1
1
  module BraintreeRails
2
- Version = "1.2.2"
2
+ Version = "1.2.3"
3
3
  end
@@ -46,6 +46,31 @@ describe 'Credit Card Integration' do
46
46
  braintree_credit_card.billing_address.postal_code.must_equal '56789'
47
47
  end
48
48
 
49
+ it 'should be able to update just expiration year' do
50
+ customer = BraintreeRails::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree')
51
+ credit_card = customer.credit_cards.create!(credit_card_hash.merge(:expiration_month => '07', :expiration_year => '2012'))
52
+
53
+ credit_card.update_attributes!(:cardholder_name => 'Foo Bar', :number => '4111111111111111', :options => {:verify_card => true}, :expiration_month => '07', :expiration_year => '2013', :billing_address => address_hash.merge(:postal_code => '56789'))
54
+ braintree_credit_card = Braintree::CreditCard.find(credit_card.id)
55
+ braintree_credit_card.cardholder_name.must_equal 'Foo Bar'
56
+ braintree_credit_card.billing_address.postal_code.must_equal '56789'
57
+ braintree_credit_card.expiration_month.must_equal '07'
58
+ braintree_credit_card.expiration_year.must_equal '2013'
59
+ end
60
+
61
+ it 'should be able to update by expiration date' do
62
+ customer = BraintreeRails::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree')
63
+ credit_card = customer.credit_cards.create!(credit_card_hash.merge(:expiration_month => '07', :expiration_year => '2012'))
64
+
65
+ credit_card.update_attributes!(:cardholder_name => 'Foo Bar', :number => '4111111111111111', :options => {:verify_card => true}, :expiration_date => '08/2013', :billing_address => address_hash.merge(:postal_code => '56789'))
66
+ braintree_credit_card = Braintree::CreditCard.find(credit_card.id)
67
+ braintree_credit_card.cardholder_name.must_equal 'Foo Bar'
68
+ braintree_credit_card.billing_address.postal_code.must_equal '56789'
69
+ braintree_credit_card.expiration_month.must_equal '08'
70
+ braintree_credit_card.expiration_year.must_equal '2013'
71
+ braintree_credit_card.expiration_date.must_equal '08/2013'
72
+ end
73
+
49
74
  it 'should be able to destroy existing credit card' do
50
75
  braintree_customer = Braintree::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree', :credit_card => credit_card_hash)
51
76
  credit_card = BraintreeRails::CreditCard.new(braintree_customer.credit_cards.first.token)
@@ -22,6 +22,12 @@ describe 'Customer Integration' do
22
22
  braintree_customer.last_name.must_equal 'Tree'
23
23
  end
24
24
 
25
+ it 'should be able to create new customer with a credit card' do
26
+ customer = BraintreeRails::Customer.create(:first_name => 'Brain', :last_name => 'Tree', :credit_card => credit_card_hash)
27
+ braintree_customer = Braintree::Customer.find(customer.id)
28
+ braintree_customer.credit_cards.count.must_equal 1
29
+ end
30
+
25
31
  it 'should be able to update existing customer' do
26
32
  customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
27
33
  customer.update_attributes!(:first_name => 'Foo', :last_name => 'Bar')
@@ -31,6 +37,14 @@ describe 'Customer Integration' do
31
37
  braintree_customer.last_name.must_equal 'Bar'
32
38
  end
33
39
 
40
+ it 'should be able to update existing customer with new credit card' do
41
+ customer = BraintreeRails::Customer.create(:first_name => 'Brain', :last_name => 'Tree', :credit_card => credit_card_hash)
42
+ customer.update_attributes!(:first_name => 'Foo', :last_name => 'Bar', :credit_card => credit_card_hash.merge(:cardholder_name => "FooBar"))
43
+
44
+ braintree_customer = Braintree::Customer.find(customer.id)
45
+ braintree_customer.credit_cards.first.cardholder_name.must_equal "FooBar"
46
+ end
47
+
34
48
  it 'should be able to destroy existing customer' do
35
49
  customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
36
50
  customer.destroy
@@ -123,6 +123,18 @@ describe BraintreeRails::Customer do
123
123
  customer.errors[attribute].wont_be :blank?
124
124
  end
125
125
  end
126
+
127
+ describe 'credit_card' do
128
+ it 'is valid if new credit card is valid' do
129
+ customer = BraintreeRails::Customer.new(:credit_card => credit_card_hash)
130
+ customer.valid?.must_equal true
131
+ end
132
+
133
+ it 'is not valid if new credit card is invalid' do
134
+ customer = BraintreeRails::Customer.new(:credit_card => credit_card_hash.except(:number))
135
+ customer.valid?.must_equal false
136
+ end
137
+ end
126
138
  end
127
139
 
128
140
  describe 'persistence' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braintree-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Yang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-14 00:00:00.000000000 Z
11
+ date: 2014-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: braintree