merchant

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

taler_merchant_util.h (47334B)


      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    * List of inputs the wallet must provision (all of them) to satisfy the
    720    * conditions for the order.
    721    */
    722   struct TALER_MERCHANT_OrderInput *inputs;
    723 
    724   /**
    725    * Length of the @e inputs array.
    726    */
    727   unsigned int inputs_len;
    728 
    729   /**
    730    * List of outputs the merchant promises to yield (all of them) once
    731    * the order is paid.
    732    */
    733   struct TALER_MERCHANT_OrderOutput *outputs;
    734 
    735   /**
    736    * Length of the @e outputs array.
    737    */
    738   unsigned int outputs_len;
    739 };
    740 
    741 
    742 /**
    743  * Public key and corresponding metadata for a token family.
    744  */
    745 struct TALER_MERCHANT_ContractTokenFamilyKey
    746 {
    747   /**
    748    * Public key.
    749    */
    750   struct TALER_TokenIssuePublicKey pub;
    751 
    752   /**
    753    * Start time of the token family duration.
    754    */
    755   struct GNUNET_TIME_Timestamp valid_after;
    756 
    757   /**
    758    * Tokens signed by this key will be valid until this time.
    759    */
    760   struct GNUNET_TIME_Timestamp valid_before;
    761 };
    762 
    763 
    764 /**
    765  * Represents a family of tokens issued by merchants that can be used in contracts.
    766  */
    767 struct TALER_MERCHANT_ContractTokenFamily
    768 {
    769   /**
    770    * Slug of the token family.
    771    */
    772   char *slug;
    773 
    774   /**
    775    * Human-readable name of the token family.
    776    */
    777   char *name;
    778 
    779   /**
    780    * Human-readable description of the semantics of the tokens issued by
    781    * this token family.
    782    */
    783   char *description;
    784 
    785   /**
    786    * Map from IETF BCP 47 language tags to localized description.
    787    * Optional.
    788    */
    789   json_t *description_i18n;
    790 
    791   /**
    792    * Relevant public keys of this token family for the given contract.
    793    */
    794   struct TALER_MERCHANT_ContractTokenFamilyKey *keys;
    795 
    796   /**
    797    * Length of the @e keys array.
    798    */
    799   unsigned int keys_len;
    800 
    801   /**
    802    * Must a wallet understand this token type to process contracts that
    803    * consume or yield it?
    804    */
    805   bool critical;
    806 
    807   /**
    808    * Kind of the token family.
    809    */
    810   enum TALER_MERCHANT_ContractTokenKind kind;
    811 
    812   /**
    813    * Kind-specific information about the token.
    814    */
    815   union
    816   {
    817     /**
    818      * Subscription token.
    819      */
    820     struct
    821     {
    822       /**
    823        * Array of domain names where this subscription can be safely used
    824        * (e.g. the issuer warrants that these sites will re-issue tokens of
    825        * this type if the respective contract says so). May contain "*" for
    826        * any domain or subdomain.
    827        */
    828       char **trusted_domains;
    829 
    830       /**
    831        * Length of the @e trusted_domains array.
    832        */
    833       unsigned int trusted_domains_len;
    834     } subscription;
    835 
    836     /**
    837     * Discount token.
    838     */
    839     struct
    840     {
    841       /**
    842        * Array of domain names where this discount token is intended to be
    843        * used. May contain "*" for any domain or subdomain. Users should be
    844        * warned about sites proposing to consume discount tokens of this
    845        * type that are not in this list that the merchant is accepting a
    846        * coupon from a competitor and thus may be attaching different
    847        * semantics (like get 20% discount for my competitors 30% discount
    848        * token).
    849        */
    850       char **expected_domains;
    851 
    852       /**
    853        * Length of the @e expected_domains array.
    854        */
    855       unsigned int expected_domains_len;
    856 
    857     } discount;
    858   } details;
    859 };
    860 
    861 /**
    862  * Specifies the quantity of a product (to be) sold.
    863  */
    864 struct TALER_MERCHANT_ProductQuantity
    865 {
    866   /**
    867    * Integer component of the quantity.
    868    */
    869   uint64_t integer;
    870 
    871   /**
    872    * Fractional component of the quantity, in the
    873    * range of 0..TALER_MERCHANT_UNIT_FRAC_BASE-1.
    874    */
    875   uint32_t fractional;
    876 
    877 };
    878 
    879 
    880 /**
    881  * How to round when computing with amounts?
    882  */
    883 enum TALER_MERCHANT_RoundMode
    884 {
    885   TALER_MERCHANT_ROUND_NEAREST,
    886   TALER_MERCHANT_ROUND_UP,
    887   TALER_MERCHANT_ROUND_DOWN
    888 };
    889 
    890 
    891 /**
    892  * Template type discriminator.
    893  */
    894 enum TALER_MERCHANT_TemplateType
    895 {
    896   TALER_MERCHANT_TEMPLATE_TYPE_INVALID = 0,
    897   TALER_MERCHANT_TEMPLATE_TYPE_FIXED_ORDER,
    898   TALER_MERCHANT_TEMPLATE_TYPE_INVENTORY_CART,
    899   TALER_MERCHANT_TEMPLATE_TYPE_PAIVANA
    900 };
    901 
    902 /**
    903  * Determine template type from string.
    904  *
    905  * @param template_type string value (NULL means fixed-order)
    906  * @return template type (defaults to fixed order)
    907  */
    908 enum TALER_MERCHANT_TemplateType
    909 TALER_MERCHANT_template_type_from_string (const char *template_type);
    910 
    911 /**
    912  * Convert template type to its string representation.
    913  *
    914  * @param template_type template type to convert
    915  * @return string name or NULL for invalid types
    916  */
    917 const char *
    918 TALER_MERCHANT_template_type_to_string (
    919   enum TALER_MERCHANT_TemplateType template_type);
    920 
    921 /**
    922  * Determine template type from a template contract.
    923  *
    924  * @param template_contract contract JSON
    925  * @return template type (defaults to fixed order)
    926  */
    927 enum TALER_MERCHANT_TemplateType
    928 TALER_MERCHANT_template_type_from_contract (const json_t *template_contract);
    929 
    930 
    931 /**
    932  * Template contract fields for inventory templates.
    933  */
    934 struct TALER_MERCHANT_TemplateContractInventory
    935 {
    936   /**
    937    * Selected categories from the template contract.
    938    */
    939   const json_t *selected_categories;
    940 
    941   /**
    942    * Selected products from the template contract.
    943    */
    944   const json_t *selected_products;
    945 
    946   /**
    947    * Whether all products are selectable.
    948    */
    949   bool selected_all;
    950 
    951   /**
    952    * Template requires exactly one selection.
    953    */
    954   bool choose_one;
    955 
    956 };
    957 
    958 /**
    959  * Template contract fields for paivana templates.
    960  */
    961 struct TALER_MERCHANT_TemplateContractPaivana
    962 {
    963   /**
    964    * Paivana website regular expression.
    965    * NULL to allow any site.
    966    */
    967   const char *website_regex;
    968 
    969   /**
    970    * Array of possible specific contracts the wallet/customer may choose
    971    * from by selecting the respective index when signing the deposit
    972    * confirmation.
    973    */
    974   struct TALER_MERCHANT_OrderChoice *choices;
    975 
    976   /**
    977    * Length of the @e choices array.
    978    */
    979   unsigned int choices_len;
    980 
    981 };
    982 
    983 /**
    984  * Parsed template contract.
    985  */
    986 struct TALER_MERCHANT_TemplateContract
    987 {
    988   /**
    989    * Template type.
    990    */
    991   enum TALER_MERCHANT_TemplateType type;
    992 
    993   /**
    994    * Summary from the template contract.
    995    */
    996   const char *summary;
    997 
    998   /**
    999    * Currency from the template contract.
   1000    */
   1001   const char *currency;
   1002 
   1003   /**
   1004    * Amount from the template contract.
   1005    */
   1006   struct TALER_Amount amount;
   1007 
   1008   /**
   1009    * True if @e amount was not provided.
   1010    */
   1011   bool no_amount;
   1012 
   1013   /**
   1014    * Template allows tips.
   1015    */
   1016   bool request_tip;
   1017 
   1018   /**
   1019    * Minimum age required by the template.
   1020    */
   1021   uint32_t minimum_age;
   1022 
   1023   /**
   1024    * How long does the customer have to pay for the order.
   1025    * 0 if not specified (use instance default).
   1026    */
   1027   struct GNUNET_TIME_Relative pay_duration;
   1028 
   1029   /**
   1030    * How long does the user have at most to access/pickup the (digital)
   1031    * goods or service they bought. Optional, defaults to FOREVER.
   1032    */
   1033   struct GNUNET_TIME_Relative max_pickup_duration;
   1034 
   1035   union
   1036   {
   1037 
   1038     /**
   1039      * Parsed fields for inventory templates.
   1040      */
   1041     struct TALER_MERCHANT_TemplateContractInventory inventory;
   1042 
   1043     /**
   1044      * Parsed fields for paivana templates.
   1045      */
   1046     struct TALER_MERCHANT_TemplateContractPaivana paivana;
   1047 
   1048   } details;
   1049 
   1050 };
   1051 
   1052 /**
   1053  * Parse template contract JSON into @a out.
   1054  *
   1055  * @param template_contract JSON object containing the template contract
   1056  * @param[out] out parsed template contract
   1057  * @param[out] error_name pointer to the name of the failed field, or NULL
   1058  * @return #GNUNET_SYSERR if @a template_contract is malformed; #GNUNET_OK otherwise
   1059  */
   1060 enum GNUNET_GenericReturnValue
   1061 TALER_MERCHANT_template_contract_parse (
   1062   const json_t *template_contract,
   1063   struct TALER_MERCHANT_TemplateContract *out,
   1064   const char **error_name);
   1065 
   1066 
   1067 /**
   1068  * Release memory from template contract @a tc.
   1069  * Does not free @a tc itself.
   1070  *
   1071  * @param[in] tc contract to free
   1072  */
   1073 void
   1074 TALER_MERCHANT_template_contract_free (
   1075   struct TALER_MERCHANT_TemplateContract *tc);
   1076 
   1077 
   1078 /**
   1079  * Check if @a template_contract is valid.
   1080  *
   1081  * @param template_contract template contract to validate
   1082  * @return true if @a template_contract is valid
   1083  */
   1084 bool
   1085 TALER_MERCHANT_template_contract_valid (const json_t *template_contract);
   1086 
   1087 
   1088 /**
   1089  * Convert critical operation enumeration value to string
   1090  * suitable for human readers.
   1091  *
   1092  * @param co input to convert
   1093  * @return operation value as string
   1094  */
   1095 const char *
   1096 TALER_MERCHANT_MFA_co2s (
   1097   enum TALER_MERCHANT_MFA_CriticalOperation co);
   1098 
   1099 
   1100 /**
   1101  * Convert critical operation enumeration value to string.
   1102  *
   1103  * @param co input to convert
   1104  * @return operation value as string
   1105  */
   1106 const char *
   1107 TALER_MERCHANT_MFA_co_to_string (
   1108   enum TALER_MERCHANT_MFA_CriticalOperation co);
   1109 
   1110 
   1111 /**
   1112  * Convert string to critical operation enumeration value.
   1113  *
   1114  * @param str input to convert
   1115  * @return #TALER_MERCHANT_MFA_CO_NONE on failure
   1116  */
   1117 enum TALER_MERCHANT_MFA_CriticalOperation
   1118 TALER_MERCHANT_MFA_co_from_string (const char *str);
   1119 
   1120 
   1121 /**
   1122  * Convert MFA channel enumeration value to string.
   1123  *
   1124  * @param ch input to convert
   1125  * @return operation value as string
   1126  */
   1127 const char *
   1128 TALER_MERCHANT_MFA_channel_to_string (
   1129   enum TALER_MERCHANT_MFA_Channel ch);
   1130 
   1131 
   1132 /**
   1133  * Convert string to MFA channel enumeration value.
   1134  *
   1135  * @param str input to convert
   1136  * @return #TALER_MERCHANT_MFA_CHANNEL_NONE on failure
   1137  */
   1138 enum TALER_MERCHANT_MFA_Channel
   1139 TALER_MERCHANT_MFA_channel_from_string (const char *str);
   1140 
   1141 
   1142 /**
   1143  * @brief Salted hash of a body for a request that required MFA.
   1144  */
   1145 struct TALER_MERCHANT_MFA_BodyHash
   1146 {
   1147   /**
   1148    * Hash of the body and salt.
   1149    */
   1150   struct GNUNET_ShortHashCode hash;
   1151 };
   1152 
   1153 
   1154 /**
   1155  * @brief Salt used when computing a `struct TALER_MERCHANT_MFA_BodyHash`
   1156  */
   1157 struct TALER_MERCHANT_MFA_BodySalt
   1158 {
   1159   /**
   1160    * Salt.
   1161    */
   1162   uint64_t salt[128 / 64];
   1163 };
   1164 
   1165 
   1166 /**
   1167  * @brief Token used to authorize report generation.
   1168  */
   1169 struct TALER_MERCHANT_ReportToken
   1170 {
   1171   /**
   1172    * Salt.
   1173    */
   1174   uint64_t salt[256 / 64];
   1175 };
   1176 
   1177 
   1178 /**
   1179  * Hash the given request @a body with the given @a salt to
   1180  * produce @a h_body for MFA checks.
   1181  *
   1182  * @param body HTTP request body, NULL if body was empty
   1183  * @param salt salt to use
   1184  * @param h_body resulting hash
   1185  */
   1186 void
   1187 TALER_MERCHANT_mfa_body_hash (
   1188   const json_t *body,
   1189   const struct TALER_MERCHANT_MFA_BodySalt *salt,
   1190   struct TALER_MERCHANT_MFA_BodyHash *h_body);
   1191 
   1192 
   1193 /**
   1194  * Multiply the @a unit_price by the quantity given in @a factor.
   1195  * Round the result using the given rounding mode @a rm to a
   1196  * multiple of the @a atomic_amount.
   1197  *
   1198  * @param[out] result where to store the result
   1199  * @param unit_price price for one item
   1200  * @param factor quantity to purchase, can be factional
   1201  * @param rm rounding mode to apply
   1202  * @param atomic_amount granularity to round to
   1203  * @return #GNUNET_OK on success
   1204  *   #GNUNET_NO on integer overflow (resulting amount cannot be represented)
   1205  *   #GNUNET_SYSERR on internal failure (e.g. currency
   1206  *     @a unit_price and @a atomic_amount do not match)
   1207  */
   1208 enum GNUNET_GenericReturnValue
   1209 TALER_MERCHANT_amount_multiply_by_quantity (
   1210   struct TALER_Amount *result,
   1211   const struct TALER_Amount *unit_price,
   1212   const struct TALER_MERCHANT_ProductQuantity *factor,
   1213   enum TALER_MERCHANT_RoundMode rm,
   1214   const struct TALER_Amount *atomic_amount);
   1215 
   1216 
   1217 /**
   1218  * Details about a product (to be) sold.  This structure represents
   1219  * both the MinimalInventoryProduct and the ProductSold. Note that
   1220  * the two have different mandatory and/or optional fields but are
   1221  * both possible in `struct TALER_MERCHANT_Order`. However, only
   1222  * ProductSold is allowed in the contracts.
   1223  */
   1224 struct TALER_MERCHANT_ProductSold
   1225 {
   1226 
   1227   /**
   1228    * Merchant-internal identifier for the product. NULL for none.
   1229    */
   1230   char *product_id;
   1231 
   1232   /**
   1233    * Name of the product. NULL for pre **v20** contract terms.
   1234    */
   1235   char *product_name;
   1236 
   1237   /**
   1238    * Human-readable product description.
   1239    */
   1240   char *description;
   1241 
   1242   /**
   1243    * Map from IETF BCP 47 language tags to localized descriptions.
   1244    * NULL if no translations are available.
   1245    */
   1246   json_t *description_i18n;
   1247 
   1248   /**
   1249    * Quantity (in multiples of @e unit) to be sold.
   1250    */
   1251   struct TALER_MERCHANT_ProductQuantity unit_quantity;
   1252 
   1253   /**
   1254    * Unit in which the product is measured (liters, kilograms, packages,
   1255    * etc.). Can be NULL if not specified.
   1256    */
   1257   char *unit;
   1258 
   1259   /**
   1260    * Length of the @e prices array.
   1261    */
   1262   unsigned int prices_length;
   1263 
   1264   /**
   1265    * Array of prices (in different currencies) for @e unit_quantity
   1266    * units of this product (these are longer the per-unit prices!).
   1267    */
   1268   struct TALER_Amount *prices;
   1269 
   1270   /**
   1271    * True if the @e prices given are the net price,
   1272    * false if they are the gross price.  Note that even @e prices are the
   1273    * gross price, @e taxes may be missing if the merchant configured
   1274    * gross @e prices but did not configure any @e taxes.
   1275    * Similarly, the merchant may have configured net @e prices
   1276    * for products but deals with taxes on a per-order basis. Thus, it
   1277    * may not always be possible to compute the gross price from the net
   1278    * price for an individual product, necessitating this flag.
   1279    */
   1280   bool prices_are_net;
   1281 
   1282   /**
   1283    * An optional base64-encoded image of the product.
   1284    */
   1285   char *image;
   1286 
   1287   /**
   1288    * A list of taxes paid by the merchant for this product. Can be NULL.
   1289    * Will likely change soon!
   1290    */
   1291   json_t *taxes;
   1292 
   1293   /**
   1294    * Time indicating when this product should be delivered.
   1295    * #GNUNET_TIME_UNIT_FOREVER_TS for unknown / not specified.
   1296    */
   1297   struct GNUNET_TIME_Timestamp delivery_date;
   1298 
   1299   /**
   1300    * Money pot to use for this product, overrides value from
   1301    * the inventory if given.  Not useful to wallets, only for
   1302    * merchant-internal accounting.
   1303    */
   1304   uint64_t product_money_pot;
   1305 
   1306 };
   1307 
   1308 
   1309 /**
   1310  * Merchant information.
   1311  */
   1312 struct TALER_MERCHANT_MetaData
   1313 {
   1314   /**
   1315    * Legal name of the instance
   1316    */
   1317   char *name;
   1318 
   1319   /**
   1320    * Merchant's site url
   1321    */
   1322   char *website;
   1323 
   1324   /**
   1325    * Email contact for customers
   1326    */
   1327   char *email;
   1328 
   1329   /**
   1330    * merchant's logo data uri
   1331    */
   1332   char *logo;
   1333 
   1334   /**
   1335    * Merchant address
   1336    */
   1337   json_t *address;
   1338 
   1339   /**
   1340    * Jurisdiction of the business
   1341    */
   1342   json_t *jurisdiction;
   1343 
   1344 };
   1345 
   1346 
   1347 /**
   1348  * Struct to hold terms common to both orders and contracts and
   1349  * are basically present (or optional) all the time.
   1350  */
   1351 struct TALER_MERCHANT_ContractBaseTerms
   1352 {
   1353 
   1354   /**
   1355    * Version of the contract terms.
   1356    */
   1357   enum TALER_MERCHANT_ContractVersion version;
   1358 
   1359   /**
   1360    * Summary of the contract.
   1361    */
   1362   char *summary;
   1363 
   1364   /**
   1365    * Internationalized summary. Optional.
   1366    */
   1367   json_t *summary_i18n;
   1368 
   1369   /**
   1370    * URL where the same contract could be ordered again (if available).
   1371    * Optional.
   1372    */
   1373   char *public_reorder_url;
   1374 
   1375   /**
   1376    * URL that will show that the contract was successful
   1377    * after it has been paid for.
   1378    * Optional.
   1379    */
   1380   char *fulfillment_url;
   1381 
   1382   /**
   1383    * Message shown to the customer after paying for the contract.
   1384    * Either fulfillment_url or fulfillment_message must be specified.
   1385    * Optional.
   1386    */
   1387   char *fulfillment_message;
   1388 
   1389   /**
   1390    * Map from IETF BCP 47 language tags to localized fulfillment messages.
   1391    * Optional.
   1392    */
   1393   json_t *fulfillment_message_i18n;
   1394 
   1395   /**
   1396    * Delivery location.
   1397    * Optional.
   1398    */
   1399   json_t *delivery_location;
   1400 
   1401   /**
   1402    * Delivery date.
   1403    * Optional.
   1404    */
   1405   struct GNUNET_TIME_Timestamp delivery_date;
   1406 
   1407   /**
   1408    * Specifies for how long the wallet should try to get an
   1409    * automatic refund for the purchase.
   1410    * Optional, zero if not applicable.
   1411    */
   1412   struct GNUNET_TIME_Relative auto_refund;
   1413 
   1414   /**
   1415    * Extra data that is only interpreted by the merchant frontend.
   1416    */
   1417   json_t *extra;
   1418 
   1419   /**
   1420    * Minimum age the buyer must have (in years).
   1421    */
   1422   uint8_t minimum_age;
   1423 
   1424   /**
   1425    * Default money pot to use for this order, applies to the
   1426    * amount remaining that was not claimed by money pots of
   1427    * products or taxes.  Not useful to wallets, only for
   1428    * merchant-internal accounting.  If zero, the remaining
   1429    * account is simply not accounted for in any money pot.
   1430    */
   1431   uint64_t default_money_pot;
   1432 
   1433   /**
   1434    * Latest time until which the good or service may be
   1435    * picked up by the customer. This is usually for digital
   1436    * goods where the customer has a finite window for downloading.
   1437    * Optional. FOREVER if not set.
   1438    */
   1439   struct GNUNET_TIME_Timestamp max_pickup_time;
   1440 
   1441 };
   1442 
   1443 
   1444 /**
   1445  * Struct to hold an order.
   1446  */
   1447 struct TALER_MERCHANT_Order
   1448 {
   1449 
   1450   /**
   1451    * Shared base terms of contracts and orders.
   1452    */
   1453   struct TALER_MERCHANT_ContractBaseTerms *base;
   1454 
   1455   /**
   1456    * Our order ID. Optional.
   1457    */
   1458   char *order_id;
   1459 
   1460   /**
   1461    * Array of products that are part of the purchase.
   1462    */
   1463   struct TALER_MERCHANT_ProductSold *products;
   1464 
   1465   /**
   1466    * Length of the @e products array.
   1467    */
   1468   size_t products_len;
   1469 
   1470   /**
   1471    * Timestamp of the contract.
   1472    * Optional. Set to "now" on parsing if not given.
   1473    */
   1474   struct GNUNET_TIME_Timestamp timestamp;
   1475 
   1476   /**
   1477    * Deadline for refunds.
   1478    * Optional. Set to FOREVER if not given.
   1479    */
   1480   struct GNUNET_TIME_Timestamp refund_deadline;
   1481 
   1482   /**
   1483    * Payment deadline.
   1484    * Optional. Set to zero if not given.
   1485    */
   1486   struct GNUNET_TIME_Timestamp pay_deadline;
   1487 
   1488   /**
   1489    * Wire transfer deadline.
   1490    * Optional. Set to FOREVER if not given.
   1491    */
   1492   struct GNUNET_TIME_Timestamp wire_transfer_deadline;
   1493 
   1494   /**
   1495    * Details depending on the @e base.version.
   1496    */
   1497   union
   1498   {
   1499 
   1500     /**
   1501      * Details for v0 orders.
   1502      */
   1503     struct
   1504     {
   1505 
   1506       /**
   1507        * Price to be paid for the transaction. Could be 0. The price is in addition
   1508        * to other instruments, such as rations and tokens.
   1509        * The exchange will subtract deposit fees from that amount
   1510        * before transferring it to the merchant.
   1511        */
   1512       struct TALER_Amount brutto;
   1513 
   1514       /**
   1515        * Tip included by the customer (part of the total amount).
   1516        */
   1517       struct TALER_Amount tip;
   1518 
   1519       /**
   1520        * True if @e tip was not provided.
   1521        */
   1522       bool no_tip;
   1523 
   1524       /**
   1525        * Maximum fee as given by the client request.
   1526        */
   1527       struct TALER_Amount max_fee;
   1528 
   1529       /**
   1530        * True if @e max_fee was not provided.
   1531        */
   1532       bool no_max_fee;
   1533 
   1534     } v0;
   1535 
   1536     /**
   1537      * Details for v1 contracts.
   1538      */
   1539     struct
   1540     {
   1541 
   1542       /**
   1543        * Array of possible specific payment choices the wallet/customer may choose
   1544        * from by selecting the respective index when signing the deposit
   1545 2       * confirmation.
   1546        */
   1547       struct TALER_MERCHANT_OrderChoice *choices;
   1548 
   1549       /**
   1550        * Length of the @e choices array.
   1551        */
   1552       unsigned int choices_len;
   1553 
   1554     } v1;
   1555 
   1556   } details;
   1557 
   1558 };
   1559 
   1560 
   1561 /**
   1562  * Struct to hold proto-contracts.
   1563  */
   1564 struct TALER_MERCHANT_ProtoContract
   1565 {
   1566 
   1567   /**
   1568    * Shared base terms of contracts and orders.
   1569    */
   1570   struct TALER_MERCHANT_ContractBaseTerms *base;
   1571 
   1572   /**
   1573    * Our order ID.
   1574    */
   1575   char *order_id;
   1576 
   1577   /**
   1578    * Timestamp of the contract.
   1579    */
   1580   struct GNUNET_TIME_Timestamp timestamp;
   1581 
   1582   /**
   1583    * Deadline for refunds.
   1584    */
   1585   struct GNUNET_TIME_Timestamp refund_deadline;
   1586 
   1587   /**
   1588    * Payment deadline.
   1589    */
   1590   struct GNUNET_TIME_Timestamp pay_deadline;
   1591 
   1592   /**
   1593    * Wire transfer deadline.
   1594    */
   1595   struct GNUNET_TIME_Timestamp wire_deadline;
   1596 
   1597   /**
   1598    * Merchant public key.
   1599    */
   1600   struct TALER_MerchantPublicKeyP merchant_pub;
   1601 
   1602   /**
   1603    * Merchant base URL.
   1604    */
   1605   char *merchant_base_url;
   1606 
   1607   /**
   1608    * Metadata about the merchant.
   1609    */
   1610   struct TALER_MERCHANT_MetaData merchant;
   1611 
   1612   /**
   1613    * Array of products that are part of the purchase.
   1614    */
   1615   struct TALER_MERCHANT_ProductSold *products;
   1616 
   1617   /**
   1618    * Length of the @e products array.
   1619    */
   1620   size_t products_len;
   1621 
   1622   /**
   1623    * The hash of the merchant instance's wire details.
   1624    */
   1625   struct TALER_MerchantWireHashP h_wire;
   1626 
   1627   /**
   1628    * Wire transfer method identifier for the wire method associated with
   1629    * @e h_wire.
   1630    */
   1631   char *wire_method;
   1632 
   1633   /**
   1634    * Exchanges that the merchant accepts even if it does not accept any auditors that audit them.
   1635    * TODO: appropriate type
   1636    */
   1637   json_t *exchanges;
   1638 
   1639   /**
   1640    * Details depending on the @e version.
   1641    */
   1642   union
   1643   {
   1644 
   1645     /**
   1646      * Details for v0 contracts.
   1647      */
   1648     struct
   1649     {
   1650 
   1651       /**
   1652        * Price to be paid for the transaction. Could be 0. The price is in addition
   1653        * to other instruments, such as rations and tokens.
   1654        * The exchange will subtract deposit fees from that amount
   1655        * before transferring it to the merchant.
   1656        */
   1657       struct TALER_Amount brutto;
   1658 
   1659       /**
   1660        * Tip included by the customer (part of the total amount).
   1661        */
   1662       struct TALER_Amount tip;
   1663 
   1664       /**
   1665        * True if @e tip was not provided.
   1666        */
   1667       bool no_tip;
   1668 
   1669       /**
   1670        * Maximum fee as given by the client request.
   1671        */
   1672       struct TALER_Amount max_fee;
   1673 
   1674     } v0;
   1675 
   1676     /**
   1677      * Details for v1 contracts.
   1678      */
   1679     struct
   1680     {
   1681 
   1682       /**
   1683        * Array of possible specific contracts the wallet/customer may choose
   1684        * from by selecting the respective index when signing the deposit
   1685        * confirmation.
   1686        */
   1687       struct TALER_MERCHANT_ContractChoice *choices;
   1688 
   1689       /**
   1690        * Length of the @e choices array.
   1691        */
   1692       unsigned int choices_len;
   1693 
   1694       /**
   1695        * Array of token authorities.
   1696        */
   1697       struct TALER_MERCHANT_ContractTokenFamily *token_authorities;
   1698 
   1699       /**
   1700        * Length of the @e token_authorities array.
   1701        */
   1702       unsigned int token_authorities_len;
   1703 
   1704     } v1;
   1705 
   1706   } details;
   1707 
   1708 };
   1709 
   1710 
   1711 /**
   1712  * Struct to hold contract terms.
   1713  */
   1714 struct TALER_MERCHANT_Contract
   1715 {
   1716 
   1717   /**
   1718    * The proto-contract.
   1719    */
   1720   struct TALER_MERCHANT_ProtoContract *pc;
   1721 
   1722   /**
   1723    * Nonce generated by the wallet and echoed by the merchant
   1724    * in this field when the proposal is generated.
   1725    */
   1726   char *nonce;
   1727 
   1728 };
   1729 
   1730 
   1731 /**
   1732  * Provide specification to parse an JSON contract input type.
   1733  * The value is provided as a descriptive string.
   1734  *
   1735  * @param name name of the JSON member with the contract type
   1736  * @param[out] cit where to store the contract input type
   1737  * @return spec for parsing a contract input type
   1738  */
   1739 struct GNUNET_JSON_Specification
   1740 TALER_MERCHANT_json_spec_cit (
   1741   const char *name,
   1742   enum TALER_MERCHANT_ContractInputType *cit);
   1743 
   1744 
   1745 /**
   1746  * Create JSON specification to parse a merchant contract
   1747  * version.
   1748  *
   1749  * @param name name of the field
   1750  * @param[out] version where to write the contract version
   1751  * @return JSON specification object
   1752  */
   1753 struct GNUNET_JSON_Specification
   1754 TALER_MERCHANT_spec_contract_version (
   1755   const char *name,
   1756   enum TALER_MERCHANT_ContractVersion *version);
   1757 
   1758 
   1759 /**
   1760  * Provide specification to parse given JSON object to merchant details in the
   1761  * order terms. All fields from @a order are copied.
   1762  *
   1763  * @param name name of the merchant details field in the JSON
   1764  * @param[out] order where the merchant details have to be written
   1765  */
   1766 struct GNUNET_JSON_Specification
   1767 TALER_MERCHANT_spec_merchant_details (
   1768   const char *name,
   1769   struct TALER_MERCHANT_MetaData *merchant);
   1770 
   1771 
   1772 /**
   1773  * Get JSON representation of merchant details.
   1774  *
   1775  * @param[in] merchant metadata to serialize
   1776  * @return JSON object with merchant details; NULL on error
   1777  */
   1778 json_t *
   1779 TALER_MERCHANT_metadata_to_json (
   1780   const struct TALER_MERCHANT_MetaData *merchant);
   1781 
   1782 
   1783 /**
   1784  * Free representation of merchant details.
   1785  *
   1786  * @param[in,out] merchant metadata to release, excluding the pointer itself
   1787  */
   1788 void
   1789 TALER_MERCHANT_metadata_free (
   1790   struct TALER_MERCHANT_MetaData *merchant);
   1791 
   1792 
   1793 /**
   1794  * Provide specification to parse given JSON array to order
   1795  * choices. All fields from @a choices elements are copied.
   1796  *
   1797  * @param name name of the choices field in the JSON
   1798  * @param[out] choices where the order choices array has to be written
   1799  * @param[out] choices_len length of the @a choices array
   1800  */
   1801 struct GNUNET_JSON_Specification
   1802 TALER_MERCHANT_spec_order_choices (
   1803   const char *name,
   1804   struct TALER_MERCHANT_OrderChoice **choices,
   1805   unsigned int *choices_len);
   1806 
   1807 
   1808 /**
   1809  * Provide specification to parse given JSON array to contract terms
   1810  * choices. All fields from @a choices elements are copied.
   1811  *
   1812  * @param name name of the choices field in the JSON
   1813  * @param[out] choices where the contract choices array has to be written
   1814  * @param[out] choices_len length of the @a choices array
   1815  */
   1816 struct GNUNET_JSON_Specification
   1817 TALER_MERCHANT_spec_contract_choices (
   1818   const char *name,
   1819   struct TALER_MERCHANT_ContractChoice **choices,
   1820   unsigned int *choices_len);
   1821 
   1822 
   1823 /**
   1824  * Provide specification to parse given JSON array to token families in the
   1825  * contract terms. All fields from @a families items are copied.
   1826  *
   1827  * @param name name of the token families field in the JSON
   1828  * @param[out] families where the token families array has to be written
   1829  * @param[out] families_len length of the @a families array
   1830  */
   1831 struct GNUNET_JSON_Specification
   1832 TALER_MERCHANT_spec_token_families (
   1833   const char *name,
   1834   struct TALER_MERCHANT_ContractTokenFamily **families,
   1835   unsigned int *families_len);
   1836 
   1837 
   1838 /**
   1839  * Check if @a taxes is an array of valid Taxes in the sense of
   1840  * Taler's API definition.
   1841  *
   1842  * @param taxes array to check
   1843  * @return true if @a taxes is an array and all
   1844  *         entries are valid Taxes.
   1845  */
   1846 bool
   1847 TALER_MERCHANT_taxes_array_valid (
   1848   const json_t *taxes);
   1849 
   1850 
   1851 /**
   1852  * Provide specification to parse an JSON contract output type.
   1853  * The value is provided as a descriptive string.
   1854  *
   1855  * @param name name of the JSON member with the contract type
   1856  * @param[out] cot where to store the contract output type
   1857  * @return spec for parsing a contract output type
   1858  */
   1859 struct GNUNET_JSON_Specification
   1860 TALER_MERCHANT_json_spec_cot (
   1861   const char *name,
   1862   enum TALER_MERCHANT_ContractOutputType *cot);
   1863 
   1864 
   1865 /**
   1866  * Get JSON representation of contract choice.
   1867  *
   1868  * @param[in] choice contract choice to serialize
   1869  * @param order whether @a choice is contained in order or contract terms
   1870  * @return JSON representation of @a choice; NULL on error
   1871  */
   1872 json_t *
   1873 TALER_MERCHANT_json_from_contract_choice (
   1874   const struct TALER_MERCHANT_ContractChoice *choice);
   1875 
   1876 
   1877 /**
   1878  * Free all the fields in the given @a choice, but not @a choice itself, since
   1879  * it is normally part of an array.
   1880  *
   1881  * @param[in] choice contract terms choice to free
   1882  */
   1883 void
   1884 TALER_MERCHANT_contract_choice_free (
   1885   struct TALER_MERCHANT_ContractChoice *choice);
   1886 
   1887 
   1888 /**
   1889  * Get JSON representation of an order choice.
   1890  *
   1891  * @param[in] choice contract choice to serialize
   1892  * @return JSON representation of @a choice; NULL on error
   1893  */
   1894 json_t *
   1895 TALER_MERCHANT_json_from_order_choice (
   1896   const struct TALER_MERCHANT_OrderChoice *choice);
   1897 
   1898 
   1899 /**
   1900  * Free all the fields in the given @a choice, but not @a choice itself, since
   1901  * it is normally part of an array.
   1902  *
   1903  * @param[in] choice contract terms choice to free
   1904  */
   1905 void
   1906 TALER_MERCHANT_order_choice_free (
   1907   struct TALER_MERCHANT_OrderChoice *choice);
   1908 
   1909 
   1910 /**
   1911  * Get JSON representation of contract token family.
   1912  *
   1913  * @param[in] family contract token family to serialize
   1914  * @return JSON representation of @a family; NULL on error
   1915  */
   1916 json_t *
   1917 TALER_MERCHANT_json_from_token_family (
   1918   const struct TALER_MERCHANT_ContractTokenFamily *family);
   1919 
   1920 
   1921 /**
   1922  * Find token family in contract terms from slug and validity date.
   1923  *
   1924  * @param slug slug of the token family
   1925  * @param valid_after validity start of the token family
   1926  * @param[in] families array of token families in the contract terms
   1927  * @param families_len length of @a families array
   1928  * @param[out] family matching token family; NULL if no result;
   1929  *   the pointer returned will be an alias into @e families
   1930  * @param[out] key key of matching token family; NULL if no result
   1931  * @return #GNUNET_SYSERR if no matching family found; #GNUNET_OK otherwise
   1932  */
   1933 enum GNUNET_GenericReturnValue
   1934 TALER_MERCHANT_find_token_family_key (
   1935   const char *slug,
   1936   struct GNUNET_TIME_Timestamp valid_after,
   1937   const struct TALER_MERCHANT_ContractTokenFamily *families,
   1938   unsigned int families_len,
   1939   struct TALER_MERCHANT_ContractTokenFamily *family,
   1940   struct TALER_MERCHANT_ContractTokenFamilyKey *key);
   1941 
   1942 
   1943 /**
   1944  * Free all the fields in the given @a family, but not @a family itself, since
   1945  * it is normally part of an array.
   1946  *
   1947  * @param[in] family contract token family to free
   1948  */
   1949 void
   1950 TALER_MERCHANT_contract_token_family_free (
   1951   struct TALER_MERCHANT_ContractTokenFamily *family);
   1952 
   1953 
   1954 /**
   1955  * Parse JSON product given in @a p, returning the result in
   1956  * @a r.
   1957  *
   1958  * @param p JSON specifying a ``ProductSold`` to parse
   1959  * @param[out] r where to write the result
   1960  * @param allow_mip true to allow for MinimalInventoryProduct
   1961  * @return #GNUNET_OK on success
   1962  */
   1963 enum GNUNET_GenericReturnValue
   1964 TALER_MERCHANT_parse_product_sold (
   1965   const json_t *p,
   1966   struct TALER_MERCHANT_ProductSold *r,
   1967   bool allow_mip);
   1968 
   1969 
   1970 /**
   1971  * Serialize @a p to JSON.
   1972  *
   1973  * @param p product to serialize
   1974  * @return JSON object representing the product @a p
   1975  */
   1976 json_t *
   1977 TALER_MERCHANT_product_sold_serialize (
   1978   const struct TALER_MERCHANT_ProductSold *p);
   1979 
   1980 
   1981 /**
   1982  * Release memory inside of @a product, but not @a product itself.
   1983  *
   1984  * @param[in] product data structure to clean up
   1985  */
   1986 void
   1987 TALER_MERCHANT_product_sold_free (
   1988   struct TALER_MERCHANT_ProductSold *product);
   1989 
   1990 
   1991 /**
   1992  * Parse base terms (of orders and contracts) in @a input.
   1993  *
   1994  * @param[in] input JSON object containing contract terms
   1995  * @return NULL if @a input is malformed
   1996  */
   1997 struct TALER_MERCHANT_ContractBaseTerms *
   1998 TALER_MERCHANT_base_terms_parse (
   1999   json_t *input);
   2000 
   2001 
   2002 /**
   2003  * Serialize contract base terms into JSON object.
   2004  *
   2005  * @param[in] ct base contract terms to serialize
   2006  * @return JSON representation of @a ct; NULL on error
   2007  */
   2008 json_t *
   2009 TALER_MERCHANT_base_terms_serialize (
   2010   const struct TALER_MERCHANT_ContractBaseTerms *ct);
   2011 
   2012 
   2013 /**
   2014  * Release memory in @a ct
   2015  *
   2016  * @param[in] ct common terms to clean up
   2017  */
   2018 void
   2019 TALER_MERCHANT_base_terms_free (
   2020   struct TALER_MERCHANT_ContractBaseTerms *ct);
   2021 
   2022 
   2023 /**
   2024  * Parse JSON order in @a input.
   2025  *
   2026  * @param[in] input JSON object containing an order
   2027  * @return parsed contract terms; NULL if @a input is malformed
   2028  */
   2029 struct TALER_MERCHANT_Order *
   2030 TALER_MERCHANT_order_parse (
   2031   json_t *input);
   2032 
   2033 
   2034 #if NOT_NEEDED_YET
   2035 /**
   2036  * Serialize order into JSON object.
   2037  *
   2038  * @param[in] input contract terms to serialize
   2039  * @param nonce_optional whether `nonce' field is optional
   2040  * @return JSON representation of @a input; NULL on error
   2041  */
   2042 json_t *
   2043 TALER_MERCHANT_order_serialize (
   2044   const struct TALER_MERCHANT_Order *input);
   2045 
   2046 #endif
   2047 
   2048 
   2049 /**
   2050  * Free the @a order and all fields in it.
   2051  *
   2052  * @param[in] order order to free
   2053  */
   2054 void
   2055 TALER_MERCHANT_order_free (
   2056   struct TALER_MERCHANT_Order *order);
   2057 
   2058 
   2059 /**
   2060  * Parse JSON proto contract terms in @a input.
   2061  *
   2062  * @param[in] input JSON object containing contract terms
   2063  * @return NULL on failure
   2064  */
   2065 struct TALER_MERCHANT_ProtoContract *
   2066 TALER_MERCHANT_proto_contract_parse (
   2067   json_t *input);
   2068 
   2069 
   2070 /**
   2071  * Serialize proto contract into JSON object.
   2072  *
   2073  * @param[in] pc proto contract to serialize
   2074  * @return JSON representation of @a pc; NULL on error
   2075  */
   2076 json_t *
   2077 TALER_MERCHANT_proto_contract_serialize (
   2078   const struct TALER_MERCHANT_ProtoContract *pc);
   2079 
   2080 
   2081 /**
   2082  * Free the proto-contract at @a pc
   2083  *
   2084  * @param[in] pc proto-contract to free
   2085  */
   2086 void
   2087 TALER_MERCHANT_proto_contract_free (
   2088   struct TALER_MERCHANT_ProtoContract *pc);
   2089 
   2090 
   2091 /**
   2092  * Parse JSON contract terms in @a input.
   2093  *
   2094  * @param[in] input JSON object containing contract terms
   2095  * @return parsed contract terms; NULL if @a input is malformed
   2096  */
   2097 struct TALER_MERCHANT_Contract *
   2098 TALER_MERCHANT_contract_parse (
   2099   json_t *input);
   2100 
   2101 
   2102 /**
   2103  * Serialize contract terms into JSON object.
   2104  *
   2105  * @param[in] input contract terms to serialize
   2106  * @return JSON representation of @a input; NULL on error
   2107  */
   2108 json_t *
   2109 TALER_MERCHANT_contract_serialize (
   2110   const struct TALER_MERCHANT_Contract *input);
   2111 
   2112 
   2113 /**
   2114  * Free the @a contract and all fields in it.
   2115  *
   2116  * @param[in] contract contract to free
   2117  */
   2118 void
   2119 TALER_MERCHANT_contract_free (
   2120   struct TALER_MERCHANT_Contract *contract);
   2121 
   2122 
   2123 #endif