[SOLVED] PayPal IPN issues

144 views
Skip to first unread message

Andrew A.

unread,
Aug 9, 2019, 1:02:06 PM8/9/19
to google-apps-sc...@googlegroups.com

I need clarification on a few issues. I hope someone can help:
  • How long does the test IPN from PayPal take to return return the VERIFIED message?
  • Does my add-on need to be published before I can test the IPN?
  • [Noob question?] Can I use doPost and doGet if I deploy as add-on and not a web app? Google's documentation is confusing
... and crucially:
  • What IPN messages would you look out for when verifying that a user has paid a PayPal subscription (using the subscription button)?

Alan Wells

unread,
Aug 9, 2019, 1:39:58 PM8/9/19
to Google Apps Script Community
The IPN should fire very soon after something happens in your PayPal account.
Your add-on does not need to be published to test your payment system.
doPost and doGet can be used and published from the Apps Script project you are using for your add-on, or you could create a separate stand alone Apps Script file for that.
You need to think about how the entire flow of events will happen, and where you will be storing the values that tell the add-on whether your user has paid or not.
If you use a doPost function in the Apps Script project that is for your add-on, you need to understand that the Apps Script project for your add-on, can't directly access the user properties, or document properties of the account that installed the add-on.  You could put a value into your script properties, then the add-on user could access script properties, and then transfer the payment status to their document or user properties.  If you do that, then I'd have the code delete the script property after the transfer because I wouldn't want a lot of user payment information in the Apps Script project.
If you have a separate apps script web app to handle the IPN, then you'd need to save the payment status information somewhere external, like your spreadsheet, or firebase database, and then the add-on would need to look at that external data source for payment information if it's not found in user or document properties.  On the initial payment, the add-on would need to look at the external data source, get the payment status information, and then the add-on could store that payment information in the users document or user properties.  If you put payment status information into user or document properties, then you'll avoid the need for the add-on to make an external request every time that the add-on needs to test for payment status.  If you put the payment information into document properties, then user might be able to access that data.  Do you want them to pay for every document used?

There are 3 URLs that you need to understand 


1) regular paypal.com 

2) sandboxed paypal and 

3) developer.paypal


To create the test seller and test buyer accounts, you must be logged in with your regular pay pal account at developer.paypal


To manage and edit your test seller and test buyer accounts, you must log into them from the sandboxed.paypal URL


How to Create a Custom PayPal Check Out


  • Log in to your PayPal account

  • Tools - Click the Tools menu

  • All Tools - Click All Tools

  • Scroll to PayPal buttons and click Open

  • For an add-on the user is buying a single item - Choose the first option to buy a single item

  • Choose a button type of “Buy Now”

  • Add a text field for the email or domain name that the add-on will be running under

  • Click “Create Button”

  • Select and copy the code (HTML) for the new button

  • Paste your PayPal HTML code into your HTML

  • Change the target="_top" to target="_blank" so that PayPal opens in a new browser tab or window (May depend on how the user set up their browser)



To test your button, create sandboxed test credentials

 https://developer.paypal.com/docs/classic/paypal-payments-standard/ht_test-pps-buttons/


To create a sandbox account:

  1. Log in to the Developer site at https://developer.paypal.com. On the Dashboard tab, navigate to Sandbox > Accounts.

  2. Click Create Account and populate the following fields:


https://developer.paypal.com/developer/accounts/


To test a button you need to have two accounts - A “Business” account and a “Buyer” account.


example-Emai...@gmail.com

example-E...@gmail.com


Log in to PayPal with the seller account: example-Emai...@gmail.com


YOU MUST LOG IN AT THE SANDBOXED URL


https://www.sandbox.paypal.com/us/signin/?country.x=US&locale.x=en_US


For the Test HTML:


  • Create a button with the steps as outlined at the top of the page

  • The test HTML is slightly different than the “Live” html

  • Add an IPN listener URL to the test SELLER PayPal account

    • In your PayPal account profile- Add the Apps Script Web App Url as the IPN listener page.

    • Go into your Profile Settings

    • Choose “My Selling Tools”

    • Click “update” in “Instant payment notifications” row

    • Click the “Choose IPN Settings” button

  • Use the test HTML to open up the PayPal log in dialog box

  • When prompted to log in on the PayPal payment page, log in with your test buyer account credentials for the sandbox.

  • In the test SELLER PayPal Developer Dashboard, under Sandbox, click Notifications.

  • Verify that you have received email notifications for your transactions.

  • The test system WILL invoke a IPN (Instant Payment Notification) but the IPN Url must be put into the test account of the facilitator (seller)


You can determine what PayPal button that your payment came from by using the


item_number


property in the object sent to the IPN.  And that id value is set in your button definition in the optional "item ID"


Receive payment information from PayPal to an Apps Script Web App


NOTE: For testing, the IPN must be added to the test SELLER account.


You can create test buyer and seller accounts.  These are different than your regular PayPal account.  In order to run tests, you must create test account. Otherwise you’d need to make an actual payment every time that you wanted to test your payment system.


IPN or Instant Payment Notification, informs you of Payments received from Pay Pal.


You need to click on settings, then My Selling Tools, and look for Instant Payment Notifications


  • General Pay Pal information about the IPN listener.

    • PayPal has 200 or 300 pieces of information that can be accessed.  Each piece of information has it’s own property name.

    • Important property name: txn_type Transaction type

    • txn_type (Transaction type) For example: express_checkout which means that a single payment was received from the express checkout.  Or new_case meaning that a dispute was filed.  Transaction Types

    • Next important property names:  Payment Information Variables

    • The payment information property that you’ll probably want to use is payment_status  which has many sub-properties indicating the payment status.  Of the 12 or so sub-properties of payment_status, you’ll probably be the most interested in:  Completed and Processed ; and also Denied and Failed.  There is also:  Pending Refunded Reversed and Voided.  But, if you didn’t get the payment, then you probably won’t want to give the user access to whatever you’re selling.  The goal of this article is to get a payment from PayPal. I’ll deal with the other stuff later.

    • Confirm to PayPal that a message was received.

      • There are two things that must happen from the Apps Script end to respond back to Pay Pay:

        1. Quote:After receiving the IPN message from PayPal, your listener returns an empty HTTP 200 response to PayPal. Otherwise, PayPal resends the IPN message.  This probably happens automatically and internally. I don’t think that you need to program anything for this to happen.

        2. Quote: Your listener sends the complete message back to PayPal using HTTPS POST.  Prefix the returned message with the cmd=_notify-validate variable, but do not change the message fields, the order of the fields, or the character encoding from the original message.

      • There is another quote that states to add the "cmd=_notify-validate" to the end, and not the beginning:  Quote: After receiving an IPN message from PayPal, you must respond to PayPal with a POST message that is an exact copy of the received message but with "cmd=_notify-validate" added to the end of the message.  One part of the documentation states to add the "cmd=_notify-validate" to the beginning, and another part of the documentation states to add it to the end.  I just added a cmd property to the object with the value _notify-validate" and the Simulator stated that the handshake was verified.

  • Create and Publish an Apps Script Web App as the IPN listener page

    • The Web App needs a doPost() function to receive the request sent from PayPal

    • Add code to the Apps Script code editor - for code see:


  • Create an IPN listener page

    • IPN Set up Guide

    • In your PayPal account profile- Add the Apps Script Web App Url as the IPN listener page.

    • Go into your Profile Settings

    • Choose “My Selling Tools”

    • Click “update” in “Instant payment notifications” row

    • Click the “Choose IPN Settings” button

    • Note:  You do not need to set your profile settings to test the Apps Script Web App, but for it to be working live, you must add the Web App url to the profile settings.

  • Test your IPN Listener - Which is an Apps Script Web App

    • You can use the IPN simulator: Docs IPN Simulator

    • There is a test property from the IPN simulator named: test_ipn  PayPal doesn’t call it a property, they call it a variable.  That variable is not included in the live IPN message.

    • There are 2 basic things that you must do to use the IPN Simulator.  You need to add the word “sandbox” to the postback URL: https://ipnpb.sandbox.paypal.com/cgi-bin/webscr

    • Secondly, the Apps Script Web App must be published and functioning.  That was covered above.

    • Next Log into the Developer part of Pay Pal:  https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNSimulator/  I can not find a way to get to the IPN simulator documentation by navigating the menus.  Unless you are logged in, the Dashboard is not displayed, and you can not get to the IPN Simulator

    • Paste your Apps Script URL into the IPN handler URL field.

    • Choose the Transaction type - Probably “Express Checkout”

    • Make sure that the latest version of your Apps Script Web App is published, or it won’t run the newest version.

    • If you don’t use the special test URL with “sandbox” you may get a message in the simulator that the handshake was verified, but get an INVALID response back from the POST request.

    • You should receive a VERIFIED response back from the handshake.



Andrew A.

unread,
Aug 9, 2019, 1:50:30 PM8/9/19
to google-apps-sc...@googlegroups.com
Thanks for the wealth of information. I'm going to chew on this for a while and see how to add it to my code.
A few things to note:
  • I already implemented the IPN listener code based on something I saw on GitHub
  • I'm like to storing relevant messages (like payment_status, email, etc) using the Properties Service
  • The rest of the info goes to an external spreadsheet
EDIT:
Anyone know how to resolve this error?: "IPN was not sent, and the handshake was not verified. Review your information."
Reply all
Reply to author
Forward
0 new messages