commit a16c57558202aa03fc3fb7e76663b183c801c6de
parent d5f2bef8f5e376741cc04369749b70f50b0bd68a
Author: Christian Grothoff <christian@grothoff.org>
Date: Wed, 5 Nov 2025 20:27:07 +0100
add price/subscription hints on payment page
Diffstat:
4 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/src/Entity/TurnstilePriceCategory.php b/src/Entity/TurnstilePriceCategory.php
@@ -91,6 +91,53 @@ class TurnstilePriceCategory extends ConfigEntityBase {
}
/**
+ * Gets a brief hint to display about non-subscriber prices.
+ *
+ * @return string
+ * A hint about the price
+ */
+ public function getPriceHint() {
+ $prices = $this->getPrices();
+ $nosub = $prices['%none%'];
+ $rval = NULL;
+ foreach ($nosub as $currency => $price) {
+ if (NULL === $rval)
+ {
+ $rval = "$price $currency";
+ }
+ else
+ {
+ $rval = "$price $currency, " . $rval;
+ }
+ }
+ return $rval ?? $this->t(/* Need to buy a subscription */ 'Not sold individually');
+ }
+
+ /**
+ * Gets a brief hint to display about applicable subscriptions.
+ *
+ * @return string
+ * A hint about subscriptions
+ */
+ public function getSubscriptionHint() {
+ $prices = $this->getPrices();
+ $rval = NULL;
+ foreach ($prices as $subscription => $details) {
+ if ('%none%' === $subscription)
+ continue;
+ if (NULL === $rval)
+ {
+ $rval = $subscription;
+ }
+ else
+ {
+ $rval = $subscription . ", " . $rval;
+ }
+ }
+ return $rval ?? $this->t(/* No subscriptions */ 'None');
+ }
+
+ /**
* Gets all prices.
*
* @return array
diff --git a/src/Form/PriceCategoryForm.php b/src/Form/PriceCategoryForm.php
@@ -130,7 +130,7 @@ class PriceCategoryForm extends EntityForm {
parent::validateForm($form, $form_state);
$prices = $form_state->getValue('prices');
-
+ $ok = FALSE;
if (is_array($prices)) {
foreach ($prices as $subscription_id => $currencies) {
if (is_array($currencies)) {
@@ -147,10 +147,17 @@ class PriceCategoryForm extends EntityForm {
$this->t('Prices cannot be negative.')
);
}
+ $ok = TRUE;
}
}
}
}
+ if (! $ok) {
+ $form_state->setErrorByName(
+ "",
+ $this->t('At least one price must be set.')
+ );
+ }
}
@@ -164,11 +171,9 @@ class PriceCategoryForm extends EntityForm {
$prices = $form_state->getValue('prices');
$filtered_prices = [];
foreach ($prices as $subscription_id => $currencies) {
- $sub_active = FALSE;
foreach ($currencies as $currency_code => $price) {
if ($price !== '') {
$filtered_prices[$subscription_id][$currency_code] = $price;
- $sub_active = TRUE;
}
}
}
diff --git a/taler_turnstile.module b/taler_turnstile.module
@@ -164,6 +164,8 @@ function taler_turnstile_entity_view_alter(array &$build, EntityInterface $entit
'#session_id' => $order_info['session_id'],
'#payment_url' => $order_info['payment_url'],
'#node_title' => $node->getTitle(),
+ '#price_hint' => $price_category->getPriceHint(),
+ '#subscription_hint' => $price_category->getSubscriptionHint(),
'#attached' => [
'library' => ['taler_turnstile/payment_button'],
],
@@ -278,6 +280,8 @@ function taler_turnstile_theme() {
'session_id' => NULL,
'payment_url' => NULL,
'node_title' => NULL,
+ 'price_hint' => NULL,
+ 'subscription_hint' => NULL,
],
'template' => 'taler-turnstile-payment-button',
],
diff --git a/templates/taler-turnstile-payment-button.html.twig b/templates/taler-turnstile-payment-button.html.twig
@@ -24,6 +24,12 @@
{{ 'Open GNU Taler payment Web page'|t }}
</a>
</div>
+ <div class="taler-turnstile-payment-pricing">
+ <h4>{{ 'Price per article'|t}}</h4>
+ <p class="taler-turnstile-hint-price">{{ price_hint }}</p><br>
+ <h4>{{ 'Acceptable subscriptions'|t}}</h4>
+ <p class="taler-turnstile-hint-subscriptions">{{ subscription_hint }}</p>
+ </div>
<div class="taler-turnstile-payment-status">
<p class="taler-turnstile-status-message">{{ 'Waiting for payment...'|t }}</p>