Permalink
Browse files

some more cleanup and tweaking. still not there, but pretty usable ATM

  • Loading branch information...
1 parent 1834729 commit a05a6b446d1b11d3655dff733232f3adf5a7c84f @tomash tomash committed Dec 12, 2010
@@ -0,0 +1,22 @@
+# commented-out as this is too invasive at the moment
+# (assumes paypal is the only payment method available in your store)
+
+=begin
+CheckoutController.class_eval do
+
+ def edit
+ if (@order.valid? && @order.state == "payment")
+ puts "valid, processing"
+ if @order.payable_via_paypal?
+ puts "payable via paypal, adding payment"
+ payment = Payment.new
+ payment.amount = @order.total
+ payment.payment_method = Order.paypal_payment_method
+ @order.payments << payment
+ payment.started_processing
+ render(:partial => 'checkout/paypal_checkout')
+ end
+ end
+ end
+end
+=end
@@ -1,29 +1,25 @@
-class PaymentNotificationsController < CheckoutController
+class PaymentNotificationsController < ApplicationController
protect_from_forgery :except => [:create]
skip_before_filter :restriction_access
def create
@order = Order.find_by_number(params[:invoice])
PaymentNotification.create!(:params => params,
- :order_id => Order.number_eq(params[:invoice]).first,
+ :order_id => @order.id,
:status => params[:payment_status],
:transaction_id => params[:txn_id])
# this logging stuff won't live here for long...
- logger.info("created PP Notification for order #{@order.number} (#{@order.id}). thread #{Thread.current.to_s}")
-
Order.transaction do
# main part of hacks
order = @order
order.payment.complete
logger.info("order #{order.number} (#{order.id}) -- completed payment")
while order.state != "complete"
- logger.info("advancing state of Order #{order.number} (#{order.id}). current state #{order.state}. thread #{Thread.current.to_s}")
order.next
logger.info("advanced state of Order #{order.number} (#{order.id}). current state #{order.state}. thread #{Thread.current.to_s}. issuing callback")
state_callback(:after) # that line will run all _not run before_ callbacks
- logger.info("Order #{order.number} (#{order.id}) -- callback complete")
end
order.update_totals
order.update!
@@ -32,5 +28,36 @@ def create
render :nothing => true
end
+
+ private
+
+ # those methods are copy-pasted from CheckoutController
+ # we cannot inherit from that class unless we want to skip_before_filter
+ # half of calls in SpreeBase module
+ def state_callback(before_or_after = :before)
+ method_name = :"#{before_or_after}_#{@order.state}"
+ send(method_name) if respond_to?(method_name, true)
+ end
+
+ def before_address
+ @order.bill_address ||= Address.new(:country => default_country)
+ @order.ship_address ||= Address.new(:country => default_country)
+ end
+
+ def before_delivery
+ @order.shipping_method ||= (@order.rate_hash.first && @order.rate_hash.first[:shipping_method])
+ end
+
+ def before_payment
+ current_order.payments.destroy_all if request.put?
+ end
+
+ def after_complete
+ session[:order_id] = nil
+ end
+
+ def default_country
+ Country.find Spree::Config[:default_country_id]
+ end
end
@@ -4,4 +4,12 @@
def shipment_cost
adjustment_total - credit_total
end
+
+ def payable_via_paypal?
+ !!self.class.paypal_payment_method
+ end
+
+ def self.paypal_payment_method
+ PaymentMethod.available(:front_end).select{ |pm| pm if pm.name.downcase =~ /paypal/}.first
+ end
end
@@ -7,7 +7,7 @@ class PaymentNotification < ActiveRecord::Base
def mark_order_as_paid
if(status == "Completed")
- logger.info "Order #{self.order.number} should be marked as paid now -- IPN status 'Completed'"
+ logger.info "Order #{order.number} should be marked as paid now -- IPN status 'Completed'"
#order.update_attributes({:paid_on => Date.today, :status_id => Status.PAID.id})
end
end

0 comments on commit a05a6b4

Please sign in to comment.