stripe service can create subscriptions

This commit is contained in:
Rimian Perkins 2017-05-23 12:27:34 +10:00
parent f99aed364b
commit 9d2cd86f54
2 changed files with 50 additions and 18 deletions

View File

@ -23,6 +23,18 @@ module DiscourseDonations
@charge
end
def subscribe(email, opts)
customer = ::Stripe::Customer.create(
email: email,
source: opts[:stripeToken]
)
@subscription = ::Stripe::Subscription.create(
customer: customer.id,
plan: opts[:plan]
)
@subscription
end
def successful?
@charge[:paid]
end

View File

@ -6,7 +6,6 @@ module DiscourseDonations
before { SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') }
let(:stripe_options) { { description: 'hi there', currency: 'AUD' } }
let(:params) { { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' } }
let(:email) { 'ray-zintoast@example.com' }
let(:customer) { stub(id: 1) }
let!(:subject) { described_class.new('secret-key-yo', stripe_options) }
@ -15,26 +14,47 @@ module DiscourseDonations
expect(::Stripe.api_key).to eq 'secret-key-yo'
end
it 'creates a customer and charges them an amount' do
::Stripe::Customer.expects(:create).with(
email: email,
source: 'stripe-token'
).returns(customer)
::Stripe::Charge.expects(:create).with(
customer: customer.id,
amount: params[:amount],
description: stripe_options[:description],
currency: stripe_options[:currency]
).returns(
{
paid: true,
outcome: { seller_message: 'yay!' }
}
)
subject.charge(email, params)
describe 'subscribe' do
let(:params) { { email: email, stripeToken: 'stripe-token', plan: 'subscription-plan-1234', other: 'redundant param' } }
it 'creates a customer and a subscription' do
::Stripe::Customer.expects(:create).with(
email: email,
source: 'stripe-token'
).returns(customer)
::Stripe::Subscription.expects(:create).with(
customer: customer.id,
plan: params[:plan]
)
subject.subscribe(email, params)
end
end
describe 'charge' do
let(:params) { { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' } }
it 'creates a customer and charges them an amount' do
::Stripe::Customer.expects(:create).with(
email: email,
source: 'stripe-token'
).returns(customer)
::Stripe::Charge.expects(:create).with(
customer: customer.id,
amount: params[:amount],
description: stripe_options[:description],
currency: stripe_options[:currency]
).returns(
{
paid: true,
outcome: { seller_message: 'yay!' }
}
)
subject.charge(email, params)
end
end
describe '.successful?' do
let(:params) { { email: email, stripeToken: 'stripe-token', amount: '1234', other: 'redundant param' } }
let(:charge_options) { { customer: customer.id, amount: params[:amount], description: stripe_options[:description], currency: stripe_options[:currency] } }
before do