-
-
Save tmorton/17bd647b35861ea57bc758e12ca45752 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentNotification < ActiveRecord::Base | |
belongs_to :cart | |
belongs_to :product | |
serialize :params | |
after_create :mark_cart_as_purchased, :update_qty | |
# after_create :send_mail | |
private | |
def mark_cart_as_purchased | |
if status == "Completed" | |
cart.update_attribute(:purchased_at, Time.current) | |
end | |
end | |
def send_mail | |
if status == "Completed" && cart.purchased_at | |
OrderNotifier.received(@order).deliver_now | |
OrderNotifier.notify(@order).deliver_now | |
end | |
end | |
def update_qty | |
if status == "Completed" | |
line_item = LineItem.find_by(id: line_item_id) | |
line_item.upd(params[:quantity]) | |
line_item.update_attribute(:status, "purchased") | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentNotificationsController < ApplicationController | |
protect_from_forgery except: [:create] | |
before_action :extract_ipn_items_params, only: [:create] | |
ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity", "option_name"] | |
def create | |
extracted_values = extract_ipn_items_params | |
# Now use extracted_values instead of params in the stuff below | |
PaymentNotification.create!(params: params, cart_id: params[:invoice], status: params[:payment_status], transaction_id: params[:txn_id], | |
#line_item_id: params[:item_number], product_title: params[:item_name], option_name: params[:option_name], quantity: params[:quantity], | |
first_name: params[:first_name], last_name: params[:last_name], email: params[:payer_email], address_name: params[:address_name], | |
address_street: params[:address_street], address_city: params[:address_city], address_state: params[:address_state], | |
address_zip: params[:address_zip], address_country: params[:address_country]) | |
render nothing: true | |
end | |
def details | |
params.extract_ipn_items_params | |
PaymentNotification.update_attributes(line_item_id: params[:item_number], product_title: params[:item_name], option_name: params[:option_name], quantity: params[:quantity]) | |
end | |
private | |
def extract_ipn_items_params | |
mod_params = Hash.new{|k, v| k[v] = {} } | |
ITEM_PARAM_PREFIXES.each do |item_data_key| | |
key_tracker = 1 | |
loop do | |
current_key = (item_data_key + key_tracker.to_s).to_sym | |
if params.include? current_key | |
mod_params[key_tracker][item_data_key] = params[current_key] | |
else | |
break | |
end | |
key_tracker += 1 | |
end | |
end | |
mod_params | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment