This repository was archived by the owner on Apr 24, 2021. It is now read-only.
forked from dsjoerg/ggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment_controller.rb
More file actions
72 lines (55 loc) · 2.35 KB
/
payment_controller.rb
File metadata and controls
72 lines (55 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class PaymentController < ApplicationController
include ActiveMerchant::Billing::Integrations
# Here's how payment integration works.
# The Paypal buttons on the ggtracker site include a parameter called 'custom' and put the user id in there.
# After the user signs up for a subscription, Paypal POSTS to us at backend/ipn.
# We set their paypal_profile_id here and turn on their prolevel here.
#
# Later, if they cancel through our settings page, home_controller#cancelled_pro is
# called. That uses the paypal_profile_id and the Paypal API to
# cancel their subscription.
#
# Or if they cancel on Paypal, we get an IPN notification and process it here.
#
# Smart things we could do someday:
#
# * record the exact date of each subscription and cancellation (some
# users subscribe and cancel multiple times, so record each one
# separately). Then we can have subscription/cancellation analytics
# to track how product improvements drive performance.
#
# * use this IPN interface to keep track of user payments. when it's
# 30 days after their last payment, that's when Pro status should
# turn off.
#
def paypal_ipn
# notify = Paypal::Notification.new(request.raw_post)
if params['txn_type'] == 'subscr_signup'
user = User.find(params['custom'].to_i)
ProblemMailer.simple_problem("ggtracker pro IPN activation: #{user.email}", user.email).deliver
user.paypal_profile_id = params['subscr_id']
user.paypal_email = params['payer_email']
user.prolevel = 1
user.pro_cancelled_at = nil
user.save!
elsif params['txn_type'] == 'subscr_cancel'
users = User.where(:paypal_profile_id => params['subscr_id'])
if users.size > 0
user = users.first
ProblemMailer.simple_problem("ggtracker pro IPN cancellation: #{user.email}", user.email).deliver
user.pro_cancelled_at = Time.now
user.save!
end
elsif params['txn_type'] == 'recurring_payment_suspended_due_to_max_failed_payment'
users = User.where(:paypal_profile_id => params['recurring_payment_id'])
if users.size > 0
user = users.first
ProblemMailer.simple_problem("ggtracker pro IPN final pay fail: #{user.email}", user.email).deliver
user.prolevel = 0
user.pro_cancelled_at = Time.now
user.save!
end
end
render :text => ''
end
end