braintree-rails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. data/Gemfile +10 -0
  2. data/Gemfile.lock +34 -0
  3. data/README.md +2 -0
  4. data/Rakefile +4 -0
  5. data/braintree-rails.gemspec +18 -0
  6. data/lib/braintree-rails.rb +17 -0
  7. data/lib/braintree_rails/address.rb +75 -0
  8. data/lib/braintree_rails/addresses.rb +24 -0
  9. data/lib/braintree_rails/credit_card.rb +67 -0
  10. data/lib/braintree_rails/credit_cards.rb +24 -0
  11. data/lib/braintree_rails/customer.rb +37 -0
  12. data/lib/braintree_rails/exceptions.rb +2 -0
  13. data/lib/braintree_rails/model.rb +192 -0
  14. data/lib/env.rb +3 -0
  15. data/lib/tasks/test.rake +18 -0
  16. data/lib/test_env.rb +4 -0
  17. data/log/braintree_test.log +225696 -0
  18. data/test/config/braintree_auth.yml +4 -0
  19. data/test/config/braintree_auth.yml.example +4 -0
  20. data/test/fixtures/address.xml +19 -0
  21. data/test/fixtures/credit_card.xml +36 -0
  22. data/test/fixtures/credit_card_validation_error.xml +26 -0
  23. data/test/fixtures/customer.xml +90 -0
  24. data/test/integration/braintree_rails/address_integration_test.rb +72 -0
  25. data/test/integration/braintree_rails/credit_card_integration_test.rb +147 -0
  26. data/test/integration/braintree_rails/customer_integration_test.rb +41 -0
  27. data/test/integration/integration_test_helper.rb +13 -0
  28. data/test/test_helper.rb +28 -0
  29. data/test/unit/braintree_rails/address_test.rb +87 -0
  30. data/test/unit/braintree_rails/addresses_test.rb +38 -0
  31. data/test/unit/braintree_rails/credit_card_test.rb +240 -0
  32. data/test/unit/braintree_rails/credit_cards_test.rb +38 -0
  33. data/test/unit/braintree_rails/customer_test.rb +171 -0
  34. data/test/unit/unit_test_helper.rb +3 -0
  35. metadata +176 -0
@@ -0,0 +1,4 @@
1
+ ---
2
+ merchant_id: 'j5n2ccyp6jt3kj35'
3
+ public_key: 'grm486rvv2b3d56h'
4
+ private_key: 'f95ba448d5cc43c944cb61de408c6f6c'
@@ -0,0 +1,4 @@
1
+ ---
2
+ merchant_id: 'merchant_id'
3
+ public_key: 'public_key'
4
+ private_key: 'private_key'
@@ -0,0 +1,19 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <address>
3
+ <id>address_id</id>
4
+ <customer-id>8404776</customer-id>
5
+ <first-name>Brain</first-name>
6
+ <last-name>Tree</last-name>
7
+ <company>Braintree</company>
8
+ <street-address>1134 Crane Avenue</street-address>
9
+ <extended-address>Suite 200</extended-address>
10
+ <locality>Menlo Park</locality>
11
+ <region>California</region>
12
+ <postal-code>94025</postal-code>
13
+ <country-code-alpha2>US</country-code-alpha2>
14
+ <country-code-alpha3>USA</country-code-alpha3>
15
+ <country-code-numeric>840</country-code-numeric>
16
+ <country-name>United States of America</country-name>
17
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
18
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
19
+ </address>
@@ -0,0 +1,36 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <credit-card>
3
+ <token>credit_card_id</token>
4
+ <unique-number-identifier>fe41071c3dc6a91f1dc0a95e64ff98d3</unique-number-identifier>
5
+ <customer-id>customer_id</customer-id>
6
+ <cardholder-name>Brain Tree</cardholder-name>
7
+ <customer-location>US</customer-location>
8
+ <card-type>MasterCard</card-type>
9
+ <bin>555555</bin>
10
+ <last-4>4444</last-4>
11
+ <expiration-month>05</expiration-month>
12
+ <expiration-year>2037</expiration-year>
13
+ <default type="boolean">true</default>
14
+ <expired type="boolean">false</expired>
15
+ <subscriptions type="array"/>
16
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
17
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
18
+ <billing-address>
19
+ <id>address_id</id>
20
+ <customer-id>8404776</customer-id>
21
+ <first-name>Brain</first-name>
22
+ <last-name>Tree</last-name>
23
+ <company>Braintree</company>
24
+ <street-address>1134 Crane Avenue</street-address>
25
+ <extended-address>Suite 200</extended-address>
26
+ <locality>Menlo Park</locality>
27
+ <region>California</region>
28
+ <postal-code>94025</postal-code>
29
+ <country-code-alpha2>US</country-code-alpha2>
30
+ <country-code-alpha3>USA</country-code-alpha3>
31
+ <country-code-numeric>840</country-code-numeric>
32
+ <country-name>United States of America</country-name>
33
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
34
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
35
+ </billing-address>
36
+ </credit-card>
@@ -0,0 +1,26 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <api-error-response>
3
+ <message>Credit card number is invalid.</message>
4
+ <errors>
5
+ <errors type="array"/>
6
+ <credit-card>
7
+ <errors type="array">
8
+ <error>
9
+ <code>81715</code>
10
+ <message>Credit card number is invalid.</message>
11
+ <attribute type="symbol">number</attribute>
12
+ </error>
13
+ </errors>
14
+ </credit-card>
15
+ </errors>
16
+ <params>
17
+ <merchant-id>merchant_id</merchant-id>
18
+ <action>update</action>
19
+ <credit-card>
20
+ <cardholder-name>Brain Tree</cardholder-name>
21
+ <customer-id>customer_id</customer-id>
22
+ <expiration-date>05/2037</expiration-date>
23
+ </credit-card>
24
+ <controller>payment_methods</controller>
25
+ </params>
26
+ </api-error-response>
@@ -0,0 +1,90 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <customer>
3
+ <id>customer_id</id>
4
+ <merchant-id>merchant_id</merchant-id>
5
+ <first-name>Brain</first-name>
6
+ <last-name>Tree</last-name>
7
+ <company>Braintree</company>
8
+ <email>braintree@example.com</email>
9
+ <phone>877.434.2894</phone>
10
+ <fax>888.781.6495</fax>
11
+ <website>www.braintreepayments.com</website>
12
+ <created-at type="datetime">2012-09-10T06:03:00Z</created-at>
13
+ <updated-at type="datetime">2012-09-10T06:03:00Z</updated-at>
14
+ <custom-fields>
15
+ </custom-fields>
16
+ <credit-cards type="array">
17
+ <credit-card>
18
+ <token>credit_card_id</token>
19
+ <unique-number-identifier>fe41071c3dc6a91f1dc0a95e64ff98d3</unique-number-identifier>
20
+ <customer-id>customer_id</customer-id>
21
+ <cardholder-name>Brain Tree</cardholder-name>
22
+ <customer-location>US</customer-location>
23
+ <card-type>MasterCard</card-type>
24
+ <bin>555555</bin>
25
+ <last-4>4444</last-4>
26
+ <expiration-month>05</expiration-month>
27
+ <expiration-year>2037</expiration-year>
28
+ <default type="boolean">true</default>
29
+ <expired type="boolean">false</expired>
30
+ <subscriptions type="array"/>
31
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
32
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
33
+ </credit-card>
34
+ <credit-card>
35
+ <token>fydb</token>
36
+ <unique-number-identifier>112489a39c1a16fa5ca277543731ec99</unique-number-identifier>
37
+ <customer-id>customer_id</customer-id>
38
+ <cardholder-name>Brain Tree</cardholder-name>
39
+ <customer-location>US</customer-location>
40
+ <card-type>Visa</card-type>
41
+ <bin>411111</bin>
42
+ <last-4>1111</last-4>
43
+ <expiration-month>05</expiration-month>
44
+ <expiration-year>2037</expiration-year>
45
+ <default type="boolean">false</default>
46
+ <expired type="boolean">false</expired>
47
+ <subscriptions type="array"/>
48
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
49
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
50
+ </credit-card>
51
+ </credit-cards>
52
+ <addresses type="array">
53
+ <address>
54
+ <id>wx</id>
55
+ <customer-id>customer_id</customer-id>
56
+ <first-name>Brain</first-name>
57
+ <last-name>Tree</last-name>
58
+ <company>Braintree</company>
59
+ <street-address>1134 Crane Avenue</street-address>
60
+ <extended-address>Suite 200</extended-address>
61
+ <locality>Menlo Park</locality>
62
+ <region>California</region>
63
+ <postal-code>94025</postal-code>
64
+ <country-code-alpha2>US</country-code-alpha2>
65
+ <country-code-alpha3>USA</country-code-alpha3>
66
+ <country-code-numeric>840</country-code-numeric>
67
+ <country-name>United States of America</country-name>
68
+ <created-at type="datetime">2012-09-11T06:05:30Z</created-at>
69
+ <updated-at type="datetime">2012-09-11T06:05:30Z</updated-at>
70
+ </address>
71
+ <address>
72
+ <id>cy</id>
73
+ <customer-id>customer_id</customer-id>
74
+ <first-name>Brain</first-name>
75
+ <last-name>Tree</last-name>
76
+ <company>Braintree</company>
77
+ <street-address>111 N Canal St</street-address>
78
+ <extended-address>Suite 455</extended-address>
79
+ <locality>Chicago</locality>
80
+ <region>Illinois</region>
81
+ <postal-code>60606</postal-code>
82
+ <country-code-alpha2>US</country-code-alpha2>
83
+ <country-code-alpha3>USA</country-code-alpha3>
84
+ <country-code-numeric>840</country-code-numeric>
85
+ <country-name>United States of America</country-name>
86
+ <created-at type="datetime">2012-09-11T06:05:38Z</created-at>
87
+ <updated-at type="datetime">2012-09-11T06:05:38Z</updated-at>
88
+ </address>
89
+ </addresses>
90
+ </customer>
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../integration_test_helper'))
2
+
3
+ describe 'Address Integration' do
4
+ before do
5
+ Braintree::Customer.all.each { |c| Braintree::Customer.delete c.id }
6
+ end
7
+
8
+ it 'should be able to add address' do
9
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
10
+ address_hash = {
11
+ :first_name => 'Brain',
12
+ :last_name => 'Tree',
13
+ :company => 'Braintree',
14
+ :street_address => '1134 Crane Avenue',
15
+ :extended_address => 'Suite 200',
16
+ :locality => 'Menlo Park',
17
+ :region => 'California',
18
+ :postal_code => '94025',
19
+ :country_name => 'United States of America'
20
+ }
21
+ customer.addresses.create!(address_hash)
22
+ braintree_customer = Braintree::Customer.find(customer.id)
23
+ braintree_address = braintree_customer.addresses.first
24
+
25
+ address_hash.each do |key, value|
26
+ braintree_address.send(key).must_equal value
27
+ end
28
+ end
29
+
30
+ it 'should be able to update existing address' do
31
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
32
+ address_hash = {
33
+ :first_name => 'Brain',
34
+ :last_name => 'Tree',
35
+ :company => 'Braintree',
36
+ :street_address => '1134 Crane Avenue',
37
+ :extended_address => 'Suite 200',
38
+ :locality => 'Menlo Park',
39
+ :region => 'California',
40
+ :postal_code => '94025',
41
+ :country_name => 'United States of America'
42
+ }
43
+ address = customer.addresses.create!(address_hash)
44
+ address.update_attributes!(:first_name => 'Foo', :last_name => 'Bar')
45
+ braintree_customer = Braintree::Customer.find(customer.id)
46
+ braintree_address = braintree_customer.addresses.first
47
+
48
+ braintree_address.first_name.must_equal 'Foo'
49
+ braintree_address.last_name.must_equal 'Bar'
50
+ end
51
+
52
+ it 'should be able to destroy existing address' do
53
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
54
+ address_hash = {
55
+ :first_name => 'Brain',
56
+ :last_name => 'Tree',
57
+ :company => 'Braintree',
58
+ :street_address => '1134 Crane Avenue',
59
+ :extended_address => 'Suite 200',
60
+ :locality => 'Menlo Park',
61
+ :region => 'California',
62
+ :postal_code => '94025',
63
+ :country_name => 'United States of America'
64
+ }
65
+ address = customer.addresses.create!(address_hash)
66
+ address.destroy!
67
+ lambda{ Braintree::Address.find(customer.id, address.id) }.must_raise Braintree::NotFoundError
68
+ address.persisted?.must_equal false
69
+ address.frozen?.must_equal true
70
+ end
71
+
72
+ end
@@ -0,0 +1,147 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../integration_test_helper'))
2
+
3
+ describe 'Credit Card Integration' do
4
+ before do
5
+ Braintree::Customer.all.each { |c| Braintree::Customer.delete c.id }
6
+ end
7
+
8
+ it 'should be able to fetch from Braintree for given token' do
9
+ address_hash = {
10
+ :first_name => 'Brain',
11
+ :last_name => 'Tree',
12
+ :company => 'Braintree',
13
+ :street_address => '1134 Crane Avenue',
14
+ :extended_address => 'Suite 200',
15
+ :locality => 'Menlo Park',
16
+ :region => 'California',
17
+ :postal_code => '94025',
18
+ :country_name => 'United States of America'
19
+ }
20
+
21
+ credit_card_hash = {
22
+ :token => 'credit_card_id',
23
+ :number => '4111111111111111',
24
+ :cvv => '123',
25
+ :cardholder_name => 'Brain Tree',
26
+ :expiration_month => '05',
27
+ :expiration_year => '2037',
28
+ :billing_address => address_hash,
29
+ }
30
+
31
+ braintree_customer = Braintree::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree', :credit_card => credit_card_hash)
32
+
33
+ credit_card = BraintreeRails::CreditCard.new(braintree_customer.credit_cards.first.token)
34
+
35
+ credit_card_hash.except(:number, :cvv, :billing_address).each do |key, value|
36
+ credit_card.send(key).must_equal value
37
+ end
38
+
39
+ address_hash.each do |key, value|
40
+ credit_card.billing_address.send(key).must_equal value
41
+ end
42
+ end
43
+
44
+ it 'should be able to add credit card' do
45
+ customer = BraintreeRails::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree')
46
+
47
+ address_hash = {
48
+ :first_name => 'Brain',
49
+ :last_name => 'Tree',
50
+ :company => 'Braintree',
51
+ :street_address => '1134 Crane Avenue',
52
+ :extended_address => 'Suite 200',
53
+ :locality => 'Menlo Park',
54
+ :region => 'California',
55
+ :postal_code => '94025',
56
+ :country_name => 'United States of America'
57
+ }
58
+
59
+ credit_card_hash = {
60
+ :token => 'credit_card_id',
61
+ :number => '4111111111111111',
62
+ :cvv => '123',
63
+ :customer_id => 'customer_id',
64
+ :cardholder_name => 'Brain Tree',
65
+ :expiration_month => '05',
66
+ :expiration_year => '2037',
67
+ :billing_address => address_hash,
68
+ }
69
+
70
+ credit_card = customer.credit_cards.create!(credit_card_hash)
71
+
72
+ braintree_credit_card = Braintree::CreditCard.find(credit_card.id)
73
+ credit_card_hash.except(:number, :cvv, :billing_address).each do |key, value|
74
+ braintree_credit_card.send(key).must_equal value
75
+ end
76
+
77
+ braintree_address = braintree_credit_card.billing_address
78
+ address_hash.each do |key, value|
79
+ braintree_address.send(key).must_equal value
80
+ end
81
+ end
82
+
83
+ it 'should be able to update existing credit card' do
84
+ customer = BraintreeRails::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree')
85
+
86
+ address_hash = {
87
+ :first_name => 'Brain',
88
+ :last_name => 'Tree',
89
+ :company => 'Braintree',
90
+ :street_address => '1134 Crane Avenue',
91
+ :extended_address => 'Suite 200',
92
+ :locality => 'Menlo Park',
93
+ :region => 'California',
94
+ :postal_code => '94025',
95
+ :country_name => 'United States of America'
96
+ }
97
+
98
+ credit_card_hash = {
99
+ :token => 'credit_card_id',
100
+ :number => '4111111111111111',
101
+ :cvv => '123',
102
+ :customer_id => 'customer_id',
103
+ :cardholder_name => 'Brain Tree',
104
+ :expiration_month => '05',
105
+ :expiration_year => '2037',
106
+ :billing_address => address_hash,
107
+ }
108
+
109
+ credit_card = customer.credit_cards.create!(credit_card_hash)
110
+
111
+ credit_card.update_attributes!(:cardholder_name => 'Foo Bar')
112
+ braintree_credit_card = Braintree::CreditCard.find(credit_card.id)
113
+ braintree_credit_card.cardholder_name.must_equal 'Foo Bar'
114
+ end
115
+
116
+ it 'should be able to destroy existing credit card' do
117
+ address_hash = {
118
+ :first_name => 'Brain',
119
+ :last_name => 'Tree',
120
+ :company => 'Braintree',
121
+ :street_address => '1134 Crane Avenue',
122
+ :extended_address => 'Suite 200',
123
+ :locality => 'Menlo Park',
124
+ :region => 'California',
125
+ :postal_code => '94025',
126
+ :country_name => 'United States of America'
127
+ }
128
+
129
+ credit_card_hash = {
130
+ :token => 'credit_card_id',
131
+ :number => '4111111111111111',
132
+ :cvv => '123',
133
+ :cardholder_name => 'Brain Tree',
134
+ :expiration_month => '05',
135
+ :expiration_year => '2037',
136
+ :billing_address => address_hash,
137
+ }
138
+
139
+ braintree_customer = Braintree::Customer.create!(:id => 'customer_id', :first_name => 'Brain', :last_name => 'Tree', :credit_card => credit_card_hash)
140
+
141
+ credit_card = BraintreeRails::CreditCard.new(braintree_customer.credit_cards.first.token)
142
+ credit_card.destroy!
143
+ lambda{ Braintree::CreditCard.find(credit_card.token) }.must_raise Braintree::NotFoundError
144
+ credit_card.persisted?.must_equal false
145
+ credit_card.frozen?.must_equal true
146
+ end
147
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '../integration_test_helper'))
2
+
3
+ describe 'Customer Integration' do
4
+ before do
5
+ Braintree::Customer.all.each { |c| Braintree::Customer.delete c.id }
6
+ end
7
+
8
+ it 'should fetch customer from Braintree for given id' do
9
+ braintree_customer = Braintree::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
10
+ customer = BraintreeRails::Customer.new(braintree_customer.id)
11
+ customer.id.must_equal braintree_customer.id
12
+ customer.first_name.must_equal 'Brain'
13
+ customer.last_name.must_equal 'Tree'
14
+ customer.persisted?.must_equal true
15
+ end
16
+
17
+ it 'should be able to create new customer' do
18
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
19
+ braintree_customer = Braintree::Customer.find(customer.id)
20
+
21
+ braintree_customer.first_name.must_equal 'Brain'
22
+ braintree_customer.last_name.must_equal 'Tree'
23
+ end
24
+
25
+ it 'should be able to update existing customer' do
26
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
27
+ customer.update_attributes!(:first_name => 'Foo', :last_name => 'Bar')
28
+
29
+ braintree_customer = Braintree::Customer.find(customer.id)
30
+ braintree_customer.first_name.must_equal 'Foo'
31
+ braintree_customer.last_name.must_equal 'Bar'
32
+ end
33
+
34
+ it 'should be able to destroy existing customer' do
35
+ customer = BraintreeRails::Customer.create!(:first_name => 'Brain', :last_name => 'Tree')
36
+ customer.destroy!
37
+ lambda{ Braintree::Customer.find(customer.id) }.must_raise Braintree::NotFoundError
38
+ customer.persisted?.must_equal false
39
+ customer.frozen?.must_equal true
40
+ end
41
+ end