mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-10-08 00:02:46 -04:00
commit
6eb9a784c0
@ -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
lib/stripe/subscription.rb
Normal file
25
lib/stripe/subscription.rb
Normal file
@ -0,0 +1,25 @@
|
||||
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
|
||||
|
||||
def delete_discount
|
||||
Stripe.request(:delete, discount_url, @api_key)
|
||||
refresh_from({ :discount => nil }, api_key, true)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def discount_url
|
||||
url + '/discount'
|
||||
end
|
||||
end
|
||||
end
|
@ -29,6 +29,7 @@ module Stripe
|
||||
'transfer' => Transfer,
|
||||
'recipient' => Recipient,
|
||||
'card' => Card,
|
||||
'subscription' => Subscription,
|
||||
'list' => ListObject,
|
||||
'application_fee' => ApplicationFee
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ module Stripe
|
||||
|
||||
@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')))
|
||||
end.returns(test_response(test_subscription(:plan => 'silver')))
|
||||
s = c.update_subscription({:plan => 'silver'})
|
||||
|
||||
assert_equal 'subscription', s.object
|
||||
@ -57,10 +57,10 @@ module Stripe
|
||||
|
||||
# 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')))
|
||||
@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(:plan => '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')))
|
||||
@mock.expects(:delete).once.with("#{Stripe.api_base}/v1/customers/c_test_customer/subscription", nil, nil).returns(test_response(test_subscription(:plan => 'silver')))
|
||||
c.cancel_subscription
|
||||
end
|
||||
|
||||
|
72
test/stripe/subscription_test.rb
Normal file
72
test/stripe/subscription_test.rb
Normal file
@ -0,0 +1,72 @@
|
||||
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
|
||||
|
||||
should "be able to delete a subscriptions's discount" 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')
|
||||
|
||||
url = "#{Stripe.api_base}/v1/customers/c_test_customer/subscriptions/test_new_subscription/discount"
|
||||
@mock.expects(:delete).once.with(url, nil, nil).returns(test_response(test_delete_discount_response))
|
||||
subscription.delete_discount
|
||||
assert_equal nil, subscription.discount
|
||||
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 = params.delete(:plan) || 'gold'
|
||||
{
|
||||
:current_period_end => 1308681468,
|
||||
:status => "trialing",
|
||||
@ -189,14 +192,23 @@ def test_subscription(plan_id="gold")
|
||||
:amount => 7500,
|
||||
:trial_period_days => 30,
|
||||
:object => "plan",
|
||||
:identifier => plan_id
|
||||
:identifier => plan
|
||||
},
|
||||
:current_period_start => 1308595038,
|
||||
:start => 1308595038,
|
||||
: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