Johhan Santana
Johhan Santana

Reputation: 2415

Paypal recurring questions ruby on rails

I've been following Railscast 289 tutorial on how to make recurring payments using Paypal.

I got everything working now except that I am now struggling to figure out how am I able to know if the user cancelled their subscription in paypal.

What I've done

I've manage to set the IPN url in the paypal merchant account and everytime someone subscribes it will send me data from paypal to my webhook.

Now I am storing all the parameters that paypal sends me but I am unable to figure out which of the parameters I should be checking in order to know if the user has cancelled their subscription or if it had not enough funds, etc.

At least this is the way I think it works.

I'm using Paypal Recurring Gem

Questions

How do I notice when an user has cancelled their subscription when paypal sends me their IPN to my webhook.

My webhook atm

def create
    user_id = Subscription.find_by(paypal_customer_token: params[:payer_id]).user_id
    PaymentNotification.create!(params: params, user_id: user_id, user_role_id: params[:item_number], status: params[:payment_status], transaction_id: params[:txn_id])
    @user = User.find_by(id: user_id)
    if PaymentNotification.last.params[:profile_status] == "Cancelled"
        @user.update_attributes(user_role_id: 1)
    end
    render nothing: true
end

Notice that I don't want to update the users attribute instantly I want to wait until their month subscription has ended.

I'm currently storing their IPN call in PaymentNotification table.

create_table "payment_notifications", force: :cascade do |t|
  t.text     "params"
  t.integer  "user_role_id"
  t.string   "status"
  t.string   "transaction_id"
  t.datetime "created_at",     null: false
  t.datetime "updated_at",     null: false
  t.integer  "user_id"
end

Also, what is the best way to check for these parameters in order to take action to users who haven't paid or have cancelled their subscription?

I have another table which stores the subscriptions

create_table "subscriptions", force: :cascade do |t|
  t.integer  "user_role_id"
  t.integer  "user_id"
  t.string   "paypal_customer_token"
  t.string   "paypal_recurring_profile_token"
  t.datetime "created_at",                     null: false
  t.datetime "updated_at",                     null: false
end

user_role being in this case the plan they are subscribing.

Upvotes: 3

Views: 615

Answers (1)

pp_pduan
pp_pduan

Reputation: 3402

IPN ensures that you're notified when there's any change to your transactions, from a recurring payment perspective, when your customer cancels the profile, an event triggered POST-Back will be sent to your IPN listener.

A sample raw POST-Back message of profile cancelation will be like this,

payment_cycle=Daily &txn_type=recurring_payment_profile_cancel &last_name=US &next_payme
nt_date=N/A &residence_country=US &initial_payment_amount=0.00 &currency_code=USD &t
ime_created=19:25:09 Sep 24, 2015 PDT &verify_sign=AgUGxKs4vGiEqeit6IyHkIjDJVpeAqCMRVC4wh9CZYotn
Jqrr-3oWsFe &period_type= Trial &payer_status=verified &test_ipn=1 &tax=0.00 &pa
[email protected] &first_name=Payer &[email protected] &payer_id=8FMFQ2
KVYYHTY &product_type=1 &shipping=0.00 &amount_per_cycle=2.00 &profile_status=Cancel
led &charset=gb2312 &notify_version=3.8 &amount=2.00 &outstanding_balance=0.00 &recurring_payment_id=I-30FKJ55UV1RW &product_name=RP BA Test &ipn_track_id=65583f3a80f10
  1. How would you be able to notice when a cancelation happens?

    Read the parameter txn_type in the IPN message, and determine event type with it (in your case, the event type would be recurring_payment_profile_cancel, find more details on txn_type list in the PayPal IPN Variables)

  2. Reconcile the profile/subscription with your database entry and take action

    Match the recurring_payment_id=I-30FKJ55UV1RW from the IPN message with your pre-stored database entry paypal_recurring_profile_token, and take action to update the user_role_id field. (Either stop the subscription instantly or update/set a new expiration date based on your business mode)

Upvotes: 2

Related Questions