Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redeem promotions #49

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 89 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Contents:

* [1](https://github.com/rspective/voucherify.js#initialize-settings) - Installation and client-side authentication
* [2](https://github.com/rspective/voucherify.js#validation) - How to validate [vouchers](https://docs.voucherify.io/reference/#vouchers-validate) and [promotions](https://docs.voucherify.io/reference/#validate-promotions-1)
* [3](https://github.com/rspective/voucherify.js#redeem-vouchers) - How to call [redemption](https://docs.voucherify.io/reference/#redeem-voucher-client-side)
* [3](https://github.com/rspective/voucherify.js#redemption) - How to call [redemption](https://docs.voucherify.io/reference/#redeem-voucher-client-side)
* [4](https://github.com/rspective/voucherify.js#publish-vouchers) - How to call [publish](https://docs.voucherify.io/reference/#publish-voucher) coupons
* [5](https://github.com/rspective/voucherify.js#list-vouchers) - How to call [list](https://docs.voucherify.io/reference/#list-vouchers) coupons
* [6](https://github.com/rspective/voucherify.js#validation-widget) - Configuring validation widget
Expand Down Expand Up @@ -307,16 +307,16 @@ Voucherify.validate("VOUCHER-CODE")

There are several reasons why validation may fail (`valid: false` response). You'll find the actual cause in the `reason` field. For more details, visit [error reference](https://docs.voucherify.io/reference#errors) section.

### Redeem vouchers
### Redemption

Next to validation, the library allows you to [redeem](https://docs.voucherify.io/reference/#redeem-voucher-client-side) vouchers.
Next to validation, the library allows you to redeem [vouchers](https://docs.voucherify.io/reference/#redeem-voucher-client-side) and [promotions](https://docs.voucherify.io/reference/#redeem-promotion).
Note: you have to enable **client-side redemptions** in your project's configuration.

Reference: [redemption object](http://docs.voucherify.io/reference#the-redemption-object), [client-side redeem](https://docs.voucherify.io/reference/#redeem-voucher-client-side)

How to use it:


#### Vouchers
`Voucherify.redeem("VOUCHER-CODE", payload, function callback (response) { })`

where `payload` is an object which can include:
Expand All @@ -325,12 +325,12 @@ where `payload` is an object which can include:
- `source_id` - if not set, `tracking_id` will be used (if `tracking_id` is set)
- `order` - with at least
- `amount`
- `metadata`

Example:

`Voucherify.redeem("gfct5ZWI1nL", { order: { amount: 5000 } }, function callback (response) { })`


Success response

```javascript
Expand Down Expand Up @@ -381,6 +381,89 @@ Success response
}
```

#### Promotion

`Voucherify.redeem(null, payload, function callback (response) { })`

where `payload` is an object which can include:

- `tier` - promotion tier ID
- `customer` - voucher customer object
- `source_id` - if not set, `tracking_id` will be used (if `tracking_id` is set)
- `order` - with at least
- `amount`
- `metadata`

Example:

`Voucherify.redeem(null, { tier: 'promo_yourpromotiontierid', order: { amount: 5000 } }, function callback (response) { })`

Success response

```javascript
{
"object": "redemption",
"customer_id": "cust_vAZ0M5nQUDv3zDoAcT6QSYhb",
"tracking_id": "(tracking_id not set)"
"result": "SUCCESS",
"amount": 30,
"order": {
"amount": 30,
"discount_amount": 30,
"items": null,
"customer": {
"id": "cust_vAZ0M5nQUDv3zDoAcT6QSYhb",
"object": "customer"
},
"referrer": null,
"status": "CREATED",
"metadata": null
},
"promotion_tier":{
"id":"promo_TvPlKsF2tNXh3GhKPh0JKtDa",
"object":"promotion_tier",
"name":"tier_2",
"banner":null,
"campaign":{
"id":"camp_mFxeXvEj7VeugrmFGdUNmKEp",
"object":"campaign",
"start_date":"2018-04-18T00:00:00Z",
"expiration_date":"2023-07-02T00:00:00Z",
"active":true
},
"condition":{
"id":"val_TerxvCfNklYF",
"created_at":"2018-04-25T07:06:29Z",
"junction":"AND",
"orders":{
"junction":"AND",
"any_order_item_price":{
"$more_than":[
1000000
]
}
}
},
"action":{
"discount":{
"type":"AMOUNT",
"amount_off":100000
}
},
"metadata":null,
"summary":{
"redemptions":{
"total_redeemed":1
},
"orders":{
"total_amount":0,
"total_discount_amount":0
}
}
}
}
```

If you are using *jQuery* in version higher than *1.5*, you can use its implementation of promises (remember to load `voucherify.js` script after loading *jQuery*):

```javascript
Expand Down Expand Up @@ -671,6 +754,7 @@ The widget is fully configurable. You can decide which fields are visible and re

### Changelog

- **2018-04-25** - `1.19.0` - Add client side method for promotion redemption
- **2018-04-24** - `1.18.0` - Add client side method for promotion validation
- **2017-12-12** - `1.17.0` - Add redeem iframe widget
- **2017-10-23** - `1.16.1` - Fix tracking custom events
Expand Down
23 changes: 18 additions & 5 deletions dist/voucherify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ window.Voucherify = (function (window, document, $) {
publish: API_BASE + "/client/v1/publish",
list: API_BASE + "/client/v1/vouchers",
track: API_BASE + "/client/v1/events",
validatePromotion: API_BASE + "/client/v1/promotions/validation"
validatePromotion: API_BASE + "/client/v1/promotions/validation",
redeemPromotion: API_BASE + "/client/v1/promotions/tiers/"
};

var OPTIONS = {};
Expand Down Expand Up @@ -285,23 +286,35 @@ window.Voucherify = (function (window, document, $) {
},

redeem: function (code, payload, callback) {
var isPromotion = false;
var tier;

if (!isValidInit(OPTIONS)) {
return null;
}

if (!code) {
console.error("Voucherify client could not verify code, because it is missing - please provide Voucher Code.");
return null;
if( !payload.tier ){
console.error("Voucherify client could not redeem promotion without tier, because it is missing.");
return null;
}
isPromotion = true;
tier = payload.tier;
delete payload.tier
}

var queryString = "?code=" + encodeURIComponent(code.replace(/[\s\r\n]/g, ""));
var queryString = ""
if(!isPromotion){
queryString += "?code=" + encodeURIComponent(code.replace(/[\s\r\n]/g, ""));
}

// -- Tracking ID fallback
payload = payload || {};
payload.customer = payload.customer || {};
payload.customer.source_id = payload.customer.source_id || OPTIONS.trackingId;

return xhrImplementation("POST", API.redeem + queryString, payload, callback);

return xhrImplementation("POST", (isPromotion ? API.redeemPromotion + tier + "/redemption" : API.redeem) + queryString, payload, callback);
},

publish: function (campaign, payload, callback) {
Expand Down
Loading