How to use telr hosted and remote?
Tags: telr, transaction
Published: 2020-05-11 12:24:03

First thing we should create order  in hosted payment page.
Using this simple code and params.

This API will call when the client click pay or checkout .
All API requests should sent from server side not client side

>note: All this API's should sent from server.

```php
<?php
$params = [
    'ivp_framed' => 2,
    'ivp_method' => 'create',
    'ivp_store' => *****,
    'ivp_authkey' => '********',
    'ivp_desc' => ' Description good',
    'ivp_cart' => 'maids - cc - 12341231d542757'.rand(1,400),
    'ivp_currency' => 'AED',
    'ivp_amount' => 1000,
    'ivp_test' => 1,
    'return_auth' => 'https://teljoy.io/telr/new/trans.php?status=Success',
    'return_decl' => 'https://teljoy.io/telr/new/trans.php?status=Declined',
    'return_can' => 'https://teljoy.io/telr/new/trans.php?status=Cancelled',
    'bill_title' => 'Mr',
    'bill_fname' => 'Mohammad',
    'bill_sname' => 'Maids',
    'bill_email' => 'memeaktaa@gmail . com',
    'bill_addr1' => 'Dubai',
    'bill_city' => 'Dubai',
    'bill_country' => 'AE',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.telr.com/gateway/order.json");
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
exit(0);
```

The response of it will be like:
```json
{
"method":"create",
"trace":"4001/7932/5eb93cca",
"order":{
    "ref":"869748BD7502CD7535765D3951855B92AAEE0478D8FF0A41FCFDE86AED79A4F9",
    "url":"https://secure.telr.com/gateway/process_framed_full.html?o=869748BD7502CD7535765D3951855B92AAEE0478D8FF0A41FCFDE86AED79A4F9"
   }
}

```

The url of order object will be shown in `iframe` ,now you can complete the payment process and after it completed you need to send request to get `transaction reference` .
This API should sent internally from server if we have the order id.
if not we need to send the order id from front end.

```php
<?php
$params = [
    'ivp_method' => 'check',
    'ivp_store' => '****',
    'ivp_authkey' => '******',
    'order_ref' => $_POST['order'],
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.telr.com/gateway/order.json");
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$results = curl_exec($ch);
curl_close($ch);
print_r($results);
exit(0);
```

and the response will be like this if it not `(approved or completed)`

```json
{
    "method": "check",
    "trace": "4000/16024/5eb93f67",
    "order": {
        "ref": "FD8F21148182123153692EDFA221C93C6313FC659B04353C2E74F4F200495D6B",
        "url": "https://secure.telr.com/gateway/process.html?o=FD8F21148182123153692EDFA221C93C6313FC659B04353C2E74F4F200495D6B",
        "cartid": "maids-cc-1234123254dfasdasdq",
        "test": 1,
        "amount": "1000.00",
        "currency": "AED",
        "description": "Description good",
        "status": {
            "code": 1,
            "text": "Pending"
        }
    }
}
```

In `completed` status

```json
{
    "method": "check",
    "trace": "4000/19723/5eb94023",
    "order": {
        "ref": "24AE0DAD7B32CE486694CFF1CFC3C9868FEA617245BB0CF48FF1DEDD563DF8A8",
        "cartid": "maids-cc-1234123254dasd21ew2",
        "test": 1,
        "amount": "1000.00",
        "currency": "AED",
        "description": "Description good",
        "status": {
            "code": 3,
            "text": "Paid"
        },
        "transaction": {
            "ref": "040024914775",
            "type": "sale",
            "class": "ECom",
            "status": "A",
            "code": "923613",
            "message": "Authorised"
        },
        "paymethod": "Card",
        "card": {
            "type": "Visa Credit",
            "last4": "4242",
            "country": "AE",
            "first6": "424242",
            "expiry": {
                "month": 5,
                "year": 2020
            }
        },
        "customer": {
            "email": "memeaktaa@gmail.com",
            "name": {
                "forenames": "Maids.cc Invoice Mohammad",
                "surname": "Maids"
            },
            "address": {
                "line1": "Dubai",
                "city": "Dubai",
                "country": "AE"
            }
        }
    }
}

```

The last this is `recurring transactions`.
If the client didn't accept to save his card details we shouldn't save his `transaction reference` and show hosted payment page every time he want to pay.
So we need to make checkbox for saving cards or not.

```php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $params = [
        'ivp_store' => '******',
        'ivp_authkey' => '*******',
        'ivp_trantype' => 'void',
        'ivp_tranclass' => 'ecom',
        'ivp_currency' => 'AED',
        'ivp_amount' => '1000',
        'ivp_test' => '1',
        'tran_ref' => '040024914775',
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://secure.telr.com/gateway/remote.html");
    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    $results = curl_exec($ch);
    curl_close($ch);
    echo $results;
    return;
}
    
```
and the response if there is no error will be like this after formatting and parsing because it's coming as a string.
```
auth_status=A&auth_code=924270&auth_message=Processed&auth_tranref=030025268255&auth_cvv=Y&auth_avs=X&auth_trace=4000%2f24269%2f5eb94102&payment_code=VC&payment_desc=Visa%20Credit%20ending%204242&payment_cardl4=4242&payment_cardl6=424242
```

I parsed it with js
```javascript
   res = res.split('&')
   let data = [];
   let value = '';
   let key = '';
   for (let i = 0; i < res.length; i++) {
       key=res[i].split('=')[0];
       value=res[i].split('=')[1];
       data[key]=value
    }
```

```json
auth_avs: "X"
auth_code: "26"
auth_cvv: "Y"
auth_message: "Original%20transaction%20already%20voided"
auth_status: "E"
auth_trace: "4001%2f4677%2f5eb942fa"
auth_tranref: "040024914812"
payment_cardl4: "4242"
payment_cardl6: "424242↵"
payment_code: "VC"
payment_desc: "Visa%20Credit%20ending%204242"
```

If there is an error the response will be:
```
auth_status=E&auth_code=01&auth_message=Invalid%20request&auth_tranref=000000000000&auth_cvv=X&auth_avs=X&auth_trace=4000%2f3110%2f5eb9439b&payment_code=&payment_desc=
```


```json
auth_avs: "X"
auth_code: "01"
auth_cvv: "X"
auth_message: "Invalid%20request"
auth_status: "E"
auth_trace: "4000%2f3110%2f5eb9439b"
auth_tranref: "000000000000"
payment_code: ""
payment_desc: "↵"
```