Skip to content

Instantly share code, notes, and snippets.

@d1t
Last active June 26, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save d1t/7f3a9451500579da617134468beeca96 to your computer and use it in GitHub Desktop.
Save d1t/7f3a9451500579da617134468beeca96 to your computer and use it in GitHub Desktop.
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
class PaymentNotificationsController < ApplicationController
protect_from_forgery except: [:create]
ITEM_PARAM_PREFIXES = ["item_name", "item_number", "quantity", "option_name1_"]
def create
e_values = extract_ipn_items_params
e_values.each do |key, value|
puts key
puts value
PaymentNotification.create!(params: params, cart_id: params[:invoice], status: params[:payment_status], transaction_id: params[:txn_id],
line_item_id: "#{e_values.fetch("item_number")}", product_title: "#{e_values.fetch("item_name")}",
option_name: "#{e_values.fetch("option_name1_")}", quantity: "#{e_values.fetch("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
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