Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Imported the customer model

  • Loading branch information...
commit 00cbd432bff99cf0a98fcde250671ba5618db63f 1 parent 31ce31b
@jferris jferris authored
View
1  lib/ashmont.rb
@@ -1,5 +1,6 @@
require "ashmont/version"
require "ashmont/subscription"
+require "ashmont/customer"
module Ashmont
class << self
View
113 lib/ashmont/customer.rb
@@ -0,0 +1,113 @@
+module Ashmont
+ class Customer
+ attr_reader :token, :errors
+
+ def initialize(token = nil)
+ @token = token
+ @errors = {}
+ end
+
+ def credit_card
+ if persisted?
+ remote_customer.credit_cards[0]
+ end
+ end
+
+ def has_billing_info?
+ credit_card.present?
+ end
+
+ def payment_method_token
+ credit_card.token if credit_card
+ end
+
+ def billing_email
+ remote_customer.email if persisted?
+ end
+
+ def save(attributes)
+ handle_result create_or_update(attributes)
+ end
+
+ def delete
+ Braintree::Customer.delete(@token)
+ end
+
+ def last_4
+ credit_card.last_4 if credit_card
+ end
+
+ def cardholder_name
+ credit_card.cardholder_name if credit_card
+ end
+
+ def expiration_month
+ credit_card.expiration_month if credit_card
+ end
+
+ def expiration_year
+ credit_card.expiration_year if credit_card
+ end
+
+ def street_address
+ credit_card.billing_address.street_address if credit_card
+ end
+
+ def extended_address
+ credit_card.billing_address.extended_address if credit_card
+ end
+
+ def locality
+ credit_card.billing_address.locality if credit_card
+ end
+
+ def region
+ credit_card.billing_address.region if credit_card
+ end
+
+ def postal_code
+ credit_card.billing_address.postal_code if credit_card
+ end
+
+ def country_name
+ credit_card.billing_address.country_name if credit_card
+ end
+
+ private
+
+ def create_or_update(attributes)
+ if persisted?
+ update(attributes)
+ else
+ create(attributes)
+ end
+ end
+
+ def create(attributes)
+ Braintree::Customer.create(attributes)
+ end
+
+ def update(attributes)
+ Braintree::Customer.update(@token, attributes)
+ end
+
+ def handle_result(result)
+ if result.success?
+ @token = result.customer.id
+ @remote_customer = result.customer
+ true
+ else
+ @errors = Ashmont::Errors.new(result.credit_card_verification, result.errors)
+ false
+ end
+ end
+
+ def persisted?
+ @token.present?
+ end
+
+ def remote_customer
+ @remote_customer ||= Braintree::Customer.find(@token)
+ end
+ end
+end
View
146 spec/ashmont/customer_spec.rb
@@ -0,0 +1,146 @@
+require 'spec_helper'
+require 'ashmont/customer'
+
+describe Ashmont::Customer do
+ it "has returns its first credit card" do
+ token = "xyz"
+ remote_customer = stub("customer", :credit_cards => ["first", "second"])
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ result = Ashmont::Customer.new(token).credit_card
+
+ Braintree::Customer.should have_received(:find).with(token)
+ result.should == "first"
+ end
+
+ it "returns nothing without a remote customer" do
+ Ashmont::Customer.new.credit_card.should be_nil
+ end
+
+ it "returns the remote email" do
+ remote_customer = stub("customer", :email => "admin@example.com")
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ Ashmont::Customer.new("abc").billing_email.should == "admin@example.com"
+ end
+
+ it "doesn't have an email without a remote customer" do
+ Ashmont::Customer.new.billing_email.should be_nil
+ end
+
+ it "creates a valid remote customer" 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(attributes).should be_true
+
+ Braintree::Customer.should have_received(:create).with(attributes)
+ customer.token.should == token
+ customer.errors.should be_empty
+ end
+
+ it "returns errors while creating an invalid remote customer" do
+ error_messages = "error messages"
+ errors = "errors"
+ verification = "failure"
+ create_result = stub("result",
+ :success? => false,
+ :errors => error_messages,
+ :credit_card_verification => verification)
+ Braintree::Customer.stubs(:create => create_result)
+ Ashmont::Errors.stubs(:new => errors)
+
+ customer = Ashmont::Customer.new
+ customer.save("email" => "ben.franklin@example.com").should be_false
+
+ Ashmont::Errors.should have_received(:new).with(verification, error_messages)
+ customer.errors.should == errors
+ end
+
+ it "updates a remote customer with valid changes" do
+ token = "xyz"
+ updates = { "email" => 'somebody@example.com' }
+ customer = stub("customer", :id => token, :email => "somebody@example.com")
+ update_result = stub('result', :success? => true, :customer => customer)
+ Braintree::Customer.stubs(:update => update_result)
+
+ customer = Ashmont::Customer.new(token)
+ customer.save(updates).should be_true
+
+ Braintree::Customer.should have_received(:update).with(token, updates)
+ customer.billing_email.should == "somebody@example.com"
+ end
+
+ it "returns errors while updating an invalid customer" do
+ error_messages = "error messages"
+ errors = "errors"
+ verification = "failure"
+ update_result = stub("result",
+ :success? => false,
+ :errors => error_messages,
+ :credit_card_verification => verification)
+ Braintree::Customer.stubs(:update => update_result)
+ Ashmont::Errors.stubs(:new => errors)
+
+ customer = Ashmont::Customer.new("xyz")
+ customer.save("email" => "ben.franklin@example.com").should be_false
+
+ Ashmont::Errors.should have_received(:new).with(verification, error_messages)
+ customer.errors.should == errors
+ end
+
+ it "delete a remote customer" do
+ token = "xyz"
+ Braintree::Customer.stubs(:delete)
+
+ customer = Ashmont::Customer.new(token)
+ customer.delete
+
+ Braintree::Customer.should have_received(:delete).with(token)
+ end
+
+ it "has billing info with a credit card" do
+ token = "abc"
+ credit_card = stub("credit_card", :token => token)
+ remote_customer = stub("customer", :credit_cards => [credit_card])
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ customer = Ashmont::Customer.new("xyz")
+ customer.should have_billing_info
+ customer.payment_method_token.should == token
+ end
+
+ it "doesn't have billing info without a credit card" do
+ remote_customer = stub("customer", :credit_cards => [])
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ customer = Ashmont::Customer.new("xyz")
+ customer.should_not have_billing_info
+ customer.payment_method_token.should be_nil
+ end
+
+ %w(last_4 cardholder_name expiration_month expiration_year).each do |credit_card_attribute|
+ it "delegates ##{credit_card_attribute} to its credit card" do
+ credit_card = stub("credit_card", credit_card_attribute => "expected")
+ remote_customer = stub("customer", :credit_cards => [credit_card])
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ Ashmont::Customer.new("xyz").send(credit_card_attribute).should == "expected"
+ end
+ end
+
+ %w(street_address extended_address locality region postal_code country_name).each do |billing_address_attribute|
+ it "delegates ##{billing_address_attribute} to its billing address" do
+ billing_address = stub("billing_address", billing_address_attribute => "expected")
+ credit_card = stub("credit_card", :billing_address => billing_address)
+ remote_customer = stub("customer", :credit_cards => [credit_card])
+ Braintree::Customer.stubs(:find => remote_customer)
+
+ Ashmont::Customer.new("xyz").send(billing_address_attribute).should == "expected"
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.