# Paypal Checkout V2 Sample (Server-Side) - Integrate Paypal with Your Custom PHP App

> Learn how to integrate your custom PHP App with Paypal. This specifically focuses on a Paypal checkout V2 Sample. Code sample is included.

HTML: https://endertech.com/blog/paypal-checkout-v2-sample-server-side-how-to-integrate-paypal-with-your-custom-php-app
Published: 2021-05-31T00:00:00+00:00
Categories: Web Development, E-Commerce

Getting Started with Paypal Checkout V2 (Server-Side)

If you have landed on this article, you’re most likely having trouble with PayPal Checkout integration. No worries, this article has been created just for you. We’ve created a step-by-step guide so you can start creating dynamic orders using PayPal Checkout. This is specifically a Paypal Checkout V2 Sample that’ll be supported with code examples and notes, helping you to implement this feature in full by the end of this tutorial.

First things first, we need to create a PayPal developers account to obtain a client ID and secret key for your app. Once you are logged in, create a new app under My Apps & Credentials. You will automatically get directed to a page that will provide these keys. Let’s set these aside, we’ll need those later.

Now let’s begin the PayPal Checkout integration by installing the PayPal Javascript SDK. You can find more info here.

Composer require paypal/paypal-checkout-sdk

After installing the sdk we need to make it available to our app and configure credentials. In your .env, create two new variables CLIENT_ID and CLIENT_SECRET. Copy and paste the client ID and secret key we saved earlier.

Now, to make the SDK available to our app we called PayPalClient or any name you like.

Notice we have two parameters in our createOrder method, $transactionAmount and $description. The value to these parameters will be generated dynamically in our app. The values are passed down to our buildRequestBody method that will be generating our purchase units.

Next we create a new controller that will handle our transactions. Within this controller we’ll create a route that will handle creating our new orders.

/**
* @Route("/api/create-order", name="api_create_order")
*/
public function apiCreateOrder(CreateOrderService $createOrderService, Request $request)
{
 $user = $this->getUser();
 $transaction = $this->transactionRepository->findTransaction($user);
 $transactionAmount = $transaction->getAmount();
 $description = $transaction->getDescription();
 
 if($request->isMethod('POST')){
 $paypalResponse = $createOrderService->CreateOrder(false, $request, $transactionAmount, $description);
 return new JsonResponse($paypalResponse->result, $paypalResponse->statusCode);
 }
}

Next, we want to create a route that will handle what happens after the Paypal transaction has been accepted.

/**
* @Route("/payment/approved", name="payment_approved")
*/
public function paymentApproved( Request $request ){
 //do something here
}

Ok we’re almost at the finish line. So far we’ve set up all of the backend logic, now we just need to set up our front-end checkout flow.

If you are building this feature in a single-page application, here is documentation on how to add the javascript SDK to your checkout page. You’ll find the following example to be similar. You can find more information on this example in Paypal documentation here.

checkout-page.html

At the top of your checkout page you want to add this script, to add the javascript SDK.

You can customize the integration by passing parameters in the SDK script tag.

 // Replace YOUR_SB_CLIENT_ID with your client ID

At the bottom of your checkout page we’ll begin to integrate the PayPal Buttons and set up the Javascript SDK for transactions.

We will begin with the PayPal Buttons.

Create a button container and give it an id.

="paypal-button-container" style="width: 200px">

paypal.Buttons().render('#paypal-button-container');

Next, we’ll set up the createOrder function which will be triggered when your user clicks on the PayPal Button. Notice we are fetching our createOrder route we created earlier.

paypal.Buttons({
 createOrder: function(data, actions) {
 return fetch('{{ path('api_create_order' ) }}', {
 method: 'POST',
 headers: {
 'content-type': 'application/json',
 }
 }).then(function (res) {
 return res.json();
 }).then(function (data) {
 return data.id;
 })
 }
 }).render('#paypal-button-container');

We can pass the transaction amount and description to our createOrder route or you can already have the transaction information in your database and pull that information into your createOrder route and your checkout page can

be an order confirmation page. Now we need to implement the OnApprove function that will be triggered when a transaction has been approved by your user. Earlier we created a paymentApproved route that would handle custom logic for our app after a user is done paying.

paypal.Buttons({
 createOrder: function(data, actions) {
 return fetch('{{ path('api_create_order' ) }}', {
 method: 'POST',
 headers: {
 'content-type': 'application/json',
 }
 }).then(function (res) {
 return res.json();
 }).then(function (data) {
 return data.id;
 })
 },
 onApprove: function(data, actions) { 
 return actions.order.capture().then(function(details) {
 fetch('{{ path('payment_approved') }}', {
 method: 'POST',
 headers: {
 'content-type': 'application/json'
 },
 body: JSON.stringify(details)
 }).then(function (res) {
 window.location.href = res.url;
 });
 })
 }
 }).render('#paypal-button-container');

Note the details variable holds a lot of helpful information like transaction status, payer information, shipping information etc.

We are done. You should now have the PayPal Checkout feature fully integrated. Below there is a list of references that may be useful for customizing this feature to your specific need. We hope this guide has been helpful to you, enjoy! 

Creating developer and sandbox account

Basic Integration

Understanding the Javascript SDK

Installing the Javascript SDK

Setting up a transaction server side (create order)

Setting up a transaction client side (create order))

Customizing checkout buttons

Create Order full parameters

PayPal Checkout Demo

Handle buyer checkout error
