JSMA voucher

Overview

The JSMA voucher allows access and manipulation of vouchers (voucher campaigns) and tokens (voucher codes) used in carts and orders.

What can JSMA voucher do for me?

With the JSMA voucher you can create, edit and delete vouchers and tokens for carts and orders on your shopping website.

It allows you to dynamically generate vouchers and import existing tokens - which enables a synchronization between IKONA and external voucher systems.

Index

Usage

writing data

Here is an example on how to write data to a JSMA voucher:

function writeVoucher(preparedTokens)
{
  [...]

  var imported = voucher.importTokens(preparedTokens);
	
  imported.update({'value' : 10, 'validFrom' : date.getTime(), 'validUntil' : dateInOneWeek.getTime(), 'discountType' : 'relative'});

  imported.set('products.whitelist', portal.get('voucherEasterWhitelist'));

  [...]	
}

reading data

Here is an example on how to read data from a JSMA voucher:

function getCartItems(cart, parameters)
{
  var items = cart.items;
  var resultItems = [];
  
  for (var i=0;i<items.length;i++)
  {
    var item = items[i];
    var resultItem = {};
    
	[...]
		
	if (item.type === "voucher")
	{
		var voucher = item.voucher;
		
		resultItem.voucher = {
			id : voucher.id,
			uuid : voucher.uuid,
			value : voucher.value,
			display : voucher.display,
			isValid : voucher.isValid,
			type : voucher.type,
			withShipping : voucher.withShipping,        
			validFrom : voucher.validFrom,
			validUntil : voucher.validUntil,
		};
	}
    
    resultItems.push(resultItem);
  }
  
  return resultItems;
}

Methods

The JSMA voucher provides the following methods:

bool voucher.activate()

Activates the voucher. Returns true if the operation was successfull.

Example

...
voucher.activate() // => true
...


bool voucher.addToken(token)

Adds a token to a voucher. Returns true if the operation was successfull.

Example

...
voucher.addToken(token) // => true
...


Parameter token (required)

token[] voucher.createTokens(count)

Creates an array of voucher tokens.

Example

...
voucher.createTokens(10); // => [{'token' : 'token1', 'used' : false, 'orderItemId' : null}, {'token' : 'token2', 'used' : false, 'orderItemId' : null}, ...]
...

Parameter int count (required)

Positive integer. Determines how many tokens will be created.

bool voucher.deactivate()

Deactivates the voucher. Returns true if the operation was successfull.

Example

...
voucher.deactivate()
...


bool voucher.delete()

Returns true if the voucher has been deleted successfully, otherwise false.

Example

...
voucher.delete(); // => true
...

string voucher.display

Returns the display field of the voucher as a string.

Example

...
voucher.display // => '14% Valentinstag-Rabatt'
...


voucher.flexAttributes

Returns the flex attributes of a voucher

Example

...
voucher.flexAttributes // => {['products.whitelist' : 'rhino\.integration\.products\.EligibleProducts\.*']}
...


string voucher.get(key)

Returns the value of key as string or null if no value is set under the key.

Example

...
voucher.get('procuts.whitelist') // => 'rhino\.integration\.products\.EligibleProducts\.*'
...

Parameter key:string (required)

The key to be looked up in the flexAttributes of the voucher.

token[] voucher.getTokens()

Returns an array of tokens that belong to a voucher

Example

...
voucher.getTokens(); // => [{'token' : 'xmas10', 'used' : true, 'orderItemId' : null},{'token' : 'xmas25', 'used' : false, 'orderItemId' : null}]
...

voucher.has(key)

Returns a boolean value depending on whether key is present for a given voucher.

Example

...
voucher.has('products.blacklist') // => false
...

Parameter key:string (required)

The key to be checked in the flexAttributes of the voucher.

int voucher.id

Returns the ID field of a voucher as an integer

Example

...
voucher.id // => 3
...


token[] voucher.importTokens([tokensArray])

Imports an existing array of tokens into a voucher

Example

...
voucher.importTokens(['xmas10','xmas25']);
...

Parameter [tokensArray] (required)

Array of voucher tokens

voucher.increment(key)

Increments the vale of a given key by one.

Example

...
voucher.increment('timesUsed') 
...

Parameter key:string (required)

The key to be incremented in the flexAttributes of the voucher.

bool voucher.isValid

Returns true if a voucher is active and not expired, otherwise false.

Example

...
voucher.isValid // => true
...


voucher.remove(key)

Removes the key from the flexAttributes of the voucher

Example

...
voucher.remove('products.blacklist')
...

Parameter key:string (required)

The key to be removed from the flexAttributes of the voucher.

voucher.set(key, value)

Sets the value of a specified key for a given voucher

Example

...
voucher.set('promoteOnFrontPage', true, [options])
...

Parameter key:string (required)

The key to be set in the flexAttributes of the order.

Parameter value:string (required)

The value to be set for the key

string voucher.type

Returns the discount type of the voucher as a string.

Example

...
voucher.type // => 'relative'
...


voucher voucher.update({attributes})

Updates a voucher with the given attributes and returns it

Example

...
voucher.update({'value' : 10, 'minValue' : 395, 'validFrom' : date.getTime(), 'validUntil' : dateInOneWeek.getTime(), 'discountType' : 'relative'});
...

Parameter attributes (required)

The attributes object can take in the following keys:

int value

Value of the voucher. Must be a positive integer in cents for absolute vouchers or a percentage for relative vouchers.

int minValue (optional)

Minimal value a voucher can be redeemed for. Must be a positive integer in cents.

int validFrom

Time in milliseconds from when a voucher will be valid from.

int validUntil

Time in milliseconds up to when a voucher will be valid until.

string voucher.uuid

Returns the UUID of the voucher as a string

Example

...
voucher.uuid // '9d4b97e5-ee02-42aa-b71d-49e07859f1b3'
...


date voucher.validFrom

Returns the 'valid from' date of a voucher

Example

...
voucher.validFrom // => Mon Mar 26 2018 03:42:00 GMT
...


date voucher.validUntil

Returns the 'valid until' date of a voucher

Example

...
voucher.validUntil // => Sun Apr 01 2018 03:42:00 GMT
...


int voucher.value

Returns the relative (percentage) or absolute (in cents) value of a voucher

Example

...
voucher.value // 495
...


bool voucher.withShipping

Returns true if voucher affects shipping costs, otherwise false

Example

...
voucher.withShipping // => false
...