Skip to content

Instantly share code, notes, and snippets.

@tgoldenberg
Last active August 29, 2015 14:21
Show Gist options
  • Save tgoldenberg/3a488c16438903b79928 to your computer and use it in GitHub Desktop.
Save tgoldenberg/3a488c16438903b79928 to your computer and use it in GitHub Desktop.
### requests/new.html.haml
= simple_form_for @request, remote: true do |f|
- if @request.errors.any?
#error_explanation
%h2= "#{pluralize(@request.errors.count, "error")} prohibited this request from being saved:"
%ul
- @request.errors.full_messages.each do |msg|
%li= msg
.field
= f.label 'Message for mentor*(optional)'
= f.text_area :body
.field
= f.hidden_field :sender_id, value: current_user.id
.field
= f.hidden_field :recipient_id, value: params[:recipient_id]
.field{style: 'text-align: center;'}
%p
To process your request we need to save your <em>payment information</em>. Click below to save your information with our secure payment gateway, <b>Stripe</b>.
%p
You will not be charged until after the instruction, and can cancel at <b>any</b> time.
= link_to "Connect to Stripe", '#', class: 'button button-stripe'
%span.glyphicon.glyphicon-check.red Pending
.field{style: 'text-align: center; width: 100%;'}
= f.hidden_field :stripe
.field
= f.hidden_field :time, id: 'time-field'
.actions
= f.submit 'Save', class: 'button'
= simple_form_for [@request, @charge], remote: true do |f|
= f.hidden_field :price, value: @request.recipient.price
= f.hidden_field :description, value: @request.recipient.name
= f.hidden_field :vendor_id, value: @request.recipient.id
= f.hidden_field :request_id, value: @request.id
%script{src:"https://checkout.stripe.com/checkout.js", class:"stripe-button", data: {key: ENV["stripe_publishable_key"], amount:"#{@request.recipient.price*100}", name:"ChessMentor", description: "#{number_to_currency(@request.recipient.price)}", remote: true, image: "/assets/landing_background.png"}}
:javascript
$(function() {
$('.click-date').click(function() {
var dateTime = " " + $(this).attr('class').match(/sunday|monday|tuesday|wednesday|thursday|friday|saturday/).toString();
dateTime += " " + $(this).parent('tr').attr('class').match(/morning|afternoon|evening/).toString() + " -";
var timeField = $('#time-field').val();
console.log(dateTime);
if ($(this).hasClass('grey-highlight')) {
$('#time-field').val(timeField + dateTime);
console.log($('#time-field').val());
} else if ($(this).hasClass('green-highlight')) {
console.log(dateTime);
timeField = timeField.replace(dateTime, "");
$('#time-field').val(timeField);
console.log($('#time-field').val());
}
$(this).toggleClass('grey-highlight');
$(this).toggleClass('green-highlight');
$(this).find('.glyphicon-ok-circle').toggle();
});
});
### charges_controller.rb
class ChargesController < ApplicationController
respond_to :html, :js
layout false
def create
customer = Stripe::Customer.create(
:email => '[email protected]',
:card => params[:stripeToken]
)
@charge = Charge.new(
:price => params[:charge]["amount"].to_i,
:user_id => current_user.id,
:vendor_id => params[:charge]["vendor_id"].to_i,
:description => params[:charge]["description"],
:token => params[:stripeToken],
:customer_id => customer.id,
:request_id => params[:request_id]
)
@charge.save
render json: {back: true}
end
def complete
@charge = Charge.find(params[:charge_id])
@request = @charge.request
Stripe.api_key = ENV["stripe_api_key"]
token = params[:token]
charge = Stripe::Charge.create({
:amount => @charge.price*100,
:description => 'ChessMentor customer',
:customer => params[:customer_id],
:currency => 'usd',
:destination => @charge.vendor.uid,
:application_fee => 200 + (@charge.price*3) + 31
})
@charge.update_attribute(:completed, true)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to root_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment