Adding IPNs

This commit is contained in:
Will Bradley 2013-08-24 02:18:37 -07:00
parent 75e4907a9c
commit ec4cf4dea9
23 changed files with 379 additions and 5 deletions

View File

@ -40,7 +40,7 @@ gem 'bcrypt-ruby', '~> 3.0.0'
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
gem 'debugger'
#gem "paperclip", "~> 3.0"
gem 'gravtastic'

View File

@ -39,6 +39,13 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.3.3)
columnize (0.3.6)
debugger (1.6.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.1)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.2)
devise (2.1.1)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
@ -120,6 +127,7 @@ DEPENDENCIES
bcrypt-ruby (~> 3.0.0)
cancan
coffee-rails (~> 3.2.1)
debugger
devise
gravtastic
jquery-rails

View 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/

View 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/

View 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

View File

@ -0,0 +1,2 @@
module IpnHelper
end

50
app/models/ipn.rb Normal file
View 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

View File

@ -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")

View File

@ -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

View 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
View 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 %>

View 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 %>

View File

@ -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>

View File

@ -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>

View File

@ -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 %> |

View File

@ -1,4 +1,6 @@
Dooraccess::Application.routes.draw do
resources :ipns
match 'ipns/:id/link' => 'ipns#link', :as => :link_ipn
resources :payments

View File

@ -0,0 +1,24 @@
class CreateIpns < ActiveRecord::Migration
def change
create_table :ipns do |t|
t.integer :payment_id
t.text :data
t.string :txn_id
t.string :txn_type
t.string :first_name
t.string :last_name
t.string :payer_business_name
t.string :payer_email
t.string :payer_id
t.string :auth_amount
t.string :payment_date
t.string :payment_fee
t.string :payment_gross
t.string :payment_status
t.string :item_name
t.string :payment_type
t.timestamps
end
end
end

View File

@ -0,0 +1,5 @@
class AddAmountToPayments < ActiveRecord::Migration
def change
add_column :payments, :amount, :decimal
end
end

View File

@ -1,3 +1,4 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@ -10,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130212083412) do
ActiveRecord::Schema.define(:version => 20130824072157) do
create_table "cards", :force => true do |t|
t.string "card_number"
@ -35,6 +36,27 @@ ActiveRecord::Schema.define(:version => 20130212083412) do
t.datetime "updated_at", :null => false
end
create_table "ipns", :force => true do |t|
t.integer "payment_id"
t.text "data"
t.string "txn_id"
t.string "txn_type"
t.string "first_name"
t.string "last_name"
t.string "payer_business_name"
t.string "payer_email"
t.string "payer_id"
t.string "auth_amount"
t.string "payment_date"
t.string "payment_fee"
t.string "payment_gross"
t.string "payment_status"
t.string "item_name"
t.string "payment_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "mac_logs", :force => true do |t|
t.string "mac"
t.string "ip"
@ -64,6 +86,7 @@ ActiveRecord::Schema.define(:version => 20130212083412) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "created_by"
t.decimal "amount"
end
add_index "payments", ["user_id"], :name => "index_payments_on_user_id"

7
test/fixtures/ipns.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
data: MyText
two:
data: MyText

View File

@ -0,0 +1,7 @@
require 'test_helper'
class IpnControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class IpnHelperTest < ActionView::TestCase
end

7
test/unit/ipn_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class IpnTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end