Skip to content

Commit

Permalink
Added support for coupons.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jun 22, 2016
1 parent ed1d332 commit 245ea64
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 27 deletions.
44 changes: 34 additions & 10 deletions public/upgrade.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
<%= partial 'public/partials/_head.html' %>
<script src='https://checkout.stripe.com/checkout.js'></script>
<link rel='stylesheet' href='https://checkout.stripe.com/v3/checkout/button.css'></link>
<% 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
%>
</head>
<body style='text-align: center'>
<p style='margin: 50px;'>
Expand All @@ -23,7 +31,7 @@
<p id='messages' />
<p id='subscribe'>
<button id='subscribeButton' class='stripe-button-el'>
<span style='display: block; min-height: 30px;'>Pay $29.99 with Card</span>
<span style='display: block; min-height: 30px;'>Pay $<%= amount_dollars %> with Card</span>
</button>
<p>
<img src='/img/stripe.png' width='119' height='26'></img>
Expand All @@ -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 <b>' + team.name + '</b> already has a premium <b>' + team.game + '</b> subscription, thank you for your support.');
$('#subscribeButton').remove();
} else if (team.id && team.name && team.game && coupon.code) {
PlayPlay.message('Upgrade team <b>' + team.name + '</b> to premium <b>' + team.game + '</b> for $' + pay.dollars + ' for the first year and $' + subscription.dollars + ' thereafter with coupon <b>' + coupon.code + '!');
} else if (team.id && team.name && team.game) {
PlayPlay.message('Upgrade team <b>' + team.name + '</b> to premium <b>' + team.game + '</b> for $29.99 a year!');
} else {
Expand All @@ -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) {
Expand All @@ -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();
});
Expand Down
4 changes: 3 additions & 1 deletion slack-gamebot/api/endpoints/subscriptions_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
59 changes: 43 additions & 16 deletions spec/api/endpoints/subscriptions_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions spec/integration/upgrade_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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');")
Expand Down Expand Up @@ -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

0 comments on commit 245ea64

Please sign in to comment.