mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-09-22 00:00:31 -04:00
Merge branch 'multiple-subscriptions' of github.com:pat/stripe-ruby into ms
This commit is contained in:
commit
97f8df2934
@ -36,6 +36,7 @@ require 'stripe/event'
|
||||
require 'stripe/transfer'
|
||||
require 'stripe/recipient'
|
||||
require 'stripe/card'
|
||||
require 'stripe/subscription'
|
||||
require 'stripe/application_fee'
|
||||
|
||||
# Errors
|
||||
|
@ -1,8 +1,8 @@
|
||||
module Stripe
|
||||
module APIOperations
|
||||
module Delete
|
||||
def delete
|
||||
response, api_key = Stripe.request(:delete, url, @api_key)
|
||||
def delete(params = {})
|
||||
response, api_key = Stripe.request(:delete, url, @api_key, params)
|
||||
refresh_from(response, api_key)
|
||||
self
|
||||
end
|
||||
|
@ -25,18 +25,6 @@ module Stripe
|
||||
Charge.all({ :customer => id }, @api_key)
|
||||
end
|
||||
|
||||
def cancel_subscription(params={})
|
||||
response, api_key = Stripe.request(:delete, subscription_url, @api_key, params)
|
||||
refresh_from({ :subscription => response }, api_key, true)
|
||||
subscription
|
||||
end
|
||||
|
||||
def update_subscription(params)
|
||||
response, api_key = Stripe.request(:post, subscription_url, @api_key, params)
|
||||
refresh_from({ :subscription => response }, api_key, true)
|
||||
subscription
|
||||
end
|
||||
|
||||
def delete_discount
|
||||
Stripe.request(:delete, discount_url, @api_key)
|
||||
refresh_from({ :discount => nil }, api_key, true)
|
||||
@ -47,9 +35,5 @@ module Stripe
|
||||
def discount_url
|
||||
url + '/discount'
|
||||
end
|
||||
|
||||
def subscription_url
|
||||
url + '/subscription'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
14
lib/stripe/subscription.rb
Normal file
14
lib/stripe/subscription.rb
Normal file
@ -0,0 +1,14 @@
|
||||
module Stripe
|
||||
class Subscription < APIResource
|
||||
include Stripe::APIOperations::Update
|
||||
include Stripe::APIOperations::Delete
|
||||
|
||||
def url
|
||||
"#{Customer.url}/#{CGI.escape(customer)}/subscriptions/#{CGI.escape(id)}"
|
||||
end
|
||||
|
||||
def self.retrieve(id, api_key=nil)
|
||||
raise NotImplementedError.new("Subscriptions cannot be retrieved without a customer ID. Retrieve a subscription using customer.subscriptions.retrieve('subscription_id')")
|
||||
end
|
||||
end
|
||||
end
|
@ -29,6 +29,7 @@ module Stripe
|
||||
'transfer' => Transfer,
|
||||
'recipient' => Recipient,
|
||||
'card' => Card,
|
||||
'subscription' => Subscription,
|
||||
'list' => ListObject,
|
||||
'application_fee' => ApplicationFee
|
||||
}
|
||||
|
@ -32,32 +32,6 @@ module Stripe
|
||||
assert_equal "c_test_customer", c.id
|
||||
end
|
||||
|
||||
should "be able to update a customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
@mock.expects(:post).once.with do |url, api_key, params|
|
||||
url == "#{Stripe.api_base}/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
|
||||
end.returns(test_response(test_subscription('silver')))
|
||||
s = c.update_subscription({:plan => 'silver'})
|
||||
|
||||
assert_equal 'subscription', s.object
|
||||
assert_equal 'silver', s.plan.identifier
|
||||
end
|
||||
|
||||
should "be able to cancel a customer's subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
||||
# Not an accurate response, but whatever
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription?at_period_end=true", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
c.cancel_subscription({:at_period_end => 'true'})
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription('silver')))
|
||||
c.cancel_subscription
|
||||
end
|
||||
|
||||
should "be able to delete a customer's discount" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
c = Stripe::Customer.retrieve("test_customer")
|
||||
|
58
test/stripe/subscription_test.rb
Normal file
58
test/stripe/subscription_test.rb
Normal file
@ -0,0 +1,58 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
module Stripe
|
||||
class SubscriptionTest < Test::Unit::TestCase
|
||||
should "subscriptions should be listable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
|
||||
customer = Stripe::Customer.retrieve('test_customer')
|
||||
|
||||
assert customer.subscriptions.first.kind_of?(Stripe::Subscription)
|
||||
end
|
||||
|
||||
should "subscriptions should be refreshable" do
|
||||
@mock.expects(:get).twice.returns(test_response(test_customer), test_response(test_subscription(:id => 'refreshed_subscription')))
|
||||
|
||||
customer = Stripe::Customer.retrieve('test_customer')
|
||||
subscription = customer.subscriptions.first
|
||||
subscription.refresh
|
||||
|
||||
assert_equal subscription.id, 'refreshed_subscription'
|
||||
end
|
||||
|
||||
should "subscriptions should be deletable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
customer = Stripe::Customer.retrieve('test_customer')
|
||||
subscription = customer.subscriptions.first
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/#{subscription.id}?at_period_end=true", nil, nil).returns(test_response(test_subscription))
|
||||
subscription.delete :at_period_end => true
|
||||
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/#{subscription.id}", nil, nil).returns(test_response(test_subscription))
|
||||
subscription.delete
|
||||
end
|
||||
|
||||
should "subscriptions should be updateable" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
@mock.expects(:post).once.returns(test_response(test_subscription({:status => 'active'})))
|
||||
|
||||
customer = Stripe::Customer.retrieve('test_customer')
|
||||
subscription = customer.subscriptions.first
|
||||
assert_equal subscription.status, 'trialing'
|
||||
|
||||
subscription.status = 'active'
|
||||
subscription.save
|
||||
|
||||
assert_equal subscription.status, 'active'
|
||||
end
|
||||
|
||||
should "create should return a new subscription" do
|
||||
@mock.expects(:get).once.returns(test_response(test_customer))
|
||||
@mock.expects(:post).once.returns(test_response(test_subscription(:id => 'test_new_subscription')))
|
||||
|
||||
customer = Stripe::Customer.retrieve('test_customer')
|
||||
subscription = customer.subscriptions.create(:plan => 'silver')
|
||||
assert_equal subscription.id, 'test_new_subscription'
|
||||
end
|
||||
end
|
||||
end
|
@ -94,17 +94,19 @@ def test_application_fee_array
|
||||
end
|
||||
|
||||
def test_customer(params={})
|
||||
id = params[:id] || 'c_test_customer'
|
||||
{
|
||||
:subscription_history => [],
|
||||
:bills => [],
|
||||
:charges => [],
|
||||
:livemode => false,
|
||||
:object => "customer",
|
||||
:id => "c_test_customer",
|
||||
:id => id,
|
||||
:default_card => "cc_test_card",
|
||||
:created => 1304114758,
|
||||
:cards => test_card_array('c_test_customer'),
|
||||
:metadata => {}
|
||||
:cards => test_card_array(id),
|
||||
:metadata => {},
|
||||
:subscriptions => test_subscription_array(id)
|
||||
}.merge(params)
|
||||
end
|
||||
|
||||
@ -180,7 +182,8 @@ def test_coupon(params={})
|
||||
end
|
||||
|
||||
#FIXME nested overrides would be better than hardcoding plan_id
|
||||
def test_subscription(plan_id="gold")
|
||||
def test_subscription(params = {})
|
||||
plan_id = params.delete(:plan_id) || 'gold'
|
||||
{
|
||||
:current_period_end => 1308681468,
|
||||
:status => "trialing",
|
||||
@ -196,7 +199,16 @@ def test_subscription(plan_id="gold")
|
||||
:object => "subscription",
|
||||
:trial_start => 1308595038,
|
||||
:trial_end => 1308681468,
|
||||
:customer => "c_test_customer"
|
||||
:customer => "c_test_customer",
|
||||
:id => 's_test_subscription'
|
||||
}.merge(params)
|
||||
end
|
||||
|
||||
def test_subscription_array(customer_id)
|
||||
{
|
||||
:data => [test_subscription, test_subscription, test_subscription],
|
||||
:object => 'list',
|
||||
:url => '/v1/customers/' + customer_id + '/subscriptions'
|
||||
}
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user