Last active
December 16, 2015 01:49
-
-
Save coderforhire/5358370 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
<%= form_for(@order) do |f| %> | |
<%= hidden_field_tag :stripe_card_token %> | |
<div class="field"> | |
</div> | |
<div class="field"> | |
<% if @order.stripe_card_token.present? %> | |
Credit card has been provided. | |
<% else %> | |
<%= label_tag :card_number, "Credit Card Number" %> | |
<%= text_field_tag :card_number, nil, name: nil %> | |
</div> | |
<div class="field"> | |
<%= label_tag :card_code, "Security Code on Card (CVV)" %> | |
<%= text_field_tag :card_code, nil, name: nil %> | |
</div> | |
<div class="field"> | |
<%= label_tag :card_month, "Card Expiration" %> | |
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %> | |
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %> | |
<%= @shopping_cart %> | |
<%= hidden_field_tag :shopping_cart_id, @shopping_cart.id %> | |
<div id="stripe_error"> | |
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> | |
</div> | |
<%= f.submit %> | |
<% 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
<form accept-charset="UTF-8" action="/orders" class="new_order" id="new_order" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="bPDWYB9zWTxFMnlqX9o8dMqRhU915md9f+NUH8xa3R4=" /></div> | |
<input id="stripe_card_token" name="stripe_card_token" type="hidden" /> | |
<div class="field"> | |
</div> | |
<div class="field"> | |
<label for="card_number">Credit Card Number</label> | |
<input id="card_number" type="text" /> | |
</div> | |
<div class="field"> | |
<label for="card_code">Security Code on Card (CVV)</label> | |
<input id="card_code" type="text" /> | |
</div> | |
<div class="field"> | |
<label for="card_month">Card Expiration</label> | |
<select id="card_month"> | |
<option value="1">1 - January</option> | |
<option value="2">2 - February</option> | |
<option value="3">3 - March</option> | |
<option value="4">4 - April</option> | |
<option value="5">5 - May</option> | |
<option value="6">6 - June</option> | |
<option value="7">7 - July</option> | |
<option value="8">8 - August</option> | |
<option value="9">9 - September</option> | |
<option value="10">10 - October</option> | |
<option value="11">11 - November</option> | |
<option value="12">12 - December</option> | |
</select> | |
<select id="card_year"> | |
<option value="2013">2013</option> | |
<option value="2014">2014</option> | |
<option value="2015">2015</option> | |
<option value="2016">2016</option> | |
<option value="2017">2017</option> | |
<option value="2018">2018</option> | |
<option value="2019">2019</option> | |
<option value="2020">2020</option> | |
<option value="2021">2021</option> | |
<option value="2022">2022</option> | |
<option value="2023">2023</option> | |
<option value="2024">2024</option> | |
<option value="2025">2025</option> | |
<option value="2026">2026</option> | |
<option value="2027">2027</option> | |
<option value="2028">2028</option> | |
</select> | |
#<ShoppingCart:0x007f0f281a0430> | |
<input id="shopping_cart_id" name="shopping_cart_id" type="hidden" value="1" /> | |
<div id="stripe_error"> | |
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> | |
</div> | |
<input name="commit" type="submit" value="Create Order" /> | |
</form> | |
<div> | |
<div>test product11</div> | |
<div>9.99</div> | |
1 | |
</div> | |
<div> | |
<div>test1 product</div> | |
<div>9.99</div> | |
2 | |
</div> | |
<div> | |
<div><b>SubTotal:</b>$139.86</div> | |
<div><b>Taxes:</b>$21.45</div> | |
<div><b>Total:</b>$161.31</div> | |
<div> | |
</body> | |
</html> |
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 Order < ActiveRecord::Base | |
attr_accessible :transaction_id, :amount | |
belongs_to :user | |
has_many :products | |
has_one :transaction | |
accepts_nested_attributes_for :transaction | |
def save_with_payment(total) | |
self.transaction.amount = total | |
if valid? | |
customer = Stripe::Customer.create(description: '[email protected]', card: stripe_card_token) | |
puts stripe_card_token | |
self.stripe_customer_token = customer.id | |
save! | |
end | |
rescue Stripe::InvalidRequestError => e | |
logger.error "Stripe error while creating customer: #{e.message}" | |
errors.add :base, "There was a problem with your credit card." | |
false | |
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
jQuery -> | |
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) | |
subscription.setupForm() | |
order = | |
setupForm: -> | |
$('#new_order').submit -> | |
$('input[type=submit]').attr('disabled', true) | |
if $('#card_number').length | |
order.processCard() | |
false | |
else | |
true | |
processCard: -> | |
card = | |
number: $('#card_number').val() | |
cvc: $('#card_code').val() | |
expMonth: $('#card_month').val() | |
expYear: $('#card_year').val() | |
Stripe.createToken(card, subscription.handleStripeResponse) | |
handleStripeResponse: (status, response) -> | |
if status == 200 | |
$('#order_stripe_card_token').val(response.id) | |
$('#new_order')[0].submit() | |
else | |
$('#stripe_error').text(response.error.message) | |
$('input[type=submit]').attr('disabled', false) |
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
def create | |
@order = Order.new | |
@order.user = current_user | |
@order.transaction = Transaction.new | |
@shopping_cart = ShoppingCart.find_by_id(params[:shopping_cart_id]) | |
total = @shopping_cart.total | |
puts @shopping_cart.total | |
respond_to do |format| | |
if @order.save_with_payment(total) | |
format.html { redirect_to @order, notice: 'Order was successfully created.' } | |
format.json { render json: @order, status: :created, location: @order } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @order.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment