---
title: Creating a Payment Session Tutorial
slug: creating-a-payment-session-tutorial
docTags: 
createdAt: 2024-05-29T13:31:11.219Z
---

:::hint{type="info"}
This tutorial is for your **back-end server**.
:::

In this tutorial, we will cover creating a payment session, by calling the Judopay [Transaction API](https://docs.judopay.com/api-reference/index.html#).&#x20;
The purpose of the payment session is to **verify the identity of the cardholder**, to prevent fraudulent transactions and refunds.

The payment session reference is used when calling our Web SDK’s payment functions from your client side.

## Full Tutorial

In this tutorial, we will be using PHP as our server-side scripting language. You may use any server-side language you wish, as we have a REST API (which responds with JSON).

::embed[]{url="https://www.youtube.com/watch?v=croKQApvb1c"}

We have created video chapters from the full tutorial, to help you easily re-watch different parts of the tutorial. We have included an overview and example code snippets for each section below:

:::hint{type="info"}
When using the code snippets, ensure to replace any field values with your own values.
:::

***

### 1. Intro

::embed[]{url="https://www.youtube.com/watch?v=7vsjRTvYMc4"}



**Payment Flow:**

![tutorial payment session diagram](https://api.archbee.com/api/optimize/QGXTyW3MgdNcMjdEa0v8r/JMok24f65gF6Op0ndYtve_how-to-video-payment-session-diagram.png "Payment Flow")

***

### 2. Defining parts of the request

To make a request to Judopay’s Transaction API we first need to define:

- The request headers
- The environment server URL
- The request endpoint
- The request body

:::hint{type="info"}
Ensure that the data used here, is also used later when calling Web SDK functions from your client side.&#x20;
For example judoId, API Token / Secret, yourConsumerReference.
:::

::embed[]{url="https://www.youtube.com/watch?v=OfvRTdj1nmM"}

:::CodeblockTabs
Define parts of the Request

```javascript
// Base64 Encode your API token and secret for the API Auth Header
$userEncode = base64_encode($yourAPIToken.":".$yourAPISecret);

// Setting the Headers
$headers = array(
  'Content-Type:application/json',
  'Authorization: Basic '.$userEncode,
  'API-Version: 6.18' //ensure to set this to the latest version
);

//Setting the endpoint
$endpoint="/paymentsession"; 

//Setting the API server url with the endpoint 
$environment = "https://api-sandbox.karatepay.com".$endpoint; //api.judopay.com is for the live environment

// Setting the data for the request's payload
$data=array(
        'judoId' => "yourJudoID", //Unique merchant/location ID supplied by Judopay
        'amount' => 1.00,
        'currency' => "GBP",
        'yourPaymentReference' => "yourPaymentReference", //Unique reference for this payment
        'yourConsumerReference' => "yourConsumerReference" //Unique reference to anonymously identify your customer
);

// Convert the data array to JSON
$requestPayload=json_encode($data);
```
:::

***

### 3. Making a request to Judopay's Transaction API

Pulling together everything we have defined, to make a POST request to Judopay’s Transaction API payment session endpoint.

::embed[]{url="https://www.youtube.com/watch?v=S4ZqNmklvWc"}

:::CodeblockTabs
POST Request Example

```javascript
//Making a POST request to Judopay's API
$ch = curl_init($environment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$requestPayload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
```
:::

***

### 4. Handling the API response

For a successful response, the payment session reference value should be stored in your backend server.&#x20;
This value should be passed to your client side when it is calling Web SDK functions (where the reference value is passed as a parameter).

::embed[]{url="https://www.youtube.com/watch?v=k7B4QTvuYVE"}

:::CodeblockTabs
Handle the Response

```javascript
// Decode the JSON reply
$obj = json_decode($response);

//Check if response contains the reference
if(isset($obj->reference)){
   $reference = $obj->reference; //store value so it can be accessed later (when calling WebSDK functions)
   echo $reference;
}

//Handle the API error
else {
   $errorMessage = $obj->message;
echo $errorMessage;
```
:::

***

### Resources

- [‍Web SDK Error Responses](docId:40dWE6LBub7vdKza1QYDC)
- [Handling a Transaction Tutorial](docId\:WC705ScaXt4SnkEFGXQR7)
- [Judopay Transaction API Documentation](https://docs.judopay.com/api-reference/index.html#)

