Search code examples
ruby-on-railspaypalpaypal-ipn

How I can use IPN handler with Pay operation and receive messages?


I have function, which paying users:

    def pay
    require 'httpclient'
    require 'xmlsimple'
    clnt = HTTPClient.new
    user = User.find(params[:user_id])
    @params_id = params[:user_id]
    credentials = {
        'USER' => 'payer_1342623102_biz_api1.gmail.com',
       'PWD' => '1342623141',
       'SIGNATURE' => 'Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL '
     }

    header =  {"X-PAYPAL-SECURITY-USERID" => "payer_1342623102_biz_api1.gmail.com",
                   "X-PAYPAL-SECURITY-PASSWORD" => "1342623141",
                   "X-PAYPAL-SECURITY-SIGNATURE" => "Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL ",
                   "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
                   "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
                   "X-PAYPAL-APPLICATION-ID" =>  "APP-80W284485P519543T"
                    }
    //here is data what is posting to PayPal
    data = {"actionType" => "PAY",
               "receiverList.receiver(0).email"=> user.email,
               "receiverList.receiver(0).amount" => "10",
               "currencyCode" => "USD",
               "cancelUrl" => "http://127.0.0.1:3000/",
               "returnUrl" => "http://127.0.0.1:3000/",          
               "requestEnvelope.errorLanguage" => "en_US"}
    uri = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"
    res = clnt.post(uri, data, header)

end From documentation:

     Adaptive Payments API operation  - use -
  ipnNotificationUrl field of the Pay or Preapproval request

So, I should add into data variable:

        "ipnNotificationUrl" => "myaapp.com"//my site url yes ?

I should put in this field my site url or not ?

How to get info from this notification ?

How to send some emails and do some action if transaction is successful ?

I can't test it locally, yes ?

How can I test it in sandbox ?


Solution

  • Yes, you should include the field if you want to receive notifications related to the payment,

    You need to specify a URL that is reachable from the outside (As this is where PayPal will post the information to, from their servers).

    This URL could be a PHP page, that upon receiving the IPN, and verifying it, sends out the emails you mentioned.

    To simulate an IPN coming from PayPal, you can use the following tool from the sandbox developer page: https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session

    (Alternatively, you can complete a purchase in the sandbox, and if you specified the ipnNotificationURL in the API call, PayPal will send you one upon completion of the payment)