taler-merchant-demos

Python-based Frontends for the Demonstration Web site
Log | Files | Refs | README | LICENSE

commit c4c541429b919eaea3fa0965558c9c255cb680e7
parent 125d237a721f712836d002669064269e61201662
Author: Florian Dold <dold@taler.net>
Date:   Wed, 25 Nov 2026 16:42:13 +0100

landing: combine coffee and exhibition checkouts and fix receipt URLs

Issue: https://bugs.taler.net/n/12331

Diffstat:
MREADME.md | 18++++++++++++------
Minternal/backend/types.go | 1+
Minternal/web/app.go | 24++++++++++++++++--------
Minternal/web/merchant_samples.go | 75+++++++++++++++++++++++++++++++++++++++++++++------------------------------
Minternal/web/merchant_samples_test.go | 99+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Minternal/web/templates/merchant-samples.gohtml | 12+++++-------
6 files changed, 135 insertions(+), 94 deletions(-)

diff --git a/README.md b/README.md @@ -150,9 +150,15 @@ contract uses one payout account for all choices. The staging exchange requires a Swiss IBAN, so the demo-currency exchange must support transfers to that IBAN as well. See the [merchant operator manual](https://docs.taler.net/taler-merchant-manual.html#exchange). -Each button creates a new order. Compare plain v0 and v1 payments, redeem -a coffee coupon once, or open multiple exhibits with one day pass. The café -basket includes three products with quantities and line prices. The coffee +Each button creates a new order. The receipt and reorder URLs use the public +`landing_url` from `[frontend-demo]`, including its path prefix. + +Compare plain v0 and v1 payments, or buy a coffee at full price to earn a +coupon. Spend one coupon for half price or two for a free coffee. The exhibition +offers single entry or a day pass. A valid pass gives automatic free entry on +repeat visits until midnight UTC. + +The café basket includes three products with quantities and line prices. The coffee and croissant PNGs come from the GNU Taler iOS POS screenshot assets (©2022–25 Taler Systems S.A., GPL-3.0-or-later). For the two-currency tests, fund the wallet in the demo currency and in staging CHF. @@ -191,7 +197,7 @@ $ taler-merchant-demos -c /etc/taler-merchant-demos/taler-merchant-demos.conf bl $ taler-merchant-demos -c /etc/taler-merchant-demos/taler-merchant-demos.conf donations ``` -When deployed behind a reverse proxy, `X-Forwarded-Host`, -`X-Forwarded-Proto`, and `X-Forwarded-Prefix` are used for public fulfillment -URLs and generated links. These headers should be removed from untrusted +Behind a reverse proxy, the demos use `X-Forwarded-Host`, +`X-Forwarded-Proto`, and `X-Forwarded-Prefix` for request-based links. +Merchant sample receipt and reorder links use the configured `landing_url`. These headers should be removed from untrusted client requests and set by the trusted proxy. diff --git a/internal/backend/types.go b/internal/backend/types.go @@ -23,6 +23,7 @@ type OrderExtra struct { // OrderInput describes an input required by an order choice. type OrderInput struct { + Count int `json:"count,omitempty"` Type string `json:"type"` TokenFamilySlug string `json:"token_family_slug,omitempty"` } diff --git a/internal/web/app.go b/internal/web/app.go @@ -54,14 +54,15 @@ type Options struct { // App serves one configured demo. type App struct { - samplesReady atomic.Bool - opts Options - catalogs catalogs - articles articleLibrary - templates map[string]*template.Template - static http.Handler - mux *http.ServeMux - routes *http.ServeMux + samplesReady atomic.Bool + samplesLandingURL *url.URL + opts Options + catalogs catalogs + articles articleLibrary + templates map[string]*template.Template + static http.Handler + mux *http.ServeMux + routes *http.ServeMux } type language struct { @@ -169,6 +170,13 @@ func New(opts Options) (*App, error) { opts: opts, catalogs: cats, templates: make(map[string]*template.Template), mux: http.NewServeMux(), routes: http.NewServeMux(), } + if opts.SamplesBackend != nil { + base, err := url.Parse(opts.PublicURLs.Landing) + if err != nil || base.Host == "" || (base.Scheme != "http" && base.Scheme != "https") { + return nil, errors.New("merchant samples require an absolute HTTP or HTTPS landing_url") + } + a.samplesLandingURL = base + } if opts.Shop == "blog" { a.articles, err = loadArticles() if err != nil { diff --git a/internal/web/merchant_samples.go b/internal/web/merchant_samples.go @@ -53,6 +53,15 @@ var coffeePNG []byte //go:embed assets/static/croissant.png var croissantPNG []byte +func (a *App) coffeeProduct(lang, quantity, price string) backend.Product { + return backend.Product{ + Name: a.catalogs.translate(lang, "Coffee"), + Description: a.catalogs.translate(lang, "Freshly brewed coffee"), + UnitQuantity: quantity, Prices: []string{price}, + Image: "data:image/png;base64," + base64.StdEncoding.EncodeToString(coffeePNG), + } +} + func (a *App) sampleOrder(lang, sample string) (backend.Order, bool) { currency := a.opts.Currency order := backend.Order{Version: 1} @@ -76,12 +85,7 @@ func (a *App) sampleOrder(lang, sample string) (backend.Order, bool) { order.Summary = a.catalogs.translate(lang, "Café basket") order.Choices = []backend.OrderChoice{{Amount: currency + ":7"}} order.Products = []backend.Product{ - { - Name: a.catalogs.translate(lang, "Coffee"), - Description: a.catalogs.translate(lang, "Freshly brewed coffee"), - UnitQuantity: "2", Prices: []string{currency + ":4"}, - Image: "data:image/png;base64," + base64.StdEncoding.EncodeToString(coffeePNG), - }, + a.coffeeProduct(lang, "2", currency+":4"), { Name: a.catalogs.translate(lang, "Croissant"), Description: a.catalogs.translate(lang, "Butter croissant"), @@ -94,36 +98,40 @@ func (a *App) sampleOrder(lang, sample string) (backend.Order, bool) { UnitQuantity: "1", Prices: []string{currency + ":1"}, }, } - case "earn", "earn-multi": + case "coffee": + order.Summary = a.catalogs.translate(lang, "Coffee") + order.Products = []backend.Product{a.coffeeProduct(lang, "1", currency+":2")} + order.Choices = []backend.OrderChoice{ + {Amount: currency + ":2", Description: a.catalogs.translate(lang, "Without coupon"), Outputs: couponOut}, + {Amount: currency + ":1", Description: a.catalogs.translate(lang, "With one coupon"), Inputs: couponIn}, + { + Amount: currency + ":0", Description: a.catalogs.translate(lang, "With two coupons"), + Inputs: []backend.OrderInput{{Type: "token", TokenFamilySlug: couponSlug, Count: 2}}, + }, + } + case "earn-multi": order.Summary = a.catalogs.translate(lang, "Coffee with a coupon") - order.Choices = []backend.OrderChoice{{Amount: currency + ":2", Outputs: couponOut}} - if sample == "earn-multi" { - order.Choices = append(order.Choices, backend.OrderChoice{Amount: "CHF:0.20", Outputs: couponOut}) + order.Choices = []backend.OrderChoice{ + {Amount: currency + ":2", Outputs: couponOut}, + {Amount: "CHF:0.20", Outputs: couponOut}, } - case "redeem", "redeem-multi": + case "redeem-multi": order.Summary = a.catalogs.translate(lang, "Coffee") discounted := a.catalogs.translate(lang, "Use a coupon") order.Choices = []backend.OrderChoice{ {Amount: currency + ":2", Description: fullPrice}, {Amount: currency + ":1", Description: discounted, Inputs: couponIn}, + {Amount: "CHF:0.20", Description: fullPrice}, + {Amount: "CHF:0.10", Description: discounted, Inputs: couponIn}, } - if sample == "redeem-multi" { - order.Choices = append(order.Choices, - backend.OrderChoice{Amount: "CHF:0.20", Description: fullPrice}, - backend.OrderChoice{Amount: "CHF:0.10", Description: discounted, Inputs: couponIn}, - ) - } - case "pass": - order.Summary = a.catalogs.translate(lang, "Exhibit day pass") - order.Choices = []backend.OrderChoice{{Amount: currency + ":3", Outputs: passOut}} - case "admission": - order.Summary = a.catalogs.translate(lang, "Exhibit admission") + case "exhibition": + order.Summary = a.catalogs.translate(lang, "Visit the exhibition") order.Choices = []backend.OrderChoice{ - {Amount: currency + ":1", Description: fullPrice}, + {Amount: currency + ":1", Description: a.catalogs.translate(lang, "Single entry")}, + {Amount: currency + ":3", Description: a.catalogs.translate(lang, "Day pass"), Outputs: passOut}, { - Amount: currency + ":0", Description: a.catalogs.translate(lang, "Use a day pass"), - Inputs: []backend.OrderInput{{Type: "token", TokenFamilySlug: passSlug}}, - Outputs: passOut, + Amount: currency + ":0", Description: a.catalogs.translate(lang, "Entry with pass"), + Inputs: []backend.OrderInput{{Type: "token", TokenFamilySlug: passSlug}}, Outputs: passOut, }, } default: @@ -135,6 +143,13 @@ func (a *App) sampleOrder(lang, sample string) (backend.Order, bool) { func (a *App) createSampleOrder(w http.ResponseWriter, r *http.Request) { lang := requestLang(r) sample := r.PostFormValue("sample") + // Accept forms opened before the coffee and exhibition buttons were combined. + switch sample { + case "earn", "redeem": + sample = "coffee" + case "pass", "admission": + sample = "exhibition" + } order, ok := a.sampleOrder(lang, sample) if !ok { a.renderError(w, r, http.StatusBadRequest, lang, "Unknown payment sample", nil) @@ -145,13 +160,13 @@ func (a *App) createSampleOrder(w http.ResponseWriter, r *http.Request) { a.renderError(w, r, http.StatusInternalServerError, lang, "Internal server error", err) return } - base := externalOrigin(r) + forwardedPrefix(r) + "/" + lang + "/merchant-samples" + base := a.samplesLandingURL.JoinPath(lang, "merchant-samples") order.OrderID = id - order.FulfillmentURL = base + "/receipts/" + id - order.PublicReorderURL = base + order.FulfillmentURL = base.JoinPath("receipts", id).String() + order.PublicReorderURL = base.String() now := time.Now() payDeadline := now.Add(5 * time.Minute).Unix() - if sample == "pass" || sample == "admission" { + if sample == "exhibition" { // A checkout issued today must finish within today's pass period. nextMidnight := now.UTC().Truncate(24 * time.Hour).Add(24 * time.Hour) payDeadline = min(payDeadline, nextMidnight.Unix()) diff --git a/internal/web/merchant_samples_test.go b/internal/web/merchant_samples_test.go @@ -19,6 +19,8 @@ import ( func sampleRequest(app *App, method, path, body string) *httptest.ResponseRecorder { req := httptest.NewRequest(method, path, strings.NewReader(body)) + req.Host = "test_landing_backends" + req.Header.Set("X-Forwarded-Host", "proxy.internal") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("X-Forwarded-Prefix", "/demo") req.Header.Set("Accept-Language", "de") @@ -29,7 +31,7 @@ func sampleRequest(app *App, method, path, body string) *httptest.ResponseRecord func sampleApp(t *testing.T, handler http.HandlerFunc) *App { t.Helper() - opts := Options{Shop: "landing", Currency: "TESTKUDOS"} + opts := Options{Shop: "landing", Currency: "TESTKUDOS", PublicURLs: PublicURLs{Landing: "https://public.example/samples-demo/"}} if handler != nil { server := httptest.NewServer(handler) t.Cleanup(server.Close) @@ -154,9 +156,14 @@ func TestSamplesStartup(t *testing.T) { t.Fatal(got) } page = sampleRequest(app, "GET", "/de/merchant-samples", "") - if page.Code != 200 || strings.Count(page.Body.String(), `name="sample"`) != 10 || !strings.Contains(page.Body.String(), `action="/demo/de/merchant-samples/orders"`) { + if page.Code != 200 || strings.Count(page.Body.String(), `name="sample"`) != 8 || !strings.Contains(page.Body.String(), `action="/demo/de/merchant-samples/orders"`) { t.Fatal("missing sample actions") } + for _, action := range []string{"coffee", "exhibition", "earn-multi", "redeem-multi"} { + if strings.Count(page.Body.String(), `name="sample" value="`+action+`"`) != 1 { + t.Errorf("expected one button for %s", action) + } + } } func TestSampleCheckouts(t *testing.T) { @@ -179,22 +186,36 @@ func TestSampleCheckouts(t *testing.T) { } }) app.samplesReady.Store(true) - type choice struct{ amount, input, output string } + coupon := []backend.OrderInput{{Type: "token", TokenFamilySlug: couponSlug}} + issueCoupon := []backend.OrderOutput{{Type: "token", TokenFamilySlug: couponSlug}} + pass := []backend.OrderOutput{{Type: "token", TokenFamilySlug: passSlug}} const one = "TESTKUDOS:1" const two = "TESTKUDOS:2" + coffee := []backend.OrderChoice{ + {Amount: two, Description: "Without coupon", Outputs: issueCoupon}, + {Amount: one, Description: "With one coupon", Inputs: coupon}, + {Amount: "TESTKUDOS:0", Description: "With two coupons", Inputs: []backend.OrderInput{{Type: "token", TokenFamilySlug: couponSlug, Count: 2}}}, + } + exhibition := []backend.OrderChoice{ + {Amount: one, Description: "Single entry"}, + {Amount: "TESTKUDOS:3", Description: "Day pass", Outputs: pass}, + {Amount: "TESTKUDOS:0", Description: "Entry with pass", Inputs: []backend.OrderInput{{Type: "token", TokenFamilySlug: passSlug}}, Outputs: pass}, + } ids := map[string]bool{} for _, tc := range []struct { name string - choices []choice + choices []backend.OrderChoice }{ - {"basket", []choice{{"TESTKUDOS:7", "", ""}}}, - {"pay-v0", nil}, {"pay-v1", []choice{{one, "", ""}}}, {"earn", []choice{{two, "", couponSlug}}}, - {"redeem", []choice{{two, "", ""}, {one, couponSlug, ""}}}, - {"pass", []choice{{"TESTKUDOS:3", "", passSlug}}}, {"admission", []choice{{one, "", ""}, {"TESTKUDOS:0", passSlug, passSlug}}}, - {"postcard", []choice{{one, "", ""}, {"CHF:0.10", "", ""}}}, - {"earn-multi", []choice{{two, "", couponSlug}, {"CHF:0.20", "", couponSlug}}}, - {"redeem-multi", []choice{{two, "", ""}, {one, couponSlug, ""}, {"CHF:0.20", "", ""}, {"CHF:0.10", couponSlug, ""}}}, - {"admission", []choice{{one, "", ""}, {"TESTKUDOS:0", passSlug, passSlug}}}, + {"basket", []backend.OrderChoice{{Amount: "TESTKUDOS:7"}}}, + {"pay-v0", nil}, {"pay-v1", []backend.OrderChoice{{Amount: one}}}, + {"coffee", coffee}, {"earn", coffee}, {"redeem", coffee}, + {"exhibition", exhibition}, {"pass", exhibition}, {"admission", exhibition}, + {"postcard", []backend.OrderChoice{{Amount: one}, {Amount: "CHF:0.10"}}}, + {"earn-multi", []backend.OrderChoice{{Amount: two, Outputs: issueCoupon}, {Amount: "CHF:0.20", Outputs: issueCoupon}}}, + {"redeem-multi", []backend.OrderChoice{ + {Amount: two, Description: "Full price"}, {Amount: one, Description: "Use a coupon", Inputs: coupon}, + {Amount: "CHF:0.20", Description: "Full price"}, {Amount: "CHF:0.10", Description: "Use a coupon", Inputs: coupon}, + }}, } { t.Run(tc.name, func(t *testing.T) { rec := sampleRequest(app, "POST", "/en/merchant-samples/orders", "sample="+tc.name) @@ -203,10 +224,22 @@ func TestSampleCheckouts(t *testing.T) { } request := <-orders order := request.Order - if tc.name == "basket" { - checkBasketProducts(t, order.Products) - } else if len(order.Products) != 0 { - t.Fatalf("unexpected products for %s", tc.name) + switch tc.name { + case "coffee", "earn", "redeem": + checkProducts(t, order.Products, []productSpec{{"Coffee", "1", two, "coffee.png"}}) + if order.Products[0].Description != "Freshly brewed coffee" { + t.Error("wrong coffee description") + } + case "basket": + checkProducts(t, order.Products, []productSpec{ + {"Coffee", "2", "TESTKUDOS:4", "coffee.png"}, + {"Croissant", "1", two, "croissant.png"}, + {"Tea", "1", one, ""}, + }) + default: + if len(order.Products) != 0 { + t.Fatalf("unexpected products for %s", tc.name) + } } id := order.OrderID if !sampleOrderID.MatchString(id) || ids[id] { @@ -218,28 +251,11 @@ func TestSampleCheckouts(t *testing.T) { t.Fatal(location) } receipt := "/en/merchant-samples/receipts/" + id - if order.FulfillmentURL != "http://example.com/demo"+receipt || order.Summary == "" { + if order.FulfillmentURL != "https://public.example/samples-demo"+receipt || order.PublicReorderURL != "https://public.example/samples-demo/en/merchant-samples" || order.Summary == "" { t.Fatal(order) } - var got []choice - for _, c := range order.Choices { - row := choice{amount: c.Amount} - for _, i := range c.Inputs { - if i.Type != "token" { - t.Fatal(i) - } - row.input += i.TokenFamilySlug - } - for _, o := range c.Outputs { - if o.Type != "token" { - t.Fatal(o) - } - row.output += o.TokenFamilySlug - } - got = append(got, row) - } - if !reflect.DeepEqual(got, tc.choices) { - t.Fatalf("choices %v, want %v", got, tc.choices) + if !reflect.DeepEqual(order.Choices, tc.choices) { + t.Fatalf("choices %+v, want %+v", order.Choices, tc.choices) } status := backend.OrderStatusResponse{OrderStatus: "paid", ContractTerms: backend.ContractTerms{Version: order.Version, Amount: order.Amount, Choices: order.Choices}} amount := one @@ -253,7 +269,7 @@ func TestSampleCheckouts(t *testing.T) { } last := len(tc.choices) - 1 status.ChoiceIndex = &last - amount = tc.choices[last].amount + amount = tc.choices[last].Amount } statuses <- status page := sampleRequest(app, "GET", receipt, "") @@ -267,13 +283,10 @@ func TestSampleCheckouts(t *testing.T) { } } -func checkBasketProducts(t *testing.T, products []backend.Product) { +type productSpec struct{ name, quantity, price, imageFile string } + +func checkProducts(t *testing.T, products []backend.Product, want []productSpec) { t.Helper() - want := []struct{ name, quantity, price, imageFile string }{ - {"Coffee", "2", "TESTKUDOS:4", "coffee.png"}, - {"Croissant", "1", "TESTKUDOS:2", "croissant.png"}, - {"Tea", "1", "TESTKUDOS:1", ""}, - } if len(products) != len(want) { t.Fatalf("got %d products, want %d", len(products), len(want)) } diff --git a/internal/web/templates/merchant-samples.gohtml b/internal/web/templates/merchant-samples.gohtml @@ -16,19 +16,17 @@ <div class="action-row"><button class="button button-secondary" name="sample" value="basket">{{tr .Lang "Buy café basket"}}</button></div> </article> <article class="step-card"> - <h2>{{tr .Lang "One-use discount"}}</h2> - <p>{{trf .Lang "Buy a coffee for 2 {currency} to earn a coupon. Spend it once to get another coffee for 1 {currency}." "currency" .Content}}</p> + <h2>{{tr .Lang "Single-use discount"}}</h2> + <p>{{trf .Lang "Buy a coffee for 2 {currency} to earn a coupon. Use one coupon to pay 1 {currency}, or two coupons for a free coffee." "currency" .Content}}</p> <div class="action-row"> - <button class="button button-secondary" name="sample" value="earn">{{tr .Lang "Earn a coupon"}}</button> - <button class="button button-secondary" name="sample" value="redeem">{{tr .Lang "Buy another coffee"}}</button> + <button class="button button-secondary" name="sample" value="coffee">{{tr .Lang "Buy coffee"}}</button> </div> </article> <article class="step-card"> - <h2>{{tr .Lang "Exhibit day pass"}}</h2> + <h2>{{tr .Lang "Day Pass for an exhibition"}}</h2> <p>{{trf .Lang "Pay 1 {currency} per visit or buy a pass for 3 {currency}. Use the pass for repeat visits until midnight UTC." "currency" .Content}}</p> <div class="action-row"> - <button class="button button-secondary" name="sample" value="pass">{{tr .Lang "Buy a day pass"}}</button> - <button class="button button-secondary" name="sample" value="admission">{{tr .Lang "Open an exhibit"}}</button> + <button class="button button-secondary" name="sample" value="exhibition">{{tr .Lang "Visit the exhibition"}}</button> </div> </article> <article class="step-card">