Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Prettied up the readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
barancw committed Aug 11, 2012
1 parent 47967ef commit 00db725
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 81 deletions.
81 changes: 0 additions & 81 deletions README

This file was deleted.

83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# config/initializers/stripe.rb

require 'stripe/model'

if Rails.env.production?
Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
STRIPE_PUBLISHABLE_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
else
Stripe.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
STRIPE_PUBLISHABLE_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end

STRIPE_PLAN_ID = "some-awesome-plan"
STRIPE_TRIAL_PERIOD = 1.month



# app/controllers/stripe_controller.rb

class StripeController < ApplicationController
def webhook
Account.subscription_event(params)
head :ok
end
end



# app/controllers/accounts_controller.rb

class AccountsController < ApplicationController
def cancel
current_account.subscription_cancel!
redirect_to settings_account_path, :notice => "Your account has been canceled."
end

def reactivate
current_account.subscription_reactivate!
redirect_to settings_account_path, :notice => "Your account has been reactivated."
end
end



# app/views/accounts/form.html.erb

You need to provide the following fields in your form:

* text_field_tag :credit_card_number, nil, :name => ""
* f.hidden_field :credit_card_token, :value => ""
* f.date_select :credit_card_expires_on, :start_year => Date.today.year, :add_month_numbers => true, :order => [:month, :year]

It doesn't matter what resource these form fields are on, if they use the above names the following JS will find them.

<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('<%= STRIPE_PUBLISHABLE_KEY %>');

$(document).ready(function() {
$("form.user_edit").submit(function(event) {
var form = $(this);
form.find(".billing-errors").html("");
if(form.find('#credit_card_number').val()) {
form.find('[type=submit]').attr("disabled", "disabled");
Stripe.createToken({
number: form.find('[id*=credit_card_number]').val(),
exp_month: form.find('[id*=credit_card_expires_on_2i]').val(),
exp_year: form.find('[id*=credit_card_expires_on_1i]').val(),
coupon: form.find('[name*=coupon]').val()
}, function(status, response) {
if (response.error) {
form.find(".billing-errors").html(response.error.message);
form.find('[type=submit]').removeAttr("disabled");
} else {
form.find("[name*=credit_card_token]").val(response['id']);
form.get(0).submit();
}
});
return false;
}
});
});
</script>

0 comments on commit 00db725

Please sign in to comment.