From 3521256c87acf1021075ee76d2b14b424132e974 Mon Sep 17 00:00:00 2001 From: Tomasz Sikora Date: Thu, 1 Dec 2016 09:43:04 +0100 Subject: [PATCH] init work on README refactor --- README.md | 1452 ++++------------------------------------------------- 1 file changed, 103 insertions(+), 1349 deletions(-) diff --git a/README.md b/README.md index 058222b..f5e34e2 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,59 @@ -## Voucherify Node.js SDK - -[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) -[![Build Status](https://travis-ci.org/rspective/voucherify-nodejs-sdk.svg?branch=refactor-and-new-methods)](https://travis-ci.org/rspective/voucherify-nodejs-sdk) -[![NPM Version](https://img.shields.io/npm/v/voucherify.svg)](https://www.npmjs.com/package/voucherify) -[![NPM Downloads](https://img.shields.io/npm/dm/voucherify.svg)](https://www.npmjs.com/package/voucherify) -[![Dependencies](https://david-dm.org/rspective/voucherify-nodejs-sdk.svg)](https://www.npmjs.com/package/voucherify) - -[Voucherify](http://voucherify.io?utm_source=github&utm_medium=sdk&utm_campaign=acq) is an API-first platform for software developers who are dissatisfied with high-maintenance custom coupon software. Our product is a coupon infrastructure through API that provides a quicker way to build coupon generation, distribution and tracking. Unlike legacy coupon software we have: - -* an API-first SaaS platform that enables customisation of every aspect of coupon campaigns -* a management console that helps cut down maintenance and reporting overhead -* an infrastructure to scale up coupon activity in no time - -Here you can find a library that makes it easier to integrate Voucherify with your Node.js server. - -Full documentation is located at [voucherify.readme.io](https://voucherify.readme.io). - - -### Usage - -#### Authentication +

+ +

+ +

Official Voucherify Node.js SDK

+ +

+ JavaScript Style Guide + Build Status + NPM Version + NPM Downloads + Dependencies +

+
+ + +

+Setup +| +Callback or Promise? +| +API +| +Error handling +| +Contributing +| +Changelog +| +License +

+
+ +## Setup + +`npm install voucherify --save` [Log-in](http://app.voucherify.io/#/login) to Voucherify web interface and obtain your Application Keys from [Configuration](https://app.voucherify.io/#/app/configuration): -![](https://www.filepicker.io/api/file/WKYkl2bSAWKHccEN9tEG) - ```javascript -const voucherifyClient = require("voucherify") +const voucherifyClient = require('voucherify') -const voucherify = voucherifyClient({ - applicationId: "YOUR-APPLICATION-ID-OBTAINED-FROM-CONFIGURATION", - clientSecretKey: "YOUR-CLIENT-SECRET-KEY-OBTAINED-FROM-CONFIGURATION" +const client = voucherifyClient({ + applicationId: 'YOUR-APPLICATION-ID', + clientSecretKey: 'YOUR-CLIENT-SECRET-KEY' }) ``` - -#### Callback or Promise? +## Callback or Promise? All methods in the SDK provide both callback based as well as promise based interactions. -If you want to use callbacks just pass them as a last parameter. For example: +If you want to use callback just pass it as a last parameter. For example: ```javascript -voucherify.get("v1GiJYuuS", function(error, result) { +client.vouchers.get('v1GiJYuuS', (error, result) => { if (error) { // handle error return @@ -54,1360 +66,102 @@ voucherify.get("v1GiJYuuS", function(error, result) { If you prefer to use promises then the code goes like this: ```javascript -voucherify.get("v1GiJYuuS") - .then(function (result) { +client.vouchers.get('v1GiJYuuS') + .then((result) => { console.log(result) }) - .catch(function (error) { + .catch((error) => { console.error("Error: %s", error) }) ``` All other examples in the readme use promises but they could be as well written with callbacks. +## API -#### Listing vouchers - -`voucherify.list(filter, callback*)` - -Filter parameters: - -- code_query -- limit (default 10) -- skip (default 0) -- category -- campaign -- customer - -Example: - -```javascript -voucherify.list({limit: 10, skip: 20, category: "API Test"}) - .then(function(vouchers) { - console.log(vouchers) - }) - .catch(function(error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -[{ - "code": "9mYBpIk", - "campaign": null, - "category": "API Test", - "discount": { - "type": "AMOUNT", - "amount_off": 400 - }, - "start_date": "2016-03-01T12:00:00Z", - "expiration_date": null, - "publish": { - "object": "list", - "count": 0, - "data_ref": "entries", - "entries": [] - }, - "redemption": { - "object": "list", - "quantity": 1, - "data_ref": "redemption_entries", - "redeemed_quantity": 0, - "redemption_entries": [] - }, - "active": true, - "additional_info": null, - "metadata": null -}, { - "code": "AzTsIH", - "campaign": null, - "category": "API Test", - "discount": { - "type": "AMOUNT", - "amount_off": 400 - }, - "start_date": "2016-03-01T10:00:00Z", - "expiration_date": null, - "publish": { - "object": "list", - "count": 0, - "data_ref": "entries", - "entries": [] - }, - "redemption": { - "object": "list", - "quantity": 1, - "data_ref": "redemption_entries", - "redeemed_quantity": 0, - "redeemed_amount": 0, - "redemption_entries": [] - }, - "active": true, - "additional_info": null, - "metadata": null -}] -``` - - -#### Getting voucher details - -`voucherify.get(voucher_code, callback*)` - -Example: - -```javascript -voucherify.get("v1GiJYuuS") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "code": "v1GiJYuuS", - "campaign": "vip", - "discount": { - "percent_off": 10.0, - "type": "PERCENT" - }, - "expiration_date": "2016-12-31T23:59:59Z", - "publish": { - "object": "list", - "count": 0, - "data_ref": "entries", - "entries": [] - }, - "redemption": { - "object": "list", - "quantity": 1, - "data_ref": "redemption_entries", - "redeemed_quantity": 0, - "redemption_entries": [{ - "id": "r_gQzOnTwmhn2nTLwW4sZslNKY", - "object": "redemption", - "date": "2016-04-24T06:03:35Z", - "customer_id": null, - "tracking_id": "GENERATED-OR-PROVIDED-TRACKING-ID" - }] - }, - "additional_info": "" -} -``` - - -#### Creating a voucher - -`voucherify.create(voucher, callback*)` - -You can create a voucher with a specified code or let Voucherify generate one. -You can define how to generate the code in `code_config` including following properties: -- `length` - Number of characters in a generated code (excluding prefix and postfix) -- `charset` - Characters that can appear in the code. -- `prefix` - A text appended before the code. -- `postfix` - A text appended after the code. -- `pattern` - A pattern for codes where hashes (#) will be replaced with random characters. Overrides `length`. - -Example: - -```javascript - voucherify.create({ - code_config: { - length: 5, - charset: "01234567890", - prefix: "PROMO-", - postfix: "-2016" - }, - discount: { - type: "AMOUNT", - amount_off: 1000 // 10.00 - }, - category: "Test", - start_date: "2016-01-01T00:00:00Z", - expiration_date: "2016-12-31T23:59:59Z" - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -#### Updating a voucher - -`voucherify.update(voucher, callback*)` - -You can modify following fields: -- category -- start_date -- expiration_date -- active -- additional_info -- metadata - -Other fields than listed above won't be modified. Even if provided they will be silently skipped. - -Example: - -```javascript - voucherify.update({ - code: "v1GiJYuuS", - category: "Updated", - start_date: "2016-08-01T00:00:00Z", - expiration_date: "2017-07-31T23:59:59Z" - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -#### Add voucher to existing Campaign - -This method is responsible for adding new voucher to campaign based on voucher definition inherited from campaign. You can give custom values for following fields: -- category -- additional_info -- metadata -- redemption.quantity - - -Example: - -```javascript -const payload = { - "additional_info": "New voucher", - "metadata": { - "test": true - }, - "redemption": { - "quantity": 5 - } -} - -voucherify.campaign.voucher.create("Campaign-Name", payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "code": "pK0ZSfaB", - "campaign": "Campaign-Name", - "category": null, - "type": "DISCOUNT_VOUCHER", - "discount": { "type": "AMOUNT", "amount_off": 1000 }, - "gift": null, - "start_date": "2016-09-14T22:00:00Z", - "expiration_date": "2016-09-30T21:59:59Z", - "publish": { - "object": "list", - "count": 0, - "data_ref": "entries", - "entries": [] - }, - "redemption": { - "object": "list", - "quantity": 0, - "data_ref": "redemption_entries", - "redeemed_quantity": 0, - "redemption_entries": [] - }, - "active": true, - "additional_info": "New voucher", - "metadata": { - "test": true - } -} -``` - -#### Deleting a voucher - -`voucherify.delete(voucher_code, params*, callback*)` - -In param object you can pass `force` flag which tells whether voucher will be removed permanently. It means that afterwards user will be able to create next voucher with the same code. - -Example: - -```javascript - voucherify.delete("v1GiJYuuS", { force: true }) - .then(function (result) { - console.log("Voucher deleted.") - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -`Result is an empty body` - -#### Disabling a voucher - -`voucherify.disable(voucher_code, callback*)` - -Example: - -```javascript - voucherify.disable("v1GiJYuuS") - .then(function (result) { - console.log("Voucher disabled.") - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -`Result is an empty body` - - -#### Enabling a voucher - -`voucherify.enable(voucher_code, callback*)` - -Example: - -```javascript - voucherify.enable("v1GiJYuuS") - .then(function (result) { - console.log("Voucher enabled.") - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -`Result is an empty body` - - -#### Getting voucher redemption - -`voucherify.redemption(voucher_code, callback*)` - -Example: - -```javascript -voucherify.redemption("v1GiJYuuS") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "quantity": 3, - "redeemed_quantity": 1, - "redemption_entries": [ - { - "id": "r_gQzOnTwmhn2nTLwW4sZslNKY", - "object": "redemption", - "date": "2016-04-24T06:03:35Z", - "customer_id": null, - "tracking_id": "GENERATED-OR-PROVIDED-TRACKING-ID" - } - ] -} -``` - - -#### Publishing voucher - -`voucherify.publish(campaign_name, callback*)` - -`voucherify.publish(params, callback*)` - -This method selects a voucher that is suitable for publication, adds a publish entry and returns the voucher. -A voucher is suitable for publication when it's active and hasn't been published more times than the redemption limit. - -Example: - -By campaign name: - -```javascript -voucherify.publish("First Ride") - .then(function (result) { - console.log(JSON.stringify(result, null, " ")) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -By object with campaign name: - -```javascript -voucherify.publish({campaign: "First Ride", channel: "Email", customer: "donny.roll@mail.com"}) - .then(function (result) { - console.log(JSON.stringify(result, null, " ")) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -By object with voucher code: - -```javascript -voucherify.publish({voucher: "FR-zT-u9I7zG", channel: "Email", customer: "donny.roll@mail.com"}) - .then(function (result) { - console.log(JSON.stringify(result, null, " ")) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` +- [Vouchers](#vouchers-api) +- [Campaigns](#campaigns-api) +- [Validations](#validations-api) +- [Redemptions](#redemptions-api) +- [Customers](#customers-api) +- [Products](#products-api) +- [Deprecated methods](#deprecated-api) -Result: +This SDK is fully consistent with restufl API Voucherify provides. +Detalied description and example responsesx you will find at [official docs](https://docs.voucherify.io/reference). -```json -{ - "code": "FR-zT-u9I7zG", - "campaign": "First Ride", - "category": null, - "discount": { - "type": "PERCENT", - "amount_off": 50 - }, - "start_date": "2015-01-01T00:00:00Z", - "expiration_date": "2016-12-31T23:59:59Z", - "publish": { - "object": "list", - "count": 1, - "data_ref": "entries", - "entries": [{ - "channel": "Email", - "customer": "donny.roll@mail.com", - "customer_id": "cust_1fnSUBno3iimKTPNDCkjg4xV", - "published_at": "2016-01-22T09:25:07Z" - }] - }, - "redemption": { - "object": "list", - "quantity": 0, - "data_ref": "redemption_entries", - "redeemed_quantity": 0, - "redemption_entries": [] - }, - "active": true, - "additional_info": null -} -``` +### Vouchers API +Methods are provided withing `client.vouchers.*` namespace. -Error: +#### Create Voucher +`client.vouchers.create(voucher)` -```json -{ - "code": 400, - "message": "Couldn't find any voucher suitable for publication." -} -``` +See [possible voucher fields](https://docs.voucherify.io/docs/vouchers#the-voucher-object) and [example results](https://docs.voucherify.io/reference#create-voucher). -#### Validating vouchers - -Validation lets you check if given voucher code can be successfully redeemed. - -`voucherify.validate(code, context)` - -The `context` param is generally optional unless you are validating a gift voucher or a voucher with order validation rules. -For gift voucher you have to pass `order.amount` expressed in cents (e.g. $10 is 1000). -Validation rules also require to pass `order.items` as a list of objects including `product_id`, `sku_id` and `quantity`. - -Example: - -``` -validation_result = voucherify.validate("91Ft4U", { - tracking_id: "john@lemon.com", - customer: { - id: "cust_07sVjVsr71Ewot9lVZSrIVLH", - source_id: "john@lemon.com", - name: "John Lemon" - }, - order: { - amount: 1250 - items: [ - { product_id: "prod_ELvEXqF4qzB7Rs", sku_id: "sku_GoXSOI4FwJZafb", quantity: 1 }, - { product_id: "prod_wye1naw5JO5dh3", sku_id: "sku_U3rHSlfOCGUnbo", quantity: 2 } - ] - } -}) -``` - -Successful validation result: -``` -{ - code: "91Ft4U", - valid: true, - gift: { - amount: 10000 - }, - tracking_id: "john@lemon.com" -} -``` +#### Get Voucher +`client.vouchers.get(code)` -Failed validation result: -``` -{ - code: "91Ft4U", - valid: false, - reason: "gift amount exceeded", - tracking_id: "john@lemon.com" -} -``` +#### Update Voucher +`client.vouchers.update(voucher)` -There are several reasons why validation may fail (`valid: false` response). You can find the actual cause in the `reason` field: +#### Delete Voucher +`client.vouchers.delete(code, [params])` -- `voucher not found` -- `voucher is disabled` -- `voucher not active yet` -- `voucher expired` -- `quantity exceeded` -- `gift amount exceeded` -- `customer does not match segment rules` -- `order does not match validation rules` +See available optional [delete params](https://docs.voucherify.io/reference#delete-voucher). -#### Redeeming vouchers +#### List Vouchers +`client.vouchers.list(query)` -`voucherify.redeem(voucher_code, tracking_id|customer_profile*, callback*)` +See available [query params](https://docs.voucherify.io/reference#list-vouchers). -##### 1. Just by code +#### Publish Voucher +- by campaign name +`client.vouchers.publish(campaignName)` +- by param +`client.vouchers.publish(bodyParams)` +See available [params](https://docs.voucherify.io/reference#publish-voucher). -Example: +#### Enable Voucher +- `client.vouchers.enable(code)` -```javascript -voucherify.redeem("v1GiJYuuS") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` +#### Disable Voucher +- `client.vouchers.disable(code)` -Result (voucher details after redemption): +#### Import Vouchers +- `client.vouchers.import(vouchersArray)` -```json -{ - "id": "r_yRmanaA6EgSE9uDYvMQ5Evfp", - "object": "redemption", - "date": "2016-04-25T10:34:57Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)", - "voucher": { - "code": "v1GiJYuuS", - "campaign": "vip", - "discount": { - "percent_off": 10.0, - "type": "PERCENT" - }, - "expiration_date": "2016-12-31T23:59:59Z", - "redemption": { - "quantity": 3, - "redeemed_quantity": 2, - "redemption_entries": [ - { - "id": "r_gQzOnTwmhn2nTLwW4sZslNKY", - "object": "redemption", - "date": "2016-04-24T06:03:35Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - }, - { - "id": "r_yRmanaA6EgSE9uDYvMQ5Evfp", - "object": "redemption", - "date": "2016-04-25T10:34:57Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - } - ] - }, - "active": true, - "additional_info": "" - } -} -``` +### Deprecated methods -Error: +We strongly encourage you to update your code with new methods. +Each deprecated method has corresponding new namespaced one with the same params, +so migration will go smooth. -```json -{ - "code": 400, - "message": "voucher expired or quantity exceeded", - "details": "v1GiJYuuS" -} -``` +- `client.list(filter)` - see [client.vouchers.list](#list-vouchers) +- `client.get(voucher_code)`- see [client.vouchers.get](#get-voucher) +- `client.create(voucher)` - see [client.vouchers.create](#create-voucher) +- `client.update(voucher)` - see [client.vouchers.update](#update-voucher) +- `client.delete(voucher_code, [params])` - see [client.vouchers.delete](#delete-voucher) +- `client.campaign.voucher.create(campaignName, payload)` - see [client.campaigns.addVoucher](#add-voucher-to-campaign) -##### 2. With tracking id +- `voucherify.disable(voucher_code)` +- `voucherify.enable(voucher_code)` -You can provide a tracking id (e.g. your customer's login or a generated id) to the voucher redemption request. -```javascript -voucherify.redeem("v1GiJYuuS", "alice.morgan") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` +- `voucherify.redemption(voucher_code)` -Result: +- `voucherify.publish(campaign_name)` -```json -{ - "id": "r_yRmanaA6EgSE9uDYvMQ5Evfp", - "object": "redemption", - "date": "2016-04-25T10:34:57Z", - "customer_id": "cust_C9qJ3xKgZFqkpMw7b21MF2ow", - "tracking_id": "alice.morgan", - "voucher": { - "code": "v1GiJYuuS", - "campaign": "vip", - "discount": { - "percent_off": 10.0, - "type": "PERCENT" - }, - "expiration_date": "2016-12-31T23:59:59Z", - "redemption": { - "quantity": 3, - "redeemed_quantity": 3, - "redemption_entries": [ - { - "id": "r_gQzOnTwmhn2nTLwW4sZslNKY", - "object": "redemption", - "date": "2016-04-24T06:03:35Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - }, - { - "id": "r_yRmanaA6EgSE9uDYvMQ5Evfp", - "object": "redemption", - "date": "2016-04-25T10:34:57Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - }, - { - "id": "r_irOQWUTAjthQwnkn5JQM1V6N", - "object": "redemption", - "date": "2016-04-25T12:04:08Z", - "customer_id": "cust_C9qJ3xKgZFqkpMw7b21MF2ow", - "tracking_id": "alice.morgan" - } - ] - }, - "active": true, - "additional_info": "" - } -} -``` +- `voucherify.publish(params)` -##### 3. With customer profile +- `voucherify.validate(code, context)` -You can record a detailed customer profile consisting of a `source_id`, `name`, `email`, `description` and a `metadata` section that can include any data you wish. -Voucherify will create (or update) provided customer profile in its database. +- `voucherify.redeem(voucher_code, tracking_id|customer_profile*)` -```javascript -voucherify.redeem({ - voucher: "v1GiJYuuS", - customer: { - soruce_id: "alice.morgan", - name: "Alice Morgan", - email: "alice@morgan.com", - description: "", - metadata: { - locale: "en-GB", - shoeSize: 5, - favourite_brands: ["Armani", "L’Autre Chose", "Vicini"] - } - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - - -##### 4. With customer id - -If you already created a customer profile in Voucherify's database, whether it was implicitly by providing it to the `redeem` function or explicitly by invoking the [`customer.create`](#create-customer) method, you can identify your customer in following redemptions by a generated id (starting with `cust_`). - -```javascript -voucherify.redeem({ - voucher: "v1GiJYuuS", - customer: { - id: "cust_C9qJ3xKgZFqkpMw7b21MF2ow" - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -##### 5. With order amount - -Redeeming a gift voucher requires to pass an amount that you wish to withdraw from the voucher. -The same applies to vouchers with validation rules on order's total amount. -Order amount have to be expressed in cents, as an integer. For example $22.50 should be provided as 2250: - -```javascript -voucherify.redeem({ - voucher: "91Ft4U", - order: { - amount: 2250 - }) -``` - -##### 6. With order items - -Vouchers with validation rules regarding products or SKUs require to pass `order.items`. -Items are a list of objects consisting of `product_id`, `sku_id` and `quantity`. - -```javascript -voucherify.redeem({ - voucher: "91Ft4U", - order: { - items: [ - { product_id: "prod_ELvEXqF4qzB7Rs", sku_id: "sku_GoXSOI4FwJZafb", quantity: 1 }, - { product_id: "prod_wye1naw5JO5dh3", sku_id: "sku_U3rHSlfOCGUnbo", quantity: 2 } - ] - }) -``` - -### Listing redemptions - -Use `voucherify.redemptions(filter, callback*)` to get a filtered list of redemptions. -Filter parameters: - -- limit (default: 100) -- page (default: 0) -- start_date (default: beginning of current month) -- end_date (default: end of current month) -- result - Success | Failure-NotExist | Failure-Inactive -- customer - -Example - 1000 successful redemptions from April 2016: - -```javascript -const filter = { - limit: 1000, - page: 0, - start_date: "2016-04-01T00:00:00", - end_date: "2016-04-30T23:59:59", - result: "Success" -} - -voucherify.redemptions(filter) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - - -#### Rollback a redemption - -Use `voucherify.rollback(redemption_id, options*, callback*)` to revert a redemption. -It will create a rollback entry in `redemption.redemption_entries` and give 1 redemption back to the pool -(decrease `redeemed_quantity` by 1). -Parameter `options` passed as object supports following attributes: -- `reason` - reason why rollback is perform -- `tracking_id` - attribute for tracking customer -- `customer` - customer id or customer object - -Possible errors are: -- 404 - Resource not found - if voucher with given `redemption_id` doesn't exist -- 400 - Already rolled back - if redemption with given `redemption_id` has been rolled back already -- 400 - Invalid redemption id - when trying to rollback a rollback. - -Example: - -Option is a `reason`: -```javascript -voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", "Wrong user") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Object which contains following options of `reason`, _customer id_ as `customer`: -```javascript -voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", { - "reason": "Wrong user ID", - "customer": "cust_1V6NirOQWUwnkn5JQMTAjthQ", - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Object which contains customer data: -* Object customer will upsert customer data if exists or will create new if not. -```javascript -voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", { - "customer": { - "name": "Alice Morgan", - "source_id": "alice.morgan", - "email": "alice.morgan@mail.com" - }, - }) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - - -```javascript -voucherify.rollback("r_irOQWUTAjthQwnkn5JQM1V6N", "wrong user") - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -``` -{ - "id": "rr_1634wLkb8glgRXrTmsxRzDBd", - "object": "redemption_rollback", - "date": "2016-04-25T10:35:02Z", - "tracking_id": "alice.morgan", - "customer_id": "cust_1V6NirOQWUwnkn5JQMTAjthQ", - "redemption": "r_irOQWUTAjthQwnkn5JQM1V6N", - "reason": "wrong user" - "voucher": { - "code": "v1GiJYuuS", - "campaign": "vip", - "discount": { - "percent_off": 10.0, - "type": "PERCENT" - }, - "expiration_date": "2016-12-31T23:59:59Z", - "redemption": { - "quantity": 3, - "redeemed_quantity": 2, - "redemption_entries": [ - { - "id": "r_gQzOnTwmhn2nTLwW4sZslNKY", - "object": "redemption", - "date": "2016-04-24T06:03:35Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - }, - { - "id": "r_yRmanaA6EgSE9uDYvMQ5Evfp", - "object": "redemption", - "date": "2016-04-25T10:34:57Z", - "customer_id": null, - "tracking_id": "(tracking_id not set)" - }, - { - "id": "r_irOQWUTAjthQwnkn5JQM1V6N", - "object": "redemption", - "customer_id": "cust_C9qJ3xKgZFqkpMw7b21MF2ow" - "date": "2016-04-25T12:04:08Z", - "tracking_id": "alice.morgan" - }, - { - "id": "rr_1634wLkb8glgRXrTmsxRzDBd", - "object": "redemption_rollback", - "date": "2016-04-25T10:35:02Z", - "customer_id": null, - "tracking_id": "alice.morgan", - "redemption": "r_irOQWUTAjthQwnkn5JQM1V6N" - } - ] - }, - "active": true, - "additional_info": "" - } -} -``` - - -#### Create Customer - -Example: - -```javascript -const payload = { - "source_id": "your-tracking-id", - "name": "John Doe", - "email": "email@example.com", - "description": "Premium user, ACME Inc.", - "metadata": { - "lang":"en" - } -} - -voucherify.customer.create(payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "cust_dJRcBf3P9HvmHK5TOrhRUHzL", - "object": "customer", - "source_id": "your-tracking-id", - "name": "John Doe", - "email": "email@example.com", - "description": "Premium user, ACME Inc.", - "metadata": { - "lang": "en" - }, - "created_at": "2016-06-06T17:14:55Z" -} -``` - - -#### Get Customer - -Example: - -```javascript -const customerId = "cust_c2SlN2rKajDdMycd3BmVawJk" - -voucherify.customer.get(customerId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "cust_c2SlN2rKajDdMycd3BmVawJk", - "object": "customer", - "source_id": "your-tracking-id", - "name": "John Doe", - "email": "email@example.com", - "description": "Premium user, ACME Inc.", - "metadata": { - "lang": "en" - }, - "created_at": "2016-06-07T07:35:59Z" -} -``` - - -#### Update Customer - -Example: - -```javascript -const payload = { - "name": "John Doe", - "email": "email@example.com", - "description":"Premium user, ACME Inc.", - "metadata": { - "lang":"en", - "type":"premium" - } -} - -voucherify.customer.update(payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "cust_c2SlN2rKajDdMycd3BmVawJk", - "object": "customer", - "source_id": "your-tracking-id", - "name": "John Doe", - "email": "email@example.com", - "description": "Premium user, ACME Inc.", - "metadata": { - "lang": "en", - "type": "premium" - }, - "created_at": "2016-06-06T17:14:55Z" -} -``` - - -#### Delete Customer - -Example: - -```javascript -const customerId = "cust_c2SlN2rKajDdMycd3BmVawJk" - -voucherify.customer.delete(customerId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -`Result is an empty body` - - -#### Create Product - -Example: - -```javascript -const payload = { - "name": "Apple iPhone 6", - "attributes": [ - "color", - "memory" - ], - "metadata": { - "type": "normal" - } -} - -voucherify.product.create(payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "prod_YWnt2mNigm76oA", - "object": "product", - "name": "Apple iPhone 6", - "attributes": [ - "color", - "memory" - ], - "metadata": { - "type": "normal" - }, - "created_at": "2016-07-29T13:15:24Z", - "skus": { - "object": "list", - "total": 0, - "data": [] - } -} -``` - - -#### Get Product - -Example: - -```javascript -const productId = "prod_YWnt2mNigm76oA" - -voucherify.product.get(productId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "prod_YWnt2mNigm76oA", - "object": "product", - "name": "Apple iPhone 6", - "attributes": [ - "color", - "memory" - ], - "metadata": { - "type": "normal" - }, - "created_at": "2016-07-29T13:15:24Z", - "skus": { - "object": "list", - "total": 0, - "data": [] - } -} -``` - - -#### Update Product - -Example: - -```javascript -const payload = { - "id": "prod_YWnt2mNigm76oA", - "object": "product", - "name": "Apple iPhone 6", - "attributes": [ - "color", - "memory" - ], - "metadata": { - "type": "ultra premium" - }, - "created_at": "2016-07-29T13:15:24Z", - "skus": { - "object": "list", - "total": 0, - "data": [] - } -} - -voucherify.product.update(payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "prod_YWnt2mNigm76oA", - "object": "product", - "name": "Apple iPhone 6", - "attributes": [ - "color", - "memory" - ], - "metadata": { - "type": "ultra premium" - }, - "created_at": "2016-07-29T13:15:24Z", - "skus": { - "object": "list", - "total": 0, - "data": [] - } -} -``` - - -#### Delete Product - -Example: - -```javascript -const productId = "prod_YWnt2mNigm76oA" - -voucherify.product.delete(productId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -`Result is an empty body` - - - -#### Create Sku - -Example: - -```javascript -const productId = "prod_e8uLMegXZJ4GGS" - -const sku = { - sku: "APPLE_IPHONE_6_BLACK", - currency: "EUR", - price: 12000, - attributes: { - color: "BLACK", - memory: "16GB" - } -} - -voucherify.product.sku.create(productId, payload) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "sku_JXYGfsGbpvsfjv", - "product_id": "prod_e8uLMegXZJ4GGS", - "sku": "APPLE_IPHONE_6_BLACK", - "created_at": "2016-08-02T16:43:47Z", - "object": "sku" -} -``` - - -#### Get Sku - -Example: - -```javascript -const productId = "prod_YWnt2mNigm76oA" - -const skuId = "sku_JXYGfsGbpvsfjv" - -voucherify.product.sku.get(productId, skuId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "sku_JXYGfsGbpvsfjv", - "product_id": "prod_e8uLMegXZJ4GGS", - "sku": "APPLE_IPHONE_6_BLACK", - "created_at": "2016-08-02T16:43:47Z", - "object": "sku" -} -``` - - -#### Update Sku - -Example: - -```javascript -const sku = { - "id": "sku_JXYGfsGbpvsfjv", - "product_id": "prod_e8uLMegXZJ4GGS", - "sku": "APPLE_IPHONE_6_BLACK", - "created_at": "2016-08-02T16:43:47Z", - "object": "sku" -} - -sku.currency = "EUR" -sku.price = 1000 - -const productId = sku.product_id - -voucherify.product.sku.update(productId, sku) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` - -Result: - -```json -{ - "id": "sku_JXYGfsGbpvsfjv", - "product_id": "prod_e8uLMegXZJ4GGS", - "sku": "APPLE_IPHONE_6_BLACK", - "currency": "EUR", - "price": 1000, - "created_at": "2016-08-02T16:43:47Z", - "object": "sku" -} -``` - - -#### Delete Sku - -Example: - -```javascript - -const productId = "prod_e8uLMegXZJ4GGS" - -const skuId = "sku_JXYGfsGbpvsfjv" - -voucherify.product.sku.delete(productId, skuId) - .then(function (result) { - console.log(result) - }) - .catch(function (error) { - console.error("Error: %s", error) - }) -``` -Result: +- `voucherify.redemptions(filter)` -`Result is an empty body` +- `voucherify.rollback(redemption_id, options*)`