Add support for financial trxn tokens
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 2 Dec 2023 06:31:00 +0000 (19:31 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 2 Dec 2023 23:47:23 +0000 (12:47 +1300)
CRM/Financial/FinancialTrxnTokens.php [new file with mode: 0644]
Civi/Core/Container.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

diff --git a/CRM/Financial/FinancialTrxnTokens.php b/CRM/Financial/FinancialTrxnTokens.php
new file mode 100644 (file)
index 0000000..236be71
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Event_Tokens
+ *
+ * Generate "event.*" tokens.
+ */
+class CRM_Financial_FinancialTrxnTokens extends CRM_Core_EntityTokens {
+
+  /**
+   * Get the entity name for api v4 calls.
+   *
+   * @return string
+   */
+  protected function getApiEntityName(): string {
+    return 'FinancialTrxn';
+  }
+
+}
index bdc23796dc8fe4f15c7815b7f46d58826f160924..8ea153495cf0322d04e053a47a2be0775d17434e 100644 (file)
@@ -364,6 +364,11 @@ class Container {
         []
       ))->addTag('kernel.event_subscriber')->setPublic(TRUE);
     }
+    $container->setDefinition("crm_financial_trxn_tokens", new Definition(
+      'CRM_Financial_FinancialTrxnTokens',
+      []
+    ))->addTag('kernel.event_subscriber')->setPublic(TRUE);
+
     $container->setDefinition('civi_token_impliedcontext', new Definition(
       'Civi\Token\ImpliedContextSubscriber',
       []
index 06dca14555c73f15b8f58f3454ee929647fd1bf7..e9d2b0bcafc2069b4bb89907b7e640242471ce95 100644 (file)
@@ -9,6 +9,7 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\Api4\FinancialTrxn;
 use Civi\Token\TokenProcessor;
 use Civi\Api4\LocBlock;
 use Civi\Api4\Email;
@@ -304,6 +305,9 @@ Czech Republic<br />', $html);
 The End
 Czech Republic
 ', $text);
+    $financialTrxn = FinancialTrxn::get()->addWhere('is_payment', '=', TRUE)->execute()->first();
+    $text = $this->renderText(['financial_trxnId' => $financialTrxn['id']], '{financial_trxn.total_amount}', [], FALSE);
+    $this->assertEquals('$100.00', $text);
   }
 
   /**