Search code examples
paypalpaypal-ipn

cancel paypal recurring payment with instant payment notification


From my gathering of several stackoverflow posts, when a user cancels a paypal recurring payment, an instant payment notification is sent to a specified url set up in the IPN settings. But what I cannot gather is what data is sent to this url in the query string. I came across this link:

https://www.paypal.com/cgi-bin/webscr?cmd=p/acc/ipn-subscriptions-outside

It provides a list of variables which I assume are sent as part of the query string sent with the url specified in IPN settings. If this is true, then that means I know this notification is a cancel notification, because the txn_type value will be "subscr_cancel".

However, I still need to know what recurring plan is actually being canceled. Hence, I need to know the recurring profile token in order to access it as a variable in query string.

Just to give you an idea of what I am trying to do here, here is some sample code:

def notify_url
if params[:txn_type] == "subscr_cancel"
  item_id = Order.where(paypal_recurring_profile_token: params[:recurring_profile_token]).unit_id_for_plan
  agent_host = CONFIG["agent_#{Rails.env}"]["host"]
  agent_port = CONFIG["agent_#{Rails.env}"]["port"]

 url = "http://#{agent_host}:#{agent_port}/home/deactivate?item_id=#{item_id}"
 begin
    resp = Net::HTTP.get(URI.parse(url))
    resp = JSON.parse(resp)
    puts "resp is: #{resp}"
    true
  rescue => error
    raise "Error: #{error}"
  end  

  if resp["status"] == "success"
    true
  end

end    
end

all I need to know is if txn_type will be equal to subscr_cancel when a notification is sent for a cancellation of recurring billing? @PP_MTS_Chad already confirmed that recurring_payment_id is included. I just need to know if txn_type is included as well.


Solution

  • When a profile is canceled, in your IPN POST you will get back the variable recurring_payment_id which will have the profile of the profile being canceled.

    Array
    (
        [amount3] => 69.95
        [address_status] => confirmed
        [recur_times] => 5
        [subscr_date] => 07:31:10 May 17, 2013 PDT
        [payer_id] => EW4KQ9CQX45F6
        [address_street] => 1 Main St
        [mc_amount3] => 69.95
        [charset] => KOI8-R
        [address_zip] => 95131
        [first_name] => MTS
        [reattempt] => 1
        [address_country_code] => US
        [address_name] => MTS Testing
        [notify_version] => 3.7
        [subscr_id] => I-628HEBW1V99M
        [payer_status] => verified
        [business] => [email protected]
        [address_country] => United States
        [address_city] => San Jose
        [verify_sign] => AQ3T0Omh4bXNzomBbYUO2LL1dphyAiWU5Sa7wpw8spAU-Pb1YFnm-mig
        [payer_email] => [email protected]
        [last_name] => Testing
        [address_state] => CA
        [receiver_email] => [email protected]
        [recurring] => 1
        [txn_type] => subscr_cancel
        [item_name] => Alice's Weekly Digest
        [mc_currency] => USD
        [item_number] => DIG Weekly
        [residence_country] => US
        [test_ipn] => 1
        [period3] => 6 M
        [ipn_track_id] => 54b49fde502a4
    )