Last active
December 27, 2015 02:09
-
-
Save jcutrell/7250783 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Stripe::StripeTransactionsController < ActionController::Base | |
layout "stripe/layout" | |
before_filter :authorize! | |
def create | |
@stripe_transaction = StripeTransaction.create(transaction_params[:stripe_transaction]) | |
puts @stripe_transaction | |
if current_user.stripe_cards.empty? | |
session[:return_to] = cart_checkout_path | |
redirect_to new_stripe_card_path and return | |
end | |
@stripe_transaction.stripe_card_id = (params[:stripe_card_id] || current_user.default_stripe_card_id) | |
@stripe_transaction.amount = @stripe_transaction.line_items.inject(0) {|sum, item| sum = sum+item.amount.to_i } | |
itemlist = @stripe_transaction.line_items.map {|item| "[#{item.purchasable_id} #{item.purchasable_type} #{item.purchasable.amount}]"}.join(", ") | |
price_cents = @stripe_transaction.price*100 | |
charge = Stripe::Charge.create( | |
:amount => price_cents, | |
:currency => "usd", | |
:card => current_user.stripe_customer_id, # obtained with Stripe.js | |
:description => itemlist | |
) | |
if charge.id | |
@stripe_transaction.stripe_charge_id = charge.id | |
@stripe_transaction.stripe_card_id = StripeCard.where(:api_card_id => charge.card.id).first.id | |
@stripe_transaction.notes = itemlist + "- Successfully completed transaction" | |
@stripe_transaction.completed = true | |
@stripe_transaction.save | |
else | |
@stripe_transaction.completed = false | |
@stripe_transaction.notes = "The transaction did not complete. Please try again." | |
end | |
end | |
end |
This file contains hidden or 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
class StripeTransaction < ActiveRecord::Base | |
has_many :line_items | |
accepts_nested_attributes_for :line_items | |
belongs_to :user | |
belongs_to :stripe_card | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment