Chapter 6. Instant Payment Notifications (IPNs)

Instant Payment Notifications (IPNs) are messages that PayPal sends you regarding a transaction status and can serve a variety of useful purposes. This brief chapter introduces IPNs, covers some of the common use cases for when IPNs can be helpful, and ultimately, teaches you how to use them to perform a follow-up action after a purchase as part of a Tweet Relevance integration.

Note

PayPal’s official documentation on IPNs is available online: Instant Payment Notification Guide.

IPNs notify you when an event occurs that affects a transaction. Typically, these events represent various kinds of payments but could also represent authorizations, Fraud Management Filter actions, refunds, disputes, chargebacks, the status of an eCheck payment, etc. More specifically, IPNs are commonly employed regarding:

Note

See Instant Payments versus eChecks for a brief overview of eChecks versus instant payments.

In many cases, the action that causes the event, such as a payment, occurs on your website; however, your website is not the only source of events. In many cases, events can be generated by Website Payment Standard buttons (not covered in this book) or various PayPal APIs such as DoExpressCheckoutPayment. You can detect and process IPN messages with a listener script that waits for messages and passes them to various backend or administrative processes that respond to them. The actions to take when your listener is notified of an event are specific to your needs. Examples of the kinds of actions you might take when your listener receives an IPN message include:

  • Trigger order fulfillment or enable media downloads when an eCheck clears or a payment is made

  • Update your list of customers

  • Update accounting records

  • Send follow-up emails regarding the transaction

You are typically notified of events by email as well, but the IPN message service enables you to automate a response to events using a well-formed API. Additionally, IPNs are asynchronous, which means 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 an algorithm that resends messages at various intervals until you acknowledge that the message has successfully been received. The delay between each successive resend attempt increases, and the IPN message service will retry for up to four days after the original message attempt.

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, and 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 even more complicated because of the possibility of retries.

IPN is designed to be secure, reliable, and asynchronous. To meet these requirements, the protocol requires you to acknowledge receipt of IPN messages. The IPN service provides a retry mechanism to handle cases in which a message is not acknowledged, for example, when a transmission or receipt failure occurs. When you enable IPN, PayPal sends messages to the IPN listener at the URL you specify in your merchant account’s profile, although you can override the URL to associate other IPN listeners with specific transactions. In this case, you specify the listener’s URL when you set up a PayPal API operation. The IPN protocol consists of three steps:

Your listener must respond to each message, whether or not you intend to do anything with it, because PayPal assumes that the message was not received and resends the message for up to four days if you do not respond. The resend algorithm increases the interval between each successive retry, which can lead to an interesting edge case: PayPal may resend 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, which is usually handled by persisting all transaction IDs that you have processed.

Once PayPal verifies the IPN, there are additional checks that your listener should take before it performs any follow-up actions:

PayPal generates an IPN message when you invoke certain API operations such as DoExpressCheckoutPayment or DoDirectPayment during checkout, or an Adaptive Payments operation such as Pay, Preapproval, or ExecutePayment. Figure 6-1 shows both the web flow and the IPN message authentication protocol.

The numbers in the diagram correspond to the following steps:

Your IPN listener must implement the IPN authentication protocol (steps 2, 3, and 4 in this diagram). After successfully completing the protocol, your back-office or administrative process vets the contents of the message and responds appropriately. For example, if the payment status for the transaction is “Completed,” your system can print a packing list or email a password to your customer for downloading digital media.

PayPal recommends that you follow a specific checklist in handling IPNs: