WooCommerce is one of the most popular plugins for e-commerce in WordPress. Payment gateways are an important part of it to receive payments for your products or to even make refunds if needed. In this tutorial we will create a WooCommerce payment gateway for PayPal using their SDK for PayPal REST.
We will wrap our payment gateway as a plugin. You will learn how to create payments and also how to process the information you get back from PayPal. Let’s first create our plugin structure.
Create a folder inside
wp-content/plugins and name it
woo-paypal. Inside that folder create a new folder
includes. That folder will contain our PayPal SDK. Create an main plugin file
woo-paypal.php also in the folder
Since we are using PHP for programming anything inside WordPress on the server side, we will have to use the PHP SDK. You can download it here: https://github.com/paypal/PayPal-PHP-SDK/releases. I have downloaded the PayPal-PHP-SDK-1.7.4.zip.
Copy everything inside the folder PayPal-PHP-SDK and paste them inside
includes/paypal-sdk. That folder now should look similar to this:
WooCommerce Payment Gateway Plugin
Our folder is prepared so now we can start coding (yey!). Open the
woo-paypal.php and insert the first line which defines our plugin. The only mandatory is the plugin’s name so let’s add only that:
I also like to add some constants for easier development. Now we will create a constant and also hook a function that will create our WooCommerce payment gateway once all the plugins are loaded.
WOO_PAYMENT_DIR will contain the path to our plugin so that we can easily include all other files such as the PayPal SDK. We will do that later in this tutorial.
We will define some namespaces that we will use from our PayPal SDK where all the classes reside, so add this also:
We need to create the function that we have hooked inside the
plugins_loaded action, so let’s start. This is the base of our WooCommerce payment gateway:
The payment gateway
Woo_PayPal_Gateway has been defined here by extending the
WC_Payment_Gateway. At the end we are also adding our class to all the registered payment gateways using the WooCommerce filter
Let’s start defining our
$apiContext will contain the authorization that PayPal SDK requires for communicating with the PayPal REST API.
In the constructor method we are defining some basic data such as:
- ID – the unique id for our payment gateway
- method_title – Title used inside the WooCommerce settings
- method_description – Description used inside the WooCommerce settings
- title – Title used on the front side at the checkout page
We have also defined that we do not use fields for our payment gateway, because we will redirect our customer to the PayPal page to finish the payment. The payment gateway can also support various features:
Our payment gateway will be shown only on purchasing products on the checkout page so we are supporting only the products feature.
After that we are getting the PayPal SDK using the method
get_paypal_sdk. We will create that method a little bit later.
To get our fields and also the settings for that fields, we have to initialize them. We do that with the
We have also hooked our
check_response method to a custom action hook
check_woopaypal. This action hook will be defined later on. The method
check_response will check for our PayPal response after the customer has payed or cancelled their payment. Both actions will redirect the customer back to our website.
The last part of the constructor method is to hook a method for saving the settings in the admin area. That method is already defined in the
Getting the PayPal SDK
We have already mentioned and called our method
get_paypal_sdk. Let’s define it now:
These is where we use our constant
WOO_PAYMENT_DIR for including our PayPal SDK. By including the
autoload.php, all our PayPal classes that we use will be called by the autoload.
Defining the Form Fields
Our form fields will be simple. One will be used to define if our payment gateway is enabled or disabled. The other two will be used for our the authorization part of our PayPal SDK.
The Client ID and the Client Secret are required by the PayPal SDK. I will show you how to get your client ID and secret at the end of this tutorial.
Processing the Payment
Once our customer clicks to Pay while the Woo Paypal is checked, our customer will be redirected to PayPal (if everything goes well). Before we redirect our customer we have to check if everything is ok and set all our products, shipping and taxes.
To communicate with PayPal, we need to have our
$apiContext defined. This will be done using a method
Now we can easily get our API context and use it to communicate with the PayPal. Let’s now define our method
I suppose you want to know what everything here is meant for, right? Let’s find out together!
First, we are getting the order object to the variable
$order. We are also setting the
$apiContext by calling the method
We are then creating a new Payer object in $payer and also assigning paypal as the payment method. The variable
$all_items will contain all our
Item objects for each product in our order. The variable
$subtotal will contain the total subtotal from all our products.
We are then iterating over the items in the order and for each item we are creating a new
Item object. If the item type is
fee, we are setting the Fee item with its cost and also the quantity of
If the item is a product, we are setting the
Item object with all the product’s information such as the name, price, quantity and also the SKU if there is any.
Once our object is done, we are pushing it to the array
After that the we are creating an
ItemList with all the items in it. The object
Details will contain all the information on the shipping, tax and subtotal. After that the object
Amount will get the currency code, the total and the
This will make sure that the total is the same as the shipping + tax + subtotal.
Once that is done, we are creating a
Transaction object where we set the
ItemList with all the items and the invoice number.
We are also creating the
$baseUrl using the order data. If the URL has a
? then we are setting a
& at the end of the URL. Otherwise, the
? will be set.
$baseUrl is done, we are creating the return URLs that PayPal will use for redirecting customers back to our website. We are setting a querystring
woopaypal that will be an indicator that our customer has returned from our WooCommerce payment gateway.
The last thing is to create the payment using
Payment. Once we create the payment, we can get the
$approvalUrl where we want to redirect our customer. If everything was successful, we are returning an array with the approval URL. Otherwise we are creating a WooCommerce notice and returning an array with failure so that our customer does not get redirected.
Response from PayPal
After our customer completes the payment or cancels it, he/she will return to our website. We have to know what happened to our order, so let’s see how we can easily check if the customer has returned from our payment gateway.
In the constructor method we have used a new action hook
check_woopaypal. At the bottom of our plugin’s file, add this code:
Here we are hooked to the action
init. In the function
check_for_woopaypal we are checking if there a
woopaypal querystring. If there is, then we are starting all the payment gateways and also create a new action hook
Now our payment gateway is active and can call the method
check_response. Let’s create that method in our
So, here we are getting the value of the querystring
woopaypal and also from the querystring
order_id which we have also passed. If the order id is equal to zero or none, we are stopping the response because we have no order.
We are also creating the
$order with the order ID. If the order has the status
processing, we are stopping there since we don’t need to check the payment. We are checking this just in case our customer refreshes the page with the same querystrings in the URL.
Customer has payed
If the value of our
true, then the payment was completed. To be sure of that, we must check everything once again, so we are getting the
$apiContext for the authorization part.
We are then getting the
$paymentId and get the
Payment from that ID and the
$apiContext. Since we will have to execute the payment (so that PayPal knows that we got the customer back to our website), we are creating an object from
PaymentExecution. We are here setting the
PayerID. The objects
Amount are the same as we used when processing the payment.
We are again iterating over all the order items just to get the subtotal value. The same steps we have used when processing the payment where we using here to set the shipping, tax, subtotal, total and currency information.
We are adding the transaction to the
$execution object. After that we are executing the payment by passing the
$execution object and the
If everything went well, we are completing the order by calling
payment_complete method on the
$order object. We are also adding a note on the order and also emptying the cart.
Otherwise, we are adding a WooCommerce notice and update the status of the order to
When a customer has cancelled the payment, we are getting the order and updating the status to
Activate the Plugin! 🙂
Getting the PayPal SDK Client ID & Secret
As I have said before, we are at the end of the tutorial and we will now see how to get the Client ID and Secret. Go to the Developer PayPal Site. Login with your account and go to the Dashboard.
Scroll down to the REST API Apps and click on Create an App. You have to have an account there to be able to use it for your App. If you don’t have one you can easily create on at Sandbox > Accounts.
Once you have created you App, click on it. You will see your client ID and secret. Copy them and paste them inside the WooCommerce > Settings > Checkout > Woo PayPal.
Creating your own WooCommerce Payment Gateway can be a lot of fun. But you really have to know how to use WordPress, WooCommerce and also your payment gateway. Once you know the workflow of your payment gateway, you can easily create the payment gateway you need.
This was a simple example of one. I do encourage you to look at other payment gateways you can find on WordPress.org plugins repository and study their code to see how to accomplish other things with the payment gateways for WooCommerce.
If you have any question regarding this payment gateway, feel free to post in the comments below!
If you want to know how to create a custom WooCommerce Shipping Method, you can learn that at my new course here. For more other specific WooCommerce tutorials, you can check out my eBook: