verifyCoupon( )
Verifies a ticket coupon.
Description
The verifyCoupon()
function returns a Promise that resolves to an
VerifyCouponResponse
when the ticket coupon
has been updated.
If the coupon has been verified, the VerifyCouponResponse
will contain
an invoice
property with an invoice that includes the coupon discounts.
If the coupon fails verification, the VerifyCouponResponse
will contain
a discountErrors
property including information about why the verification
failed.
You may want to call the verifyCoupon()
function in an input element's
onCustomValidation()
event
handler to validate a coupon code while the form is being filled out.
Note: To work with the Wix Events API, you need to publish your site.
Syntax
function verifyCoupon(eventId: string, reservationId: string, coupon: string): VerifyCouponResponse
verifyCoupon Parameters
NAME
TYPE
DESCRIPTION
ID of the event that the ticket coupon is for.
ID of the reservation that the ticket coupon is for.
Code of the coupon to verify.
Returns
Fulfilled - Information about coupon verification.
Return Type:
NAME
TYPE
DESCRIPTION
Invoice the coupon is used in.
Discount errors.
Was this helpful?
1import wixEventsFrontend from 'wix-events-frontend';23// ...45const eventId = // Get the event ID67// ...89let reservationId = // Returned from the reserve() function1011// ...1213let coupon = // get coupon1415wixEventsFrontend.tickets.verifyCoupon(eventId, reservationId, coupon)16 .then((result) => {17 if (result.discountErrors) {18 // handle verification failure19 let firstErrorCode = result.discountErrors.error[0].code;20 } else {21 // handle coupon verified22 }23 });2425/* result - error case:26 *27 * {28 * "discountErrors":{29 * "error":[30 * {31 * "code":"ERROR_COUPON_DOES_NOT_EXIST"32 * }33 * ]34 * }35 * }36*/3738/* result - verified case39 *40 * {41 * "invoice": {42 * "items": [43 * {44 * "id": "de8e3fd8-b257-40a7-933b-77c627d884e4",45 * "quantity": 2,46 * "name": "VIP ticket",47 * "price": {48 * "amount": "6.00",49 * "currency": "USD"50 * },51 * "total": {52 * "amount": "12.00",53 * "currency": "USD"54 * },55 * "discount":{56 * "amount":{57 * "amount":"2.0",58 * "currency":"USD"59 * },60 * "afterDiscount":{61 * "amount":"10.00",62 * "currency":"USD"63 * },64 * "code":"CouponCode",65 * "name":"Coupon Name",66 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",67 * "discounts":[68 * {69 * "amount":{70 * "amount":"2.0",71 * "currency":"USD"72 * },73 * "coupon":{74 * "name":"Coupon Name",75 * "code":"CouponCode",76 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"77 * }78 * }79 * ]80 * },81 * "fees": [82 * {83 * "name": "WIX_FEE",84 * "type": "FEE_ADDED",85 * "rate": "2.5",86 * "amount": {87 * "amount": "0",88 * "currency": "USD"89 * }90 * }91 * ]92 * },93 * {94 * "id": "9207637c-3d49-43af-b91e-32e496620c60",95 * "quantity": 1,96 * "name": "Regular ticket",97 * "price": {98 * "amount": "3.50",99 * "currency": "USD"100 * },101 * "total": {102 * "amount": "3.50",103 * "currency": "USD"104 * },105 * "discount":{106 * "amount":{107 * "amount":"1.0",108 * "currency":"USD"109 * },110 * "afterDiscount":{111 * "amount":"2.50",112 * "currency":"USD"113 * },114 * "code":"CouponCode",115 * "name":"Coupon Name",116 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",117 * "discounts":[118 * {119 * "amount":{120 * "amount":"1.0",121 * "currency":"USD"122 * },123 * "coupon":{124 * "name":"Coupon Name",125 * "code":"CouponCode",126 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"127 * }128 * }129 * ]130 * },131 * "fees": [132 * {133 * "name": "WIX_FEE",134 * "type": "FEE_ADDED",135 * "rate": "2.5",136 * "amount": {137 * "amount": "0",138 * "currency": "USD"139 * }140 * }141 * ]142 * }143 * ],144 * "total":{145 * "amount":"15.50",146 * "currency":"BIF"147 * },148 * "discount":{149 * "amount":{150 * "amount":"3.0",151 * "currency":"BIF"152 * },153 * "afterDiscount":{154 * "amount":"12.50",155 * "currency":"BIF"156 * },157 * "code":"CouponCode",158 * "name":"Coupon Name",159 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59",160 * "discounts":[161 * {162 * "amount":{163 * "amount":"3.0",164 * "currency":"BIF"165 * },166 * "coupon":{167 * "name":"Coupon Name",168 * "code":"CouponCode",169 * "couponId":"3b2e333c-5376-4b36-8543-00bae6dd8f59"170 * }171 * }172 * ]173 * },174 * "subTotal":{175 * "amount":"15.50",176 * "currency":"BIF"177 * },178 * "grandTotal":{179 * "amount":"12.50",180 * "currency":"BIF"181 * },182 * "fees":[],183 * "revenue":{184 * "amount":"12.50",185 * "currency":"BIF"186 * }187 * }188 * }189 */
onCustomValidation()
handler1import wixEventsFrontend from 'wix-events-frontend';23// ...45const eventId = // Get the event ID67// ...89let reservationId = // Returned from the reserve() function1011// ...1213let coupon = // get coupon1415$w("#couponCode").onCustomValidation((value, reject) => {16 wixEventsFrontend.tickets.verifyCoupon(eventId, reservationId, coupon)17 .then((result) => {18 if (result.discountErrors) {19 // handle verification failure20 $w("#couponCode").updateValidityIndication();21 $w("#couponErrorMsg").show();22 $w("#couponSuccessMsg").hide();23 reject("Coupon is invalid");24 } else {25 // handle coupon verified26 $w("#couponErrorMsg").hide()27 $w("#couponSuccessMsg").show()28 }29 });30});
1import wixEventsFrontend from 'wix-events-frontend';2import wixData from 'wix-data';3import wixPayFrontend from 'wix-pay-frontend';45let eventId;6let reservationId;7let orderNumber;8let tickets;910$w.onReady(function () {11 // Retrieve data and set up page12 await getData();13 $w("#ticketRepeater").onItemReady(displayTickets);14 $w("#ticketRepeater").data = tickets;1516 // Define button click actions17 $w("#reservationButton").onClick(reserveTickets);18 $w("#checkoutButton").onClick(checkoutTickets);19 $w("#updateButton").onClick(updateTickets);2021 // Verify coupon code when it is entered22 $w("#couponCode").onCustomValidation(verifyCouponCode);23});2425async function getData() {26 // Run a query that returns only one event. Add27 // additional filtering to the query if necessary.28 const eventResults = await wixData.query("Events/Events")29 .eq("title", "My Event")30 .find();3132 if (eventResults.items.length > 0) {33 eventId = eventResults.items[0]._id;3435 // Get tickets for the event36 const ticketResults = await wixData.query("Events/Tickets")37 .eq("event", eventId)38 .find();3940 if (ticketResults.items.length > 0) {41 tickets = ticketResults.items;42 } else {43 $w("#ticketRepeater").hide();44 console.log("Could not find tickets");45 }46 } else {47 console.log("Could not find event");48 }49}5051function displayTickets($item, itemData, index) {52 $item("#ticketName").text = itemData.name;53 $item("#ticketPrice").text = itemData.price.toString();54 $item("#ticketQuantity").value = 0;55}5657function reserveTickets() {58 const selectedTickets = getSelectedTickets();5960 wixEventsFrontend.tickets.reserve(eventId, selectedTickets)61 .then((reservation) => {62 reservationId = reservation.id;63 })64 .catch((error) => {65 console.log("Error", error.message)66 });67}6869function getSelectedTickets() {70 let selectedTickets = [];7172 $w("#ticketRepeater").forEachItem(($item, itemData, index) => {73 if ($item("#ticketQuantity").value > 0) {74 selectedTickets.push( {75 "ticketId": itemData._id,76 "quantity": $item("#ticketQuantity").value77 });78 }79 });8081 return selectedTickets;82}8384function checkoutTickets() {85 wixEventsFrontend.tickets.checkout(eventId, reservationId, {86 "formValues": getFormValues(),87 "coupon": $w("#couponCode").value88 })89 .then(({order}) => {90 orderNumber = order.orderNumber91 wixPayFrontend.startPayment(order.paymentId);92 // Note that PDF tickets are available before payment is complete93 $w("#ticketsPdfLink").value = order.ticketsPdf;94 })95 .catch((error) => {96 console.log("Error", error.message)97 });98}99100function updateTickets() {101 wixEventsFrontend.tickets.updateOrder(eventId, orderNumber, {102 "formValues": getFormValues()103 })104 .catch((error) => {105 console.log("Error", error.message)106 });107}108109function verifyCouponCode(value, reject) {110 const coupon = $w("#couponCode").value;111 wixEventsFrontend.tickets.verifyCoupon(eventId, reservationId, coupon)112 .then((result) => {113 if (result.discountErrors) {114 // handle verification failure115 $w("#couponCode").updateValidityIndication();116 $w("#couponErrorMsg").show();117 $w("#couponSuccessMsg").hide();118 reject("Coupon is invalid");119 } else {120 // handle coupon verified121 $w("#couponErrorMsg").hide()122 $w("#couponSuccessMsg").show()123 }124 });125}126127function getFormValues() {128 return [129 {"name": "firstName", "value": $w("#firstName").value},130 {"name": "lastName", "value": $w("#lastName").value},131 {"name": "email", "value": $w("#email").value},132 {"name": "custom", "value": $w("#foodAllergies").value},133134 // When a form contains an address, the way you format the135 // address information for submission depends on what type136 // of input elements you use to gather that information.137138 // Wix address input element.139 {"name": "address", "value": $w("#address").value},140141 // Single element which is not an address142 // input element, such as a text input.143 {"name": "address", "value": [$w("#address").value]},144145 // Multiple elements for the146 // various parts of an address.147 {148 "name": "address",149 "value": [150 $w("#street").value,151 $w("#city").value,152 $w("#state").value,153 $w("#country").value,154 $w("#postalCode").value155 ]156 },157158 // When a form contains an input for adding more guests to an159 // RSVP, format the guest names for submission in an array160 // where each element is the full name of a guest.161 {"name": "additionalGuests", "value": $w("#additionalGuests").value},162 {163 "name": "guestNames",164 "value": [165 `${$w("#guest1FirstName").value} ${$w("#guest1LastName").value}`,166 `${$w("#guest2FirstName").value} ${$w("#guest2LastName").value}`,167 ]168 }169 ];170}