Skip to content

Commit

Permalink
Merge pull request #79 from rspective/aw/support-order-metadata-in-va…
Browse files Browse the repository at this point in the history
…lidation

Support order metadata in validation
  • Loading branch information
tpindel authored Sep 28, 2020
2 parents f07ba85 + 105a710 commit fab4c9f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ where params is an object including:
- `amount` *(required for gift vouchers, integer, value in cents)* - order's amount that is going to be paid by voucher (entirely or partially)
- `items` *(required for order validation rules)* - order items, an array of objects with following properties `product_id`, `sku_id` and `quantity`
- `customer` *(optional)* - an object including `id` and/or `source_id` of a customer, if provided then it has higher precedence than `tracking_id`. The object can also define customer's `metadata` used for validation.
- `metadata` *(required for metadata validation rules)* - on object containing values of any type (boolean, number, string)
- `orderMetadata` *(required for metadata validation rules)* - order metadata, an object containing values of any type (boolean, number, string)
- `metadata` *(required for metadata validation rules)* - redemption metadata, an object containing values of any type (boolean, number, string)

or (only voucher validation)

Expand Down Expand Up @@ -735,6 +736,7 @@ Description and legal fields do support markdown syntax. It means that you can u
### Changelog
- **2020-09-16** - `1.32.0` - Add possibility to send order metadata with validation request
- **2019-12-23** - `1.31.0` - Add possibility to send customer metadata with validation request
- **2019-02-05** - `1.30.0` - Add method for setting base app url
- **2018-11-05** - `1.29.0` - Web widgets - new attribute for hiding a note describing Voucherify privacy policy
Expand Down
10 changes: 9 additions & 1 deletion dist/voucherify.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ window.Voucherify = (function (window, document, $) {
var amount;
var items;
var metadata;
var orderMetadata;
var customer;

if (typeof(code) === "object") {
amount = code.amount;
items = code.items;
metadata = code.metadata;
orderMetadata = code.orderMetadata;
customer = code.customer;
code = code.code;
}
Expand Down Expand Up @@ -251,6 +253,12 @@ window.Voucherify = (function (window, document, $) {
}).join("&");
}

if (orderMetadata) {
queryString += "&" + Object.keys(orderMetadata).map(function (key) {
return encodeURIComponent("order[metadata][" + key + "]") + "=" + encodeURIComponent(orderMetadata[key]);
}).join("&");
}

if (customer) {
if (typeof(customer) !== "object") {
console.error("Customer must be an object - please use instead { source_id: 'your_user' }");
Expand All @@ -268,7 +276,7 @@ window.Voucherify = (function (window, document, $) {
return encodeURIComponent("customer[metadata][" + metadataKey + "]") + "=" + encodeURIComponent(customer.metadata[metadataKey]);
}).join("&");
}

return encodeURIComponent("customer[" + key + "]") + "=" + encodeURIComponent(customer[key]);
}).join("&");
}
Expand Down
2 changes: 1 addition & 1 deletion dist/voucherify.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/voucherify.min.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions example/validate-voucher.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="../lib/voucherify.css">
</head>
<body>
<div id="voucher-validate"></div>

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.2/jquery.js"></script>
<script type="text/javascript" src="../lib/voucherify.js"></script>
<script>
Voucherify.initialize(
"011240bf-d5fc-4ef1-9e82-11eb68c43bf5",
"9e2230c5-71fb-460a-91c6-fbee64707a20"
);
Voucherify.setIdentity("[email protected]");
Voucherify.validate({
code: "CODE1",
items: [{ product_id: "prod_f1r5Tpr0DuC7" }],
orderMetadata: {
source: "website"
}
})
.done(console.log)
.fail(console.log);
</script>
</body>
</html>
10 changes: 9 additions & 1 deletion lib/voucherify.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ window.Voucherify = (function (window, document, $) {
var amount;
var items;
var metadata;
var orderMetadata;
var customer;

if (typeof(code) === "object") {
amount = code.amount;
items = code.items;
metadata = code.metadata;
orderMetadata = code.orderMetadata;
customer = code.customer;
code = code.code;
}
Expand Down Expand Up @@ -251,6 +253,12 @@ window.Voucherify = (function (window, document, $) {
}).join("&");
}

if (orderMetadata) {
queryString += "&" + Object.keys(orderMetadata).map(function (key) {
return encodeURIComponent("order[metadata][" + key + "]") + "=" + encodeURIComponent(orderMetadata[key]);
}).join("&");
}

if (customer) {
if (typeof(customer) !== "object") {
console.error("Customer must be an object - please use instead { source_id: 'your_user' }");
Expand All @@ -268,7 +276,7 @@ window.Voucherify = (function (window, document, $) {
return encodeURIComponent("customer[metadata][" + metadataKey + "]") + "=" + encodeURIComponent(customer.metadata[metadataKey]);
}).join("&");
}

return encodeURIComponent("customer[" + key + "]") + "=" + encodeURIComponent(customer[key]);
}).join("&");
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voucherify.js",
"version": "1.31.0",
"version": "1.32.0",
"homepage": "http://www.voucherify.io",
"description": "Client-side SDK for Voucherify.",
"author": "rspective",
Expand Down

0 comments on commit fab4c9f

Please sign in to comment.