unidays/unidays-php

This is the PhP library for integrating with UNiDAYS

v1.2.2 2019-02-25 09:59 UTC

This package is auto-updated.

Last update: 2024-10-25 23:38:46 UTC


README


Build Status

UNiDAYS PHP Library

This is the PHP library for integrating with UNiDAYS. This is to be used for coded and codeless integrations. The following documentation provides descriptions of the implementations and examples.

Contents

How to use this code?

Direct Tracking

Codeless Verification

Contributing

How to use this code

  • Pull the package from Packagist. The commands for doing this are displayed on the Packagist page. Please use the most recent version.
  • See the example usage section for the type of call you intend to use. Each of these contains an example.

Direct Tracking

Parameters

Here is a description of all the available parameters. Which of these you provide to us are dependent on the agreed contract.

Mandatory Parameters

Having either Code or MemberID as a parameter is also mandatory:

Additional Parameters

Note any of the following properties to which the value is unknown should be omitted from calls. Which of the following values you provide to us will depend on your agreed contract.

Example Basket

Here is an example basket with the fields relating to UNiDAYS tracking parameters,

Example Usage

Below are the three options for implementing your integration. These examples cover both coded and codeless integrations (see the live analytics PDF for details) and include all optional parameters. They are intended as a guideline for implementation.

Create Server URL

This method returns a URL which you can use to call the API.

It is a mandatory requirement that all server URLs are signed. This means you are required to pass the signing key UNiDAYS provide you with as one of the arguments. The signing key is a Base64 encoded GUID. This endpoint accepts both GET and POST requests.

Making the call

The method to get the URL to make a server-to-server request with is create_server_url($key). To implement this method you first need to use the DirectTrackingDetailsBuilder to create a direct tracking object with the properties you want to send across to us. More details about this builder can be found here.

Once the object containing the details you need to send us is created, create a Tracking helper, providing those details as an argument new TrackingHelper($directTrackingDetails) and call ->create_server_url($key) where $key is the key provided to you by UNiDAYS.

Return

A URL will be returned to you, which can then be used to call our API. If successful a response with a status code of 204 No Content will be returned. This will work for both POST and GET requests.

Example

<?php

use Unidays;

// UNiDAYS will provide your partnerId and key
$partnerId = "somePartnerId";
$key = "someSigningKey";

$details = new DirectTrackingDetailsBuilder($partnerId, 'order123', 'GBP');
$details->withOrderTotal(209.00);
$details->withItemsUnidaysDiscount(13.00);
$details->withCode('UNI123');
$details->withItemsTax(34.50);
$details->withShippingGross(5.00);
$details->withShippingDiscount(3.00);
$details->withItemsGross(230.00);
$details->withItemsOtherDiscount(10.00);
$details->withUnidaysDiscountPercentage(10.00);
$details->withNewCustomer(true);
$directTrackingDetails = $details->build();

$helper = new TrackingHelper($directTrackingDetails);

$url = $helper->create_server_url($key);

Create Script URL

This is also known as our client-to-server integration. This method returns a URL which can be placed within a script element on your post-payment/order-success page to call the API.

Unsigned or Signed

It's an option to create a signed url for your Script request. To do this you will need to send us the signing key UNiDAYS provide you with as one of the arguments for the signed method.

$url = $helper->create_signed_script_url($key);

instead of

$url = $helper->create_script_url();

Making the call

The method to get the URL to make a client-to-server request with is create_script_url(), or create_signed_script_url($key) if you've chosen to have a signed URL returned. To implement this method you first need to use the DirectTrackingDetailsBuilder to create a direct tracking object with the properties you want to send across to us. More details about this builder can be found here.

Once the object containing the details you need to send us is created, create a Tracking helper, providing those details as an argument new TrackingHelper($directTrackingDetails) and call ->create_script_url() for an unsigned url, or ->create_signed_script_url($key), where $key is the key provided to you by UNiDAYS.

Return

A URL will be returned to you which can be placed within a script element on your post-payment/order-success page to call the API. If successful a response with a status code of 200 OK will be returned. This will only work for GET requests.

Example

The below example is a request for an unsigned Script URL.

<?php

use Unidays;

// UNiDAYS will provide your partnerId and key
$partnerId = "somePartnerId";

$details = new DirectTrackingDetailsBuilder($partnerId, 'order123', 'GBP');
$details->withOrderTotal(209.00);
$details->withItemsUnidaysDiscount(13.00);
$details->withCode('UNI123');
$details->withItemsTax(34.50);
$details->withShippingGross(5.00);
$details->withShippingDiscount(3.00);
$details->withItemsGross(230.00);
$details->withItemsOtherDiscount(10.00);
$details->withUnidaysDiscountPercentage(10.00);
$details->withNewCustomer(true);
$directTrackingDetails = $details->build();

$helper = new TrackingHelper($directTrackingDetails);

$url = $helper->create_script_url();

Tracking Client

Calls to the Tracking Client are similar to create server url but rather than returning a URL, UNiDAYS sends the request and returns a response.

It is a mandatory requirement that all Tracking Client calls are provided with a key, as the requests UNiDAYS send are signed.

Making the call

To implement this method you first need to use the DirectTrackingDetailsBuilder to create a direct tracking object with the properties you want to send across to us. More details about this builder can be found here.

Once the object containing the details you need to send us is created, create an instance of the tracking client, providing those details as a parameter, along with the signing key UNiDAYS provided you with new TrackingClient($directTrackingDetails, $key) and call ->sendRequest().

Return

A HttpResponseMessage is returned. If successful the response should have a status code of 204 No Content.

Example

The below example sets up some direct tracking details, calls sendRequest on the client and echoes the response code to the console.

<?php

use Unidays;

// UNiDAYS will provide your partnerId and key
$partnerId = "somePartnerId";
$key = "someSigningKey";

$details = new DirectTrackingDetailsBuilder($partnerId, 'order123', 'GBP');
$details->withOrderTotal(209.00);
$details->withItemsUnidaysDiscount(13.00);
$details->withCode('UNI123');
$details->withItemsTax(34.50);
$details->withShippingGross(5.00);
$details->withShippingDiscount(3.00);
$details->withItemsGross(230.00);
$details->withItemsOtherDiscount(10.00);
$details->withUnidaysDiscountPercentage(10.00);
$details->withNewCustomer(true);
$directTrackingDetails = $details->build();

$client = new TrackingClient($directTrackingDetails, $key);

$response = $client->sendRequest();

echo $response->code;

Test Endpoint

UNiDAYS provide a test-endpoint configuration of the TrackingHelper object.

Return

The TrackingHelper object, configured in test mode, will add an extra parameter (&Test=True) to the URL that is returned to you, or sent for you.

Example

<?php

use Unidays;

// UNiDAYS will provide your partnerId and key
$partnerId = "somePartnerId";
$key = "someSigningKey";

$details = new DirectTrackingDetailsBuilder($partnerId, 'order123', 'GBP');
$details->withOrderTotal(209.00);
$details->withItemsUnidaysDiscount(13.00);
$details->withCode('UNI123');
$details->withItemsTax(34.50);
$details->withShippingGross(5.00);
$details->withShippingDiscount(3.00);
$details->withItemsGross(230.00);
$details->withItemsOtherDiscount(10.00);
$details->withUnidaysDiscountPercentage(10.00);
$details->withNewCustomer(true);
$directTrackingDetails = $details->build();

// Pass in an aditional argument of true to instantiate the TrackingHelper object in test mode
$helper = new TrackingHelper($directTrackingDetails, true);

// The url generated will now contain the appended $Test=True parameter, this url will call our test endpoint
$url = $helper->create_server_url($key);

Direct Tracking Details Builder

The purpose of the builder is to make it simple and intuitive when constructing any tracking request to UNiDAYS.

The arguments on the builder are the mandatory parameters:

$directTrackingDetails = new DirectTrackingDetailsBuilder($partnerId, $currency, $transactionId)

There are then a variety of methods available to build up the information you want to send us which can be chained up per the example. These match up to the parameters at the top of this document.

  • withMemberId(base64 encoded guid)
  • withCode(string)
  • withOrderTotal(decimal)
  • withItemsUnidaysDiscount(decimal)
  • withItemsTax(decimal)
  • withShippingGross(decimal)
  • withShippingDiscount(decimal)
  • withItemsGross(decimal)
  • withItemsOtherDiscount(decimal)
  • withUnidaysDiscountPercentage(decimal)
  • withNewCustomer(bool)

Only chain the values that you have contractually agreed to provide. It is not a requirement to use every method.

The final call to be chained is ->build() which creates the object.

Example

<?php

use Unidays;

$details = new DirectTrackingDetailsBuilder('somePartnerId', 'order123', 'GBP');
$details->withOrderTotal(209.00);
$details->withItemsUnidaysDiscount(13.00);
$details->withCode('UNI123');
$details->withItemsTax(34.50);
$details->withShippingGross(5.00);
$details->withShippingDiscount(3.00);
$details->withItemsGross(230.00);
$details->withItemsOtherDiscount(10.00);
$details->withUnidaysDiscountPercentage(10.00);
$details->withNewCustomer(true);
$directTrackingDetails = $details->build();

Codeless Verification

If you have agreed to provide UNiDAYS Members with a codeless experience, alongside direct tracking, you will also need to implement the 'Codeless API' which will assist you with parsing and validating the signed traffic we direct towards your site.

Codeless API

Making the call

First call the CodelessUrlVerifier with the key provided to you by UNiDAYS new CodelessUrlVerifier($key). Then call the verify_url_params($ud_s, $ud_t, $ud_h) method with the values for ud_s, ud_t and ud_h as the arguments.

Return

If the method successfully validates the hash of the incoming request, a DateTime for the request will be returned; else null will be returned.

Example

<?php

use Unidays;

// Your key as provided by UNiDAYS
$key = "someSigningKey";

// Obtain parameters from the query string. Be sure to URL Decode them
$ud_s = "Do/faqh330SGgCnn4t3X4g==";
$ud_t = "1395741712";
$ud_h = "i38dJdX+XLKuE4F5tv+Knpl5NPtu5zrdsjnqBQliJEJE4NkMmfurVnUaT46WluRYoD1/f5spAqU36YgeTMCNeg==";

$verifier = new CodelessUrlVerifier($key);
$verifiedAt = $verifier->verify_url_params($ud_s, $ud_t, $ud_h);

Contributing

This project is set up as an open source project. As such, if there are any suggestions that you have for features, for improving the code itself, or you have come across any problems; you can raise them and/or suggest changes in implementation.

If you are interested in contributing to this codebase, please follow the contributing guidelines. This contains guides on both contributing directly and raising feature requests or bug reports. Please adhere to our code of conduct when doing any of the above.