Showing posts with label paypal. Show all posts
Showing posts with label paypal. Show all posts

Sunday, 7 July 2013

Setting Up Your Custom IPN Listener on Paypal

You can make your Custom IPN Listener known to PayPal by specifying the listener’s URL in following two ways:

  1. Setting Up IPN Notifications on Paypal
  2. Dynamically Setting the IPN Notification URL via Paypal Button’s HTML Variable

Setting Up IPN Notifications on Paypal

After you log into your Paypal Account, follow these instructions to set up your IPN Listener:

  1. Click ‘Profile’ on the ‘My Account’ tab
  2. Click ‘Instant Payment Notification Preferences’ in the Selling Preferences column
  3. Click ‘Choose IPN Settings’ to specify your listener’s URL and activate the listener.
    The following screen appears:
    edit-instant-payment-notification-ip
  4. Specify the URL for your listener in the ‘Notification URL’ field
  5. Click ‘Receive IPN messages (Enabled)’ to enable your listener
  6. Click Save
    The following screen appears:
    instant-payment-notification-ipn5

Dynamically Setting the IPN Notification URL via Paypal Button’s HTML Variable

In this case, PayPal sends the IPN message to the listener specified in the notification URL for a specific button or API operation instead of the listener specified in your Profile.

To specify a notification URL for a Paypal Button, specify your IPN Listener’s URL in the ‘notify_url’ HTML form variable of that Paypal Button’s HTML Code.

Testing Your Custom IPN Listener for Paypal Instant Payment Notification (IPN) via Paypal’s IPN Simulator

You can test your custom IPN Listener by:

  1. logging into your Paypal Developer account.
  2. Select ‘Applications’ from tab menu
  3. Then select ‘IPN Simulator’ from left panel menu.
  4. Enter your IPN Handler URL
  5. Select Transaction Type:

ipn simulator_thumb[6]

Once you select the Transaction Type, the page will refresh with more form values relevant to your selected Transaction Type. Majority of the ‘Test Data’ is already filled. However, you can change any values you like to test.

Click on the Send IPN button to simulate the transaction, which will result in one or more IPN notifications being sent to your IPN Handler URL depending upon the Transaction Type.

send ipn_thumb[5]

Saturday, 6 July 2013

Paypal Button and Instant Payment Notification (IPN) Integration with Java

Integration with Payment Gateways is one of the most common integration in business applications. In this post, I will demonstrate how you can integrate your Java (Servlet/JSP/Struts/Spring/etc.) applications with Paypal using Paypal Button and Instant Payment Notification (IPN).

For those who hate to read long documentation, I’ve tried to capture “minimum” reading material here from Paypal website that is required to understand Paypal IPN Protocal and Architecture; and which is a pre-requisite for our example below.

Besides that, you should also setup following (if not done already):

  1. Create Paypal Account
  2. Activate Paypal Developer Account by signing in with your Paypal Account credentials
  3. Create Paypal Payment Button

    create paypal payment button

‘IpnHandler.java’

As for ‘IpnConfig.java’:

  1. ‘ipnUrl’ should be:
    1. For Production/Live - https://www.paypal.com/cgi-bin/webscr
    2. For Sandbox/Testing - https://www.sandbox.paypal.com/cgi-bin/webscr
  2. ‘receiverEmail’ should be the Paypal Account Email
  3. ‘paymentAmount’ should be the ‘Price’ you setup while defining Paypal Button above.
  4. ‘paymentCurrency’ should be the ‘Currency’ you mention while defining the Paypal Button above.

Now,

COMPLETE SOURCE CODE IS AVAILABLE HERE TO DOWNLOAD

Paypal Button and Instant Payment Notification (IPN) – Overview, Protocol and Architecture

This post is intended for those who hate to read long documentation. I’ve tried to capture “minimum” reading material from Paypal website, that is required to understand Paypal IPN Protocal and Architecture and which is a pre-requisite for our example of integration of Paypal Button and Instant Payment Notification (IPN) with Java. If you want to learn more, you should definitely read more details from Paypal website.

Instant Payment Notification (IPN) is PayPal's message service that sends a notification when a transaction is affected. Once IPN is integrated, sellers can automate their back office so they don’t have to wait for payments to come in to trigger order fulfillment. [Source: Paypal]

For Paypal Button and IPN integration, you have to create an “IPN Listener” that should be exposed publicly on internet so that Paypal server(s) can send notifications to your IPN listener and from there you can start your own business flow to process those notifications.

Technically in Java, an “IPN Listener” can be exposed on some URL that could be mapped to a Java Servlet, a JSP Page, a Struts Controller and Action, a Spring Controller, etc.

The following diagram shows how events can occur and how PayPal responds with IPN messages that it sends to your listener:

IPNOverview_thumb2 

The diagram shows requests and responses, which are the result of processing paypal button clicks. PayPal sends an IPN message when it sends a response to a request. The IPN message is not actually part of the response sent to your website. Rather, the IPN message is sent to the your listener, which allows you to take actions that are not directly tied to the operation of your website.

Note: The diagram does not show the IPN authentication protocol messages that validate the IPN message.

IPN is an asynchronous message service, meaning that messages are not synchronized with actions on your website. Thus, listening for an IPN message does not increase the time it takes to complete a transaction on your website.

The IPN message service does not assume that all messages will be received by your listener in a timely manner. Because the internet is not 100% reliable, messages can become lost or delayed. To handle the possibility of transmission and receipt delays or failures, the IPN message service implements a retry mechanism that resends messages at various intervals until you acknowledge that the message has successfully been received. Messages may be resent for up to four days after the original message.

Note: Unless you are certain that a failure occurred on the the Internet, the most likely cause of lost, delayed, or duplicate IPN messages is faulty logic in the listener itself.

Because messages can be delivered at any time, your listener must always be available to receive and process messages; however, the retry mechanism also handles the possibility that your listener could become swamped or stop responding.

The IPN message service should not be considered a real-time service. Your checkout flow should not wait on an IPN message before it is allowed to complete. If your website waits for an IPN message, checkout processing may be delayed due to system load and become more complicated because of the possibility of retries.

 

IPN Protocol and Architecture

The IPN protocol consists of three steps:

  1. PayPal sends your IPN listener a message that notifies you of the event
  2. Your listener sends the complete unaltered message back to PayPal; the message must contain the same fields in the same order and be encoded in the same way as the original message
  3. PayPal sends a single word back, which is either VERIFIED if the message originated with PayPal or INVALID if there is any discrepancy with what was originally sent

Your listener must respond to each message, whether or not you intend to do anything with it. If you do not respond, PayPal assumes that the message was not received and resends the message. PayPal continues to resend the message periodically until your listener sends the correct message back, although the interval between resent messages increases each time. The message can be resent for up to four days.

This resend algorithm can lead to situations in which PayPal resends the IPN message while you are sending back the original message. In this case, you should send your response again, to cover the possibility that PayPal did not actually receive your response the first time. You should also ensure that you do not process the transaction associated with the message twice.

Important: PayPal expects to receive a response to an IPN message within 30 seconds. Your listener should not perform time-consuming operations, such as creating a process, before responding to the IPN message.

After PayPal verifies the message, there are additional checks that your listener or back-end or administrative software must take:

  • Verify that you are the intended recipient of the IPN message by checking the email address in the message; this handles a situation where another merchant could accidentally or intentionally attempt to use your listener.
  • Avoid duplicate IPN messages. Check that you have not already processed the transaction identified by the transaction ID returned in the IPN message. You may need to store transaction IDs and the last payment status returned by IPN messages in a file or database so that you can check for duplicates. If the transaction ID sent by PayPal is a duplicate, you should not process it again.

    Note: You must track the last payment status returned by IPN messages because PayPal could send an IPN for a pending payment and a second one for the completed payment, both of which would have the same transaction ID. Relying on just the transaction ID could lead to the completed payment being treated as a duplicate.

  • Because IPN messages can be sent at various stages in a transaction's progress, make sure that the transaction's payment status is "completed" before enabling shipment of merchandise or allowing the download of digital media.
  • Verify that the payment amount actually matches what you intend to charge. Although not technically an IPN issue, if you do not encrypt buttons, it is possible for someone to capture the original transmission and change the price. Without this check, you could accept a lesser payment than what you expected.

[Source: Paypal]