Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.