merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler_merchant_util.h (52184B)


      1 /*
      2   This file is part of GNU Taler
      3   Copyright (C) 2024, 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file src/include/taler/taler_merchant_util.h
     18  * @brief Interface for common utility functions
     19  * @author Christian Grothoff
     20  */
     21 #ifndef TALER_MERCHANT_UTIL_H
     22 #define TALER_MERCHANT_UTIL_H
     23 
     24 #include <gnunet/gnunet_common.h>
     25 #include <gnunet/gnunet_util_lib.h>
     26 #include <gnunet/gnunet_json_lib.h>
     27 
     28 #include <stdint.h>
     29 #include <taler/taler_util.h>
     30 #include <jansson.h>
     31 
     32 /**
     33  * Fixed-point base for inventory quantities (powers of 10).
     34  * Six decimal digits are supported to match the maximum unit precision.
     35  */
     36 #define TALER_MERCHANT_UNIT_FRAC_BASE 1000000U
     37 #define TALER_MERCHANT_UNIT_FRAC_MAX_DIGITS 6U
     38 
     39 /**
     40  * Return default project data used by Taler merchant.
     41  */
     42 const struct GNUNET_OS_ProjectData *
     43 TALER_MERCHANT_project_data (void);
     44 
     45 
     46 /**
     47  * Kind of fixed-decimal value the helpers operate on.
     48  * Primarily distinguishes how special sentinel values (such as "-1"
     49  * meaning infinity for stock) must be encoded.
     50  */
     51 enum TALER_MERCHANT_ValueKind
     52 {
     53   TALER_MERCHANT_VK_QUANTITY,  /* -1 is illegal                    */
     54   TALER_MERCHANT_VK_STOCK      /* -1 means "infinity"              */
     55 };
     56 
     57 
     58 /**
     59  * Parse decimal quantity expressed as string for request handling.
     60  *
     61  * @param value string to parse
     62  * @param[out] integer_part result integer component
     63  * @param[out] fractional_part result fractional component (0..TALER_MERCHANT_UNIT_FRAC_BASE-1)
     64  * @return #GNUNET_OK on success, #GNUNET_SYSERR on validation failure
     65  */
     66 enum GNUNET_GenericReturnValue
     67 TALER_MERCHANT_vk_parse_fractional_string (
     68   const char *value,
     69   int64_t *integer_part,
     70   uint32_t *fractional_part);
     71 
     72 
     73 /**
     74  * Extract a fixed-decimal number that may be supplied either
     75  *   - as pure integer  (e.g. "total_stock"), or
     76  *   - as decimal text (e.g. "unit_total_stock").
     77  *
     78  * Rules:
     79  *  - If both forms are missing          -> error.
     80  *  - If both are present                -> they must match and the decimal must have no fraction.
     81  *  - For kind == TALER_MERCHANT_VK_STOCK the integer value -1 represents infinity.
     82  *
     83  * @param kind             See #TALER_MERCHANT_ValueKind
     84  * @param allow_fractional False: any fractional part is rejected
     85  * @param int_missing      True if client omitted the integer field
     86  * @param int_raw          Raw integer (undefined if @a int_missing is true)
     87  * @param str_missing      True if client omitted the string field
     88  * @param str_raw          Raw UTF-8 string (undefined if @a str_missing is true)
     89  * @param[out] int_out     Canonicalised integer part
     90  * @param[out] frac_out    Canonicalised fractional part
     91  * @param[out] error_param Set to offending field name on failure
     92  * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
     93  */
     94 enum GNUNET_GenericReturnValue
     95 TALER_MERCHANT_vk_process_quantity_inputs (
     96   enum TALER_MERCHANT_ValueKind kind,
     97   bool allow_fractional,
     98   bool int_missing,
     99   int64_t int_raw,
    100   bool str_missing,
    101   const char *str_raw,
    102   uint64_t *int_out,
    103   uint32_t *frac_out,
    104   const char **error_param);
    105 
    106 
    107 /**
    108  * Format a fixed-decimal pair into canonical string representation.
    109  * Recognises INT64_MAX / INT32_MAX as the "-1" sentinel used for
    110  * infinite stock if @a kind equals #TALER_MERCHANT_VK_STOCK.
    111  *
    112  * @param kind specifies whether sentinel values are permitted
    113  * @param integer integer portion
    114  * @param fractional fractional portion (0..TALER_MERCHANT_UNIT_FRAC_BASE-1 or sentinel)
    115  * @param buffer output buffer
    116  * @param buffer_length length of @a buffer
    117  */
    118 void
    119 TALER_MERCHANT_vk_format_fractional_string (
    120   enum TALER_MERCHANT_ValueKind kind,
    121   uint64_t integer,
    122   uint32_t fractional,
    123   size_t buffer_length,
    124   char buffer[static buffer_length]);
    125 
    126 
    127 /**
    128  * Check if @a image_data_url is a valid image
    129  * data URL. Does not validate the actual payload,
    130  * only the syntax and that it properly claims to
    131  * be an image.
    132  *
    133  * FIXME: use in TALER_MERCHANT_parse_product!
    134  *
    135  * @param image_data_url string to check
    136  * @return true if @a image_data_url is a data
    137  *         URL with an "image/" mime-type
    138  */
    139 bool
    140 TALER_MERCHANT_image_data_url_valid (const char *image_data_url);
    141 
    142 
    143 /**
    144  * Check if @a email is a valid email address.
    145  *
    146  * FIXME: move to libgnunetutil?
    147  *
    148  * @param email string to check
    149  * @return true if @a email is a valid e-mail address
    150  */
    151 bool
    152 TALER_MERCHANT_email_valid (const char *email);
    153 
    154 
    155 /**
    156  * Check if @a target_address can be used as the target address of a
    157  * report: it is passed to the report generator binary on the command
    158  * line, which in turn hands it to a delivery tool.
    159  *
    160  * @param target_address string to check
    161  * @return true if @a target_address is safe to use
    162  */
    163 bool
    164 TALER_MERCHANT_report_target_valid (const char *target_address);
    165 
    166 
    167 /**
    168  * Check if @a mime_type is a syntactically valid MIME type,
    169  * that is "type/subtype" possibly followed by parameters,
    170  * built only from printable US-ASCII characters.
    171  *
    172  * @param mime_type string to check
    173  * @return true if @a mime_type is a valid MIME type
    174  */
    175 bool
    176 TALER_MERCHANT_mime_type_valid (const char *mime_type);
    177 
    178 
    179 /**
    180  * Check if @a email is a valid international phone number
    181  * with "+CC" prefix.
    182  *
    183  * @param phone phone number to check
    184  * @param allow_letters to allow "A-Z" letters representing digits
    185  * @return normalized phone number if @a phone is valid,
    186  *   NULL if @a phone is not a phone number
    187  */
    188 char *
    189 TALER_MERCHANT_phone_validate_normalize (const char *phone,
    190                                          bool allow_letters);
    191 
    192 
    193 /**
    194  * Channel used to transmit MFA authorization request.
    195  */
    196 enum TALER_MERCHANT_MFA_Channel
    197 {
    198   /**
    199    * No MFA channel.
    200    */
    201   TALER_MERCHANT_MFA_CHANNEL_NONE = 0,
    202 
    203   /**
    204    * SMS ("sms")
    205    */
    206   TALER_MERCHANT_MFA_CHANNEL_SMS = 1,
    207 
    208   /**
    209    * E-mail ("email")
    210    */
    211   TALER_MERCHANT_MFA_CHANNEL_EMAIL,
    212 
    213   /**
    214    * TOTP ("totp"). (Not yet implemented, #10327.)
    215    */
    216   TALER_MERCHANT_MFA_CHANNEL_TOTP
    217 };
    218 
    219 
    220 /**
    221  * Types of critical operations of a merchant backend that may require
    222  * multi-factor authorization.
    223  */
    224 enum TALER_MERCHANT_MFA_CriticalOperation
    225 {
    226   /**
    227    * Marker used to indicate that this is NOT a critical operation.
    228    */
    229   TALER_MERCHANT_MFA_CO_NONE = 0,
    230 
    231   /**
    232    * Instance provisioning ("instance_provision").
    233    */
    234   TALER_MERCHANT_MFA_CO_INSTANCE_PROVISION,
    235 
    236   /**
    237    * Bank account configuration or reconfiguration ("account_config").
    238    */
    239   TALER_MERCHANT_MFA_CO_ACCOUNT_CONFIGURATION,
    240 
    241   /**
    242    * Authentication configuration change ("auth_config").
    243    */
    244   TALER_MERCHANT_MFA_CO_AUTH_CONFIGURATION,
    245 
    246   /**
    247    * Instance deletion ("instance_deletion").
    248    */
    249   TALER_MERCHANT_MFA_CO_INSTANCE_DELETION,
    250 
    251   /**
    252    * Authentication token creation ("auth_token_creation").
    253    */
    254   TALER_MERCHANT_MFA_CO_AUTH_TOKEN_CREATION
    255 
    256 };
    257 
    258 
    259 /**
    260  * Possible versions of the contract terms.
    261  */
    262 enum TALER_MERCHANT_ContractVersion
    263 {
    264 
    265   /**
    266    * Version 0
    267    */
    268   TALER_MERCHANT_CONTRACT_VERSION_0 = 0,
    269 
    270   /**
    271    * Version 1
    272    */
    273   TALER_MERCHANT_CONTRACT_VERSION_1 = 1
    274 };
    275 
    276 /**
    277  * Possible token kinds.
    278  */
    279 enum TALER_MERCHANT_ContractTokenKind
    280 {
    281   /**
    282    * Token kind invalid
    283    */
    284   TALER_MERCHANT_CONTRACT_TOKEN_KIND_INVALID = 0,
    285 
    286   /**
    287    * Subscription token kind
    288    */
    289   TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION = 1,
    290 
    291   /**
    292    * Discount token kind
    293    */
    294   TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT = 2,
    295 };
    296 
    297 
    298 /**
    299  * Get enum value from contract token type string.
    300  *
    301  * @param str contract token type string
    302  * @return enum value of token type
    303  */
    304 enum TALER_MERCHANT_ContractTokenKind
    305 TALER_MERCHANT_contract_token_kind_from_string (const char *str);
    306 
    307 
    308 /**
    309  * Possible input types for the contract terms.
    310  */
    311 enum TALER_MERCHANT_ContractInputType
    312 {
    313 
    314   /**
    315    * Input type invalid
    316    */
    317   TALER_MERCHANT_CONTRACT_INPUT_TYPE_INVALID = 0,
    318 
    319 #if FUTURE
    320   /**
    321    * Input type coin
    322    */
    323   TALER_MERCHANT_CONTRACT_INPUT_TYPE_COIN = 1,
    324 #endif
    325   /**
    326    * Input type token
    327    */
    328   TALER_MERCHANT_CONTRACT_INPUT_TYPE_TOKEN = 2
    329 };
    330 
    331 /**
    332  * Contract input (part of the v1 contract terms).
    333  */
    334 struct TALER_MERCHANT_ContractInput
    335 {
    336   /**
    337   * Type of the input.
    338   */
    339   enum TALER_MERCHANT_ContractInputType type;
    340 
    341   union
    342   {
    343 #if FUTURE
    344     /**
    345      * Coin-based input (ration). (Future work, only here for reference)
    346      */
    347     struct
    348     {
    349       /**
    350       * Price to be paid.
    351       */
    352       struct TALER_Amount price;
    353 
    354       /**
    355       * Base URL of the ration authority.
    356       */
    357       const char *ration_authority_url;
    358     } coin;
    359 #endif
    360 
    361     /**
    362      * Token-based input.
    363      */
    364     struct
    365     {
    366       /**
    367        * Slug of the token family to be used.
    368        */
    369       const char *token_family_slug;
    370 
    371       /**
    372        * Number of tokens of this type required. Defaults to one if the
    373        * field is not provided.
    374        */
    375       unsigned int count;
    376     } token;
    377   } details;
    378 };
    379 
    380 
    381 /**
    382  * Order input (part of the v1 orders).
    383  */
    384 struct TALER_MERCHANT_OrderInput
    385 {
    386   /**
    387   * Type of the input.
    388   */
    389   enum TALER_MERCHANT_ContractInputType type;
    390 
    391   union
    392   {
    393 #if FUTURE
    394     /**
    395      * Coin-based input (ration). (Future work, only here for reference)
    396      */
    397     struct
    398     {
    399       /**
    400       * Price to be paid.
    401       */
    402       struct TALER_Amount price;
    403 
    404       /**
    405       * Base URL of the ration authority.
    406       */
    407       const char *ration_authority_url;
    408     } coin;
    409 #endif
    410 
    411     /**
    412      * Token-based input.
    413      */
    414     struct
    415     {
    416       /**
    417        * Slug of the token family to be used.
    418        */
    419       const char *token_family_slug;
    420 
    421       /**
    422        * Number of tokens of this type required. Defaults to one if the
    423        * field is not provided.
    424        */
    425       unsigned int count;
    426     } token;
    427   } details;
    428 };
    429 
    430 
    431 /**
    432  * Possible output types for the contract terms.
    433  */
    434 enum TALER_MERCHANT_ContractOutputType
    435 {
    436 
    437   /**
    438    * Invalid output type
    439    */
    440   TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID = 0,
    441 
    442   /**
    443    * Output type token
    444    */
    445   TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN = 1,
    446 
    447   /**
    448    * Output type donation-receipt
    449    */
    450   TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT = 2,
    451 #if FUTURE
    452   /**
    453    * Output type coin
    454    */
    455   TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_COIN = 3
    456 #endif
    457 
    458 };
    459 
    460 /**
    461  * Contract output (part of the v1 contract terms).
    462  */
    463 struct TALER_MERCHANT_ContractOutput
    464 {
    465   /**
    466    * Type of the output.
    467    */
    468   enum TALER_MERCHANT_ContractOutputType type;
    469 
    470   union
    471   {
    472 #if FUTURE
    473     /**
    474      * Coin-based output.
    475      */
    476     struct
    477     {
    478       /**
    479        * Coins that will be yielded. This excludes any applicable withdraw fees.
    480        */
    481       struct TALER_Amount brutto_yield;
    482 
    483       /**
    484        * Base URL of the exchange that will issue the coins.
    485        *
    486        * NOTE: Once implemented, check if we need to allocate this here or if
    487        * we again reference the JSON as we do in other places.
    488        */
    489       char *exchange_url;
    490 
    491     } coin;
    492 #endif
    493     /**
    494      * DONAU-receipt output.
    495      */
    496     struct
    497     {
    498       /**
    499        * Amount of the donation.
    500        */
    501       struct TALER_Amount amount;
    502 
    503       /**
    504        * Base URLs of the donation authorities that will issue the tax receipt.
    505        */
    506       char **donau_urls;
    507 
    508       /**
    509        * Length of the @e donau_urls array.
    510        */
    511       unsigned int donau_urls_len;
    512 
    513     } donation_receipt;
    514 
    515     /**
    516      * Token-based output.
    517      */
    518     struct
    519     {
    520       /**
    521        * Slug of the token family to be issued.
    522        * Note: this is a pointer into the JSON of the
    523        * respective contract/request and not owned here.
    524        */
    525       const char *token_family_slug;
    526 
    527       /**
    528        * Index of the public key in the @a token_family_slug's token family
    529        * ``keys`` array that this output token will have.
    530        */
    531       unsigned int key_index;
    532 
    533       /**
    534        * Number of tokens of this type required. Defaults to one if the
    535        * field is not provided.
    536        */
    537       unsigned int count;
    538 
    539       /**
    540        * Determines when the output token should be valid.
    541        * Optional, set to zero for not specified (then we
    542        * use the current time).
    543        */
    544       struct GNUNET_TIME_Timestamp valid_at;
    545 
    546     } token;
    547 
    548   } details;
    549 
    550 };
    551 
    552 
    553 /**
    554  * Order output (part of the v1 orders).
    555  */
    556 struct TALER_MERCHANT_OrderOutput
    557 {
    558   /**
    559    * Type of the output.
    560    */
    561   enum TALER_MERCHANT_ContractOutputType type;
    562 
    563   union
    564   {
    565 #if FUTURE
    566     /**
    567      * Coin-based output.
    568      */
    569     struct
    570     {
    571       /**
    572        * Coins that will be yielded. This excludes any applicable withdraw fees.
    573        */
    574       struct TALER_Amount brutto_yield;
    575 
    576       /**
    577        * Base URL of the exchange that will issue the coins.
    578        *
    579        * NOTE: Once implemented, check if we need to allocate this here or if
    580        * we again reference the JSON as we do in other places.
    581        */
    582       char *exchange_url;
    583 
    584     } coin;
    585 #endif
    586     /**
    587      * DONAU-receipt output.
    588      */
    589     struct
    590     {
    591       /**
    592        * Amount of the donation. (optional)
    593        */
    594       struct TALER_Amount amount;
    595 
    596       /**
    597        * True if @e amount is NOT set.
    598        */
    599       bool no_amount;
    600 
    601     } donation_receipt;
    602 
    603     /**
    604      * Token-based output.
    605      */
    606     struct
    607     {
    608       /**
    609        * Slug of the token family to be issued.
    610        * Note: this is a pointer into the JSON of the
    611        * respective contract/request and not owned here.
    612        */
    613       const char *token_family_slug;
    614 
    615       /**
    616        * Number of tokens of this type required. Defaults to one if the
    617        * field is not provided.
    618        */
    619       unsigned int count;
    620 
    621       /**
    622        * Determines when the output token should be valid.
    623        * Optional, set to zero for not specified (then we
    624        * use the current time).
    625        */
    626       struct GNUNET_TIME_Timestamp valid_at;
    627 
    628     } token;
    629 
    630   } details;
    631 
    632 };
    633 
    634 
    635 /**
    636  * Contract choice (part of the v1 contract terms).
    637  */
    638 struct TALER_MERCHANT_ContractChoice
    639 {
    640 
    641   /**
    642    * Amount to be paid for this choice.
    643    */
    644   struct TALER_Amount amount;
    645 
    646   /**
    647    * Tip included by the customer (part of the total amount).
    648    */
    649   struct TALER_Amount tip;
    650 
    651   /**
    652    * True if @e tip was not provided.
    653    */
    654   bool no_tip;
    655 
    656   /**
    657    * Human readable description of the semantics of the choice within the
    658    * contract to be shown to the user at payment.
    659    */
    660   char *description;
    661 
    662   /**
    663    * Map from IETF BCP 47 language tags to localized description.
    664    * Optional.
    665    */
    666   json_t *description_i18n;
    667 
    668   /**
    669    * Maximum fee the merchant is willing to pay for this choice.
    670    * Set to an invalid amount to use instance defaults (zero or STEFAN).
    671    */
    672   struct TALER_Amount max_fee;
    673 
    674   /**
    675    * List of inputs the wallet must provision (all of them) to satisfy the
    676    * conditions for the contract.
    677    */
    678   struct TALER_MERCHANT_ContractInput *inputs;
    679 
    680   /**
    681    * Length of the @e inputs array.
    682    */
    683   unsigned int inputs_len;
    684 
    685   /**
    686    * List of outputs the merchant promises to yield (all of them) once
    687    * the contract is paid.
    688    */
    689   struct TALER_MERCHANT_ContractOutput *outputs;
    690 
    691   /**
    692    * Length of the @e outputs array.
    693    */
    694   unsigned int outputs_len;
    695 };
    696 
    697 
    698 /**
    699  * Order choice (part of the v1 order).
    700  */
    701 struct TALER_MERCHANT_OrderChoice
    702 {
    703 
    704   /**
    705    * Amount to be paid for this choice.
    706    */
    707   struct TALER_Amount amount;
    708 
    709   /**
    710    * Tip included by the customer (part of the total amount).
    711    */
    712   struct TALER_Amount tip;
    713 
    714   /**
    715    * True if @e tip was not provided.
    716    */
    717   bool no_tip;
    718 
    719   /**
    720    * Human readable description of the semantics of the choice within the
    721    * contract to be shown to the user at payment.
    722    */
    723   char *description;
    724 
    725   /**
    726    * Map from IETF BCP 47 language tags to localized description.
    727    * Optional.
    728    */
    729   json_t *description_i18n;
    730 
    731   /**
    732    * Maximum fee the merchant is willing to pay for this choice.
    733    * Set to an invalid amount to use instance defaults (zero or STEFAN).
    734    */
    735   struct TALER_Amount max_fee;
    736 
    737   /**
    738    * True if @e max_fee was not provided.
    739    */
    740   bool no_max_fee;
    741 
    742   /**
    743    * True if the client may replace @e amount when instantiating the
    744    * template this choice belongs to; @e amount then merely provides the
    745    * default.  Only meaningful for choices from a template contract,
    746    * never serialized into an order.
    747    */
    748   bool editable_amount;
    749 
    750   /**
    751    * List of inputs the wallet must provision (all of them) to satisfy the
    752    * conditions for the order.
    753    */
    754   struct TALER_MERCHANT_OrderInput *inputs;
    755 
    756   /**
    757    * Length of the @e inputs array.
    758    */
    759   unsigned int inputs_len;
    760 
    761   /**
    762    * List of outputs the merchant promises to yield (all of them) once
    763    * the order is paid.
    764    */
    765   struct TALER_MERCHANT_OrderOutput *outputs;
    766 
    767   /**
    768    * Length of the @e outputs array.
    769    */
    770   unsigned int outputs_len;
    771 };
    772 
    773 
    774 /**
    775  * Public key and corresponding metadata for a token family.
    776  */
    777 struct TALER_MERCHANT_ContractTokenFamilyKey
    778 {
    779   /**
    780    * Public key.
    781    */
    782   struct TALER_TokenIssuePublicKey pub;
    783 
    784   /**
    785    * Start time of the token family duration.
    786    */
    787   struct GNUNET_TIME_Timestamp valid_after;
    788 
    789   /**
    790    * Tokens signed by this key will be valid until this time.
    791    */
    792   struct GNUNET_TIME_Timestamp valid_before;
    793 };
    794 
    795 
    796 /**
    797  * Represents a family of tokens issued by merchants that can be used in contracts.
    798  */
    799 struct TALER_MERCHANT_ContractTokenFamily
    800 {
    801   /**
    802    * Slug of the token family.
    803    */
    804   char *slug;
    805 
    806   /**
    807    * Human-readable name of the token family.
    808    */
    809   char *name;
    810 
    811   /**
    812    * Human-readable description of the semantics of the tokens issued by
    813    * this token family.
    814    */
    815   char *description;
    816 
    817   /**
    818    * Map from IETF BCP 47 language tags to localized description.
    819    * Optional.
    820    */
    821   json_t *description_i18n;
    822 
    823   /**
    824    * Relevant public keys of this token family for the given contract.
    825    */
    826   struct TALER_MERCHANT_ContractTokenFamilyKey *keys;
    827 
    828   /**
    829    * Length of the @e keys array.
    830    */
    831   unsigned int keys_len;
    832 
    833   /**
    834    * Must a wallet understand this token type to process contracts that
    835    * consume or yield it?
    836    */
    837   bool critical;
    838 
    839   /**
    840    * Kind of the token family.
    841    */
    842   enum TALER_MERCHANT_ContractTokenKind kind;
    843 
    844   /**
    845    * Kind-specific information about the token.
    846    */
    847   union
    848   {
    849     /**
    850      * Subscription token.
    851      */
    852     struct
    853     {
    854       /**
    855        * Array of domain names where this subscription can be safely used
    856        * (e.g. the issuer warrants that these sites will re-issue tokens of
    857        * this type if the respective contract says so). May contain "*" for
    858        * any domain or subdomain.
    859        */
    860       char **trusted_domains;
    861 
    862       /**
    863        * Length of the @e trusted_domains array.
    864        */
    865       unsigned int trusted_domains_len;
    866     } subscription;
    867 
    868     /**
    869     * Discount token.
    870     */
    871     struct
    872     {
    873       /**
    874        * Array of domain names where this discount token is intended to be
    875        * used. May contain "*" for any domain or subdomain. Users should be
    876        * warned about sites proposing to consume discount tokens of this
    877        * type that are not in this list that the merchant is accepting a
    878        * coupon from a competitor and thus may be attaching different
    879        * semantics (like get 20% discount for my competitors 30% discount
    880        * token).
    881        */
    882       char **expected_domains;
    883 
    884       /**
    885        * Length of the @e expected_domains array.
    886        */
    887       unsigned int expected_domains_len;
    888 
    889     } discount;
    890   } details;
    891 };
    892 
    893 /**
    894  * Specifies the quantity of a product (to be) sold.
    895  */
    896 struct TALER_MERCHANT_ProductQuantity
    897 {
    898   /**
    899    * Integer component of the quantity.
    900    */
    901   uint64_t integer;
    902 
    903   /**
    904    * Fractional component of the quantity, in the
    905    * range of 0..TALER_MERCHANT_UNIT_FRAC_BASE-1.
    906    */
    907   uint32_t fractional;
    908 
    909 };
    910 
    911 
    912 /**
    913  * How to round when computing with amounts?
    914  */
    915 enum TALER_MERCHANT_RoundMode
    916 {
    917   TALER_MERCHANT_ROUND_NEAREST,
    918   TALER_MERCHANT_ROUND_UP,
    919   TALER_MERCHANT_ROUND_DOWN
    920 };
    921 
    922 
    923 /**
    924  * Template type discriminator.
    925  */
    926 enum TALER_MERCHANT_TemplateType
    927 {
    928   TALER_MERCHANT_TEMPLATE_TYPE_INVALID = 0,
    929   TALER_MERCHANT_TEMPLATE_TYPE_FIXED_ORDER,
    930   TALER_MERCHANT_TEMPLATE_TYPE_INVENTORY_CART,
    931   TALER_MERCHANT_TEMPLATE_TYPE_PAIVANA
    932 };
    933 
    934 /**
    935  * Determine template type from string.
    936  *
    937  * @param template_type string value (NULL means fixed-order)
    938  * @return template type (defaults to fixed order)
    939  */
    940 enum TALER_MERCHANT_TemplateType
    941 TALER_MERCHANT_template_type_from_string (const char *template_type);
    942 
    943 /**
    944  * Convert template type to its string representation.
    945  *
    946  * @param template_type template type to convert
    947  * @return string name or NULL for invalid types
    948  */
    949 const char *
    950 TALER_MERCHANT_template_type_to_string (
    951   enum TALER_MERCHANT_TemplateType template_type);
    952 
    953 /**
    954  * Determine template type from a template contract.
    955  *
    956  * @param template_contract contract JSON
    957  * @return template type (defaults to fixed order)
    958  */
    959 enum TALER_MERCHANT_TemplateType
    960 TALER_MERCHANT_template_type_from_contract (const json_t *template_contract);
    961 
    962 
    963 /**
    964  * Template contract fields for inventory templates.
    965  */
    966 struct TALER_MERCHANT_TemplateContractInventory
    967 {
    968   /**
    969    * Selected categories from the template contract.
    970    */
    971   const json_t *selected_categories;
    972 
    973   /**
    974    * Selected products from the template contract.
    975    */
    976   const json_t *selected_products;
    977 
    978   /**
    979    * Whether all products are selectable.
    980    */
    981   bool selected_all;
    982 
    983   /**
    984    * Template requires exactly one selection.
    985    */
    986   bool choose_one;
    987 
    988 };
    989 
    990 /**
    991  * Template contract fields for paivana templates.
    992  */
    993 struct TALER_MERCHANT_TemplateContractPaivana
    994 {
    995   /**
    996    * Paivana website regular expression.
    997    * NULL to allow any site.
    998    */
    999   const char *website_regex;
   1000 
   1001   /**
   1002    * Array of possible specific contracts the wallet/customer may choose
   1003    * from by selecting the respective index when signing the deposit
   1004    * confirmation.
   1005    */
   1006   struct TALER_MERCHANT_OrderChoice *choices;
   1007 
   1008   /**
   1009    * Length of the @e choices array.
   1010    */
   1011   unsigned int choices_len;
   1012 
   1013 };
   1014 
   1015 /**
   1016  * Parsed template contract.
   1017  */
   1018 struct TALER_MERCHANT_TemplateContract
   1019 {
   1020   /**
   1021    * Template type.
   1022    */
   1023   enum TALER_MERCHANT_TemplateType type;
   1024 
   1025   /**
   1026    * Summary from the template contract.
   1027    */
   1028   const char *summary;
   1029 
   1030   /**
   1031    * Currency from the template contract.
   1032    */
   1033   const char *currency;
   1034 
   1035   /**
   1036    * Amount from the template contract.
   1037    */
   1038   struct TALER_Amount amount;
   1039 
   1040   /**
   1041    * True if @e amount was not provided.
   1042    */
   1043   bool no_amount;
   1044 
   1045   /**
   1046    * Smallest amount the client may end up paying (excluding tips)
   1047    * when instantiating this template.  Only checked against amounts
   1048    * the client had an influence on.
   1049    */
   1050   struct TALER_Amount min_amount;
   1051 
   1052   /**
   1053    * True if @e min_amount was not provided.
   1054    */
   1055   bool no_min_amount;
   1056 
   1057   /**
   1058    * Largest amount the client may end up paying (excluding tips)
   1059    * when instantiating this template.  Only checked against amounts
   1060    * the client had an influence on.
   1061    */
   1062   struct TALER_Amount max_amount;
   1063 
   1064   /**
   1065    * True if @e max_amount was not provided.
   1066    */
   1067   bool no_max_amount;
   1068 
   1069   /**
   1070    * Template allows tips.
   1071    */
   1072   bool request_tip;
   1073 
   1074   /**
   1075    * Minimum age required by the template.
   1076    */
   1077   uint32_t minimum_age;
   1078 
   1079   /**
   1080    * How long does the customer have to pay for the order.
   1081    * 0 if not specified (use instance default).  FOREVER is invalid for new
   1082    * templates; it can occur only when reading legacy data.
   1083    */
   1084   struct GNUNET_TIME_Relative pay_duration;
   1085 
   1086   /**
   1087    * How long does the user have at most to access/pickup the (digital)
   1088    * goods or service they bought. Optional, defaults to FOREVER.
   1089    */
   1090   struct GNUNET_TIME_Relative max_pickup_duration;
   1091 
   1092   union
   1093   {
   1094 
   1095     /**
   1096      * Parsed fields for inventory templates.
   1097      */
   1098     struct TALER_MERCHANT_TemplateContractInventory inventory;
   1099 
   1100     /**
   1101      * Parsed fields for paivana templates.
   1102      */
   1103     struct TALER_MERCHANT_TemplateContractPaivana paivana;
   1104 
   1105   } details;
   1106 
   1107 };
   1108 
   1109 /**
   1110  * Parse template contract JSON into @a out.
   1111  *
   1112  * @param template_contract JSON object containing the template contract
   1113  * @param[out] out parsed template contract
   1114  * @param[out] error_name pointer to the name of the failed field, or NULL
   1115  * @return #GNUNET_SYSERR if @a template_contract is malformed; #GNUNET_OK otherwise
   1116  */
   1117 enum GNUNET_GenericReturnValue
   1118 TALER_MERCHANT_template_contract_parse (
   1119   const json_t *template_contract,
   1120   struct TALER_MERCHANT_TemplateContract *out,
   1121   const char **error_name);
   1122 
   1123 
   1124 /**
   1125  * Release memory from template contract @a tc.
   1126  * Does not free @a tc itself.
   1127  *
   1128  * @param[in] tc contract to free
   1129  */
   1130 void
   1131 TALER_MERCHANT_template_contract_free (
   1132   struct TALER_MERCHANT_TemplateContract *tc);
   1133 
   1134 
   1135 /**
   1136  * Check if @a template_contract is valid.
   1137  *
   1138  * @param template_contract template contract to validate
   1139  * @return true if @a template_contract is valid
   1140  */
   1141 bool
   1142 TALER_MERCHANT_template_contract_valid (const json_t *template_contract);
   1143 
   1144 
   1145 /**
   1146  * Convert critical operation enumeration value to string
   1147  * suitable for human readers.
   1148  *
   1149  * @param co input to convert
   1150  * @return operation value as string
   1151  */
   1152 const char *
   1153 TALER_MERCHANT_MFA_co2s (
   1154   enum TALER_MERCHANT_MFA_CriticalOperation co);
   1155 
   1156 
   1157 /**
   1158  * Convert critical operation enumeration value to string.
   1159  *
   1160  * @param co input to convert
   1161  * @return operation value as string
   1162  */
   1163 const char *
   1164 TALER_MERCHANT_MFA_co_to_string (
   1165   enum TALER_MERCHANT_MFA_CriticalOperation co);
   1166 
   1167 
   1168 /**
   1169  * Convert string to critical operation enumeration value.
   1170  *
   1171  * @param str input to convert
   1172  * @return #TALER_MERCHANT_MFA_CO_NONE on failure
   1173  */
   1174 enum TALER_MERCHANT_MFA_CriticalOperation
   1175 TALER_MERCHANT_MFA_co_from_string (const char *str);
   1176 
   1177 
   1178 /**
   1179  * Convert MFA channel enumeration value to string.
   1180  *
   1181  * @param ch input to convert
   1182  * @return operation value as string
   1183  */
   1184 const char *
   1185 TALER_MERCHANT_MFA_channel_to_string (
   1186   enum TALER_MERCHANT_MFA_Channel ch);
   1187 
   1188 
   1189 /**
   1190  * Convert string to MFA channel enumeration value.
   1191  *
   1192  * @param str input to convert
   1193  * @return #TALER_MERCHANT_MFA_CHANNEL_NONE on failure
   1194  */
   1195 enum TALER_MERCHANT_MFA_Channel
   1196 TALER_MERCHANT_MFA_channel_from_string (const char *str);
   1197 
   1198 
   1199 /**
   1200  * @brief Salted hash of a body for a request that required MFA.
   1201  */
   1202 struct TALER_MERCHANT_MFA_BodyHash
   1203 {
   1204   /**
   1205    * Hash of the body and salt.
   1206    */
   1207   struct GNUNET_ShortHashCode hash;
   1208 };
   1209 
   1210 
   1211 /**
   1212  * @brief Salt used when computing a `struct TALER_MERCHANT_MFA_BodyHash`
   1213  */
   1214 struct TALER_MERCHANT_MFA_BodySalt
   1215 {
   1216   /**
   1217    * Salt.
   1218    */
   1219   uint64_t salt[128 / 64];
   1220 };
   1221 
   1222 
   1223 /**
   1224  * @brief Token used to authorize report generation.
   1225  */
   1226 struct TALER_MERCHANT_ReportToken
   1227 {
   1228   /**
   1229    * Salt.
   1230    */
   1231   uint64_t salt[256 / 64];
   1232 };
   1233 
   1234 
   1235 /**
   1236  * Hash the given request @a body with the given @a salt to
   1237  * produce @a h_body for MFA checks.
   1238  *
   1239  * @param body HTTP request body, NULL if body was empty
   1240  * @param salt salt to use
   1241  * @param h_body resulting hash
   1242  */
   1243 void
   1244 TALER_MERCHANT_mfa_body_hash (
   1245   const json_t *body,
   1246   const struct TALER_MERCHANT_MFA_BodySalt *salt,
   1247   struct TALER_MERCHANT_MFA_BodyHash *h_body);
   1248 
   1249 
   1250 /**
   1251  * Multiply the @a unit_price by the quantity given in @a factor.
   1252  * Round the result using the given rounding mode @a rm to a
   1253  * multiple of the @a atomic_amount.
   1254  *
   1255  * @param[out] result where to store the result
   1256  * @param unit_price price for one item
   1257  * @param factor quantity to purchase, can be factional
   1258  * @param rm rounding mode to apply
   1259  * @param atomic_amount granularity to round to
   1260  * @return #GNUNET_OK on success
   1261  *   #GNUNET_NO on integer overflow (resulting amount cannot be represented)
   1262  *   #GNUNET_SYSERR on internal failure (e.g. currency
   1263  *     @a unit_price and @a atomic_amount do not match)
   1264  */
   1265 enum GNUNET_GenericReturnValue
   1266 TALER_MERCHANT_amount_multiply_by_quantity (
   1267   struct TALER_Amount *result,
   1268   const struct TALER_Amount *unit_price,
   1269   const struct TALER_MERCHANT_ProductQuantity *factor,
   1270   enum TALER_MERCHANT_RoundMode rm,
   1271   const struct TALER_Amount *atomic_amount);
   1272 
   1273 
   1274 /**
   1275  * Details about a product (to be) sold.  This structure represents
   1276  * both the MinimalInventoryProduct and the ProductSold. Note that
   1277  * the two have different mandatory and/or optional fields but are
   1278  * both possible in `struct TALER_MERCHANT_Order`. However, only
   1279  * ProductSold is allowed in the contracts.
   1280  */
   1281 struct TALER_MERCHANT_ProductSold
   1282 {
   1283 
   1284   /**
   1285    * Merchant-internal identifier for the product. NULL for none.
   1286    */
   1287   char *product_id;
   1288 
   1289   /**
   1290    * Name of the product. NULL for pre **v20** contract terms.
   1291    */
   1292   char *product_name;
   1293 
   1294   /**
   1295    * Human-readable product description.
   1296    */
   1297   char *description;
   1298 
   1299   /**
   1300    * Map from IETF BCP 47 language tags to localized descriptions.
   1301    * NULL if no translations are available.
   1302    */
   1303   json_t *description_i18n;
   1304 
   1305   /**
   1306    * Quantity (in multiples of @e unit) to be sold.
   1307    */
   1308   struct TALER_MERCHANT_ProductQuantity unit_quantity;
   1309 
   1310   /**
   1311    * Unit in which the product is measured (liters, kilograms, packages,
   1312    * etc.). Can be NULL if not specified.
   1313    */
   1314   char *unit;
   1315 
   1316   /**
   1317    * Length of the @e prices array.
   1318    */
   1319   unsigned int prices_length;
   1320 
   1321   /**
   1322    * Array of prices (in different currencies) for @e unit_quantity
   1323    * units of this product (these are longer the per-unit prices!).
   1324    */
   1325   struct TALER_Amount *prices;
   1326 
   1327   /**
   1328    * True if the @e prices given are the net price,
   1329    * false if they are the gross price.  Note that even @e prices are the
   1330    * gross price, @e taxes may be missing if the merchant configured
   1331    * gross @e prices but did not configure any @e taxes.
   1332    * Similarly, the merchant may have configured net @e prices
   1333    * for products but deals with taxes on a per-order basis. Thus, it
   1334    * may not always be possible to compute the gross price from the net
   1335    * price for an individual product, necessitating this flag.
   1336    */
   1337   bool prices_are_net;
   1338 
   1339   /**
   1340    * An optional base64-encoded image of the product.
   1341    */
   1342   char *image;
   1343 
   1344   /**
   1345    * A list of taxes paid by the merchant for this product. Can be NULL.
   1346    * Will likely change soon!
   1347    */
   1348   json_t *taxes;
   1349 
   1350   /**
   1351    * Time indicating when this product should be delivered.
   1352    * #GNUNET_TIME_UNIT_FOREVER_TS for unknown / not specified.
   1353    */
   1354   struct GNUNET_TIME_Timestamp delivery_date;
   1355 
   1356   /**
   1357    * Money pot to use for this product, overrides value from
   1358    * the inventory if given.  Not useful to wallets, only for
   1359    * merchant-internal accounting.
   1360    */
   1361   uint64_t product_money_pot;
   1362 
   1363 };
   1364 
   1365 
   1366 /**
   1367  * Merchant information.
   1368  */
   1369 struct TALER_MERCHANT_MetaData
   1370 {
   1371   /**
   1372    * Legal name of the instance
   1373    */
   1374   char *name;
   1375 
   1376   /**
   1377    * Merchant's site url
   1378    */
   1379   char *website;
   1380 
   1381   /**
   1382    * Email contact for customers
   1383    */
   1384   char *email;
   1385 
   1386   /**
   1387    * merchant's logo data uri
   1388    */
   1389   char *logo;
   1390 
   1391   /**
   1392    * Merchant address
   1393    */
   1394   json_t *address;
   1395 
   1396   /**
   1397    * Jurisdiction of the business
   1398    */
   1399   json_t *jurisdiction;
   1400 
   1401 };
   1402 
   1403 
   1404 /**
   1405  * Struct to hold terms common to both orders and contracts and
   1406  * are basically present (or optional) all the time.
   1407  */
   1408 struct TALER_MERCHANT_ContractBaseTerms
   1409 {
   1410 
   1411   /**
   1412    * Version of the contract terms.
   1413    */
   1414   enum TALER_MERCHANT_ContractVersion version;
   1415 
   1416   /**
   1417    * Summary of the contract.
   1418    */
   1419   char *summary;
   1420 
   1421   /**
   1422    * Internationalized summary. Optional.
   1423    */
   1424   json_t *summary_i18n;
   1425 
   1426   /**
   1427    * URL where the same contract could be ordered again (if available).
   1428    * Optional.
   1429    */
   1430   char *public_reorder_url;
   1431 
   1432   /**
   1433    * URL that will show that the contract was successful
   1434    * after it has been paid for.
   1435    * Optional.
   1436    */
   1437   char *fulfillment_url;
   1438 
   1439   /**
   1440    * Message shown to the customer after paying for the contract.
   1441    * Either fulfillment_url or fulfillment_message must be specified.
   1442    * Optional.
   1443    */
   1444   char *fulfillment_message;
   1445 
   1446   /**
   1447    * Map from IETF BCP 47 language tags to localized fulfillment messages.
   1448    * Optional.
   1449    */
   1450   json_t *fulfillment_message_i18n;
   1451 
   1452   /**
   1453    * Delivery location.
   1454    * Optional.
   1455    */
   1456   json_t *delivery_location;
   1457 
   1458   /**
   1459    * Delivery date.
   1460    * Optional.
   1461    */
   1462   struct GNUNET_TIME_Timestamp delivery_date;
   1463 
   1464   /**
   1465    * Specifies for how long the wallet should try to get an
   1466    * automatic refund for the purchase.
   1467    * Optional, zero if not applicable.
   1468    */
   1469   struct GNUNET_TIME_Relative auto_refund;
   1470 
   1471   /**
   1472    * Extra data that is only interpreted by the merchant frontend.
   1473    */
   1474   json_t *extra;
   1475 
   1476   /**
   1477    * Minimum age the buyer must have (in years).
   1478    */
   1479   uint8_t minimum_age;
   1480 
   1481   /**
   1482    * Default money pot to use for this order, applies to the
   1483    * amount remaining that was not claimed by money pots of
   1484    * products or taxes.  Not useful to wallets, only for
   1485    * merchant-internal accounting.  If zero, the remaining
   1486    * account is simply not accounted for in any money pot.
   1487    */
   1488   uint64_t default_money_pot;
   1489 
   1490   /**
   1491    * Latest time until which the good or service may be
   1492    * picked up by the customer. This is usually for digital
   1493    * goods where the customer has a finite window for downloading.
   1494    * Optional. FOREVER if not set.
   1495    */
   1496   struct GNUNET_TIME_Timestamp max_pickup_time;
   1497 
   1498   /**
   1499    * Array of external payment entries settled outside of Taler.
   1500    * Common to all contract versions; for v1 contracts all choices
   1501    * must use the currency of these entries. NULL if not given.
   1502    */
   1503   json_t *amount_external;
   1504 
   1505 };
   1506 
   1507 
   1508 /**
   1509  * Struct to hold an order.
   1510  */
   1511 struct TALER_MERCHANT_Order
   1512 {
   1513 
   1514   /**
   1515    * Shared base terms of contracts and orders.
   1516    */
   1517   struct TALER_MERCHANT_ContractBaseTerms *base;
   1518 
   1519   /**
   1520    * Our order ID. Optional.
   1521    */
   1522   char *order_id;
   1523 
   1524   /**
   1525    * Array of products that are part of the purchase.
   1526    */
   1527   struct TALER_MERCHANT_ProductSold *products;
   1528 
   1529   /**
   1530    * Length of the @e products array.
   1531    */
   1532   size_t products_len;
   1533 
   1534   /**
   1535    * Timestamp of the contract.
   1536    * Optional. Set to "now" on parsing if not given.
   1537    */
   1538   struct GNUNET_TIME_Timestamp timestamp;
   1539 
   1540   /**
   1541    * Deadline for refunds.
   1542    * Optional. Set to FOREVER if not given.
   1543    */
   1544   struct GNUNET_TIME_Timestamp refund_deadline;
   1545 
   1546   /**
   1547    * Payment deadline.
   1548    * Optional. Set to zero if not given.
   1549    */
   1550   struct GNUNET_TIME_Timestamp pay_deadline;
   1551 
   1552   /**
   1553    * Wire transfer deadline.
   1554    * Optional. Set to FOREVER if not given.
   1555    */
   1556   struct GNUNET_TIME_Timestamp wire_transfer_deadline;
   1557 
   1558   /**
   1559    * Details depending on the @e base.version.
   1560    */
   1561   union
   1562   {
   1563 
   1564     /**
   1565      * Details for v0 orders.
   1566      */
   1567     struct
   1568     {
   1569 
   1570       /**
   1571        * Price to be paid for the transaction. Could be 0. The price is in addition
   1572        * to other instruments, such as rations and tokens.
   1573        * The exchange will subtract deposit fees from that amount
   1574        * before transferring it to the merchant.
   1575        */
   1576       struct TALER_Amount brutto;
   1577 
   1578       /**
   1579        * Tip included by the customer (part of the total amount).
   1580        */
   1581       struct TALER_Amount tip;
   1582 
   1583       /**
   1584        * True if @e tip was not provided.
   1585        */
   1586       bool no_tip;
   1587 
   1588       /**
   1589        * Maximum fee as given by the client request.
   1590        */
   1591       struct TALER_Amount max_fee;
   1592 
   1593       /**
   1594        * True if @e max_fee was not provided.
   1595        */
   1596       bool no_max_fee;
   1597 
   1598     } v0;
   1599 
   1600     /**
   1601      * Details for v1 contracts.
   1602      */
   1603     struct
   1604     {
   1605 
   1606       /**
   1607        * Array of possible specific payment choices the wallet/customer may choose
   1608        * from by selecting the respective index when signing the deposit
   1609 2       * confirmation.
   1610        */
   1611       struct TALER_MERCHANT_OrderChoice *choices;
   1612 
   1613       /**
   1614        * Length of the @e choices array.
   1615        */
   1616       unsigned int choices_len;
   1617 
   1618     } v1;
   1619 
   1620   } details;
   1621 
   1622 };
   1623 
   1624 
   1625 /**
   1626  * Struct to hold proto-contracts.
   1627  */
   1628 struct TALER_MERCHANT_ProtoContract
   1629 {
   1630 
   1631   /**
   1632    * Shared base terms of contracts and orders.
   1633    */
   1634   struct TALER_MERCHANT_ContractBaseTerms *base;
   1635 
   1636   /**
   1637    * Our order ID.
   1638    */
   1639   char *order_id;
   1640 
   1641   /**
   1642    * Timestamp of the contract.
   1643    */
   1644   struct GNUNET_TIME_Timestamp timestamp;
   1645 
   1646   /**
   1647    * Deadline for refunds.
   1648    */
   1649   struct GNUNET_TIME_Timestamp refund_deadline;
   1650 
   1651   /**
   1652    * Payment deadline.
   1653    */
   1654   struct GNUNET_TIME_Timestamp pay_deadline;
   1655 
   1656   /**
   1657    * Wire transfer deadline.
   1658    */
   1659   struct GNUNET_TIME_Timestamp wire_deadline;
   1660 
   1661   /**
   1662    * Merchant public key.
   1663    */
   1664   struct TALER_MerchantPublicKeyP merchant_pub;
   1665 
   1666   /**
   1667    * Merchant base URL.
   1668    */
   1669   char *merchant_base_url;
   1670 
   1671   /**
   1672    * Metadata about the merchant.
   1673    */
   1674   struct TALER_MERCHANT_MetaData merchant;
   1675 
   1676   /**
   1677    * Array of products that are part of the purchase.
   1678    */
   1679   struct TALER_MERCHANT_ProductSold *products;
   1680 
   1681   /**
   1682    * Length of the @e products array.
   1683    */
   1684   size_t products_len;
   1685 
   1686   /**
   1687    * The hash of the merchant instance's wire details.
   1688    */
   1689   struct TALER_MerchantWireHashP h_wire;
   1690 
   1691   /**
   1692    * Wire transfer method identifier for the wire method associated with
   1693    * @e h_wire.
   1694    */
   1695   char *wire_method;
   1696 
   1697   /**
   1698    * Exchanges that the merchant accepts even if it does not accept any auditors that audit them.
   1699    * TODO: appropriate type
   1700    */
   1701   json_t *exchanges;
   1702 
   1703   /**
   1704    * Details depending on the @e version.
   1705    */
   1706   union
   1707   {
   1708 
   1709     /**
   1710      * Details for v0 contracts.
   1711      */
   1712     struct
   1713     {
   1714 
   1715       /**
   1716        * Price to be paid for the transaction. Could be 0. The price is in addition
   1717        * to other instruments, such as rations and tokens.
   1718        * The exchange will subtract deposit fees from that amount
   1719        * before transferring it to the merchant.
   1720        */
   1721       struct TALER_Amount brutto;
   1722 
   1723       /**
   1724        * Tip included by the customer (part of the total amount).
   1725        */
   1726       struct TALER_Amount tip;
   1727 
   1728       /**
   1729        * True if @e tip was not provided.
   1730        */
   1731       bool no_tip;
   1732 
   1733       /**
   1734        * Maximum fee as given by the client request.
   1735        */
   1736       struct TALER_Amount max_fee;
   1737 
   1738     } v0;
   1739 
   1740     /**
   1741      * Details for v1 contracts.
   1742      */
   1743     struct
   1744     {
   1745 
   1746       /**
   1747        * Array of possible specific contracts the wallet/customer may choose
   1748        * from by selecting the respective index when signing the deposit
   1749        * confirmation.
   1750        */
   1751       struct TALER_MERCHANT_ContractChoice *choices;
   1752 
   1753       /**
   1754        * Length of the @e choices array.
   1755        */
   1756       unsigned int choices_len;
   1757 
   1758       /**
   1759        * Array of token authorities.
   1760        */
   1761       struct TALER_MERCHANT_ContractTokenFamily *token_authorities;
   1762 
   1763       /**
   1764        * Length of the @e token_authorities array.
   1765        */
   1766       unsigned int token_authorities_len;
   1767 
   1768     } v1;
   1769 
   1770   } details;
   1771 
   1772 };
   1773 
   1774 
   1775 /**
   1776  * Struct to hold contract terms.
   1777  */
   1778 struct TALER_MERCHANT_Contract
   1779 {
   1780 
   1781   /**
   1782    * The proto-contract.
   1783    */
   1784   struct TALER_MERCHANT_ProtoContract *pc;
   1785 
   1786   /**
   1787    * Nonce generated by the wallet and echoed by the merchant
   1788    * in this field when the proposal is generated.
   1789    */
   1790   char *nonce;
   1791 
   1792 };
   1793 
   1794 
   1795 /**
   1796  * Provide specification to parse an JSON contract input type.
   1797  * The value is provided as a descriptive string.
   1798  *
   1799  * @param name name of the JSON member with the contract type
   1800  * @param[out] cit where to store the contract input type
   1801  * @return spec for parsing a contract input type
   1802  */
   1803 struct GNUNET_JSON_Specification
   1804 TALER_MERCHANT_json_spec_cit (
   1805   const char *name,
   1806   enum TALER_MERCHANT_ContractInputType *cit);
   1807 
   1808 
   1809 /**
   1810  * Create JSON specification to parse a merchant contract
   1811  * version.
   1812  *
   1813  * @param name name of the field
   1814  * @param[out] version where to write the contract version
   1815  * @return JSON specification object
   1816  */
   1817 struct GNUNET_JSON_Specification
   1818 TALER_MERCHANT_spec_contract_version (
   1819   const char *name,
   1820   enum TALER_MERCHANT_ContractVersion *version);
   1821 
   1822 
   1823 /**
   1824  * Provide specification to parse given JSON object to merchant details in the
   1825  * order terms. All fields from @a order are copied.
   1826  *
   1827  * @param name name of the merchant details field in the JSON
   1828  * @param[out] order where the merchant details have to be written
   1829  */
   1830 struct GNUNET_JSON_Specification
   1831 TALER_MERCHANT_spec_merchant_details (
   1832   const char *name,
   1833   struct TALER_MERCHANT_MetaData *merchant);
   1834 
   1835 
   1836 /**
   1837  * Get JSON representation of merchant details.
   1838  *
   1839  * @param[in] merchant metadata to serialize
   1840  * @return JSON object with merchant details; NULL on error
   1841  */
   1842 json_t *
   1843 TALER_MERCHANT_metadata_to_json (
   1844   const struct TALER_MERCHANT_MetaData *merchant);
   1845 
   1846 
   1847 /**
   1848  * Free representation of merchant details.
   1849  *
   1850  * @param[in,out] merchant metadata to release, excluding the pointer itself
   1851  */
   1852 void
   1853 TALER_MERCHANT_metadata_free (
   1854   struct TALER_MERCHANT_MetaData *merchant);
   1855 
   1856 
   1857 /**
   1858  * Provide specification to parse given JSON array to order
   1859  * choices. All fields from @a choices elements are copied.
   1860  *
   1861  * @param name name of the choices field in the JSON
   1862  * @param[out] choices where the order choices array has to be written
   1863  * @param[out] choices_len length of the @a choices array
   1864  */
   1865 struct GNUNET_JSON_Specification
   1866 TALER_MERCHANT_spec_order_choices (
   1867   const char *name,
   1868   struct TALER_MERCHANT_OrderChoice **choices,
   1869   unsigned int *choices_len);
   1870 
   1871 
   1872 /**
   1873  * Provide specification to parse given JSON array to contract terms
   1874  * choices. All fields from @a choices elements are copied.
   1875  *
   1876  * @param name name of the choices field in the JSON
   1877  * @param[out] choices where the contract choices array has to be written
   1878  * @param[out] choices_len length of the @a choices array
   1879  */
   1880 struct GNUNET_JSON_Specification
   1881 TALER_MERCHANT_spec_contract_choices (
   1882   const char *name,
   1883   struct TALER_MERCHANT_ContractChoice **choices,
   1884   unsigned int *choices_len);
   1885 
   1886 
   1887 /**
   1888  * Maximum number of issue keys accepted per token family by the shared JSON
   1889  * parser. This resource limit also applies to fountain-info responses.
   1890  */
   1891 #define TALER_MERCHANT_MAX_TOKEN_FAMILY_KEYS 1024
   1892 
   1893 
   1894 /**
   1895  * Maximum number of expected or trusted domains accepted per token family by
   1896  * the shared JSON parser.
   1897  */
   1898 #define TALER_MERCHANT_MAX_TOKEN_FAMILY_DOMAINS 1024
   1899 
   1900 
   1901 /**
   1902  * Provide specification to parse given JSON array to token families in the
   1903  * contract terms. All fields from @a families items are copied.
   1904  * Rejects keys and domain arrays above #TALER_MERCHANT_MAX_TOKEN_FAMILY_KEYS
   1905  * and #TALER_MERCHANT_MAX_TOKEN_FAMILY_DOMAINS, respectively, before allocating
   1906  * their storage.
   1907  *
   1908  * @param name name of the token families field in the JSON
   1909  * @param[out] families where the token families array has to be written
   1910  * @param[out] families_len length of the @a families array
   1911  */
   1912 struct GNUNET_JSON_Specification
   1913 TALER_MERCHANT_spec_token_families (
   1914   const char *name,
   1915   struct TALER_MERCHANT_ContractTokenFamily **families,
   1916   unsigned int *families_len);
   1917 
   1918 
   1919 /**
   1920  * Check if @a taxes is an array of valid Taxes in the sense of
   1921  * Taler's API definition.
   1922  *
   1923  * @param taxes array to check
   1924  * @return true if @a taxes is an array and all
   1925  *         entries are valid Taxes.
   1926  */
   1927 bool
   1928 TALER_MERCHANT_taxes_array_valid (
   1929   const json_t *taxes);
   1930 
   1931 
   1932 /**
   1933  * Check if @a method is a valid name for a payment method handled
   1934  * outside of Taler: a non-empty identifier made of ASCII
   1935  * alphanumerics and "-", and never "taler", which is what the plain
   1936  * "amount" stands for.
   1937  *
   1938  * @param method method name to check
   1939  * @return true if @a method may be used as an external payment method
   1940  */
   1941 bool
   1942 TALER_MERCHANT_payment_method_valid (
   1943   const char *method);
   1944 
   1945 
   1946 /**
   1947  * Check if @a amount_external is a valid array of external payment
   1948  * entries. Each entry must be an object with a "method" (ASCII
   1949  * identifier, never "taler"), an "id" (unique within the array) and
   1950  * an "amount"; all entries must agree on one currency. Any additional
   1951  * method-specific fields must be flat (no objects or arrays).
   1952  *
   1953  * Use #TALER_MERCHANT_amount_external_currency_valid() to check the
   1954  * entries against the currency of the Taler amount of an order.
   1955  *
   1956  * @param amount_external array to check
   1957  * @return true if @a amount_external is valid
   1958  */
   1959 bool
   1960 TALER_MERCHANT_amount_external_valid (
   1961   const json_t *amount_external);
   1962 
   1963 
   1964 /**
   1965  * Check that the external payment entries in @a amount_external use
   1966  * the currency of @a amount. The entries have to be valid according
   1967  * to #TALER_MERCHANT_amount_external_valid(), so that it is enough to
   1968  * look at one of them.
   1969  *
   1970  * @param amount_external array to check
   1971  * @param amount Taler amount the entries must match in currency
   1972  * @return true if the currencies match, also if there are no entries
   1973  */
   1974 bool
   1975 TALER_MERCHANT_amount_external_currency_valid (
   1976   const json_t *amount_external,
   1977   const struct TALER_Amount *amount);
   1978 
   1979 
   1980 /**
   1981  * Check that the order total stays representable: @a amount plus the
   1982  * amounts of all entries in @a amount_external must not exceed the
   1983  * largest amount Taler can express. The entries have to be valid
   1984  * according to #TALER_MERCHANT_amount_external_valid() and to agree
   1985  * with @a amount on the currency.
   1986  *
   1987  * Without this check an order could be stored whose total can never
   1988  * be computed again, which would only surface much later, when a
   1989  * refund is attempted.
   1990  *
   1991  * @param amount_external array to check
   1992  * @param amount Taler amount of the order or choice
   1993  * @return true if the total is representable, also if there are no
   1994  *         entries
   1995  */
   1996 bool
   1997 TALER_MERCHANT_amount_external_total_valid (
   1998   const json_t *amount_external,
   1999   const struct TALER_Amount *amount);
   2000 
   2001 
   2002 /**
   2003  * Provide specification to parse an JSON contract output type.
   2004  * The value is provided as a descriptive string.
   2005  *
   2006  * @param name name of the JSON member with the contract type
   2007  * @param[out] cot where to store the contract output type
   2008  * @return spec for parsing a contract output type
   2009  */
   2010 struct GNUNET_JSON_Specification
   2011 TALER_MERCHANT_json_spec_cot (
   2012   const char *name,
   2013   enum TALER_MERCHANT_ContractOutputType *cot);
   2014 
   2015 
   2016 /**
   2017  * Get JSON representation of contract choice.
   2018  *
   2019  * @param[in] choice contract choice to serialize
   2020  * @param order whether @a choice is contained in order or contract terms
   2021  * @return JSON representation of @a choice; NULL on error
   2022  */
   2023 json_t *
   2024 TALER_MERCHANT_json_from_contract_choice (
   2025   const struct TALER_MERCHANT_ContractChoice *choice);
   2026 
   2027 
   2028 /**
   2029  * Free all the fields in the given @a choice, but not @a choice itself, since
   2030  * it is normally part of an array.
   2031  *
   2032  * @param[in] choice contract terms choice to free
   2033  */
   2034 void
   2035 TALER_MERCHANT_contract_choice_free (
   2036   struct TALER_MERCHANT_ContractChoice *choice);
   2037 
   2038 
   2039 /**
   2040  * Get JSON representation of an order choice.
   2041  *
   2042  * @param[in] choice contract choice to serialize
   2043  * @return JSON representation of @a choice; NULL on error
   2044  */
   2045 json_t *
   2046 TALER_MERCHANT_json_from_order_choice (
   2047   const struct TALER_MERCHANT_OrderChoice *choice);
   2048 
   2049 
   2050 /**
   2051  * Free all the fields in the given @a choice, but not @a choice itself, since
   2052  * it is normally part of an array.
   2053  *
   2054  * @param[in] choice contract terms choice to free
   2055  */
   2056 void
   2057 TALER_MERCHANT_order_choice_free (
   2058   struct TALER_MERCHANT_OrderChoice *choice);
   2059 
   2060 
   2061 /**
   2062  * Get JSON representation of contract token family.
   2063  *
   2064  * @param[in] family contract token family to serialize
   2065  * @return JSON representation of @a family; NULL on error
   2066  */
   2067 json_t *
   2068 TALER_MERCHANT_json_from_token_family (
   2069   const struct TALER_MERCHANT_ContractTokenFamily *family);
   2070 
   2071 
   2072 /**
   2073  * Find token family in contract terms from slug and validity date.
   2074  *
   2075  * @param slug slug of the token family
   2076  * @param valid_after validity start of the token family
   2077  * @param[in] families array of token families in the contract terms
   2078  * @param families_len length of @a families array
   2079  * @param[out] family matching token family; NULL if no result;
   2080  *   the pointer returned will be an alias into @e families
   2081  * @param[out] key key of matching token family; NULL if no result
   2082  * @return #GNUNET_SYSERR if no matching family found; #GNUNET_OK otherwise
   2083  */
   2084 enum GNUNET_GenericReturnValue
   2085 TALER_MERCHANT_find_token_family_key (
   2086   const char *slug,
   2087   struct GNUNET_TIME_Timestamp valid_after,
   2088   const struct TALER_MERCHANT_ContractTokenFamily *families,
   2089   unsigned int families_len,
   2090   struct TALER_MERCHANT_ContractTokenFamily *family,
   2091   struct TALER_MERCHANT_ContractTokenFamilyKey *key);
   2092 
   2093 
   2094 /**
   2095  * Free all the fields in the given @a family, but not @a family itself, since
   2096  * it is normally part of an array.
   2097  *
   2098  * @param[in] family contract token family to free
   2099  */
   2100 void
   2101 TALER_MERCHANT_contract_token_family_free (
   2102   struct TALER_MERCHANT_ContractTokenFamily *family);
   2103 
   2104 
   2105 /**
   2106  * Parse JSON product given in @a p, returning the result in
   2107  * @a r.
   2108  *
   2109  * @param p JSON specifying a ``ProductSold`` to parse
   2110  * @param[out] r where to write the result
   2111  * @param allow_mip true to allow for MinimalInventoryProduct
   2112  * @return #GNUNET_OK on success
   2113  */
   2114 enum GNUNET_GenericReturnValue
   2115 TALER_MERCHANT_parse_product_sold (
   2116   const json_t *p,
   2117   struct TALER_MERCHANT_ProductSold *r,
   2118   bool allow_mip);
   2119 
   2120 
   2121 /**
   2122  * Serialize @a p to JSON.
   2123  *
   2124  * @param p product to serialize
   2125  * @return JSON object representing the product @a p
   2126  */
   2127 json_t *
   2128 TALER_MERCHANT_product_sold_serialize (
   2129   const struct TALER_MERCHANT_ProductSold *p);
   2130 
   2131 
   2132 /**
   2133  * Release memory inside of @a product, but not @a product itself.
   2134  *
   2135  * @param[in] product data structure to clean up
   2136  */
   2137 void
   2138 TALER_MERCHANT_product_sold_free (
   2139   struct TALER_MERCHANT_ProductSold *product);
   2140 
   2141 
   2142 /**
   2143  * Parse base terms (of orders and contracts) in @a input.
   2144  *
   2145  * @param[in] input JSON object containing contract terms
   2146  * @return NULL if @a input is malformed
   2147  */
   2148 struct TALER_MERCHANT_ContractBaseTerms *
   2149 TALER_MERCHANT_base_terms_parse (
   2150   json_t *input);
   2151 
   2152 
   2153 /**
   2154  * Serialize contract base terms into JSON object.
   2155  *
   2156  * @param[in] ct base contract terms to serialize
   2157  * @return JSON representation of @a ct; NULL on error
   2158  */
   2159 json_t *
   2160 TALER_MERCHANT_base_terms_serialize (
   2161   const struct TALER_MERCHANT_ContractBaseTerms *ct);
   2162 
   2163 
   2164 /**
   2165  * Release memory in @a ct
   2166  *
   2167  * @param[in] ct common terms to clean up
   2168  */
   2169 void
   2170 TALER_MERCHANT_base_terms_free (
   2171   struct TALER_MERCHANT_ContractBaseTerms *ct);
   2172 
   2173 
   2174 /**
   2175  * Parse JSON order in @a input.
   2176  *
   2177  * @param[in] input JSON object containing an order
   2178  * @return parsed contract terms; NULL if @a input is malformed
   2179  */
   2180 struct TALER_MERCHANT_Order *
   2181 TALER_MERCHANT_order_parse (
   2182   json_t *input);
   2183 
   2184 
   2185 #if NOT_NEEDED_YET
   2186 /**
   2187  * Serialize order into JSON object.
   2188  *
   2189  * @param[in] input contract terms to serialize
   2190  * @param nonce_optional whether `nonce' field is optional
   2191  * @return JSON representation of @a input; NULL on error
   2192  */
   2193 json_t *
   2194 TALER_MERCHANT_order_serialize (
   2195   const struct TALER_MERCHANT_Order *input);
   2196 
   2197 #endif
   2198 
   2199 
   2200 /**
   2201  * Free the @a order and all fields in it.
   2202  *
   2203  * @param[in] order order to free
   2204  */
   2205 void
   2206 TALER_MERCHANT_order_free (
   2207   struct TALER_MERCHANT_Order *order);
   2208 
   2209 
   2210 /**
   2211  * Parse JSON proto contract terms in @a input.
   2212  *
   2213  * @param[in] input JSON object containing contract terms
   2214  * @return NULL on failure
   2215  */
   2216 struct TALER_MERCHANT_ProtoContract *
   2217 TALER_MERCHANT_proto_contract_parse (
   2218   json_t *input);
   2219 
   2220 
   2221 /**
   2222  * Serialize proto contract into JSON object.
   2223  *
   2224  * @param[in] pc proto contract to serialize
   2225  * @return JSON representation of @a pc; NULL on error
   2226  */
   2227 json_t *
   2228 TALER_MERCHANT_proto_contract_serialize (
   2229   const struct TALER_MERCHANT_ProtoContract *pc);
   2230 
   2231 
   2232 /**
   2233  * Free the proto-contract at @a pc
   2234  *
   2235  * @param[in] pc proto-contract to free
   2236  */
   2237 void
   2238 TALER_MERCHANT_proto_contract_free (
   2239   struct TALER_MERCHANT_ProtoContract *pc);
   2240 
   2241 
   2242 /**
   2243  * Parse JSON contract terms in @a input.
   2244  *
   2245  * @param[in] input JSON object containing contract terms
   2246  * @return parsed contract terms; NULL if @a input is malformed
   2247  */
   2248 struct TALER_MERCHANT_Contract *
   2249 TALER_MERCHANT_contract_parse (
   2250   json_t *input);
   2251 
   2252 
   2253 /**
   2254  * Serialize contract terms into JSON object.
   2255  *
   2256  * @param[in] input contract terms to serialize
   2257  * @return JSON representation of @a input; NULL on error
   2258  */
   2259 json_t *
   2260 TALER_MERCHANT_contract_serialize (
   2261   const struct TALER_MERCHANT_Contract *input);
   2262 
   2263 
   2264 /**
   2265  * Free the @a contract and all fields in it.
   2266  *
   2267  * @param[in] contract contract to free
   2268  */
   2269 void
   2270 TALER_MERCHANT_contract_free (
   2271   struct TALER_MERCHANT_Contract *contract);
   2272 
   2273 
   2274 #endif