iVendNextDevelopers My apps

Coupons for a first purchase, one customer group, or a sale of a given size — the recipe

A retailer's coupons already carry dates, stores, week days and a cap per store. Three rules retailers ask for are not there: a coupon only a new customer may use (a welcome coupon), a coupon only one group of customers may use (staff, members of a campaign) and a coupon that needs a sale of a given size for each use (one coupon of 100 for each 1,000 spent). This recipe adds them from outside, through the check before payment: the retailer writes one rule per coupon in Desk, and your service refuses a coupon the rule does not allow when the cashier presses Pay. It is for an iVendNext Partner, or the CitiXsys Consulting Team.

Next: the sample, S32 · Coupon rulesAll recipes

In the App library: Coupon rules

Reference implementation: samples/s32_coupon_rules/ — Coupon rules.

When to use it. Your rule depends on who the customer is or what they bought before, which the coupon itself cannot say. A rule the coupon already holds (dates, stores, week days, an excluded customer group, a cap per store) is configuration: set it on the coupon (the check recipe's coupon section).

On a demo site: a rule with Sale needed for each use set to 50.00

On a demo site: the coupon refused at Pay on a sale of USD 42.77

Steps

  1. Declare the rule record and the check in your manifest (samples/s32_coupon_rules/manifest.json):

    • a record type <PREFIX> Rule, one record per coupon, named by it ("autoname": "field:coupon", a Link to Coupon, unique): Enabled, First purchase only (a tick), Only for customers in this group (a Link to Customer Group) and Sale needed for each use (a Currency, never required: blank is no cap). Give System Manager the rights to edit it and your integration role read only; name the role <PREFIX> Integration, so the install makes it;
    • one check subscription at your service's address, and that address alone in allowed_urls;
    • a feed registration, one per app, with no user named: from POS version 2.1.0.667 the install makes your app's own user, <app>@integration.invalid, and names it, and the retailer's site hands that user's key to your hosted service after the install or an update (Connect again only if the site could not reach the app, or a key was lost or changed);
    • a help address on the rule record type (a documentation property setter).
  2. Choose what happens when your service is down. Coupon rules declares allow_and_flag: an outage never stops a retailer selling, and a coupon sale is flagged on Sales to review. A retailer that would rather lose the sale than give a restricted coupon unchecked changes its subscription to refuse; every sale then waits on your service, coupon or not.

  3. Check every call with the check's secret (<app>:sub:<key>; the sample's key is coupons), and refuse a timestamp more than five minutes from yours.

  4. Answer a sale with no coupon at once. coupon_codes is empty: allow, and read nothing from iVendNext. Most sales are answered this way.

  5. Read what the coupon carries. For each coupon on the sale, coupon_codes holds the Coupon record's name (its id), not the code the cashier typed. Look it up among the retailer's switched-on rules: GET /api/resource/<PREFIX> Rule with fields ["*"], filters [["enabled","=",1]] and limit_page_length 0, with your integration user's key, kept a minute. Ask for every field rather than naming them: one hosted service answers retailers on an earlier version of your app too, and a read naming a field their record type lacks fails for them. A coupon with no rule is never refused.

  6. Judge, cheapest first. Group rules read nothing more, so they go first; then the sale needed, which reads nothing either (below); then a first-purchase rule counts the customer's earlier sales, live, through the retailer's own count, GET /api/method/ivendnext_mpos.api.v1.get_count (doctype, filters): POS Invoices with docstatus 1 and is_return 0, and Sales Invoices with the same and is_consolidated 0 (a sale made in the back office; a consolidated invoice is the till's sales a second time), both at once, since each read has its own timeout. Any one means the customer has bought before. The count checks your user's rights and its User Permissions, so iVendNext gives your role a plain read on POS Invoice and Sales Invoice (not Only If Creator) and gives the user no User Permissions; otherwise the count sees only part of a customer's sales and a returning customer can pass as new.

    The coupon's rule The sale Answer
    only for customers in Staff the customer's group is not exactly Staff {"verdict": "refuse", "reason": "Coupon STAFF15 is only for customers in Staff. Remove it, or add a customer in that group."}
    first purchase only the customer has a submitted sale (before the cashier adds the new customer, the till names its walk-in customer, who usually has) {"verdict": "refuse", "reason": "Coupon WELCOME10 is for a first purchase only; this customer has bought before. Remove it, or add the new customer."}
    first purchase only no customer (the till always names one; a defensive answer) {"verdict": "refuse", "reason": "Coupon WELCOME10 is for a first purchase only. Add the customer to the sale first."}
    a sale needed of 1,000 on each of C100A and C100B a sale (stage sale) that is less than the two together before its discounts {"verdict": "refuse", "reason": "Coupons C100A, C100B need a sale of AED 2,000.00 before discounts; this sale is AED 1,450.00. Remove a coupon, or add to the sale."}
    any, and it holds; or no rule — {"verdict": "allow"}

    The sale needed, worked out. Add the amounts of every coupon on the sale whose rule names one, each in the sale's currency's minor units (currency_exponent, rounded up: it is a minimum), and compare the sum with the sale before its discounts: totals.grand_minor (as rounded for payment, tax included) plus each line's discount_minor, leaving out a line a promotion marked as its own (is_free_item 1 or promo_qty_milli above 0) and the sale's own totals.discount_minor, a negative line discount (a price above the list) counted as 0. That is the till's Grand Total plus its Discount. A coupon books into its lines' discount beside the promotions' and the cashier's, and the check is never sent a coupon's own amount, so all are added back: a coupon never counts against its own minimum. An exchange's replacement is its own sale, judged at its own total. One amount serves every store, read in each sale's own currency. More than that is refused; equal is allowed. One coupon of 100 for each 1,000 spent is a rule of 1,000 on each such coupon: two need a sale of 2,000, the same as allowing floor(total ÷ 1,000) coupons. The check sees each coupon once (coupon_codes is a set of names) and never a coupon's own amount, so the rule is per coupon, and a coupon repeated on one sale (the Coupon's hidden Can Repeat In Transaction) counts once. It is a minimum sale per coupon, never a cap on what a percentage coupon takes off; for one coupon for each so much spent, the retailer gives each coupon its own record and rule. No coupon reaches a return at the till today: a return's coupons are cleared, and a coupon cannot ride beside a returned item. As a backstop, refuse a capped coupon on stage return whatever the total (its total would be paid back). Keep the sentence under 140 characters with its amounts and instruction whole: name the coupons, or, when their names are long, say These 3 coupons need.

    The group and first-purchase rules are judged on whatever stage the check sends; a return carries no coupon at today's till (its coupons are cleared, and a coupon cannot ride beside a returned item), so in practice they judge sales and an exchange's replacement.

  7. When iVendNext cannot be read, say so the right way. No answer, an error answer other than 401 or 403, or not a list of records: answer 503, so the till applies your down answer. iVendNext refuses your key (401 or 403: a key withdrawn, a user disabled, a right not given): refuse a sale with a coupon in words that say so ("Coupon rules cannot read your site: it refused the app's user. Remove the coupon, or ask your administrator to check the app's setup."), so a refused key never gives a restricted coupon unchecked. Never follow a redirect with the key; treat an answer cut short as no answer.

  8. Test it. The sample's tests: each rule, the check's secret, the rules read and kept, the count asked only when needed and never kept, one retailer and many, every answer against the contract; canaries.py breaks each guarded rule and expects its test to fail. Then press Pay on a sandbox with a coupon on the sale.

Rules

The retailer's records of <PREFIX> Rule, one per coupon: Enabled (off: the coupon is checked by the product's own rules only), First purchase only (refused when the sale has no customer, or the customer already has a submitted sale that is not a return, at the till or in the back office), Only for customers in this group (blank: any customer; exactly this group, so a customer in a group under it is refused) and Sale needed for each use (blank or 0: no cap; the sale before its discounts, tax included, that each use of the coupon takes). A coupon used at most so many times in each store is not a rule here: set the Coupon's own Maximum Redemption Count Per Store, which iVendNext enforces when the coupon is applied (and does not apply offline). A change reaches the service within a minute.

What the cashier does. The till shows "This sale can't be paid" and the app's sentence, then goes back to the basket. The cashier removes the coupon (or adds the new customer, or the right one) and presses Pay again. Once the check has allowed a coupon, the basket is held to what was checked: another line or another coupon is refused before any money until the cashier presses Pay again, which asks your service again (the till's own lock: "The sale changed after the check. Press Pay again.").

What iVendNext needs from you

You give iVendNext checks
an HTTPS address its server trusts the address is consented; a private address is refused on a customer's site
a rule record type, by manifest the installer makes it and removes it at uninstall
a signature check on every call — (yours to do)
an answer within 3 s your declared down answer applies otherwise

Technical notes

What was proven where

Needs this serves

This page in the kit