Screencast: Spree – Einstieg in Ecommerce mit Rails

Spree ist eine open-source e-Commerce-Lösung auf Basis von Rails und wurde von Sean Schofield entwickelt. Inzwischen wird es von einer Community gepflegt ist aktuell die meistgenutzte Shop-Lösung im Rails-Umfeld. Ryan bietet diese Woche einen Einstieg in das Framework.

 

Downloads in verschiedenen Formaten:

mp4
m4v
webm
ogg

 

Resourcen:

bash

[bash]
brew install imagemagick
rails new store
bundle
rails g spree:site
rake db:bootstrap
[/bash]

Gemfile

[ruby]
gem ’spree‘, ‚0.70.1‘
gem ’spree_blue_theme‘, :git => ‚git://github.com/spree/spree_blue_theme.git‘
[/ruby]

config/initializers/spree_config.rb

[ruby]
Spree::Config.set(logo: "store/rails.png")
[/ruby]

overrides/logo.rb

[ruby]
Deface::Override.new(:virtual_path => "layouts/spree_application",
:name => "logo",
:replace_contents => "#logo",
:text => "Store")
[/ruby]

app/assets/stylesheets/store/all.css

[css]
/*
*= require store/screen
* …
*/
[/css]

app/assets/stylesheets/store/layout.css.scss

[css]
#logo {
font-size: 32px;
color: #FFF;
}
[/css]

Screencast: Abrechnungen in Rails mit Stripe

Abrechnungen werden von verschiedenen Bezahlsystemen angeboten. Je nach Umfang der Anforderungen kann dies ein komplexe Integrationsarbeit erfordern. Stripe versucht an dieser Stelle zu helfen und den gesammten Prozess zu vereinfachen und ist zusätzlich relativ günstig. Ryan zeigt in seinem Screencast wie es installiert und eingesetzt werden kann.

 

Downloads in verschiedenen Formaten:

mp4
m4v
webm
ogg

 

Resourcen:

Gemfile

[ruby]
gem ’stripe‘
[/ruby]

config/initializers/stripe.rb

[ruby]
Stripe.api_key = "tGaNWsIG3Qy6zvXB8wv4rEcizJpSXjF4"
STRIPE_PUBLIC_KEY = "pk_KcSyS2qPWdT5SdrwkQg0vNSyhZgqP"
[/ruby]

layouts/application.html.erb

[html]
<%= javascript_include_tag "https://js.stripe.com/v1/", "application" %>
<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>
[/html]

subscriptions/new.html.erb

[html]
<%= f.hidden_field :stripe_card_token %>

<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<% if @subscription.stripe_card_token.present? %>
Credit card has been provided.
<% else %>
<div class="field">
<%= 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"} %>
</div>
<% end %>
<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>
[/html]

app/assets/javascripts/subscriptions.js.coffee

[javascript]

jQuery ->
Stripe.setPublishableKey($(‚meta[name="stripe-key"]‘).attr(‚content‘))
subscription.setupForm()

subscription =
setupForm: ->
$(‚#new_subscription‘).submit ->
$(‚input[type=submit]‘).attr(‚disabled‘, true)
if $(‚#card_number‘).length
subscription.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
$(‚#subscription_stripe_card_token‘).val(response.id)
$(‚#new_subscription‘)[0].submit()
else
$(‚#stripe_error‘).text(response.error.message)
$(‚input[type=submit]‘).attr(‚disabled‘, false)
[/javascript]

controllers/subscriptions_controller.rb

[ruby]
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
[/ruby]

models/subscription.rb

[ruby]
attr_accessor :stripe_card_token

def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: 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
[/ruby]

bash

[bash]
rails g migration add_stripe_to_subscriptions stripe_customer_token:string
rake db:migrate
[/bash]