diff --git a/public/upgrade.html.erb b/public/upgrade.html.erb index 26665908..ec86203b 100644 --- a/public/upgrade.html.erb +++ b/public/upgrade.html.erb @@ -4,8 +4,16 @@ <%= partial 'public/partials/_head.html' %> - <% game = Game.where(name: request['game']).first %> - <% team = game && game.teams.where(team_id: request['team_id']).first %> + <% + game = Game.where(name: request['game']).first + team = game && game.teams.where(team_id: request['team_id']).first + coupon = Stripe::Coupon.retrieve(request[:coupon]) if request[:coupon] + subscription_cents = 2999 + subscription_dollars = subscription_cents.to_f / 100 + amount_cents = subscription_cents + amount_cents -= coupon.amount_off if coupon + amount_dollars = amount_cents.to_f / 100 + %>
@@ -23,7 +31,7 @@
@@ -36,15 +44,31 @@
$(document).ready(function() {
var team = {
- id: '<%= team && team._id %>',
- game: '<%= game && game.name %>',
- name: '<%= team && team.name %>',
- premium: <%= !!(team && team.premium) %>,
- }
+ id: <%= team ? "'#{team._id}'" : 'null' %>,
+ game: <%= game ? "'#{game.name}'" : 'null' %>,
+ name: <%= team ? "'#{team.name}'" : 'null' %>,
+ premium: <%= !!(team && team.premium) %>
+ };
+
+ var coupon = {
+ code: <%= coupon ? "'#{coupon.id}'" : 'null' %>
+ };
+
+ var subscription = {
+ dollars: <%= subscription_dollars %>,
+ cents: <%= subscription_cents %>
+ };
+
+ var pay = {
+ dollars: <%= amount_dollars %>,
+ cents: <%= amount_cents %>
+ };
if (team.premium) {
PlayPlay.message('Team ' + team.name + ' already has a premium ' + team.game + ' subscription, thank you for your support.');
$('#subscribeButton').remove();
+ } else if (team.id && team.name && team.game && coupon.code) {
+ PlayPlay.message('Upgrade team ' + team.name + ' to premium ' + team.game + ' for $' + pay.dollars + ' for the first year and $' + subscription.dollars + ' thereafter with coupon ' + coupon.code + '!');
} else if (team.id && team.name && team.game) {
PlayPlay.message('Upgrade team ' + team.name + ' to premium ' + team.game + ' for $29.99 a year!');
} else {
@@ -64,6 +88,7 @@
stripe_email: token.email,
stripe_token: token.id,
stripe_token_type: token.type,
+ stripe_coupon: coupon.code,
team_id: team.id
},
success: function(data) {
@@ -76,11 +101,10 @@
});
$('#subscribeButton').on('click', function(e) {
- var amount = 2999;
handler.open({
name: 'PlayPlay.io Premium',
description: '1 Year Subscription',
- amount: amount
+ amount: pay.cents
});
e.preventDefault();
});
diff --git a/slack-gamebot/api/endpoints/subscriptions_endpoint.rb b/slack-gamebot/api/endpoints/subscriptions_endpoint.rb
index 1fa3ff8a..7d5bf71d 100644
--- a/slack-gamebot/api/endpoints/subscriptions_endpoint.rb
+++ b/slack-gamebot/api/endpoints/subscriptions_endpoint.rb
@@ -9,17 +9,19 @@ class SubscriptionsEndpoint < Grape::API
requires :stripe_token, type: String
requires :stripe_token_type, type: String
requires :stripe_email, type: String
+ optional :stripe_coupon, type: String
requires :team_id, type: String
end
post do
team = Team.find(params[:team_id]) || error!('Not Found', 404)
- Api::Middleware.logger.info "Creating a subscription for team #{team}."
+ Api::Middleware.logger.info "Creating a subscription for team #{team}, email=#{params[:stripe_email]}, coupon=#{params[:stripe_coupon]}."
error!('Already a Premium Subscription', 400) if team.premium
error!('Customer Already Registered', 400) if team.stripe_customer_id
customer = Stripe::Customer.create(
source: params[:stripe_token],
plan: 'slack-playplay-yearly',
email: params[:stripe_email],
+ coupon: params[:stripe_coupon],
metadata: {
id: team._id,
team_id: team.team_id,
diff --git a/spec/api/endpoints/subscriptions_endpoint_spec.rb b/spec/api/endpoints/subscriptions_endpoint_spec.rb
index 72b9d9c6..f6d52a91 100644
--- a/spec/api/endpoints/subscriptions_endpoint_spec.rb
+++ b/spec/api/endpoints/subscriptions_endpoint_spec.rb
@@ -44,23 +44,50 @@
context 'existing team' do
include_context :stripe_mock
let!(:team) { Fabricate(:team) }
- before do
- stripe_helper.create_plan(id: 'slack-playplay-yearly', amount: 2999)
- client.subscriptions._post(
- team_id: team._id,
- stripe_token: stripe_helper.generate_card_token,
- stripe_token_type: 'card',
- stripe_email: 'foo@bar.com'
- )
- team.reload
+ context 'with a plan' do
+ before do
+ stripe_helper.create_plan(id: 'slack-playplay-yearly', amount: 2999)
+ client.subscriptions._post(
+ team_id: team._id,
+ stripe_token: stripe_helper.generate_card_token,
+ stripe_token_type: 'card',
+ stripe_email: 'foo@bar.com'
+ )
+ team.reload
+ end
+ it 'creates a subscription' do
+ expect(team.premium).to be true
+ expect(team.stripe_customer_id).to_not be_blank
+ customer = Stripe::Customer.retrieve(team.stripe_customer_id)
+ expect(customer).to_not be nil
+ expect(customer.discount).to be nil
+ subscriptions = customer.subscriptions
+ expect(subscriptions.count).to eq 1
+ end
end
- it 'creates a subscription' do
- expect(team.premium).to be true
- expect(team.stripe_customer_id).to_not be_blank
- customer = Stripe::Customer.retrieve(team.stripe_customer_id)
- expect(customer).to_not be nil
- subscriptions = customer.subscriptions
- expect(subscriptions.count).to eq 1
+ context 'with a coupon' do
+ before do
+ stripe_helper.create_plan(id: 'slack-playplay-yearly', amount: 2999)
+ stripe_helper.create_coupon(id: 'slack-playplay-yearly-twenty-nine-ninety-nine', amount_off: 2999)
+ client.subscriptions._post(
+ team_id: team._id,
+ stripe_token: stripe_helper.generate_card_token,
+ stripe_token_type: 'card',
+ stripe_email: 'foo@bar.com',
+ stripe_coupon: 'slack-playplay-yearly-twenty-nine-ninety-nine'
+ )
+ team.reload
+ end
+ it 'creates a subscription' do
+ expect(team.premium).to be true
+ expect(team.stripe_customer_id).to_not be_blank
+ customer = Stripe::Customer.retrieve(team.stripe_customer_id)
+ expect(customer).to_not be nil
+ subscriptions = customer.subscriptions
+ expect(subscriptions.count).to eq 1
+ discount = customer.discount
+ expect(discount.coupon.id).to eq 'slack-playplay-yearly-twenty-nine-ninety-nine'
+ end
end
end
end
diff --git a/spec/integration/upgrade_spec.rb b/spec/integration/upgrade_spec.rb
index 2b92d20f..74da8736 100644
--- a/spec/integration/upgrade_spec.rb
+++ b/spec/integration/upgrade_spec.rb
@@ -39,6 +39,8 @@
expect(Stripe::Customer).to receive(:create).and_return('id' => 'customer_id')
find('#subscribeButton').click
+ sleep 1
+
stripe_iframe = all('iframe[name=stripe_checkout_app]').last
Capybara.within_frame stripe_iframe do
page.execute_script("$('input#email').val('foo@bar.com');")
@@ -79,5 +81,38 @@
let!(:team) { Fabricate(:team, team_id: team2.team_id, game: Fabricate(:game)) }
it_behaves_like 'upgrades to premium'
end
+ context 'with a coupon' do
+ let!(:team) { Fabricate(:team) }
+ it 'applies the coupon' do
+ coupon = double(Stripe::Coupon, id: 'coupon-id', amount_off: 1200)
+ expect(Stripe::Coupon).to receive(:retrieve).with('coupon-id').and_return(coupon)
+ visit "/upgrade?team_id=#{team.team_id}&game=#{team.game.name}&coupon=coupon-id"
+ expect(find('#messages')).to have_text("Upgrade team #{team.name} to premium #{team.game.name} for $17.99 for the first year and $29.99 thereafter with coupon coupon-id!")
+ find('#subscribe', visible: true)
+
+ expect(Stripe::Customer).to receive(:create).with(hash_including(coupon: 'coupon-id')).and_return('id' => 'customer_id')
+
+ find('#subscribeButton').click
+ sleep 1
+
+ stripe_iframe = all('iframe[name=stripe_checkout_app]').last
+ Capybara.within_frame stripe_iframe do
+ page.execute_script("$('input#email').val('foo@bar.com');")
+ page.execute_script("$('input#card_number').val('4242 4242 4242 4242');")
+ page.execute_script("$('input#cc-exp').val('12/16');")
+ page.execute_script("$('input#cc-csc').val('123');")
+ page.execute_script("$('#submitButton').click();")
+ end
+
+ sleep 5
+
+ expect(find('#messages')).to have_text("Team #{team.name} successfully upgraded to premium #{team.game.name}. Thank you for your support!")
+ find('#subscribe', visible: false)
+
+ team.reload
+ expect(team.premium).to be true
+ expect(team.stripe_customer_id).to eq 'customer_id'
+ end
+ end
end
end