Skip to content

Instantly share code, notes, and snippets.

@brand-it
Created November 28, 2012 23:25
PayPal Cart
class Cart < ActiveRecord::Base
scope :find_standard_subscription_purchased, where("line_items.product_name = ? AND purchased = ?", "Standard Subscription", true).includes(:line_items)
belongs_to :user
has_many :line_items
has_many :payment_notification
def total_price
# convert to array so it doesn't try to do sum on database directly
line_items.to_a.sum(&:full_price)
end
def paypal_url(return_url, notify_url)
if Rails.env.production?
business = 'production@email.com'
else
business = "development@email.com"
end
values = {
:business => business,
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => id,
:notify_url => notify_url
}
line_items.each_with_index do |item, index|
values.merge!({
"amount_#{index+1}" => item.unit_price,
"item_name_#{index+1}" => item.product_name,
"item_number_#{index+1}" => item.id,
"quantity_#{index+1}" => item.quantity
})
end
if Rails.env.production?
"https://www.paypal.com/cgi-bin/webscr?" + values.to_query
else
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment