merchant

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

taler_merchant_util.h (51484B)


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