Adding IPNs
This commit is contained in:
3
app/assets/javascripts/ipn.js.coffee
Normal file
3
app/assets/javascripts/ipn.js.coffee
Normal file
@@ -0,0 +1,3 @@
|
||||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
|
||||
3
app/assets/stylesheets/ipn.css.scss
Normal file
3
app/assets/stylesheets/ipn.css.scss
Normal file
@@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the Ipn controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
||||
34
app/controllers/ipns_controller.rb
Normal file
34
app/controllers/ipns_controller.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class IpnsController < ApplicationController
|
||||
load_and_authorize_resource :ipn, :except => "create"
|
||||
before_filter :authenticate_user!
|
||||
|
||||
protect_from_forgery :except => [:create]
|
||||
|
||||
def index
|
||||
@ipns = Ipn.all
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
#TODO: ensure the request is actually from paypal
|
||||
@ipn = Ipn.new_from_dynamic_params(params)
|
||||
@ipn.data = params.to_json
|
||||
@ipn.save
|
||||
render :nothing => true
|
||||
end
|
||||
|
||||
def link
|
||||
result = @ipn.link_payment
|
||||
if result.first
|
||||
redirect_to ipns_url, :notice => 'Payment was successfully linked.'
|
||||
else
|
||||
redirect_to ipns_url, :notice => result.last
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
2
app/helpers/ipn_helper.rb
Normal file
2
app/helpers/ipn_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module IpnHelper
|
||||
end
|
||||
50
app/models/ipn.rb
Normal file
50
app/models/ipn.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
class Ipn < ActiveRecord::Base
|
||||
attr_accessible :data
|
||||
belongs_to :payment
|
||||
|
||||
after_create :create_payment
|
||||
|
||||
def self.new_from_dynamic_params(params)
|
||||
ipn = Ipn.new()
|
||||
|
||||
ipn.attributes.each do |c|
|
||||
unless params[c.first.to_sym].nil?
|
||||
ipn[c.first.to_sym] = params[c.first.to_sym]
|
||||
end
|
||||
end
|
||||
|
||||
return ipn
|
||||
end
|
||||
|
||||
def link_payment
|
||||
create_payment
|
||||
end
|
||||
|
||||
private
|
||||
def create_payment
|
||||
# find user by email, then by payee
|
||||
user = User.find_by_email(self.payer_email)
|
||||
user = User.find_by_payee(self.payer_email) if user.nil? && self.payer_email.present?
|
||||
|
||||
# Only create payments if the amount matches a member level
|
||||
if user.present?
|
||||
if User.member_levels[self.payment_gross.to_i].present?
|
||||
payment = Payment.new
|
||||
payment.date = self.payment_date
|
||||
payment.user_id = user.id
|
||||
payment.amount = self.payment_gross
|
||||
if payment.save
|
||||
self.payment_id = payment.id
|
||||
self.save!
|
||||
else
|
||||
return [false, "Unable to link payment. Payment error: #{payment.errors.full_messages.first}"]
|
||||
end
|
||||
else
|
||||
return [false, "Unable to link payment. Couldn't find membership level '#{self.payment_gross.to_i}'."]
|
||||
end
|
||||
else
|
||||
return [false, "Unable to link payment. Couldn't find user/payee '#{self.payer_email}'."]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,10 +1,12 @@
|
||||
class Payment < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
attr_accessible :date, :user_id, :created_by
|
||||
has_one :ipn
|
||||
attr_accessible :date, :user_id, :created_by, :amount
|
||||
|
||||
validates_presence_of :user_id, :date, :created_by
|
||||
validates_presence_of :user_id, :date, :amount # not created_by
|
||||
validates_uniqueness_of :date, :scope => :user_id, :message => ' of payment already exists for this user.'
|
||||
|
||||
|
||||
def human_date
|
||||
if date.year < DateTime.now.year
|
||||
date.strftime("%b %e, %y")
|
||||
|
||||
@@ -85,6 +85,10 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def self.member_levels
|
||||
{25 => "Associate", 50 => "Basic", 100 => "Plus"}
|
||||
end
|
||||
|
||||
def member_status
|
||||
case self.member_level.to_i
|
||||
when 0
|
||||
|
||||
26
app/views/ipns/index.html.erb
Normal file
26
app/views/ipns/index.html.erb
Normal file
@@ -0,0 +1,26 @@
|
||||
<html><body>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Name</th>
|
||||
<th>Item</th>
|
||||
<th>Amount</th>
|
||||
</tr>
|
||||
<% @ipns.reverse!.each do |ipn| %>
|
||||
<tr>
|
||||
<td><%= ipn.payment_date %></td>
|
||||
<td><%= ipn.first_name %> <%= ipn.last_name %></td>
|
||||
<td><%= ipn.item_name %></td>
|
||||
<td><%= ipn.payment_gross %></td>
|
||||
<td>
|
||||
<% if ipn.payment.present? %>
|
||||
<%= link_to "Linked Payment", ipn.payment %>
|
||||
<% else %>
|
||||
<%= link_to "Try to link email '#{ipn.payer_email}' at membership level '#{ipn.payment_gross.to_i}'", link_ipn_path(ipn) %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= link_to "Details", ipn %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
</body></html>
|
||||
123
app/views/ipns/new.html.erb
Normal file
123
app/views/ipns/new.html.erb
Normal file
@@ -0,0 +1,123 @@
|
||||
<%= form_tag('/ipns') do |f| %>
|
||||
|
||||
<style type="text/css">
|
||||
label {
|
||||
width: 10em;
|
||||
display: inline-block;}
|
||||
</style>
|
||||
|
||||
<div class="actions">
|
||||
<%= submit_tag %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :first_name %>
|
||||
<%= text_field_tag :first_name, "John" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :last_name %>
|
||||
<%= text_field_tag :last_name, "Smith" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payer_email %>
|
||||
<%= text_field_tag :payer_email, "jsmith@example.com" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :item_name %>
|
||||
<%= text_field_tag :item_name, "Associate Membership" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payment_gross %>
|
||||
<%= text_field_tag :payment_gross, "25.00" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :transaction_subject %>
|
||||
<%= text_field_tag :transaction_subject, "" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payment_date %>
|
||||
<%= text_field_tag :payment_date, Date.today.to_s %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :txn_type %>
|
||||
<%= text_field_tag :txn_type, "subscr_payment" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :subscr_id %>
|
||||
<%= text_field_tag :subscr_id, "I-1234567890" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :residence_country %>
|
||||
<%= text_field_tag :residence_country, "US" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :mc_currency %>
|
||||
<%= text_field_tag :mc_currency, "USD" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :business %>
|
||||
<%= text_field_tag :business, "hslfinances@gmail.com" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payment_type %>
|
||||
<%= text_field_tag :payment_type, "instant" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :protection_eligibility %>
|
||||
<%= text_field_tag :protection_eligibility, "Ineligible" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :verify_sign %>
|
||||
<%= text_field_tag :verify_sign, "12ru9021j9f21j90fj1290fj2910fj0219fj0" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payer_status %>
|
||||
<%= text_field_tag :payer_status, "verified" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :txn_id %>
|
||||
<%= text_field_tag :txn_id, "1234567890" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :receiver_email %>
|
||||
<%= text_field_tag :receiver_email, "hslfinances@gmail.com" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payer_id %>
|
||||
<%= text_field_tag :payer_id, "V812314914" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :receiver_id %>
|
||||
<%= text_field_tag :receiver_id, "V90R1280182" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payment_status %>
|
||||
<%= text_field_tag :payment_status, "Completed" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :payment_fee %>
|
||||
<%= text_field_tag :payment_fee, "0.85" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :mc_fee %>
|
||||
<%= text_field_tag :mc_fee, "0.85" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :mc_gross %>
|
||||
<%= text_field_tag :mc_gross, "25.00" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :charset %>
|
||||
<%= text_field_tag :charset, "windows-1252" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :notify_version %>
|
||||
<%= text_field_tag :notify_version, "3.7" %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= label_tag :ipn_track_id %>
|
||||
<%= text_field_tag :ipn_track_id, "9d3d032d9070" %>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<%= submit_tag %>
|
||||
</div>
|
||||
<% end %>
|
||||
17
app/views/ipns/show.html.erb
Normal file
17
app/views/ipns/show.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<h2>IPN Details</h2>
|
||||
|
||||
<% @ipn.attributes.except("data","payment_id").each do |attr| %>
|
||||
<p>
|
||||
<b><%= attr.first.to_s %>:</b>
|
||||
<%= @ipn.attributes[attr.first] %>
|
||||
</p>
|
||||
<% end %>
|
||||
<p>
|
||||
<% if @ipn.payment.present? %>
|
||||
<%= link_to "Payment", @ipn.payment %>
|
||||
<% else %>
|
||||
Couldn't link '<%= @ipn.payer_email %>' or payment amount '<%= @ipn.payment_gross.to_i %>' not a valid membership level. Please create payment manually.
|
||||
<% end %>
|
||||
</p>
|
||||
|
||||
<%= link_to "Back", ipns_path %>
|
||||
@@ -19,6 +19,10 @@
|
||||
<%= f.label :date, "Paid for month beginning" %><br />
|
||||
<%= f.date_select :date, :default => (DateTime.now - 1.month) %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :amount %><br />
|
||||
<%= f.number_field :amount %>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<%= f.submit %>
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>Paid for month <br/>beginning</th>
|
||||
<th>Amount</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
@@ -15,6 +16,7 @@
|
||||
<tr>
|
||||
<td><%= link_to payment.user.name_with_payee_and_member_level, payment.user unless payment.user.blank? %></td>
|
||||
<td><%= payment.human_date %></td>
|
||||
<td><%= payment.amount %></td>
|
||||
<td><%= link_to 'Details', payment %></td>
|
||||
<td><%= link_to 'Edit', edit_payment_path(payment) %></td>
|
||||
</tr>
|
||||
|
||||
@@ -8,9 +8,19 @@
|
||||
<%= @payment.date %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Amount:</b>
|
||||
<%= @payment.amount %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>Last Modified by:</b>
|
||||
<%= user = @users.find{|u| u.id == @payment.created_by}; link_to user.name, user unless user.blank? %>
|
||||
<% user = @users.find{|u| u.id == @payment.created_by} %>
|
||||
<% if user.blank? %>
|
||||
n/a
|
||||
<% else %>
|
||||
<%= link_to user.name, user %>
|
||||
<% end %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
@@ -23,6 +33,13 @@
|
||||
<%= @payment.updated_at %>
|
||||
</p>
|
||||
|
||||
<% if @payment.ipn.present? %>
|
||||
<p>
|
||||
<%= link_to "Paid via PayPal", @payment.ipn %>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
|
||||
|
||||
<%= link_to 'Edit', edit_payment_path(@payment) %> |
|
||||
<%= link_to 'Destroy', @payment, :confirm => 'Are you sure you want to destroy this payment?', :method => :delete if can? :destroy, @payment %> |
|
||||
|
||||
Reference in New Issue
Block a user