Permalink
Browse files

Customer takes a flat hash for #save and figures out the remote attri…

…bute structure
  • Loading branch information...
1 parent d78330d commit b5332b2c03a41b1fa2de1765995ba829db1b11c0 @jferris jferris committed Dec 9, 2011
Showing with 119 additions and 4 deletions.
  1. +25 −2 lib/ashmont/customer.rb
  2. +94 −2 spec/ashmont/customer_spec.rb
View
@@ -1,5 +1,9 @@
module Ashmont
class Customer
+ CREDIT_CARD_ATTRIBUTES = [:cardholder_name, :number, :cvv, :expiration_month, :expiration_year].freeze
+ ADDRESS_ATTRIBUTES = [:street_address, :extended_address, :locality, :region, :postal_code, :country_name].freeze
+ BILLING_ATTRIBUTES = (CREDIT_CARD_ATTRIBUTES + ADDRESS_ATTRIBUTES).freeze
+
attr_reader :token, :errors
def initialize(token = nil)
@@ -76,11 +80,30 @@ def country_name
private
def create_or_update(attributes)
+ remote_attributes = build_attribute_hash(attributes.symbolize_keys)
if persisted?
- update(attributes)
+ update(remote_attributes)
+ else
+ create(remote_attributes)
+ end
+ end
+
+ def build_attribute_hash(attributes)
+ result = { :email => attributes[:email] }
+ if BILLING_ATTRIBUTES.any? { |attribute| attributes[attribute].present? }
+ result[:credit_card] = CREDIT_CARD_ATTRIBUTES.inject({}) do |credit_card_attributes, attribute|
+ credit_card_attributes.update(attribute => attributes[attribute])
+ end
+ result[:credit_card][:billing_address] = ADDRESS_ATTRIBUTES.inject({}) do |address_attributes, attribute|
+ address_attributes.update(attribute => attributes[attribute])
+ end
+ if payment_method_token
+ result[:credit_card][:options] = { :update_existing_token => payment_method_token }
+ end
else
- create(attributes)
+ result[:credit_card] = {}
end
+ result
end
def create(attributes)
@@ -38,11 +38,54 @@
customer = Ashmont::Customer.new
customer.save(attributes).should be_true
- Braintree::Customer.should have_received(:create).with(attributes)
+ Braintree::Customer.should have_received(:create).with(:email => "ben@example.com", :credit_card => {})
customer.token.should == token
customer.errors.should be_empty
end
+ it "creates a remote customer with a credit card" do
+ token = "xyz"
+ remote_customer = stub("customer", :credit_cards => [], :id => token)
+ create_result = stub("result", :success? => true, :customer => remote_customer)
+ attributes = { "email" => "ben@example.com" }
+ Braintree::Customer.stubs(:create => create_result)
+
+ customer = Ashmont::Customer.new
+ customer.save(
+ :email => "jrobot@example.com",
+ :cardholder_name => "Jim Robot",
+ :number => "4111111111111115",
+ :cvv => "123",
+ :expiration_month => 5,
+ :expiration_year => 2013,
+ :street_address => "1 E Main St",
+ :extended_address => "Suite 3",
+ :locality => "Chicago",
+ :region => "Illinois",
+ :postal_code => "60622",
+ :country_name => "United States of America"
+ )
+
+ Braintree::Customer.should have_received(:create).with(
+ :email => "jrobot@example.com",
+ :credit_card => {
+ :cardholder_name => "Jim Robot",
+ :number => "4111111111111115",
+ :cvv => "123",
+ :expiration_month => 5,
+ :expiration_year => 2013,
+ :billing_address => {
+ :street_address => "1 E Main St",
+ :extended_address => "Suite 3",
+ :locality => "Chicago",
+ :region => "Illinois",
+ :postal_code => "60622",
+ :country_name => "United States of America"
+ }
+ }
+ )
+ end
+
it "returns errors while creating an invalid remote customer" do
error_messages = "error messages"
errors = "errors"
@@ -71,10 +114,59 @@
customer = Ashmont::Customer.new(token)
customer.save(updates).should be_true
- Braintree::Customer.should have_received(:update).with(token, updates)
+ Braintree::Customer.should have_received(:update).with(token, :email => "somebody@example.com", :credit_card => {})
customer.billing_email.should == "somebody@example.com"
end
+ it "updates a remote customer with a credit card" do
+ token = "xyz"
+ payment_method_token = "abc"
+ credit_card = stub("credit_card", :token => payment_method_token)
+ remote_customer = stub("customer", :credit_cards => [credit_card], :id => token)
+ update_result = stub("result", :success? => true, :customer => remote_customer)
+ Braintree::Customer.stubs(:update => update_result)
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ customer = Ashmont::Customer.new(token)
+ customer.save(
+ :email => "jrobot@example.com",
+ :cardholder_name => "Jim Robot",
+ :number => "4111111111111115",
+ :cvv => "123",
+ :expiration_month => 5,
+ :expiration_year => 2013,
+ :street_address => "1 E Main St",
+ :extended_address => "Suite 3",
+ :locality => "Chicago",
+ :region => "Illinois",
+ :postal_code => "60622",
+ :country_name => "United States of America"
+ )
+
+ Braintree::Customer.should have_received(:update).with(
+ token,
+ :email => "jrobot@example.com",
+ :credit_card => {
+ :cardholder_name => "Jim Robot",
+ :number => "4111111111111115",
+ :cvv => "123",
+ :expiration_month => 5,
+ :expiration_year => 2013,
+ :billing_address => {
+ :street_address => "1 E Main St",
+ :extended_address => "Suite 3",
+ :locality => "Chicago",
+ :region => "Illinois",
+ :postal_code => "60622",
+ :country_name => "United States of America"
+ },
+ :options => {
+ :update_existing_token => payment_method_token
+ }
+ }
+ )
+ end
+
it "returns errors while updating an invalid customer" do
error_messages = "error messages"
errors = "errors"

0 comments on commit b5332b2

Please sign in to comment.