Merge pull request #75 from stripe/sp-cards

Support for new cards API
This commit is contained in:
spakanati 2013-07-11 17:42:56 -07:00
commit 1f1ebafe36
6 changed files with 90 additions and 22 deletions

View File

@ -33,6 +33,7 @@ require 'stripe/token'
require 'stripe/event'
require 'stripe/transfer'
require 'stripe/recipient'
require 'stripe/card'
# Errors
require 'stripe/errors/stripe_error'

14
lib/stripe/card.rb Normal file
View File

@ -0,0 +1,14 @@
module Stripe
class Card < APIResource
include Stripe::APIOperations::Update
include Stripe::APIOperations::Delete
def url
"#{Customer.url}/#{CGI.escape(customer)}/cards/#{CGI.escape(id)}"
end
def self.retrieve(id, api_key=nil)
raise NotImplementedError.new("Cards cannot be retrieved without a customer ID. Retrieve a card using customer.cards.retrieve('card_id')")
end
end
end

View File

@ -1,5 +1,6 @@
module Stripe
class ListObject < StripeObject
def [](k)
case k
when String, Symbol
@ -13,10 +14,22 @@ module Stripe
self.data.each(&blk)
end
def all(filters={})
response, api_key = Stripe.request(:get, url, @api_key, filters)
def retrieve(id, api_key=nil)
api_key ||= @api_key
response, api_key = Stripe.request(:get,"#{url}/#{CGI.escape(id)}", api_key)
Util.convert_to_stripe_object(response, api_key)
end
def create(params={}, api_key=nil)
api_key ||= @api_key
response, api_key = Stripe.request(:post, url, api_key, params)
Util.convert_to_stripe_object(response, api_key)
end
def all(params={}, api_key=nil)
api_key ||= @api_key
response, api_key = Stripe.request(:get, url, api_key, params)
Util.convert_to_stripe_object(response, api_key)
end
end
end

View File

@ -26,6 +26,7 @@ module Stripe
'event' => Event,
'transfer' => Transfer,
'recipient' => Recipient,
'card' => Card,
'list' => ListObject
}
end

View File

@ -5,7 +5,7 @@ require 'mocha/setup'
include Mocha
#monkeypatch request methods
module Stripe
module Stripe
@mock_rest_client = nil
def self.mock_rest_client=(mock_client)
@ -42,16 +42,9 @@ def test_customer(params={})
:livemode => false,
:object => "customer",
:id => "c_test_customer",
:active_card => {
:type => "Visa",
:last4 => "4242",
:exp_month => 11,
:country => "US",
:exp_year => 2012,
:id => "cc_test_card",
:object => "card"
},
:created => 1304114758
:default_card => "cc_test_card",
:created => 1304114758,
:cards => test_card_array('c_test_customer')
}.merge(params)
end
@ -88,12 +81,20 @@ end
def test_charge_array
{
:data => [test_charge, test_charge, test_charge],
:data => [test_charge, test_charge, test_charge],
:object => 'list',
:url => '/v1/charges'
}
end
def test_card_array(customer_id)
{
:data => [test_card, test_card, test_card],
:object => 'list',
:url => '/v1/customers/' + customer_id + '/cards'
}
end
def test_card(params={})
{
:type => "Visa",
@ -102,7 +103,8 @@ def test_card(params={})
:country => "US",
:exp_year => 2012,
:id => "cc_test_card",
:object => "card"
:customer => 'c_test_customer',
:object => "card"
}.merge(params)
end
@ -112,7 +114,7 @@ def test_coupon(params={})
:duration_in_months => 3,
:percent_off => 25,
:id => "co_test_coupon",
:object => "coupon"
:object => "coupon"
}.merge(params)
end

View File

@ -454,12 +454,6 @@ class TestStripeRuby < Test::Unit::TestCase
assert_equal c.mnemonic, "bar"
end
should "customers should have Card objects associated with their active_ard property" do
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.retrieve("test_customer")
assert c.active_card.kind_of?(Stripe::StripeObject) && c.active_card.object == 'card'
end
should "create should return a new customer" do
@mock.expects(:post).once.returns(test_response(test_customer))
c = Stripe::Customer.create
@ -512,8 +506,51 @@ class TestStripeRuby < Test::Unit::TestCase
end
context "card tests" do
should "be able to create a new card for a customer" do
@mock.expects(:get).once.returns(test_response(test_customer))
customer = Stripe::Customer.retrieve("test_customer")
@mock.expects(:post).once.returns(test_response(test_card))
card = customer.cards.create(:card => 'card')
assert card.kind_of? Stripe::Card
end
should "be able to retrieve a card for a customer" do
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
customer = Stripe::Customer.retrieve("c_test_customer")
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
card = customer.cards.retrieve("cc_test_card")
assert card.kind_of? Stripe::Card
end
should "be able to list all cards for a customer" do
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
customer = Stripe::Customer.retrieve("c_test_customer")
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards", nil, nil).returns(test_response(test_card_array("c_test_customer")))
cards = customer.cards.all()
assert cards.data.kind_of? Array
cards.each do |card|
assert card.kind_of?(Stripe::Card)
end
end
should "be able to update a card for a customer" do
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer", nil, nil).returns(test_response(test_customer))
customer = Stripe::Customer.retrieve("c_test_customer")
@mock.expects(:get).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, nil).returns(test_response(test_card))
card = customer.cards.retrieve("cc_test_card")
@mock.expects(:post).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/cards/cc_test_card", nil, "address_zip=zippy").returns(test_response(test_card(:address_zip => 'zippy')))
card.address_zip = "zippy"
card.save
assert_equal "zippy", card.address_zip
end
end
context "coupon tests" do
should "create should return a new coupon" do
@mock.expects(:post).once.returns(test_response(test_coupon))