-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added premium info, allow users to change credit cards. Closes #149.
- Loading branch information
Showing
16 changed files
with
300 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<html> | ||
<head> | ||
<title>PlayPlay.io - Update Credit Card</title> | ||
<%= partial 'public/partials/_head.html' %> | ||
<% | ||
game = Game.where(name: request['game']).first | ||
team = game && game.teams.where(team_id: request['team_id']).first | ||
stripe_token = request['stripeToken'] | ||
stripe_token_type = request['stripeTokenType'] | ||
stripe_email = request['stripeEmail'] | ||
%> | ||
</head> | ||
<body style='text-align: center'> | ||
<p style='margin: 50px;'> | ||
<a href='/'><img src='img/<%= game ? game.name : 'pong' %>.png' width='120px'></a> | ||
</p> | ||
<p> | ||
<h3>PlayPlay.io: Update Credit Card Info</h3> | ||
</p> | ||
<p id='messages' /> | ||
<p id='update_cc'> | ||
<form action="" method="POST"> | ||
<script | ||
src="https://checkout.stripe.com/checkout.js" class="stripe-button" | ||
data-key="<%= ENV['STRIPE_API_PUBLISHABLE_KEY'] %>" | ||
data-image='/img/<%= game ? game.name : 'pong' %>.png' | ||
data-name="Playplay.io" | ||
data-panel-label="Update Credit Card" | ||
data-label="Update Credit Card" | ||
data-allow-remember-me=false | ||
data-locale="auto"> | ||
</script> | ||
</form> | ||
<p> | ||
<img src='/img/stripe.png' width='119' height='26'></img> | ||
<div class='small'> | ||
Questions? Contact dblock[at]dblock[dot]org or DM <a href='https://twitter.com/playplayio'>@playplayio</a>. | ||
</div> | ||
</p> | ||
<%= partial 'public/partials/_scripts.html' %> | ||
<script> | ||
$(document).ready(function() { | ||
var data = { | ||
stripe_token: "<%= stripe_token %>", | ||
stripe_token_type: "<%= stripe_token_type %>", | ||
stripe_email: "<%= stripe_email %>", | ||
team_id: "<%= team.id %>" | ||
}; | ||
|
||
if (data.stripe_token) { | ||
|
||
var team = { | ||
id: <%= team ? "'#{team._id}'" : 'null' %>, | ||
game: <%= game ? "'#{game.name}'" : 'null' %>, | ||
name: <%= team ? "'#{team.name}'" : 'null' %> | ||
}; | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: '/api/credit_cards', | ||
data: data, | ||
success: function(data) { | ||
PlayPlay.message('Successfully updated team <b>' + team.name + '</b> credit card for <b>' + team.game + '</b>.<br><br>Thank you for your support!'); | ||
$('form').remove(); | ||
}, | ||
error: PlayPlay.error | ||
}); | ||
} | ||
}); | ||
</script> | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Api | ||
module Endpoints | ||
class CreditCardsEndpoint < Grape::API | ||
format :json | ||
|
||
namespace :credit_cards do | ||
desc 'Updates a credit card.' | ||
params do | ||
requires :stripe_token, type: String | ||
optional :stripe_token_type, type: String | ||
optional :stripe_email, type: String | ||
requires :team_id, type: String | ||
end | ||
post do | ||
team = Team.find(params[:team_id]) || error!('Not Found', 404) | ||
error!('Not a Premium Customer', 400) unless team.stripe_customer_id | ||
customer = Stripe::Customer.retrieve(team.stripe_customer_id) | ||
customer.source = params['stripe_token'] | ||
customer.save | ||
Api::Middleware.logger.info "Updated credit card for team #{team}, email=#{params[:stripe_email]}." | ||
present team, with: Api::Presenters::TeamPresenter | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module SlackGamebot | ||
module Commands | ||
class Premium < SlackRubyBot::Commands::Base | ||
include SlackGamebot::Commands::Mixins::Premium | ||
|
||
premium_command 'premium' do |client, data, _match| | ||
user = ::User.find_create_or_update_by_slack_id!(client, data.user) | ||
customer = Stripe::Customer.retrieve(client.owner.stripe_customer_id) | ||
customer_info = "Customer since #{Time.at(customer.created).strftime('%B %d, %Y')}." | ||
customer.subscriptions.each do |subscription| | ||
customer_info += "\nSubscribed to #{subscription.plan.name} (#{ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100)})" | ||
end | ||
if user.captain? | ||
customer.invoices.each do |invoice| | ||
customer_info += "\nInvoice for #{ActiveSupport::NumberHelper.number_to_currency(invoice.amount_due.to_f / 100)} on #{Time.at(invoice.date).strftime('%B %d, %Y')}, #{invoice.paid ? 'paid' : 'unpaid'}." | ||
end | ||
customer.sources.each do |source| | ||
customer_info += "\nOn file #{source.brand} #{source.object}, #{source.name} ending with #{source.last4}, expires #{source.exp_month}/#{source.exp_year}." | ||
end | ||
customer_info += "\n#{client.owner.update_cc_text}" | ||
end | ||
client.say(channel: data.channel, text: customer_info) | ||
logger.info "PREMIUM: #{client.owner} - #{data.user}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
|
||
describe Api::Endpoints::CreditCardsEndpoint do | ||
include Api::Test::EndpointTest | ||
|
||
context 'credit cards' do | ||
it 'requires stripe parameters' do | ||
expect { client.credit_cards._post }.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['message']).to eq 'Invalid parameters.' | ||
expect(json['type']).to eq 'param_error' | ||
end | ||
end | ||
context 'premium team without a stripe customer id' do | ||
let!(:team) { Fabricate(:team, premium: true, stripe_customer_id: nil) } | ||
it 'fails to update credit_card' do | ||
expect do | ||
client.credit_cards._post( | ||
team_id: team._id, | ||
stripe_token: 'token') | ||
end.to raise_error Faraday::ClientError do |e| | ||
json = JSON.parse(e.response[:body]) | ||
expect(json['error']).to eq 'Not a Premium Customer' | ||
end | ||
end | ||
end | ||
context 'existing premium team' do | ||
include_context :stripe_mock | ||
let!(:team) { Fabricate(:team) } | ||
before do | ||
stripe_helper.create_plan(id: 'slack-playplay-yearly', amount: 2999) | ||
customer = Stripe::Customer.create( | ||
source: stripe_helper.generate_card_token, | ||
plan: 'slack-playplay-yearly', | ||
email: '[email protected]' | ||
) | ||
expect_any_instance_of(Team).to receive(:inform!).once | ||
team.update_attributes!(premium: true, stripe_customer_id: customer['id']) | ||
end | ||
it 'updates a credit card' do | ||
new_source = stripe_helper.generate_card_token | ||
client.credit_cards._post( | ||
team_id: team._id, | ||
stripe_token: new_source, | ||
stripe_token_type: 'card' | ||
) | ||
team.reload | ||
customer = Stripe::Customer.retrieve(team.stripe_customer_id) | ||
expect(customer.source).to eq new_source | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Update cc', js: true, type: :feature do | ||
let!(:game) { Fabricate(:game, name: 'pong') } | ||
|
||
shared_examples 'updates cc' do | ||
it 'updates cc' do | ||
visit "/update_cc?team_id=#{team.team_id}&game=#{team.game.name}" | ||
expect(find('h3')).to have_text('PLAYPLAY.IO: UPDATE CREDIT CARD INFO') | ||
|
||
customer = double | ||
expect(Stripe::Customer).to receive(:retrieve).and_return(customer) | ||
expect(customer).to receive(:source=) | ||
expect(customer).to receive(:save) | ||
|
||
click_button 'Update Credit Card' | ||
sleep 1 | ||
stripe_iframe = all('iframe[name=stripe_checkout_app]').last | ||
Capybara.within_frame stripe_iframe do | ||
page.find_field('Email').set '[email protected]' | ||
page.find_field('Card number').set '4012 8888 8888 1881' | ||
page.find_field('MM / YY').set '12/42' | ||
page.find_field('CVC').set '345' | ||
find('button[type="submit"]').click | ||
end | ||
|
||
sleep 5 | ||
|
||
expect(find('#messages')).to have_text("Successfully updated team #{team.name} credit card for #{team.game.name}. Thank you for your support!") | ||
end | ||
end | ||
context 'with a stripe key' do | ||
before do | ||
ENV['STRIPE_API_PUBLISHABLE_KEY'] = 'pk_test_804U1vUeVeTxBl8znwriXskf' | ||
end | ||
after do | ||
ENV.delete 'STRIPE_API_PUBLISHABLE_KEY' | ||
end | ||
context 'a team' do | ||
let!(:team) { Fabricate(:team, game: game, stripe_customer_id: 'stripe_customer_id') } | ||
it_behaves_like 'updates cc' | ||
end | ||
end unless ENV['CI'] # see https://github.com/dblock/slack-gamebot/issues/139 | ||
end |
Oops, something went wrong.