Skip to content

voucherifyio/voucherify-php-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Official Voucherify SDK for PHP


Migration from 0.x | Setup | Error handling | Contributing | Changelog

API: Vouchers | Campaigns | Distributions | Validations | Redemptions | Customers | Orders | Products | Validation Rules | Segments | Events | Promotions | Async Actions | Utils

Setup

Add Voucherify dependency into your composer.json:

"rspective/voucherify": "v2.0.*"

Update project dependencies:

$ composer install

Log-in to Voucherify web interace and obtain your Application Keys from Configuration:

require_once('vendor/autoload.php');

use Voucherify\VoucherifyClient;
use Voucherify\ClientException;

$apiID  = "YOUR-APPLICATION-ID";
$apiKey = "YOUR-CLIENT-SECRET-KEY";

$client = new VoucherifyClient($apiID, $apiKey);

Versioning

All requests will use your account API settings, unless you override the API version. The changelog lists every available version.

$apiVersion = "v2018-08-01";

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion);

Check versioning.

Custom API URL

By default client is sending request to https://api.voucherify.io. You can override $apiUrl while creating client instance if you want to use Voucherify running in a specific region

$apiVersion = null;
$apiUrl = "https://<region>.api.voucherify.io";

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl);

Custom Headers

It is possible to send custom headers in Voucherify API request.

$apiVersion = null;
$apiUrl = null;
$customHeaders = [
    "X-Custom-1" => "Value-1"
];

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl, $customHeaders);

# RESULT:
#   x-custom-1: Value-1

Special: Voucherify-Channel customization

$apiVersion = null;
$apiUrl = null;
$customHeaders = [
    "V-Voucherify-Channel" => "Value-1"
];

$client = new VoucherifyClient($apiID, $apiKey, $apiVersion, $apiUrl, $customHeaders);

# RESULT:
#   x-voucherify-channel: PHP-SDK-Value-1

PHP autoloading

When you aren't using composer you can load Voucherify module by including autoload.php file from /src directory.

require_once('{voucherify_src_path}/autoload.php');

use Voucherify\VoucherifyClient;
use Voucherify\ClientException;

$client = new VoucherifyClient($apiID, $apiKey);

API

This SDK is fully consistent with restufl API Voucherify provides. Detalied description and example responses you will find at official docs. Method headers point to more detalied params description you can use.

Vouchers API

Methods are provided within $client->vouchers->* namespace.

Check voucher object.

$client->vouchers->create($voucher);
$client->vouchers->get($code);
$client->vouchers->update($voucher_update);
$client->vouchers->delete($code);
$client->vouchers->delete($code, $force);
$client->vouchers->getList();
$client->vouchers->getList($params);
$client->vouchers->enable($code);
$client->vouchers->disable($code);
$client->vouchers->addBalance($code, $balance);
$client->vouchers->import($vouchers);

Campaigns API

Methods are provided within $client->campaigns->* namespace.

$client->campaigns->create($campaign);
$client->campaigns->get($name);
$client->campaigns->addVoucher($campaignName);
$client->campaigns->addVoucher($campaignName, $params);
$client->campaigns->addVoucherWithCode($campaignName, $code);
$client->campaigns->addVoucherWithCode($campaignName, $code, $params);
$client->campaigns->importVouchers($campaignName, $vouchers);
$client->campaigns->delete($campaignName);

Distributions API

Methods are provided within $client->distributions->* namespace.

$client->distributions->publish($campaign_name);
$client->distributions->publish($params);
$client->distributions->createExport($params);
$client->distributions->getExport($exportId);
$client->distributions->deleteExport($exportId);
$client->distributions->getPublications();
$client->distributions->getPublications($params);

Validations API

Methods are provided within $client->validations->* namespace.

$client->validations->validate($code);
$client->validations->validate($code, $params);

// OR

$client->validations->validateVoucher($code);
$client->validations->validateVoucher($code, $params);
$client->validations->validate($params);

Redemptions API

Methods are provided within $client->redemptions->* namespace.

Check redemption rollback object.

$client->redemptions->redeem($code);
$client->redemptions->redeem($code, $params);
$client->redemptions->redeem($promotionsTier, $params);
$client->redemptions->get($redemptionId);
$client->redemptions->getList();
$client->redemptions->getList($params);
$client->redemptions->getForVoucher($code);
$client->redemptions->rollback($redemption_id);
$client->redemptions->rollback($redemption_id, $params);
$client->redemptions->rollback($redemption_id, $reason);

Customers API

Methods are provided within $client->customers->* namespace.

Check customer object.

$client->customers->create($customer);
$client->customers->get($customer_id);
$client->customers->update($customer_update);
$client->customers->delete($customer_id);
$client->customers->getList();
$client->customers->getList($params);

Orders API

Methods are provided within $client->orders->* namespace.

Check customer object.

$client->orders->create($order);
$client->orders->get($order_id);
$client->orders->update($order_update);
$client->orders->getList();

Products API

Methods are provided within $client->products->* namespace.

Check product object.

Check sku object.

$client->products->create($product);
$client->products->get($product_id);
$client->products->getList();
$client->products->update($product_update);
$client->products->delete($product_id);
$client->products->delete($product_id, $force);
$client->products->createSku($product_id, $sku);
$client->products->getSku($product_id, $sku_id);
$client->products->getSkus($product_id);
$client->products->updateSku($product_id, $sku_update);
$client->products->deleteSku($product_id, $sku_id);
$client->products->deleteSku($product_id, $sku_id, $force);

Validation Rules API

Methods are provided within $client->validationRules->* namespace.

Check validation rule object.

$client->validationRules->create($rule);
$client->validationRules->get($rule_id);
$client->validationRules->getList();
$client->validationRules->update($rule_update);
$client->validationRules->delete($rule_id);
$client->validationRules->createAssignment($rule_id, $assignment);
$client->validationRules->getAssignments($rule_id);
$client->validationRules->deleteAssignment($rule_id, $assignment_id);

Segments API

Methods are provided within $client->segments->* namespace.

Check segment object.

$client->segments->create($params);
$client->segments->get($segment_id);
$client->segments->delete($segment_id);

Events API

Methods are provided within $client->customEvents->* namespace.

Check event object.

$client->customEvent->track($event, $customer);

Promotions API

Methods are provided within $client->promotions->* namespace.

Check promotion campaign object.

Check promotion's tier object.

$client->promotions->create($promotionCampaign);
$client->promotions->validate($validationContext);
$client->promotions->tiers->getList($promotionCampaignId);
$client->promotions->tiers->create($promotionCampaignId, $promotionsTier);
$client->promotions->tiers->redeem($promotionsTierId, $redemptionContext);
$client->promotions->tiers->update($promotionTierId);
$client->promotions->tiers->delete($promotionTierId);
$client->promotions->tiers->getAvailable();

Async Actions API

Methods are provided within $client->asyncActions->* namespace.

$client->asyncActions->get($id);
$client->asyncActions->getList();
$client->asyncActions->getList($params);

Utils

To use utils you have to import Voucherify Utils class.

require_once('vendor/autoload.php');

use Voucherify\Utils;

Available methods:

Verify Webhook Signature

Utils::verifyWebhookSignature($signature, $message, $secretKey)

Migration from 0.x

Version 1.x of the PHP is fully backward compatible with version 0.x. Changes made in version 1.x mostly relate to grouping methods within namespaces. So all you need to do is to follow the list bellow and just replace deprecated methods with their namespaced equivalent.

Deprecated methods


Error handling

VoucherifyClient will throw custom ClientException object. To get sutructure described in our API reference please use:

try {
    ...
}
catch (ClientException $e) {
    $error = $e->getError();
}

Logging

VoucherifyClient has method setLogger() which can be used to set PSR-3 logger interface.

Set own logger if you want to preview curl request and response data.

$logger = /* Initialaze logger i.e Monolog, Analog */

$client = new VoucherifyClient($apiID, $apiKey);
$client->setLogger($logger);

Connection Options

Use setConnectionOptions() method to set client connection options.

Options:

  • timeout - curl 'CURLOPT_TIMEOUT_MS'
  • connectTimeout - curl 'CURLOPT_CONNECTTIMEOUT'
$options = [
    "timeout" => 1500,
    "connectTimeout" => 2
];

$client = new VoucherifyClient($apiID, $apiKey);
$client->setConnectionOptions($options);

Use case - CodeIgniter

Simple example of adding Voucherify to your CodeIgniter project.

Setup

Download voucherify /src directory to application/third_party/voucherify.

Custom Library

Create new library file in /application/libraries directory, i.e. Coupons.php.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

$src_voucherify = APPPATH . "third_party/voucherify/autoload.php";

include($src_voucherify);

use Voucherify\VoucherifyClient;
use Voucherify\ClientException;

class Coupons {

    public $voucherify;

    public function __construct() {
        $apiID  = "YOUR-APPLICATION-ID";
        $apiKey = "YOUR-CLIENT-SECRET-KEY";

        $this->voucherify = new VoucherifyClient($apiId, $apiKey);
    }
}

Using Voucherify

Load new library and start using voucherify client.

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Voucher extends CI_Controller {

    public function index()
    {
        $this->load->library('coupons');

        $voucherCode = "TEST-VOUCHER-CODE";
        $voucher = $this->coupons->voucherify->get($voucherCode);

        ...
    }
}

Contributing

Bug reports and pull requests are welcome through GitHub Issues.

Changelog

  • 2022-05-16 - 2.2.0 - Add CustomEvents support
  • 2022-03-11 - 2.1.0 - Add AsyncActions support and a $customHeaders param
  • 2019-07-19 - 2.0.0 - Hide API versioning in $apiUrl param
  • 2018-12-28 - 1.7.10 - Add Validation Rule Assignments
  • 2018-03-18 - 1.7.9 - Add Utils with verifyWebhookSignature method
  • 2018-02-18 - 1.7.8 - Product delete force option support
  • 2018-02-13 - 1.7.7 - Fix Promotions Tiers getAvailable method param
  • 2018-02-13 - 1.7.6 - Promotions Tiers getAvailable method
  • 2018-02-11 - 1.7.4 - Customers getList method
  • 2018-01-14 - 1.7.3 - Promotions API
  • 2017-07-24 - 1.7.2 - Fix get publications missing params
  • 2017-07-23 - 1.7.1 - Api Client conneciton options
  • 2017-07-12 - 1.7.0 - Orders API
  • 2017-07-10 - 1.6.2 - PHP autoloading support
  • 2017-07-07 - 1.6.1 - Remove Psr/Log dependency
  • 2017-06-26 - 1.6.0 - Api Client logger support
  • 2017-06-21 - 1.5.0 - Custom API URL support
  • 2017-05-02 - 1.4.0 - API Version Header support
  • 2017-05-02 - 1.3.0 - Validation rules API, Segments API, Products API
  • 2017-04-27 - 1.2.0 - Validations API, Redemptions-Get, Distributions-Export
  • 2017-04-26 - 1.1.0 - Campaigns API, Vouchers import method
  • 2017-04-19 - 1.0.2 - Unit tests, bug fixes
  • 2017-03-17 - 1.0.1 - Vouchers addBalance method
  • 2017-02-19 - 1.0.0 - Namespace refectoring
  • 2016-09-13 - 0.11.0 - Added new API method for voucher - publish
  • 2016-09-13 - 0.10.0 - Added new API method for voucher - delete
  • 2016-09-13 - 0.9.1 - Fix to maintain builder pattern.
  • 2016-07-20 - 0.9.0 - Voucher code pattern.
  • 2016-07-19 - 0.8.0 - Voucher update method.
  • 2016-06-23 - 0.7.0 - Gift vouchers.
  • 2016-04-27 - 0.6.0 - Added new API methods for customer - create, get, update, delete.
  • 2016-04-27 - 0.5.0 - Rollback redemption.
  • 2016-04-18 - 0.4.0 - List vouchers. Filter by customer.
  • 2016-04-07 - 0.3.0 - List redemptions.
  • 2016-04-04 - 0.2.2 - Updated API URL.
  • 2016-03-03 - 0.2.1 - Fixed a typo (diasble -> disable).
  • 2016-01-21 - 0.2.0 - Added new API methods - create, disable and enable.
  • 2015-12-11 - 0.1.1 - New discount model. Added UNIT - a new discount type.
  • 2015-12-02 - 0.1.0 - First version:
    • Authentication
    • Voucher informations: get, redemption
    • Voucher operations: redeem