Permalink
Browse files

Refactoring to support upcoming Spree 0.6.0 release. NOTE: More work …

…to be done before this works properly.
  • Loading branch information...
1 parent 1efe0d2 commit 0626b9ca0e8139f47c56f6e2c61d63190da9fe84 @schof schof committed Jan 28, 2009
View
@@ -1,5 +1,11 @@
-1.1
----
+0.5.x
+-----
* `checkout_controller` is obsolete now that Spree has been refactored to use REST.
* `after_notify` and `after_success` hooks are gone. See the `README` for how to implement them as hooks in the "fat" order model.
+
+0.6.x
+-----
+
+* Significant database changes, do not run migrations until you have backed up your payment data.
+* Migrations will not port over your old payments, its suggested you write your own migration to do this in order to preserve legacy payments.
View
@@ -23,15 +23,41 @@ Order.class_eval do
end
end
</pre>
+
+# Configuration
- * TODO: User account creation (if necessary) after notify and associate order with a user
- * TODO: Make the paypal account stuff configurable via new preferences system
- * TODO: Taxes
- * TODO: Shipping
- * TODO: Refunds
+Be sure to configure the following configuration parameters.
+
+Example
+
+<pre>
+Spree::Paypal::Config[:account] = "foo@example.com"
+Spree::Paypal::Config[:ipn_notify_host] = "http://123.456.78:3000"
+Spree::Paypal::Config[:success_url] = "http://localhost:3000/checkout/success"
+</pre>
+
+Or even better, you can configure these in a migration for your site extension.
+
+<pre>
+class AddPaypalStandardConfigurations < ActiveRecord::Migration
+ def self.up
+ Spree::Paypal::Config.set(:account => "foo@example.com")
+ Spree::Paypal::Config.set(:ipn_notify_host => "http://123.456.78:3000")
+ Spree::Paypal::Config.set(:success_url => "http://localhost:3000/checkout/success")
+ end
+
+ def self.down
+ end
+end
+</pre>
# Installation
<pre>
script/extension install git://github.com/Gregg/spree-pp-website-standard.git
-</pre>
+</pre>
+
+* TODO: User account creation (if necessary) after notify and associate order with a user
+* TODO: Taxes
+* TODO: Shipping
+* TODO: Refunds
@@ -4,7 +4,7 @@ class PaypalPaymentsController < Spree::BaseController
before_filter :load_object, :only => :successful
layout 'application'
- resource_controller :singleton
+ resource_controller
belongs_to :order
# NOTE: The Paypal Instant Payment Notification (IPN) results in the creation of a PaypalPayment
@@ -56,18 +56,19 @@ class PaypalPaymentsController < Spree::BaseController
def successful
@order.update_attribute("ip_address", request.env['REMOTE_ADDR'] || "unknown")
# its possible that the IPN has already been received at this point so that
- unless @order.paypal_payment
+ if @order.paypal_payments.empty?
# create a payment and record the successful transaction
- paypal_payment = PaypalPayment.create(:order => @order, :email => params[:payer_email], :payer_id => params[:payer_id])
- @order.paypal_payment = paypal_payment
+ paypal_payment = @order.paypal_payments.create(:email => params[:payer_email], :payer_id => params[:payer_id])
paypal_payment.txns.create(:amount => params[:mc_gross].to_d,
:status => "Processed",
:transaction_id => params[:txn_id],
:fee => params[:payment_fee],
:currency_type => params[:mc_currency],
:received_at => params[:payment_date])
# advance the state
- @order.pend_payment!
+ @order.pend_payment!
+ else
+ paypal_payment = @order.paypal_payments.last
end
# remove order from the session (its not really practical to allow the user to edit the session anymore)
@@ -78,7 +79,7 @@ def successful
redirect_to order_url(@order) and return
else
flash[:notice] = "Please create an account or login so we can associate this order with an account"
- session[:return_to] = "#{order_url(@order)}?payer_id=#{@order.paypal_payment.payer_id}"
+ session[:return_to] = "#{order_url(@order)}?payer_id=#{paypal_payment.payer_id}"
redirect_to signup_path
end
end
@@ -1,6 +1,4 @@
-class PaypalPayment < ActiveRecord::Base
- has_many :paypal_txns
- belongs_to :order
-
+class PaypalPayment < Payment
+ has_many :paypal_txns
alias :txns :paypal_txns
end
@@ -20,17 +20,21 @@
<input id="cmd" name="cmd" type="hidden" value="_cart" />
<input type="hidden" name="upload" value="1" />
-<p> Please select your country <select name="country" id="country">
-<%= country_options_for_select('United States', ['United States']) %>
-</select></p>
-<span style="color:red; font-weight:bold; display:none;" id="needZipcode">Please enter a valid zipcode.</span>
+<p> <%= t('please_select_your_country') %>
+ <select name="country" id="country">
+ <%= options_from_collection_for_select(Country.all, :id, :name, 214) %>
+ </select>
+</p>
+<span style="color:red; font-weight:bold; display:none;" id="needZipcode">
+ <%= t("please_enter_valid_zip") %>
+</span>
<p>
-Zipcode (if you have one): <input id="zip" name="zip" type="text" value="" />
+<%= t('zip_code_if_you_have_one')%>: <input id="zip" name="zip" type="text" value="" />
</p>
-<input id="notify_url" name="notify_url" type="hidden" value="<%= Spree::Paypal::Config[:ipn_notify_host] + order_paypal_payment_path(@order) %>" />
+<input id="notify_url" name="notify_url" type="hidden" value="<%= Spree::Paypal::Config[:ipn_notify_host] + order_paypal_payments_path(@order) %>" />
<input type="hidden" name="rm" value ="2"> <!-- tells paypal that the return should be POST instead of GET -->
-<input id="return" name="return" type="hidden" value="<%= successful_order_paypal_payment_url(@order) %>" />
+<input id="return" name="return" type="hidden" value="<%= successful_order_paypal_payments_url(@order) %>" />
<!-- input id="address_override" name="address_override" type="hidden" value="0" />
<input id="charset" name="charset" type="hidden" value="utf-8" />
@@ -1,19 +1,19 @@
<%=error_messages_for :order%>
-<h1><%= t("Shopping Cart")%></h1>
+<h1><%= t("shopping_cart")%></h1>
<% form_for(:order, :url => object_url, :html => { :method => :put}) do |f| %>
<%= render :partial => 'form', :locals => {:f => f} %>
<div id="subtotal">
- <h3><%= "#{t("Subtotal")}: #{order_price(@order)}" %></h3>
- <%= submit_tag t('Update') %>
+ <h3><%= "#{t("subtotal")}: #{order_price(@order)}" %></h3>
+ <%= submit_tag t('update') %>
</div>
- <p><%=link_to t("Continue Shopping"), products_path %></p>
+ <p><%=link_to t("continue_shopping"), products_path %></p>
<% unless @order.line_items.empty? %>
<p id="clear_cart_link">
<small>
- <%= link_to t("Empty Cart"), object_url, :method => :delete %>
+ <%= link_to t("empty_cart"), object_url, :method => :delete %>
</small>
</p>
<% end %>
View
@@ -0,0 +1,7 @@
+---
+en-US:
+ please_select_your_country: "Please select your country"
+ please_enter_valid_zip: "Please enter a valid zipcode."
+ zip_code_if_you_have_one: "Zipcode (if you have one)"
+ payment_failure: "Payment Failure"
+ payment_pending: "Payment Pending"
View
@@ -0,0 +1,5 @@
+map.resources :orders do |order|
+ # we're kind of abusing the notion of a restful collection here but we're in the weird position of
+ # not being able to create the payment before sending the request to paypal
+ order.resources :paypal_payments, :collection => {:successful => :post}
+end
@@ -0,0 +1,13 @@
+class RefactorPaymentsAsSti < ActiveRecord::Migration
+ def self.up
+ change_table :payments do |t|
+ t.string :email
+ t.string :payer_id
+ end
+ drop_table :paypal_payments
+ end
+
+ def self.down
+ # No going back!
+ end
+end
@@ -5,8 +5,8 @@ class PaypalConfiguration < Configuration
preference :paypal_url, :string, :default => "https://www.paypal.com/cgi-bin/webscr"
# these are just default preferences of course, you'll need to change them to something meaningful
- preference :account, :string, :default => "kevin@schoftech.net"
- preference :ipn_notify_host, :string, :default => "http://96.255.82.213:3000"
+ preference :account, :string, :default => "foo@example.com"
+ preference :ipn_notify_host, :string, :default => "http://123.456.78:3000"
preference :success_url, :string, :default => "http://localhost:3000/checkout/success"
validates_presence_of :name
@@ -1,25 +1,17 @@
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application'
-
+=begin
unless RAILS_ENV == 'production'
PAYPAL_ACCOUNT = 'joe@bidness.com'
ActiveMerchant::Billing::Base.mode = :test
else
PAYPAL_ACCOUNT = 'Gregg@railsenvy.com'
end
-
+=end
class PpWebsiteStandardExtension < Spree::Extension
- version "1.1"
+ version "0.6.x"
description "Describe your extension here"
- url "http://yourwebsite.com/spree_pp_website_standard"
-
- define_routes do |map|
- map.resources :orders do |order|
- # we're kind of abusing the notion of a restful collection here but we're in the weird position of
- # not being able to create the payment before sending the request to paypal
- order.resource :paypal_payment, :collection => {:successful => :post}
- end
- end
+ url "http://github.com/Gregg/spree-pp-website-standard/tree/master"
def activate
@@ -38,7 +30,7 @@ def add_pp_standard_txns
private
def associate_order
return unless payer_id = params[:payer_id]
- orders = Order.find(:all, :include => :paypal_payment, :conditions => ['paypal_payments.payer_id = ? AND orders.user_id is null', payer_id])
+ orders = Order.find(:all, :include => :paypal_payments, :conditions => ['payments.payer_id = ? AND orders.user_id is null', payer_id])
orders.each do |order|
order.update_attribute("user", current_user)
end
@@ -54,17 +46,11 @@ def associate_order
fsm.events["pend_payment"].transition(:to => 'payment_pending', :from => 'in_progress')
fsm.after_transition :to => 'payment_pending', :do => lambda {|order| order.update_attribute(:checkout_complete, true)}
- fsm.events["pay"] = PluginAWeek::StateMachine::Event.new(fsm, "pay")
fsm.events["pay"].transition(:to => 'paid', :from => ['payment_pending', 'in_progress'])
- fsm.after_transition :to => 'paid', :do => :complete_order
-
- fsm.events["ship"].transition(:to => 'shipped', :from => 'paid')
-
- # add a PaypalPayment association to the Order model
+
Order.class_eval do
- has_one :paypal_payment
+ has_many :paypal_payments
end
-
end
def deactivate

0 comments on commit 0626b9c

Please sign in to comment.