| ### registrations.controller: | |
| class RegistrationsController < ApplicationController | |
| before_action :set_registration, only: [:show, :edit, :update, :destroy] | |
| # GET /registrations | |
| def index | |
| @registrations = Registration.all | |
| end | |
| # GET /registrations/1 | |
| def show | |
| end | |
| # GET /registrations/new | |
| def new | |
| @registration = Registration.new | |
| @level = Level.find_by id: params["level_id"] | |
| end | |
| # POST /registrations | |
| def create | |
| @registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], | |
| card_token: stripe_params["stripeToken"]) | |
| raise "Please, check registration errors" unless @registration.valid? | |
| @registration.process_payment(stripe_params["stripeEmail"], stripe_params["stripeToken"]) | |
| @registration.save | |
| redirect_to @registration, notice: 'Registration was successfully created.' | |
| end | |
| private | |
| def stripe_params | |
| params.permit :stripeEmail, :stripeToken | |
| end | |
| # Use callbacks to share common setup or constraints between actions. | |
| def set_registration | |
| @registration = Registration.find(params[:id]) | |
| end | |
| # Never trust parameters from the scary internet, only allow the white list through. | |
| def registration_params | |
| params.require(:registration).permit(:level_id, :firstname, :lastname, :phone, :email, :card_token) | |
| end | |
| end | |
| ### registration model | |
| class Registration < ActiveRecord::Base | |
| belongs_to :level | |
| def process_payment(email, card_token) | |
| customer = Stripe::Customer.create email: email, | |
| card: card_token | |
| Stripe::Charge.create customer: customer.id, | |
| amount: level.price*100, | |
| description: level.name, | |
| currency: 'usd' | |
| end | |
| end | |
| ## haml view | |
| = javascript_include_tag "https://js.stripe.com/v2/" | |
| :javascript | |
| Stripe.setPublishableKey('#{Rails.application.secrets.stripe_publishable_key}'); | |
| %section#course-content | |
| %section#ruby | |
| %section.detailed-syllabus | |
| .wrapper-inside | |
| .container | |
| .row | |
| .col-md-4 | |
| .col-md-4 | |
| #flash-messages | |
| = form_for @registration, html: { class: "basic-grey" } do |f| | |
| - if @registration.errors.any? | |
| #error_explanation | |
| %h2 | |
| = pluralize(@registration.errors.count, "error") | |
| prohibited this registration from being saved: | |
| %ul | |
| - @registration.errors.full_messages.each do |message| | |
| %li= message | |
| .field | |
| = f.hidden_field :level_id, value: @level.id | |
| .field | |
| = f.label :"First Name" | |
| = f.text_field :firstname | |
| .field | |
| = f.label :"Last Name" | |
| = f.text_field :lastname | |
| .field | |
| = f.label :Phone | |
| = f.text_field :phone | |
| .field | |
| = f.label :Email | |
| = f.text_field :email | |
| = label_tag "Card Number", nil, required: true | |
| = text_field_tag :card_number, nil, class: "input-block-level", "data-stripe" => "number" | |
| = label_tag "Card Verification (CVV)", nil, required: true | |
| = text_field_tag :card_verification, nil, class: "input-block-level", "data-stripe" => "cvv" | |
| = label_tag "Card Expiration Date", nil, required: true | |
| = select_tag :exp_month, options_for_select(Date::MONTHNAMES.compact.each_with_index.map { |name,i| ["#{i+1} - #{name}", i+1] }), include_blank: false, "data-stripe" => "exp-month", class: "span2" | |
| = select_tag :exp_year, options_for_select((Date.today.year..(Date.today.year+10)).to_a), include_blank: false, "data-stripe" => "exp-year", class: "span1" | |
| .actions | |
| = f.submit "Register for The Climb", class: "btn", style: "color: white;background: #c92624;width: 50%;margin-top: 20px;" | |
| ### card-checkout.js | |
| jQuery(function ($) { | |
| var show_error, stripeResponseHandler; | |
| $("#new_registration").submit(function (event) { | |
| var $form; | |
| $form = $(this); | |
| $form.find("input[type=submit]").prop("disabled", true); | |
| Stripe.card.createToken($form, stripeResponseHandler); | |
| return false; | |
| }); | |
| stripeResponseHandler = function (status, response) { | |
| var $form, token; | |
| $form = $("#new_registration"); | |
| if (response.error) { | |
| show_error(response.error.message); | |
| $form.find("input[type=submit]").prop("disabled", false); | |
| } else { | |
| token = response.id; | |
| $form.append($("<input type=\"hidden\" name=\"registration[card_token]\" />").val(token)); | |
| $("[data-stripe=number]").remove(); | |
| $("[data-stripe=cvv]").remove(); | |
| $("[data-stripe=exp-year]").remove(); | |
| $("[data-stripe=exp-month]").remove(); | |
| $form.get(0).submit(); | |
| } | |
| return false; | |
| }; | |
| show_error = function (message) { | |
| $("#flash-messages").html('<div class="alert alert-warning"><a class="close" data-dismiss="alert">×</a><div id="flash_alert">' + message + '</div></div>'); | |
| $('.alert').delay(5000).fadeOut(3000); | |
| return false; | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment