PayPal Invoice REST API in PHP: Part 1

The WP Crowd
Published: April 28, 2016

I recently had the displeasure of building a web app for a client that needed to create and send invoices through the PayPal API.  And yes, I charged a premium rate for two reasons: It was needed super fast and PayPal.

I scoured the internet for some prebuilt “stuff” that might help me get a jump start, but after sorting through a handful of code examples nothing functioned how I was hoping.  Also, I couldn’t get Composer on their server and yea, that’s basically the only method that is well documented.

So I’m going to hook you guys up with some goods.  This will be a series of blog posts starting now.

Create an App

Now let’s talk sandbox.  Over at the lovely https://developer.paypal.com/ you can create your REST API App.  Go to your Dashboard then Sandbox > Accounts.  Create a sandbox account.

Next pop over to Dashboard > My Apps & Credentials.  Create a new app and attach your sandbox account to it.  You now have your Client ID and your Secret and you are ready to start developing!

Getting the SDK without Composer

Since we are doing this without Composer we need to use the direct download installation method.  Grab the latest version here: https://github.com/paypal/PayPal-PHP-SDK/releases

Unzip and upload to your root directory.

This is where things get a little tricky without Composer.  There’s a lot of good example code in the samples directory and we are even going to use its bootstrap.php file for simplicity.  There are two files in there we need to edit.  Go into PayPal-PHP-SDK/paypal/rest-api-sdk-php/sample and open up bootstrap.php.  Later you will need to edit sdk_config.ini when wanting to go live, we’ll get to that in a future post though.  Change lines 33 and 34 to your sandbox credentials:

// Replace these values by entering your own ClientId and Secret by visiting https://developer.paypal.com/webapps/developer/applications/myapps
$clientId = ‘AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS’;
$clientSecret = ‘EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL’;

Save the file and close it.  You are all set to start sandboxing!

First Test Example

Time to start coding.  We are going to build a VERY simple script to create an invoice.  First let’s include our necessities.  We need the autoloader and the bootstrap and let’s set some Namespace variables for easier coding.

All set to start creating invoices now, so let’s build one.  It is absolutely vital that you fill in the email, first name and last name that match your PayPal account when going live.  The following code can look a little daunting but it’s not too bad.  I’ll go over it in detail below.

On line 1 we create a new invoice object.  Then we need to fill in some information starting with the basics: Merchant info, billing info and so on.

On line 36 we start adding items to the invoice.  Here we just have one single item, we will go over multiple items in a future post.  Set the item name, quantity and price.  I’ve also gone ahead and added tax.  The $tax variable is not set in my script but it’s used so you can define it if you’d like.  I define it depending on state selected in my bigger invoicing app.

Next on line 52 we have getPaymentTerm() -> setTermType.  Here you must choose your payment terms method.  You must use predefined methods from the PayPal API.  Most common is NET_45 and DUE_ON_RECEIPT.

Finally we create our invoice and spit out an error if there is one.  The ResultPrinter class is very useful for sandboxing.  It’s included in bootstrap.php in the file named common.php.  Turn this off before going live.  And that’s it, if you run this script it will make an invoice!  We will go over sending the invoices in a later post.

Up Next

Multiple Items

Add Shipping Price

Customize Invoice Contents

Get the latest from The WP Crowd

12
Leave a Reply

avatar
8 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
GeekRootPayPal Invoice REST API in PHP: Part 3 - The WP Crowd The WP CrowdPayPal Invoice REST API in PHP: Part 2 - The WP Crowd The WP CrowdChris FlannaganRoy Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest
trackback

[…] PayPal Invoice REST API in PHP: Part 1 […]

Shelly Newsom Peacock
Guest

I would love to see an integration of PayPal’s “Bill Me Later” feature into a Gravity Form.

Roy
Guest

Isn’t “Bill Me Later” a Credit Card type thing? or is it tied into the owner’s PayPal funds?

Shelly Newsom Peacock
Guest

I would love to see an integration of PayPal’s “Bill Me Later” feature into a Gravity Form.

Roy
Guest

Isn’t “Bill Me Later” a Credit Card type thing? or is it tied into the owner’s PayPal funds?

trackback

[…] back.  If you missed Part 1 you can find it here.  Part 2 is going to be way simpler.  We’ve got everything in place already to create our […]

trackback

[…] back.  If you missed Part 1 you can find it here.  Part 2 is going to be way simpler.  We’ve got everything in place already to create our […]

GeekRoot
Guest

Hi, how can I use the Amount Only template? I don’t need the Quantity in my case.

Thanks