merchant

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

taler-merchant-httpd_post-orders-ORDER_ID-pay.c (181041B)


      1 /*
      2    This file is part of TALER
      3    (C) 2014-2026 Taler Systems SA
      4 
      5    TALER is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU Affero General Public License as
      7    published by the Free Software Foundation; either version 3,
      8    or (at your option) any later version.
      9 
     10    TALER is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public
     16    License along with TALER; see the file COPYING.  If not,
     17    see <http://www.gnu.org/licenses/>
     18  */
     19 
     20 /**
     21  * @file src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
     22  * @brief handling of POST /orders/$ID/pay requests
     23  * @author Marcello Stanisci
     24  * @author Christian Grothoff
     25  * @author Florian Dold
     26  */
     27 #include "platform.h"
     28 struct ExchangeGroup;
     29 #define TALER_EXCHANGE_POST_BATCH_DEPOSIT_RESULT_CLOSURE struct ExchangeGroup
     30 #include <gnunet/gnunet_common.h>
     31 #include <gnunet/gnunet_db_lib.h>
     32 #include <gnunet/gnunet_json_lib.h>
     33 #include <gnunet/gnunet_time_lib.h>
     34 #include <jansson.h>
     35 #include <microhttpd.h>
     36 #include <stddef.h>
     37 #include <stdint.h>
     38 #include <string.h>
     39 #include <taler/taler_dbevents.h>
     40 #include <taler/taler_error_codes.h>
     41 #include <taler/taler_signatures.h>
     42 #include <taler/taler_json_lib.h>
     43 #include <taler/taler_exchange_service.h>
     44 #include "taler-merchant-httpd.h"
     45 #include "taler-merchant-httpd_exchanges.h"
     46 #include "taler-merchant-httpd_get-exchanges.h"
     47 #include "taler-merchant-httpd_helper.h"
     48 #include "taler-merchant-httpd_post-orders-ORDER_ID-pay.h"
     49 #include "taler-merchant-httpd_get-private-orders.h"
     50 #include "taler/taler_merchant_util.h"
     51 #include "merchantdb_lib.h"
     52 #include <donau/donau_service.h>
     53 #include <donau/donau_util.h>
     54 #include <donau/donau_json_lib.h>
     55 #include "merchant-database/update_money_pot_totals.h"
     56 #include "merchant-database/insert_deposit.h"
     57 #include "merchant-database/insert_deposit_confirmation.h"
     58 #include "merchant-database/insert_issued_token.h"
     59 #include "merchant-database/insert_order_token_blinded_sig.h"
     60 #include "merchant-database/insert_used_token.h"
     61 #include "merchant-database/get_contract_terms_pos.h"
     62 #include "merchant-database/get_contract_terms_status.h"
     63 #include "merchant-database/iterate_deposits.h"
     64 #include "merchant-database/iterate_deposits_by_order.h"
     65 #include "merchant-database/get_donau_instance_by_url.h"
     66 #include "merchant-database/iterate_refunds.h"
     67 #include "merchant-database/set_instance.h"
     68 #include "merchant-database/iterate_used_tokens_by_order.h"
     69 #include "merchant-database/get_token_family_key.h"
     70 #include "merchant-database/update_to_contract_terms_paid.h"
     71 #include "merchant-database/iterate_order_token_blinded_sigs.h"
     72 #include "merchant-database/start.h"
     73 #include "merchant-database/preflight.h"
     74 #include "merchant-database/event_notify.h"
     75 #include "merchant-database/update_donau_instance_receipts_amount.h"
     76 
     77 /**
     78  * How often do we retry the (complex!) database transaction?
     79  */
     80 #define MAX_RETRIES 5
     81 
     82 /**
     83  * Maximum number of coins that we allow per transaction.
     84  * Note that the limit for each batch deposit request to
     85  * the exchange is lower, so we may break a very large
     86  * number of coins up into multiple smaller requests to
     87  * the exchange.
     88  */
     89 #define MAX_COIN_ALLOWED_COINS 1024
     90 
     91 /**
     92  * Maximum number of tokens that we allow as inputs per transaction
     93  */
     94 #define MAX_TOKEN_ALLOWED_INPUTS 64
     95 
     96 /**
     97  * Maximum number of tokens that we allow as outputs per transaction
     98  */
     99 #define MAX_TOKEN_ALLOWED_OUTPUTS 64
    100 
    101 /**
    102  * How often do we ask the exchange again about our
    103  * KYC status? Very rarely, as if the user actively
    104  * changes it, we should usually notice anyway.
    105  */
    106 #define KYC_RETRY_FREQUENCY GNUNET_TIME_UNIT_WEEKS
    107 
    108 /**
    109  * Information we keep for an individual call to the pay handler.
    110  */
    111 struct PayContext;
    112 
    113 
    114 /**
    115  * Different phases of processing the /pay request.
    116  */
    117 enum PayPhase
    118 {
    119   /**
    120    * Initial phase where the request is parsed.
    121    */
    122   PP_PARSE_PAY = 0,
    123 
    124   /**
    125    * Parse wallet data object from the pay request.
    126    */
    127   PP_PARSE_WALLET_DATA,
    128 
    129   /**
    130    * Check database state for the given order.
    131    */
    132   PP_CHECK_CONTRACT,
    133 
    134   /**
    135    * Validate provided tokens and token envelopes.
    136    */
    137   PP_VALIDATE_TOKENS,
    138 
    139   /**
    140    * Check if contract has been paid.
    141    */
    142   PP_CONTRACT_PAID,
    143 
    144   /**
    145    * Compute money pot changes.
    146    */
    147   PP_COMPUTE_MONEY_POTS,
    148 
    149   /**
    150    * Execute payment transaction.
    151    */
    152   PP_PAY_TRANSACTION,
    153 
    154   /**
    155    * Communicate with DONAU to generate a donation receipt from the donor BUDIs.
    156    */
    157   PP_REQUEST_DONATION_RECEIPT,
    158 
    159   /**
    160    * Process the donation receipt response from DONAU (save the donau_sigs to the db).
    161    */
    162   PP_FINAL_OUTPUT_TOKEN_PROCESSING,
    163 
    164   /**
    165    * Notify other processes about successful payment.
    166    */
    167   PP_PAYMENT_NOTIFICATION,
    168 
    169   /**
    170    * Create final success response.
    171    */
    172   PP_SUCCESS_RESPONSE,
    173 
    174   /**
    175    * Perform batch deposits with exchange(s).
    176    */
    177   PP_BATCH_DEPOSITS,
    178 
    179   /**
    180    * Return response in payment context.
    181    */
    182   PP_RETURN_RESPONSE,
    183 
    184   /**
    185    * An exchange denied a deposit, fail for
    186    * legal reasons.
    187    */
    188   PP_FAIL_LEGAL_REASONS,
    189 
    190   /**
    191    * Return #MHD_YES to end processing.
    192    */
    193   PP_END_YES,
    194 
    195   /**
    196    * Return #MHD_NO to end processing.
    197    */
    198   PP_END_NO
    199 };
    200 
    201 
    202 /**
    203  * Information kept during a pay request for each coin.
    204  */
    205 struct DepositConfirmation
    206 {
    207 
    208   /**
    209    * Reference to the main PayContext
    210    */
    211   struct PayContext *pc;
    212 
    213   /**
    214    * URL of the exchange that issued this coin.
    215    */
    216   char *exchange_url;
    217 
    218   /**
    219    * Details about the coin being deposited.
    220    */
    221   struct TALER_EXCHANGE_CoinDepositDetail cdd;
    222 
    223   /**
    224    * Fee charged by the exchange for the deposit operation of this coin.
    225    */
    226   struct TALER_Amount deposit_fee;
    227 
    228   /**
    229    * Fee charged by the exchange for the refund operation of this coin.
    230    */
    231   struct TALER_Amount refund_fee;
    232 
    233   /**
    234    * Fee charged by the exchange for the wire transfer.
    235    */
    236   struct TALER_Amount wire_fee;
    237 
    238   /**
    239    * If a minimum age was required (i. e. pc->minimum_age is large enough),
    240    * this is the signature of the minimum age (as a single uint8_t), using the
    241    * private key to the corresponding age group.  Might be all zeroes for no
    242    * age attestation.
    243    */
    244   struct TALER_AgeAttestationP minimum_age_sig;
    245 
    246   /**
    247    * If a minimum age was required (i. e. pc->minimum_age is large enough),
    248    * this is the age commitment (i. e. age mask and vector of EdDSA public
    249    * keys, one per age group) that went into the mining of the coin.  The
    250    * SHA256 hash of the mask and the vector of public keys was bound to the
    251    * key.
    252    */
    253   struct TALER_AgeCommitment age_commitment;
    254 
    255   /**
    256    * Age mask in the denomination that defines the age groups.  Only
    257    * applicable, if minimum age was required.
    258    */
    259   struct TALER_AgeMask age_mask;
    260 
    261   /**
    262    * Offset of this coin into the `dc` array of all coins in the
    263    * @e pc.
    264    */
    265   unsigned int index;
    266 
    267   /**
    268    * true, if no field "age_commitment" was found in the JSON blob
    269    */
    270   bool no_age_commitment;
    271 
    272   /**
    273    * True, if no field "minimum_age_sig" was found in the JSON blob
    274    */
    275   bool no_minimum_age_sig;
    276 
    277   /**
    278    * true, if no field "h_age_commitment" was found in the JSON blob
    279    */
    280   bool no_h_age_commitment;
    281 
    282   /**
    283    * true if we found this coin in the database.
    284    */
    285   bool found_in_db;
    286 
    287   /**
    288    * true if we #deposit_paid_check() matched this coin in the database.
    289    */
    290   bool matched_in_db;
    291 
    292   /**
    293    * true if #deposit_paid_check() found a deposit of this coin for
    294    * this order in the database, regardless of the exchange URL and
    295    * amount given by the client.  Such a coin must never be offered
    296    * a refund in the "already paid" response, as the refund would
    297    * be honored by the exchange against the existing deposit.
    298    */
    299   bool deposited_in_db;
    300 
    301   /**
    302    * True if this coin is in the current batch.
    303    */
    304   bool in_batch;
    305 
    306 };
    307 
    308 struct TokenUseConfirmation
    309 {
    310 
    311   /**
    312    * Signature on the deposit request made using the token use private key.
    313    */
    314   struct TALER_TokenUseSignatureP sig;
    315 
    316   /**
    317    * Token use public key. This key was blindly signed by the merchant during
    318    * the token issuance process.
    319    */
    320   struct TALER_TokenUsePublicKeyP pub;
    321 
    322   /**
    323    * Unblinded signature on the token use public key done by the merchant.
    324    */
    325   struct TALER_TokenIssueSignature unblinded_sig;
    326 
    327   /**
    328    * Hash of the token issue public key associated with this token.
    329    * Note this is set in the validate_tokens phase.
    330    */
    331   struct TALER_TokenIssuePublicKeyHashP h_issue;
    332 
    333   /**
    334    * true if we found this token in the database.
    335    */
    336   bool found_in_db;
    337 
    338 };
    339 
    340 
    341 /**
    342  * Information about a token envelope.
    343  */
    344 struct TokenEnvelope
    345 {
    346 
    347   /**
    348    * Blinded token use public keys waiting to be signed.
    349    */
    350   struct TALER_TokenEnvelope blinded_token;
    351 
    352 };
    353 
    354 
    355 /**
    356  * (Blindly) signed token to be returned to the wallet.
    357  */
    358 struct SignedOutputToken
    359 {
    360 
    361   /**
    362    * Index of the output token that produced this blindly signed token.
    363    */
    364   unsigned int output_index;
    365 
    366   /**
    367    * Blinded token use public keys waiting to be signed.
    368    */
    369   struct TALER_BlindedTokenIssueSignature sig;
    370 
    371   /**
    372    * Hash of token issue public key.
    373    */
    374   struct TALER_TokenIssuePublicKeyHashP h_issue;
    375 
    376 };
    377 
    378 
    379 /**
    380  * Information kept during a pay request for each exchange.
    381  */
    382 struct ExchangeGroup
    383 {
    384 
    385   /**
    386    * Payment context this group is part of.
    387    */
    388   struct PayContext *pc;
    389 
    390   /**
    391    * Handle to the batch deposit operation currently in flight for this
    392    * exchange, NULL when no operation is pending.
    393    */
    394   struct TALER_EXCHANGE_PostBatchDepositHandle *bdh;
    395 
    396   /**
    397    * Handle for operation to lookup /keys (and auditors) from
    398    * the exchange used for this transaction; NULL if no operation is
    399    * pending.
    400    */
    401   struct TMH_EXCHANGES_KeysOperation *fo;
    402 
    403   /**
    404    * URL of the exchange that issued this coin. Aliases
    405    * the exchange URL of one of the coins, do not free!
    406    */
    407   const char *exchange_url;
    408 
    409   /**
    410    * The keys of the exchange.
    411    */
    412   struct TALER_EXCHANGE_Keys *keys;
    413 
    414   /**
    415    * Total deposit amount in this exchange group.
    416    */
    417   struct TALER_Amount total;
    418 
    419   /**
    420    * Wire fee that applies to this exchange for the
    421    * given payment context's wire method.
    422    */
    423   struct TALER_Amount wire_fee;
    424 
    425   /**
    426    * true if we already tried a forced /keys download.
    427    */
    428   bool tried_force_keys;
    429 
    430   /**
    431    * Did this exchange deny the transaction for legal reasons?
    432    */
    433   bool got_451;
    434 };
    435 
    436 
    437 /**
    438  * Information about donau, that can be fetched even
    439  * if the merhchant doesn't support donau
    440  */
    441 struct DonauData
    442 {
    443   /**
    444    * The user-selected Donau URL.
    445    */
    446   char *donau_url;
    447 
    448   /**
    449    * The donation year, as parsed from "year".
    450    */
    451   uint64_t donation_year;
    452 
    453   /**
    454    * The original BUDI key-pairs array from the donor
    455    * to be used for the receipt creation.
    456    */
    457   const json_t *budikeypairs;
    458 };
    459 
    460 /**
    461  * Information we keep for an individual call to the /pay handler.
    462  */
    463 struct PayContext
    464 {
    465 
    466   /**
    467    * Stored in a DLL.
    468    */
    469   struct PayContext *next;
    470 
    471   /**
    472    * Stored in a DLL.
    473    */
    474   struct PayContext *prev;
    475 
    476   /**
    477    * MHD connection to return to
    478    */
    479   struct MHD_Connection *connection;
    480 
    481   /**
    482    * Details about the client's request.
    483    */
    484   struct TMH_HandlerContext *hc;
    485 
    486   /**
    487    * Transaction ID given in @e root.
    488    */
    489   const char *order_id;
    490 
    491   /**
    492    * Response to return, NULL if we don't have one yet.
    493    */
    494   struct MHD_Response *response;
    495 
    496   /**
    497    * Array with @e output_tokens_len signed tokens returned in
    498    * the response to the wallet. This array combines both the
    499    * token family-signed outputs and the donation authority
    500    * outputs.  Each output has a field ``output_index``
    501    * which matches the index into the choice's outputs array.
    502    * The Donau outputs are those where the `output_index` matches
    503    * the @e validate_tokens.donau_output_index.
    504    */
    505   struct SignedOutputToken *output_tokens;
    506 
    507   /**
    508    * Number of output tokens to return in the response.
    509    * Length of the @e output_tokens array.
    510    */
    511   unsigned int output_tokens_len;
    512 
    513   /**
    514    * Counter used to generate the output index in append_output_token_sig().
    515    */
    516   unsigned int output_index_gen;
    517 
    518   /**
    519    * Counter used to generate the output index in append_output_token_sig().
    520    *
    521    * Counts the generated tokens _within_ the current output_index_gen.
    522    */
    523   unsigned int output_token_cnt;
    524 
    525   /**
    526    * HTTP status code to use for the reply, i.e 200 for "OK".
    527    * Special value UINT_MAX is used to indicate hard errors
    528    * (no reply, return #MHD_NO).
    529    */
    530   unsigned int response_code;
    531 
    532   /**
    533    * Payment processing phase we are in.
    534    */
    535   enum PayPhase phase;
    536 
    537   /**
    538    * #GNUNET_NO if the @e connection was not suspended,
    539    * #GNUNET_YES if the @e connection was suspended,
    540    * #GNUNET_SYSERR if @e connection was resumed to as
    541    * part of #MH_force_pc_resume during shutdown.
    542    */
    543   enum GNUNET_GenericReturnValue suspended;
    544 
    545   /**
    546    * Results from the phase_parse_pay()
    547    */
    548   struct
    549   {
    550 
    551     /**
    552      * Array with @e num_exchanges exchanges we are depositing
    553      * coins into.
    554      */
    555     struct ExchangeGroup **egs;
    556 
    557     /**
    558      * Array with @e coins_cnt coins we are despositing.
    559      */
    560     struct DepositConfirmation *dc;
    561 
    562     /**
    563      * Array with @e tokens_cnt input tokens passed to this request.
    564      */
    565     struct TokenUseConfirmation *tokens;
    566 
    567     /**
    568      * Optional session id given in @e root.
    569      * NULL if not given.
    570      */
    571     char *session_id;
    572 
    573     /**
    574      * Wallet data json object from the request. Containing additional
    575      * wallet data such as the selected choice_index.
    576      */
    577     const json_t *wallet_data;
    578 
    579     /**
    580      * Number of coins this payment is made of.  Length
    581      * of the @e dc array.
    582      */
    583     size_t coins_cnt;
    584 
    585     /**
    586      * Number of input tokens passed to this request.  Length
    587      * of the @e tokens array.
    588      */
    589     size_t tokens_cnt;
    590 
    591     /**
    592      * Number of exchanges involved in the payment. Length
    593      * of the @e eg array.
    594      */
    595     unsigned int num_exchanges;
    596 
    597   } parse_pay;
    598 
    599   /**
    600    * Results from the phase_wallet_data()
    601    */
    602   struct
    603   {
    604 
    605     /**
    606      * Array with @e token_envelopes_cnt (blinded) token envelopes.
    607      */
    608     struct TokenEnvelope *token_envelopes;
    609 
    610     /**
    611      * Index of selected choice in the @e contract_terms choices array.
    612      */
    613     int16_t choice_index;
    614 
    615     /**
    616      * Number of token envelopes passed to this request.
    617      * Length of the @e token_envelopes array.
    618      */
    619     size_t token_envelopes_cnt;
    620 
    621     /**
    622      * Hash of the canonicalized wallet data json object.
    623      */
    624     struct GNUNET_HashCode h_wallet_data;
    625 
    626     /**
    627      * Donau related information
    628      */
    629     struct DonauData donau;
    630 
    631     /**
    632      * Serial from the DB of the donau instance that we are using
    633      */
    634     uint64_t donau_instance_serial;
    635 
    636     /**
    637      * Number of the blinded key pairs @e bkps
    638      */
    639     unsigned int num_bkps;
    640 
    641     /**
    642      * Blinded key pairs received from the wallet
    643      */
    644     struct DONAU_BlindedUniqueDonorIdentifierKeyPair *bkps;
    645 
    646     /**
    647      * The id of the charity as saved on the donau.
    648      */
    649     uint64_t charity_id;
    650 
    651     /**
    652      * Private key of the charity(related to the private key of the merchant).
    653      */
    654     struct DONAU_CharityPrivateKeyP charity_priv;
    655 
    656     /**
    657      * Maximum amount of donations that the charity can receive per year.
    658      */
    659     struct TALER_Amount charity_max_per_year;
    660 
    661     /**
    662      * Amount of donations that the charity has received so far this year.
    663      */
    664     struct TALER_Amount charity_receipts_to_date;
    665 
    666     /**
    667      * Donau keys, that we are using to get the information about the bkps.
    668      */
    669     struct DONAU_Keys *donau_keys;
    670 
    671     /**
    672      * Amount from BKPS
    673      */
    674     struct TALER_Amount donation_amount;
    675 
    676   } parse_wallet_data;
    677 
    678   /**
    679    * Results from the phase_check_contract()
    680    */
    681   struct
    682   {
    683 
    684     /**
    685      * Hashed @e contract_terms.
    686      */
    687     struct TALER_PrivateContractHashP h_contract_terms;
    688 
    689     /**
    690      * Our contract (or NULL if not available).
    691      */
    692     json_t *contract_terms_json;
    693 
    694     /**
    695      * Parsed contract terms, NULL when parsing failed.
    696      */
    697     struct TALER_MERCHANT_Contract *contract_terms;
    698 
    699     /**
    700      * What wire method (of the @e mi) was selected by the wallet?
    701      * Set in #phase_parse_pay().
    702      */
    703     struct TMH_WireMethod *wm;
    704 
    705     /**
    706      * Set to the POS key, if applicable for this order.
    707      */
    708     char *pos_key;
    709 
    710     /**
    711      * Challenge to sign, if @e pos_alg is a challenge-signature
    712      * algorithm.
    713      */
    714     struct TALER_PosChallengeP pos_challenge;
    715 
    716     /**
    717      * Serial number of this order in the database (set once we did the lookup).
    718      */
    719     uint64_t order_serial;
    720 
    721     /**
    722      * Algorithm chosen for generating the confirmation code.
    723      */
    724     enum TALER_MerchantConfirmationAlgorithm pos_alg;
    725 
    726   } check_contract;
    727 
    728   /**
    729    * Results from the phase_validate_tokens()
    730    */
    731   struct
    732   {
    733 
    734     /**
    735      * Maximum fee the merchant is willing to pay, from @e root.
    736      * Note that IF the total fee of the exchange is higher, that is
    737      * acceptable to the merchant if the customer is willing to
    738      * pay the difference
    739      * (i.e. amount - max_fee <= actual_amount - actual_fee).
    740      */
    741     struct TALER_Amount max_fee;
    742 
    743     /**
    744      * Amount from @e root.  This is the amount the merchant expects
    745      * to make, minus @e max_fee.
    746      */
    747     struct TALER_Amount brutto;
    748 
    749     /**
    750      * Index of the donau output in the list of tokens.
    751      * Set to -1 if no donau output exists.
    752      */
    753     int donau_output_index;
    754 
    755   } validate_tokens;
    756 
    757 
    758   struct
    759   {
    760     /**
    761      * Length of the @a pots and @a increments arrays.
    762      */
    763     unsigned int num_pots;
    764 
    765     /**
    766      * Serial IDs of money pots to increment.
    767      */
    768     uint64_t *pots;
    769 
    770     /**
    771      * Increment for the respective money pot.
    772      */
    773     struct TALER_Amount *increments;
    774 
    775     /**
    776      * True if the money pots have already been computed.
    777      */
    778     bool pots_computed;
    779 
    780   } compute_money_pots;
    781 
    782   /**
    783    * Results from the phase_execute_pay_transaction()
    784    */
    785   struct
    786   {
    787 
    788     /**
    789      * Considering all the coins with the "found_in_db" flag
    790      * set, what is the total amount we were so far paid on
    791      * this contract?
    792      */
    793     struct TALER_Amount total_paid;
    794 
    795     /**
    796      * Considering all the coins with the "found_in_db" flag
    797      * set, what is the total amount we had to pay in deposit
    798      * fees so far on this contract?
    799      */
    800     struct TALER_Amount total_fees_paid;
    801 
    802     /**
    803      * Considering all the coins with the "found_in_db" flag
    804      * set, what is the total amount we already refunded?
    805      */
    806     struct TALER_Amount total_refunded;
    807 
    808     /**
    809      * Number of coin deposits pending.
    810      */
    811     unsigned int pending;
    812 
    813     /**
    814      * How often have we retried the 'main' transaction?
    815      */
    816     unsigned int retry_counter;
    817 
    818     /**
    819      * Set to true if the deposit currency of a coin
    820      * does not match the contract currency.
    821      */
    822     bool deposit_currency_mismatch;
    823 
    824     /**
    825      * Set to true if the database contains a (bogus)
    826      * refund for a different currency.
    827      */
    828     bool refund_currency_mismatch;
    829 
    830   } pay_transaction;
    831 
    832   /**
    833    * Results from the phase_batch_deposits()
    834    */
    835   struct
    836   {
    837 
    838     /**
    839      * Task called when the (suspended) processing for
    840      * the /pay request times out.
    841      * Happens when we don't get a response from the exchange.
    842      */
    843     struct GNUNET_SCHEDULER_Task *timeout_task;
    844 
    845     /**
    846      * Number of batch transactions pending.
    847      */
    848     unsigned int pending_at_eg;
    849 
    850     /**
    851      * Did any exchange deny a deposit for legal reasons?
    852      */
    853     bool got_451;
    854 
    855   } batch_deposits;
    856 
    857   /**
    858    * Struct for #phase_request_donation_receipt()
    859    */
    860   struct
    861   {
    862     /**
    863      * Handler of the donau request
    864      */
    865     struct DONAU_BatchIssueReceiptHandle *birh;
    866 
    867   } donau_receipt;
    868 };
    869 
    870 
    871 /**
    872  * Head of active pay context DLL.
    873  */
    874 static struct PayContext *pc_head;
    875 
    876 /**
    877  * Tail of active pay context DLL.
    878  */
    879 static struct PayContext *pc_tail;
    880 
    881 
    882 void
    883 TMH_force_pc_resume ()
    884 {
    885   for (struct PayContext *pc = pc_head;
    886        NULL != pc;
    887        pc = pc->next)
    888   {
    889     if (NULL != pc->batch_deposits.timeout_task)
    890     {
    891       GNUNET_SCHEDULER_cancel (pc->batch_deposits.timeout_task);
    892       pc->batch_deposits.timeout_task = NULL;
    893     }
    894     if (GNUNET_YES == pc->suspended)
    895     {
    896       pc->suspended = GNUNET_SYSERR;
    897       MHD_resume_connection (pc->connection);
    898     }
    899   }
    900 }
    901 
    902 
    903 /**
    904  * Resume payment processing.
    905  *
    906  * @param[in,out] pc payment process to resume
    907  */
    908 static void
    909 pay_resume (struct PayContext *pc)
    910 {
    911   GNUNET_assert (GNUNET_YES == pc->suspended);
    912   /* We only ever suspend while we interact with an exchange or the
    913      Donau; thus, once we resume, the timeout for that interaction is
    914      no longer relevant and MUST be cancelled: otherwise it could fire
    915      after we already resumed (and possibly even after we queued the
    916      response) and then hit the "GNUNET_YES == pc->suspended" assertion
    917      in handle_pay_timeout(). */
    918   if (NULL != pc->batch_deposits.timeout_task)
    919   {
    920     GNUNET_SCHEDULER_cancel (pc->batch_deposits.timeout_task);
    921     pc->batch_deposits.timeout_task = NULL;
    922   }
    923   pc->suspended = GNUNET_NO;
    924   MHD_resume_connection (pc->connection);
    925   TALER_MHD_daemon_trigger (); /* we resumed, kick MHD */
    926 }
    927 
    928 
    929 /**
    930  * Resume the given pay context and send the given response.
    931  * Stores the response in the @a pc and signals MHD to resume
    932  * the connection.  Also ensures MHD runs immediately.
    933  *
    934  * @param pc payment context
    935  * @param response_code response code to use
    936  * @param response response data to send back
    937  */
    938 static void
    939 resume_pay_with_response (struct PayContext *pc,
    940                           unsigned int response_code,
    941                           struct MHD_Response *response)
    942 {
    943   if ( (NULL != pc->response) &&
    944        (pc->response != response) )
    945     MHD_destroy_response (pc->response);
    946   pc->response_code = response_code;
    947   pc->response = response;
    948   GNUNET_log (
    949     GNUNET_ERROR_TYPE_INFO,
    950     "Processing /pay in phase %dg: finalizing response with status %u\n",
    951     (int) pc->phase,
    952     response_code);
    953   for (unsigned int i = 0; i<pc->parse_pay.num_exchanges; i++)
    954   {
    955     struct ExchangeGroup *eg = pc->parse_pay.egs[i];
    956 
    957     if (NULL != eg->fo)
    958     {
    959       TMH_EXCHANGES_keys4exchange_cancel (eg->fo);
    960       eg->fo = NULL;
    961       pc->batch_deposits.pending_at_eg--;
    962     }
    963     if (NULL != eg->bdh)
    964     {
    965       TALER_EXCHANGE_post_batch_deposit_cancel (eg->bdh);
    966       eg->bdh = NULL;
    967       pc->batch_deposits.pending_at_eg--;
    968     }
    969   }
    970   GNUNET_assert (0 == pc->batch_deposits.pending_at_eg);
    971   if (NULL != pc->batch_deposits.timeout_task)
    972   {
    973     GNUNET_SCHEDULER_cancel (pc->batch_deposits.timeout_task);
    974     pc->batch_deposits.timeout_task = NULL;
    975   }
    976   pc->phase = PP_RETURN_RESPONSE;
    977   pay_resume (pc);
    978 }
    979 
    980 
    981 /**
    982  * Resume payment processing with an error.
    983  *
    984  * @param pc operation to resume
    985  * @param ec taler error code to return
    986  * @param msg human readable error message
    987  */
    988 static void
    989 resume_pay_with_error (struct PayContext *pc,
    990                        enum TALER_ErrorCode ec,
    991                        const char *msg)
    992 {
    993   resume_pay_with_response (
    994     pc,
    995     TALER_ErrorCode_get_http_status_safe (ec),
    996     TALER_MHD_make_error (ec,
    997                           msg));
    998 }
    999 
   1000 
   1001 /**
   1002  * Conclude payment processing for @a pc with the
   1003  * given @a res MHD status code.
   1004  *
   1005  * @param[in,out] pc payment context for final state transition
   1006  * @param res MHD return code to end with
   1007  */
   1008 static void
   1009 pay_end (struct PayContext *pc,
   1010          enum MHD_Result res)
   1011 {
   1012   pc->phase = (MHD_YES == res)
   1013     ? PP_END_YES
   1014     : PP_END_NO;
   1015 }
   1016 
   1017 
   1018 /**
   1019  * Return response stored in @a pc.
   1020  *
   1021  * @param[in,out] pc payment context we are processing
   1022  */
   1023 static void
   1024 phase_return_response (struct PayContext *pc)
   1025 {
   1026   GNUNET_assert (0 != pc->response_code);
   1027   /* We are *done* processing the request, just queue the response (!) */
   1028   if (UINT_MAX == pc->response_code)
   1029   {
   1030     GNUNET_break (0);
   1031     pay_end (pc,
   1032              MHD_NO); /* hard error */
   1033     return;
   1034   }
   1035   pay_end (pc,
   1036            MHD_queue_response (pc->connection,
   1037                                pc->response_code,
   1038                                pc->response));
   1039 }
   1040 
   1041 
   1042 /**
   1043  * Return a response indicating failure for legal reasons.
   1044  *
   1045  * @param[in,out] pc payment context we are processing
   1046  */
   1047 static void
   1048 phase_fail_for_legal_reasons (struct PayContext *pc)
   1049 {
   1050   json_t *exchanges;
   1051 
   1052   GNUNET_assert (0 == pc->pay_transaction.pending);
   1053   GNUNET_assert (pc->batch_deposits.got_451);
   1054   exchanges = json_array ();
   1055   GNUNET_assert (NULL != exchanges);
   1056   for (unsigned int i = 0; i<pc->parse_pay.num_exchanges; i++)
   1057   {
   1058     struct ExchangeGroup *eg = pc->parse_pay.egs[i];
   1059 
   1060     GNUNET_assert (NULL == eg->fo);
   1061     GNUNET_assert (NULL == eg->bdh);
   1062     if (! eg->got_451)
   1063       continue;
   1064     GNUNET_assert (
   1065       0 ==
   1066       json_array_append_new (
   1067         exchanges,
   1068         json_string (eg->exchange_url)));
   1069   }
   1070   pay_end (pc,
   1071            TALER_MHD_REPLY_JSON_PACK (
   1072              pc->connection,
   1073              MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS,
   1074              TALER_JSON_pack_ec (
   1075                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_EXCHANGE_LEGALLY_REFUSED),
   1076              GNUNET_JSON_pack_array_steal ("exchange_base_urls",
   1077                                            exchanges)));
   1078 }
   1079 
   1080 
   1081 /**
   1082  * Do database transaction for a completed batch deposit.
   1083  *
   1084  * @param eg group that completed
   1085  * @param dr response from the server
   1086  * @return transaction status
   1087  */
   1088 static enum GNUNET_DB_QueryStatus
   1089 batch_deposit_transaction (
   1090   const struct ExchangeGroup *eg,
   1091   const struct TALER_EXCHANGE_PostBatchDepositResponse *dr)
   1092 {
   1093   const struct PayContext *pc = eg->pc;
   1094   enum GNUNET_DB_QueryStatus qs;
   1095   enum TALER_MERCHANTDB_DepositConfirmationStatus dcs;
   1096   uint64_t b_dep_serial;
   1097   uint32_t off = 0;
   1098 
   1099   qs = TALER_MERCHANTDB_set_instance (
   1100     TMH_db,
   1101     pc->hc->instance->settings.id);
   1102   if (qs <= 0)
   1103     return qs; /* failure, we're done */
   1104   dcs = TALER_MERCHANTDB_insert_deposit_confirmation (
   1105     TMH_db,
   1106     pc->hc->instance->settings.id,
   1107     dr->details.ok.deposit_timestamp,
   1108     &pc->check_contract.h_contract_terms,
   1109     eg->exchange_url,
   1110     pc->check_contract.contract_terms->pc->wire_deadline,
   1111     &dr->details.ok.accumulated_total_without_fee,
   1112     &eg->wire_fee,
   1113     &pc->check_contract.wm->h_wire,
   1114     dr->details.ok.exchange_sig,
   1115     dr->details.ok.exchange_pub,
   1116     &b_dep_serial);
   1117   switch (dcs)
   1118   {
   1119   case TALER_MERCHANTDB_DCS_SUCCESS:
   1120     break;
   1121   case TALER_MERCHANTDB_DCS_SOFT_ERROR:
   1122     qs = GNUNET_DB_STATUS_SOFT_ERROR;
   1123     goto cleanup;
   1124   case TALER_MERCHANTDB_DCS_CONFLICT:
   1125   case TALER_MERCHANTDB_DCS_NO_SIGNKEY:
   1126   case TALER_MERCHANTDB_DCS_NO_ACCOUNT:
   1127   case TALER_MERCHANTDB_DCS_NO_ORDER:
   1128   case TALER_MERCHANTDB_DCS_HARD_ERROR:
   1129   case TALER_MERCHANTDB_DCS_NO_RESULTS:
   1130     /* We must NOT commit here: the coins were deposited at the
   1131        exchange, but we failed to persist the deposit confirmation.
   1132        Committing would leave us with a paid order and no deposit
   1133        records at all, which breaks our accounting. Note that this
   1134        is still a VERY bad case: the customer lost their payment,
   1135        and the exchange will pay *somebody*. It really should not
   1136        happen as we should not have accepted an unknown order or
   1137        an account we do not know, etc.; still, best outcome is for
   1138        the wallet to be forced to replay and then hopefully next
   1139        time we succeed... */
   1140     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1141                 "Failed to store deposit confirmation for order `%s' (status %d), failing payment\n",
   1142                 pc->hc->infix,
   1143                 (int) dcs);
   1144     qs = GNUNET_DB_STATUS_HARD_ERROR;
   1145     goto cleanup;
   1146   }
   1147 
   1148   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1149   {
   1150     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1151 
   1152     /* might want to group deposits by batch more explicitly ... */
   1153     if (0 != strcmp (eg->exchange_url,
   1154                      dc->exchange_url))
   1155       continue;
   1156     if (dc->found_in_db)
   1157       continue;
   1158     if (! dc->in_batch)
   1159       continue;
   1160     dc->wire_fee = eg->wire_fee;
   1161     /* FIXME-#9457: We might want to check if the order was fully paid concurrently
   1162        by some other wallet here, and if so, issue an auto-refund. Right now,
   1163        it is possible to over-pay if two wallets literally make a concurrent
   1164        payment, as the earlier check for 'paid' is not in the same transaction
   1165        scope as this 'insert' operation. */
   1166     qs = TALER_MERCHANTDB_insert_deposit (
   1167       TMH_db,
   1168       off++, /* might want to group deposits by batch more explicitly ... */
   1169       b_dep_serial,
   1170       &dc->cdd.coin_pub,
   1171       &dc->cdd.coin_sig,
   1172       &dc->cdd.amount,
   1173       &dc->deposit_fee,
   1174       &dc->refund_fee,
   1175       GNUNET_TIME_absolute_add (
   1176         pc->check_contract.contract_terms->pc->wire_deadline.abs_time,
   1177         GNUNET_TIME_randomize (GNUNET_TIME_UNIT_MINUTES)));
   1178     if (qs < 0)
   1179       goto cleanup;
   1180     GNUNET_break (qs > 0);
   1181   }
   1182 cleanup:
   1183   GNUNET_break (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT ==
   1184                 TALER_MERCHANTDB_set_instance (
   1185                   TMH_db,
   1186                   NULL));
   1187   return qs;
   1188 }
   1189 
   1190 
   1191 /**
   1192  * Handle case where the batch deposit completed
   1193  * with a status of #MHD_HTTP_OK.
   1194  *
   1195  * @param eg group that completed
   1196  * @param dr response from the server
   1197  */
   1198 static void
   1199 handle_batch_deposit_ok (
   1200   struct ExchangeGroup *eg,
   1201   const struct TALER_EXCHANGE_PostBatchDepositResponse *dr)
   1202 {
   1203   struct PayContext *pc = eg->pc;
   1204   enum GNUNET_DB_QueryStatus qs
   1205     = GNUNET_DB_STATUS_SUCCESS_NO_RESULTS;
   1206 
   1207   /* store result to DB */
   1208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   1209               "Storing successful payment %s (%s) at instance `%s'\n",
   1210               pc->hc->infix,
   1211               GNUNET_h2s (&pc->check_contract.h_contract_terms.hash),
   1212               pc->hc->instance->settings.id);
   1213   for (unsigned int r = 0; r<MAX_RETRIES; r++)
   1214   {
   1215     TALER_MERCHANTDB_preflight (TMH_db);
   1216     if (GNUNET_OK !=
   1217         TALER_MERCHANTDB_start (TMH_db,
   1218                                 "batch-deposit-insert-confirmation"))
   1219     {
   1220       resume_pay_with_response (
   1221         pc,
   1222         MHD_HTTP_INTERNAL_SERVER_ERROR,
   1223         TALER_MHD_MAKE_JSON_PACK (
   1224           TALER_JSON_pack_ec (
   1225             TALER_EC_GENERIC_DB_START_FAILED),
   1226           TMH_pack_exchange_reply (&dr->hr)));
   1227       return;
   1228     }
   1229     qs = batch_deposit_transaction (eg,
   1230                                     dr);
   1231     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   1232     {
   1233       TALER_MERCHANTDB_rollback (TMH_db);
   1234       continue;
   1235     }
   1236     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
   1237     {
   1238       GNUNET_break (0);
   1239       resume_pay_with_error (pc,
   1240                              TALER_EC_GENERIC_DB_COMMIT_FAILED,
   1241                              "batch_deposit_transaction");
   1242       TALER_MERCHANTDB_rollback (TMH_db);
   1243       return;
   1244     }
   1245     qs = TALER_MERCHANTDB_commit (TMH_db);
   1246     if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   1247     {
   1248       TALER_MERCHANTDB_rollback (TMH_db);
   1249       continue;
   1250     }
   1251     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
   1252     {
   1253       GNUNET_break (0);
   1254       resume_pay_with_error (pc,
   1255                              TALER_EC_GENERIC_DB_COMMIT_FAILED,
   1256                              "insert_deposit");
   1257     }
   1258     break; /* DB transaction succeeded */
   1259   }
   1260   if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   1261   {
   1262     resume_pay_with_error (pc,
   1263                            TALER_EC_GENERIC_DB_SOFT_FAILURE,
   1264                            "insert_deposit");
   1265     return;
   1266   }
   1267 
   1268   /* Transaction is done, mark affected coins as complete as well. */
   1269   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1270   {
   1271     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1272 
   1273     if (0 != strcmp (eg->exchange_url,
   1274                      dc->exchange_url))
   1275       continue;
   1276     if (dc->found_in_db)
   1277       continue;
   1278     if (! dc->in_batch)
   1279       continue;
   1280     dc->found_in_db = true;     /* well, at least NOW it'd be true ;-) */
   1281     dc->in_batch = false;
   1282     pc->pay_transaction.pending--;
   1283   }
   1284 }
   1285 
   1286 
   1287 /**
   1288  * Notify taler-merchant-kyccheck that we got a KYC
   1289  * rule violation notification and should start to
   1290  * check our KYC status.
   1291  *
   1292  * @param eg exchange group we were notified for
   1293  */
   1294 static void
   1295 notify_kyc_required (const struct ExchangeGroup *eg)
   1296 {
   1297   struct GNUNET_DB_EventHeaderP es = {
   1298     .size = htons (sizeof (es)),
   1299     .type = htons (TALER_DBEVENT_MERCHANT_EXCHANGE_KYC_RULE_TRIGGERED)
   1300   };
   1301   char *hws;
   1302   char *extra;
   1303 
   1304   hws = GNUNET_STRINGS_data_to_string_alloc (
   1305     &eg->pc->check_contract.contract_terms->pc->h_wire,
   1306     sizeof (eg->pc->check_contract.contract_terms->pc->h_wire));
   1307   GNUNET_asprintf (&extra,
   1308                    "%s %s",
   1309                    hws,
   1310                    eg->exchange_url);
   1311   GNUNET_free (hws);
   1312   TALER_MERCHANTDB_event_notify (TMH_db,
   1313                                  &es,
   1314                                  extra,
   1315                                  strlen (extra) + 1);
   1316   GNUNET_free (extra);
   1317 }
   1318 
   1319 
   1320 /**
   1321  * Run batch deposits for @a eg.
   1322  *
   1323  * @param[in,out] eg group to do batch deposits for
   1324  */
   1325 static void
   1326 do_batch_deposits (struct ExchangeGroup *eg);
   1327 
   1328 
   1329 /**
   1330  * Retain the first batch error until outstanding deposits have been recorded.
   1331  * Cancelling another exchange's request cannot undo its accepted deposit.
   1332  *
   1333  * @param pc payment context
   1334  * @param response_code HTTP status to return
   1335  * @param response response to retain
   1336  */
   1337 static void
   1338 defer_batch_deposit_error (struct PayContext *pc,
   1339                            unsigned int response_code,
   1340                            struct MHD_Response *response)
   1341 {
   1342   if (NULL == pc->response)
   1343   {
   1344     pc->response = response;
   1345     pc->response_code = response_code;
   1346   }
   1347   else
   1348   {
   1349     MHD_destroy_response (response);
   1350   }
   1351   if (0 == pc->batch_deposits.pending_at_eg)
   1352     resume_pay_with_response (pc,
   1353                               pc->response_code,
   1354                               pc->response);
   1355 }
   1356 
   1357 
   1358 /**
   1359  * Callback to handle a batch deposit permission's response.
   1360  *
   1361  * @param cls a `struct ExchangeGroup`
   1362  * @param dr HTTP response code details
   1363  */
   1364 static void
   1365 batch_deposit_cb (
   1366   struct ExchangeGroup *eg,
   1367   const struct TALER_EXCHANGE_PostBatchDepositResponse *dr)
   1368 {
   1369   struct PayContext *pc = eg->pc;
   1370 
   1371   eg->bdh = NULL;
   1372   pc->batch_deposits.pending_at_eg--;
   1373   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1374               "Processing /pay in phase %dd: batch deposit response received\n",
   1375               (int) pc->phase);
   1376   GNUNET_assert (GNUNET_YES == pc->suspended);
   1377   switch (dr->hr.http_status)
   1378   {
   1379   case MHD_HTTP_OK:
   1380     GNUNET_log (
   1381       GNUNET_ERROR_TYPE_INFO,
   1382       "Processing /pay in phase %de: batch deposit response persisted\n",
   1383       (int) pc->phase);
   1384     handle_batch_deposit_ok (eg,
   1385                              dr);
   1386     if (GNUNET_YES != pc->suspended)
   1387       return; /* handle_batch_deposit_ok already resumed with an error */
   1388     GNUNET_log (
   1389       GNUNET_ERROR_TYPE_INFO,
   1390       "Processing /pay in phase %df: batch deposit response persisted\n",
   1391       (int) pc->phase);
   1392     do_batch_deposits (eg);
   1393     return;
   1394   case MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS:
   1395     for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1396     {
   1397       struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1398 
   1399       if (0 != strcmp (eg->exchange_url,
   1400                        dc->exchange_url))
   1401         continue;
   1402       dc->in_batch = false;
   1403     }
   1404     notify_kyc_required (eg);
   1405     eg->got_451 = true;
   1406     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1407                 "Processing /pay in phase %dx: hit kyc requirement\n",
   1408                 (int) pc->phase);
   1409     pc->batch_deposits.got_451 = true;
   1410     /* update pc->pay_transaction.pending */
   1411     for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1412     {
   1413       struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1414 
   1415       if (0 != strcmp (eg->exchange_url,
   1416                        pc->parse_pay.dc[i].exchange_url))
   1417         continue;
   1418       if (dc->found_in_db)
   1419         continue;
   1420       pc->pay_transaction.pending--;
   1421     }
   1422     if (0 == pc->batch_deposits.pending_at_eg)
   1423     {
   1424       if (NULL != pc->response)
   1425         resume_pay_with_response (pc,
   1426                                   pc->response_code,
   1427                                   pc->response);
   1428       else
   1429       {
   1430         pc->phase = PP_COMPUTE_MONEY_POTS;
   1431         pay_resume (pc);
   1432       }
   1433     }
   1434     return;
   1435   default:
   1436     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   1437                 "Processing /pay in phase %dw: HTTP error %u/%d\n",
   1438                 (int) pc->phase,
   1439                 dr->hr.http_status,
   1440                 (int) dr->hr.ec);
   1441     for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1442     {
   1443       struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1444 
   1445       if (0 != strcmp (eg->exchange_url,
   1446                        dc->exchange_url))
   1447         continue;
   1448       dc->in_batch = false;
   1449     }
   1450     /* Transaction failed */
   1451     if (5 == dr->hr.http_status / 100)
   1452     {
   1453       /* internal server error at exchange */
   1454       defer_batch_deposit_error (pc,
   1455                                  MHD_HTTP_BAD_GATEWAY,
   1456                                  TALER_MHD_MAKE_JSON_PACK (
   1457                                    TALER_JSON_pack_ec (
   1458                                      TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS),
   1459                                    TMH_pack_exchange_reply (&dr->hr)));
   1460       return;
   1461     }
   1462     if (NULL == dr->hr.reply)
   1463     {
   1464       /* We can't do anything meaningful here, the exchange did something wrong */
   1465       defer_batch_deposit_error (
   1466         pc,
   1467         MHD_HTTP_BAD_GATEWAY,
   1468         TALER_MHD_MAKE_JSON_PACK (
   1469           TALER_JSON_pack_ec (
   1470             TALER_EC_MERCHANT_GENERIC_EXCHANGE_REPLY_MALFORMED),
   1471           TMH_pack_exchange_reply (&dr->hr)));
   1472       return;
   1473     }
   1474 
   1475     /* Forward error, adding the "exchange_url" for which the
   1476        error was being generated */
   1477     if (TALER_EC_EXCHANGE_GENERIC_INSUFFICIENT_FUNDS == dr->hr.ec)
   1478     {
   1479       defer_batch_deposit_error (
   1480         pc,
   1481         MHD_HTTP_CONFLICT,
   1482         TALER_MHD_MAKE_JSON_PACK (
   1483           TALER_JSON_pack_ec (
   1484             TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_FUNDS),
   1485           TMH_pack_exchange_reply (&dr->hr),
   1486           GNUNET_JSON_pack_string ("exchange_url",
   1487                                    eg->exchange_url)));
   1488       return;
   1489     }
   1490     defer_batch_deposit_error (
   1491       pc,
   1492       MHD_HTTP_BAD_GATEWAY,
   1493       TALER_MHD_MAKE_JSON_PACK (
   1494         TALER_JSON_pack_ec (
   1495           TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNEXPECTED_STATUS),
   1496         TMH_pack_exchange_reply (&dr->hr),
   1497         GNUNET_JSON_pack_string ("exchange_url",
   1498                                  eg->exchange_url)));
   1499     return;
   1500   } /* end switch */
   1501 }
   1502 
   1503 
   1504 static void
   1505 do_batch_deposits (struct ExchangeGroup *eg)
   1506 {
   1507   struct PayContext *pc = eg->pc;
   1508   struct TMH_HandlerContext *hc = pc->hc;
   1509   unsigned int group_size = 0;
   1510 
   1511   if (NULL != pc->response)
   1512   {
   1513     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1514                 "Processing /pay in phase %dk: response during batch deposit\n",
   1515                 (int) pc->phase);
   1516     if (0 == pc->batch_deposits.pending_at_eg)
   1517       resume_pay_with_response (pc,
   1518                                 pc->response_code,
   1519                                 pc->response);
   1520     return;
   1521   }
   1522   /* Initiate /batch-deposit operation for all coins of
   1523      the current exchange (!) */
   1524 
   1525   GNUNET_assert (NULL != eg->keys);
   1526   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1527   {
   1528     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1529 
   1530     if (0 != strcmp (eg->exchange_url,
   1531                      pc->parse_pay.dc[i].exchange_url))
   1532       continue;
   1533     if (dc->found_in_db)
   1534       continue;
   1535     group_size++;
   1536     if (group_size >= TALER_MAX_COINS)
   1537       break;
   1538   }
   1539   if (0 == group_size)
   1540   {
   1541     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1542                 "Group size zero, %u batch transactions remain pending\n",
   1543                 pc->batch_deposits.pending_at_eg);
   1544     if (0 == pc->batch_deposits.pending_at_eg)
   1545     {
   1546       pc->phase = PP_COMPUTE_MONEY_POTS;
   1547       pay_resume (pc);
   1548       return;
   1549     }
   1550     return;
   1551   }
   1552   /* Dispatch the next batch of up to TALER_MAX_COINS coins.
   1553      On success, batch_deposit_cb() will re-invoke
   1554      do_batch_deposits() to send further batches until
   1555      all coins are done. */
   1556   {
   1557     struct TALER_EXCHANGE_DepositContractDetail dcd = {
   1558       .wire_deadline
   1559         = pc->check_contract.contract_terms->pc->wire_deadline,
   1560       .merchant_payto_uri
   1561         = pc->check_contract.wm->payto_uri,
   1562       .extra_wire_subject_metadata
   1563         = pc->check_contract.wm->extra_wire_subject_metadata,
   1564       .wire_salt
   1565         = pc->check_contract.wm->wire_salt,
   1566       .h_contract_terms
   1567         = pc->check_contract.h_contract_terms,
   1568       .wallet_data_hash
   1569         = pc->parse_wallet_data.h_wallet_data,
   1570       .wallet_timestamp
   1571         = pc->check_contract.contract_terms->pc->timestamp,
   1572       .merchant_pub
   1573         = hc->instance->merchant_pub,
   1574       .refund_deadline
   1575         = pc->check_contract.contract_terms->pc->refund_deadline
   1576     };
   1577     /* Collect up to TALER_MAX_COINS eligible coins for this batch */
   1578     struct TALER_EXCHANGE_CoinDepositDetail cdds[group_size];
   1579     unsigned int batch_size = 0;
   1580     enum TALER_ErrorCode ec;
   1581 
   1582     /* FIXME-optimization: move signing outside of this 'loop'
   1583        and into the code that runs long before we look at a
   1584        specific exchange, otherwise we sign repeatedly! */
   1585     TALER_merchant_contract_sign (&pc->check_contract.h_contract_terms,
   1586                                   &pc->hc->instance->merchant_priv,
   1587                                   &dcd.merchant_sig);
   1588     for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1589     {
   1590       struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1591 
   1592       if (dc->found_in_db)
   1593         continue;
   1594       if (0 != strcmp (dc->exchange_url,
   1595                        eg->exchange_url))
   1596         continue;
   1597       dc->in_batch = true;
   1598       cdds[batch_size++] = dc->cdd;
   1599       if (batch_size == group_size)
   1600         break;
   1601     }
   1602     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1603                 "Initiating batch deposit with %u coins\n",
   1604                 batch_size);
   1605     /* Note: the coin signatures over the wallet_data_hash are
   1606        checked inside of this call */
   1607     eg->bdh = TALER_EXCHANGE_post_batch_deposit_create (
   1608       TMH_curl_ctx,
   1609       eg->exchange_url,
   1610       eg->keys,
   1611       &dcd,
   1612       batch_size,
   1613       cdds,
   1614       &ec);
   1615     if (NULL == eg->bdh)
   1616     {
   1617       /* Signature was invalid or some other constraint was not satisfied.  If
   1618          the exchange was unavailable, we'd get that information in the
   1619          callback. */
   1620       GNUNET_break_op (0);
   1621       resume_pay_with_response (
   1622         pc,
   1623         TALER_ErrorCode_get_http_status_safe (ec),
   1624         TALER_MHD_MAKE_JSON_PACK (
   1625           TALER_JSON_pack_ec (ec),
   1626           GNUNET_JSON_pack_string ("exchange_url",
   1627                                    eg->exchange_url)));
   1628       return;
   1629     }
   1630     pc->batch_deposits.pending_at_eg++;
   1631     if (TMH_force_audit)
   1632     {
   1633       GNUNET_assert (
   1634         GNUNET_OK ==
   1635         TALER_EXCHANGE_post_batch_deposit_set_options (
   1636           eg->bdh,
   1637           TALER_EXCHANGE_post_batch_deposit_option_force_dc ()));
   1638     }
   1639     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1640                 "Processing /pay in phase %dc: /batch-deposit request\n",
   1641                 (int) pc->phase);
   1642     TALER_EXCHANGE_post_batch_deposit_start (eg->bdh,
   1643                                              &batch_deposit_cb,
   1644                                              eg);
   1645   }
   1646 }
   1647 
   1648 
   1649 /**
   1650  * Force re-downloading keys for @a eg.
   1651  *
   1652  * @param[in,out] eg group to re-download keys for
   1653  */
   1654 static void
   1655 force_keys (struct ExchangeGroup *eg);
   1656 
   1657 
   1658 /**
   1659  * Function called with the result of our exchange keys lookup.
   1660  *
   1661  * @param cls the `struct ExchangeGroup`
   1662  * @param keys the keys of the exchange
   1663  * @param exchange representation of the exchange
   1664  */
   1665 static void
   1666 process_pay_with_keys (
   1667   void *cls,
   1668   struct TALER_EXCHANGE_Keys *keys,
   1669   struct TMH_Exchange *exchange)
   1670 {
   1671   struct ExchangeGroup *eg = cls;
   1672   struct PayContext *pc = eg->pc;
   1673   struct TMH_HandlerContext *hc = pc->hc;
   1674   struct TALER_Amount max_amount;
   1675   enum TMH_ExchangeStatus es;
   1676 
   1677   eg->fo = NULL;
   1678   pc->batch_deposits.pending_at_eg--;
   1679   GNUNET_SCHEDULER_begin_async_scope (&hc->async_scope_id);
   1680   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1681               "Processing /pay in phase %da: got keys from exchange %s\n",
   1682               (int) pc->phase,
   1683               eg->exchange_url);
   1684   GNUNET_assert (GNUNET_YES == pc->suspended);
   1685   if (NULL == keys)
   1686   {
   1687     GNUNET_break_op (0);
   1688     resume_pay_with_error (
   1689       pc,
   1690       TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT,
   1691       NULL);
   1692     return;
   1693   }
   1694   if (NULL != eg->keys)
   1695     TALER_EXCHANGE_keys_decref (eg->keys);
   1696   eg->keys = TALER_EXCHANGE_keys_incref (keys);
   1697   if (0 != strcasecmp (keys->currency,
   1698                        eg->total.currency))
   1699   {
   1700     GNUNET_break_op (0);
   1701     resume_pay_with_error (
   1702       pc,
   1703       TALER_EC_MERCHANT_GENERIC_CURRENCY_MISMATCH,
   1704       eg->exchange_url);
   1705     return;
   1706   }
   1707   if (! TMH_EXCHANGES_is_below_limit (keys,
   1708                                       TALER_KYCLOGIC_KYC_TRIGGER_TRANSACTION,
   1709                                       &eg->total))
   1710   {
   1711     GNUNET_break_op (0);
   1712     resume_pay_with_error (
   1713       pc,
   1714       TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_EXCHANGE_TRANSACTION_LIMIT_VIOLATION,
   1715       eg->exchange_url);
   1716     return;
   1717   }
   1718 
   1719   max_amount = eg->total;
   1720   es = TMH_exchange_check_debit (
   1721     pc->hc->instance->settings.id,
   1722     exchange,
   1723     pc->check_contract.wm,
   1724     &max_amount);
   1725   if ( (TMH_ES_OK != es) &&
   1726        (TMH_ES_RETRY_OK != es) )
   1727   {
   1728     if (eg->tried_force_keys ||
   1729         (0 == (TMH_ES_RETRY_OK & es)) )
   1730     {
   1731       GNUNET_break_op (0);
   1732       resume_pay_with_error (
   1733         pc,
   1734         TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_WIRE_METHOD_UNSUPPORTED,
   1735         NULL);
   1736       return;
   1737     }
   1738     force_keys (eg);
   1739     return;
   1740   }
   1741   if (-1 ==
   1742       TALER_amount_cmp (&max_amount,
   1743                         &eg->total))
   1744   {
   1745     /* max_amount < eg->total */
   1746     GNUNET_break_op (0);
   1747     resume_pay_with_error (
   1748       pc,
   1749       TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_EXCHANGE_TRANSACTION_LIMIT_VIOLATION,
   1750       eg->exchange_url);
   1751     return;
   1752   }
   1753 
   1754   if (GNUNET_OK !=
   1755       TMH_EXCHANGES_lookup_wire_fee (exchange,
   1756                                      pc->check_contract.wm->wire_method,
   1757                                      &eg->wire_fee))
   1758   {
   1759     if (eg->tried_force_keys)
   1760     {
   1761       GNUNET_break_op (0);
   1762       resume_pay_with_error (
   1763         pc,
   1764         TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_WIRE_METHOD_UNSUPPORTED,
   1765         pc->check_contract.wm->wire_method);
   1766       return;
   1767     }
   1768     force_keys (eg);
   1769     return;
   1770   }
   1771   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1772               "Got wire data for %s\n",
   1773               eg->exchange_url);
   1774 
   1775   /* Check all coins satisfy constraints like deposit deadlines
   1776      and age restrictions */
   1777   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   1778   {
   1779     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   1780     const struct TALER_EXCHANGE_DenomPublicKey *denom_details;
   1781     bool is_age_restricted_denom = false;
   1782 
   1783     if (0 != strcmp (eg->exchange_url,
   1784                      pc->parse_pay.dc[i].exchange_url))
   1785       continue;
   1786     if (dc->found_in_db)
   1787       continue;
   1788 
   1789     denom_details
   1790       = TALER_EXCHANGE_get_denomination_key_by_hash (keys,
   1791                                                      &dc->cdd.h_denom_pub);
   1792     if (NULL == denom_details)
   1793     {
   1794       if (eg->tried_force_keys)
   1795       {
   1796         GNUNET_break_op (0);
   1797         resume_pay_with_response (
   1798           pc,
   1799           MHD_HTTP_BAD_REQUEST,
   1800           TALER_MHD_MAKE_JSON_PACK (
   1801             TALER_JSON_pack_ec (
   1802               TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DENOMINATION_KEY_NOT_FOUND),
   1803             GNUNET_JSON_pack_data_auto ("h_denom_pub",
   1804                                         &dc->cdd.h_denom_pub),
   1805             GNUNET_JSON_pack_allow_null (
   1806               GNUNET_JSON_pack_object_steal (
   1807                 "exchange_keys",
   1808                 TALER_EXCHANGE_keys_to_json (keys)))));
   1809         return;
   1810       }
   1811       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1812                   "Missing denomination %s from exchange %s, updating keys\n",
   1813                   GNUNET_h2s (&dc->cdd.h_denom_pub.hash),
   1814                   eg->exchange_url);
   1815       force_keys (eg);
   1816       return;
   1817     }
   1818     dc->deposit_fee = denom_details->fees.deposit;
   1819     dc->refund_fee = denom_details->fees.refund;
   1820 
   1821     if (GNUNET_TIME_absolute_is_past (
   1822           denom_details->expire_deposit.abs_time))
   1823     {
   1824       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   1825                   "Denomination key offered by client has expired for deposits\n");
   1826       resume_pay_with_response (
   1827         pc,
   1828         MHD_HTTP_GONE,
   1829         TALER_MHD_MAKE_JSON_PACK (
   1830           TALER_JSON_pack_ec (
   1831             TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DENOMINATION_DEPOSIT_EXPIRED),
   1832           GNUNET_JSON_pack_data_auto ("h_denom_pub",
   1833                                       &denom_details->h_key)));
   1834       return;
   1835     }
   1836 
   1837     /* Now that we have the details about the denomination, we can verify age
   1838      * restriction requirements, if applicable. Note that denominations with an
   1839      * age_mask equal to zero always pass the age verification.  */
   1840     is_age_restricted_denom = (0 != denom_details->key.age_mask.bits);
   1841 
   1842     if (is_age_restricted_denom &&
   1843         (0 < pc->check_contract.contract_terms->pc->base->minimum_age))
   1844     {
   1845       /* Minimum age given and restricted coin provided: We need to verify the
   1846        * minimum age */
   1847       unsigned int code = 0;
   1848 
   1849       if (dc->no_age_commitment)
   1850       {
   1851         GNUNET_break_op (0);
   1852         code = TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AGE_COMMITMENT_MISSING;
   1853         goto AGE_FAIL;
   1854       }
   1855       dc->age_commitment.mask = denom_details->key.age_mask;
   1856       if (((int) (dc->age_commitment.num + 1)) !=
   1857           __builtin_popcount (dc->age_commitment.mask.bits))
   1858       {
   1859         GNUNET_break_op (0);
   1860         code =
   1861           TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AGE_COMMITMENT_SIZE_MISMATCH;
   1862         goto AGE_FAIL;
   1863       }
   1864       if (GNUNET_OK !=
   1865           TALER_age_commitment_verify (
   1866             &dc->age_commitment,
   1867             pc->check_contract.contract_terms->pc->base->minimum_age,
   1868             &dc->minimum_age_sig))
   1869         code = TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AGE_VERIFICATION_FAILED;
   1870 AGE_FAIL:
   1871       if (0 < code)
   1872       {
   1873         GNUNET_break_op (0);
   1874         TALER_age_commitment_free (&dc->age_commitment);
   1875         resume_pay_with_response (
   1876           pc,
   1877           MHD_HTTP_BAD_REQUEST,
   1878           TALER_MHD_MAKE_JSON_PACK (
   1879             TALER_JSON_pack_ec (code),
   1880             GNUNET_JSON_pack_data_auto ("h_denom_pub",
   1881                                         &denom_details->h_key)));
   1882         return;
   1883       }
   1884 
   1885       /* Age restriction successfully verified!
   1886        * Calculate the hash of the age commitment. */
   1887       TALER_age_commitment_hash (&dc->age_commitment,
   1888                                  &dc->cdd.h_age_commitment);
   1889       TALER_age_commitment_free (&dc->age_commitment);
   1890     }
   1891     else if (is_age_restricted_denom &&
   1892              dc->no_h_age_commitment)
   1893     {
   1894       /* The contract did not ask for a minimum_age but the client paid
   1895        * with a coin that has age restriction enabled.  We lack the hash
   1896        * of the age commitment in this case in order to verify the coin
   1897        * and to deposit it with the exchange. */
   1898       GNUNET_break_op (0);
   1899       resume_pay_with_response (
   1900         pc,
   1901         MHD_HTTP_BAD_REQUEST,
   1902         TALER_MHD_MAKE_JSON_PACK (
   1903           TALER_JSON_pack_ec (
   1904             TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AGE_COMMITMENT_HASH_MISSING),
   1905           GNUNET_JSON_pack_data_auto ("h_denom_pub",
   1906                                       &denom_details->h_key)));
   1907       return;
   1908     }
   1909   }
   1910   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1911               "Processing /pay in phase %db: preparing batch deposit\n",
   1912               (int) pc->phase);
   1913 
   1914   do_batch_deposits (eg);
   1915 }
   1916 
   1917 
   1918 static void
   1919 force_keys (struct ExchangeGroup *eg)
   1920 {
   1921   struct PayContext *pc = eg->pc;
   1922 
   1923   eg->tried_force_keys = true;
   1924   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1925               "Processing /pay in phase %dy: forcing keys\n",
   1926               (int) pc->phase);
   1927   eg->fo = TMH_EXCHANGES_keys4exchange (
   1928     eg->exchange_url,
   1929     true,
   1930     &process_pay_with_keys,
   1931     eg);
   1932   if (NULL == eg->fo)
   1933   {
   1934     GNUNET_break_op (0);
   1935     resume_pay_with_error (pc,
   1936                            TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNTRUSTED,
   1937                            eg->exchange_url);
   1938     return;
   1939   }
   1940   pc->batch_deposits.pending_at_eg++;
   1941 }
   1942 
   1943 
   1944 /**
   1945  * Handle a timeout for the processing of the pay request.
   1946  *
   1947  * @param cls our `struct PayContext`
   1948  */
   1949 static void
   1950 handle_pay_timeout (void *cls)
   1951 {
   1952   struct PayContext *pc = cls;
   1953 
   1954   pc->batch_deposits.timeout_task = NULL;
   1955   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1956               "Processing /pay in phase %dz: timeout\n",
   1957               (int) pc->phase);
   1958   GNUNET_assert (GNUNET_YES == pc->suspended);
   1959   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   1960               "Resuming pay with error after timeout\n");
   1961   resume_pay_with_error (pc,
   1962                          TALER_EC_MERCHANT_GENERIC_EXCHANGE_TIMEOUT,
   1963                          NULL);
   1964 }
   1965 
   1966 
   1967 /**
   1968  * Compute the timeout for a /pay request based on the number of coins
   1969  * involved.
   1970  *
   1971  * @param num_coins number of coins
   1972  * @returns timeout for the /pay request
   1973  */
   1974 static struct GNUNET_TIME_Relative
   1975 get_pay_timeout (unsigned int num_coins)
   1976 {
   1977   struct GNUNET_TIME_Relative t;
   1978 
   1979   /* FIXME-Performance-Optimization: Do some benchmarking to come up with a
   1980    * better timeout.  We've increased this value so the wallet integration
   1981    * test passes again on my (Florian) machine.
   1982    */
   1983   t = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
   1984                                      15 * (1 + (num_coins / 5)));
   1985 
   1986   return t;
   1987 }
   1988 
   1989 
   1990 /**
   1991  * Start batch deposits for all exchanges involved
   1992  * in this payment.
   1993  *
   1994  * @param[in,out] pc payment context we are processing
   1995  */
   1996 static void
   1997 phase_batch_deposits (struct PayContext *pc)
   1998 {
   1999   for (unsigned int i = 0; i<pc->parse_pay.num_exchanges; i++)
   2000   {
   2001     struct ExchangeGroup *eg = pc->parse_pay.egs[i];
   2002     bool have_coins = false;
   2003 
   2004     for (size_t j = 0; j<pc->parse_pay.coins_cnt; j++)
   2005     {
   2006       struct DepositConfirmation *dc = &pc->parse_pay.dc[j];
   2007 
   2008       if (0 != strcmp (eg->exchange_url,
   2009                        dc->exchange_url))
   2010         continue;
   2011       if (dc->found_in_db)
   2012         continue;
   2013       have_coins = true;
   2014       break;
   2015     }
   2016     if (! have_coins)
   2017       continue; /* no coins left to deposit at this exchange */
   2018     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2019                 "Getting /keys for %s\n",
   2020                 eg->exchange_url);
   2021     eg->fo = TMH_EXCHANGES_keys4exchange (
   2022       eg->exchange_url,
   2023       false,
   2024       &process_pay_with_keys,
   2025       eg);
   2026     if (NULL == eg->fo)
   2027     {
   2028       GNUNET_break_op (0);
   2029       /* We reply without suspending: the /keys lookups already started
   2030          for other exchanges must not call back into this request. */
   2031       for (unsigned int j = 0; j<i; j++)
   2032       {
   2033         struct ExchangeGroup *egj = pc->parse_pay.egs[j];
   2034 
   2035         if (NULL == egj->fo)
   2036           continue;
   2037         TMH_EXCHANGES_keys4exchange_cancel (egj->fo);
   2038         egj->fo = NULL;
   2039         pc->batch_deposits.pending_at_eg--;
   2040       }
   2041       GNUNET_assert (0 == pc->batch_deposits.pending_at_eg);
   2042       pay_end (pc,
   2043                TALER_MHD_reply_with_error (
   2044                  pc->connection,
   2045                  MHD_HTTP_BAD_REQUEST,
   2046                  TALER_EC_MERCHANT_GENERIC_EXCHANGE_UNTRUSTED,
   2047                  eg->exchange_url));
   2048       return;
   2049     }
   2050     pc->batch_deposits.pending_at_eg++;
   2051   }
   2052   if (0 == pc->batch_deposits.pending_at_eg)
   2053   {
   2054     pc->phase = PP_COMPUTE_MONEY_POTS;
   2055     pay_resume (pc);
   2056     return;
   2057   }
   2058   /* Suspend while we interact with the exchange */
   2059   MHD_suspend_connection (pc->connection);
   2060   pc->suspended = GNUNET_YES;
   2061   GNUNET_assert (NULL == pc->batch_deposits.timeout_task);
   2062   pc->batch_deposits.timeout_task
   2063     = GNUNET_SCHEDULER_add_delayed (get_pay_timeout (pc->parse_pay.coins_cnt),
   2064                                     &handle_pay_timeout,
   2065                                     pc);
   2066 }
   2067 
   2068 
   2069 /**
   2070  * Build JSON array of blindly signed token envelopes,
   2071  * to be used in the response to the wallet.
   2072  *
   2073  * @param[in,out] pc payment context to use
   2074  */
   2075 static json_t *
   2076 build_token_sigs (struct PayContext *pc)
   2077 {
   2078   json_t *token_sigs;
   2079 
   2080   if (0 == pc->output_tokens_len)
   2081     return NULL;
   2082   token_sigs = json_array ();
   2083   GNUNET_assert (NULL != token_sigs);
   2084   for (unsigned int i = 0; i < pc->output_tokens_len; i++)
   2085   {
   2086     if (NULL == pc->output_tokens[i].sig.signature)
   2087       continue; /* must be optional TF and wallet did not provide it */
   2088     GNUNET_assert (0 ==
   2089                    json_array_append_new (
   2090                      token_sigs,
   2091                      GNUNET_JSON_PACK (
   2092                        GNUNET_JSON_pack_blinded_sig (
   2093                          "blind_sig",
   2094                          pc->output_tokens[i].sig.signature)
   2095                        )));
   2096   }
   2097   return token_sigs;
   2098 }
   2099 
   2100 
   2101 /**
   2102  * Generate response (payment successful)
   2103  *
   2104  * @param[in,out] pc payment context where the payment was successful
   2105  */
   2106 static void
   2107 phase_success_response (struct PayContext *pc)
   2108 {
   2109   struct TALER_MerchantSignatureP sig;
   2110   char *pos_confirmation;
   2111 
   2112   /* Sign on our end (as the payment did go through, even if it may
   2113      have been refunded already) */
   2114   TALER_merchant_pay_sign (&pc->check_contract.h_contract_terms,
   2115                            &pc->hc->instance->merchant_priv,
   2116                            &sig);
   2117   /* Build the response */
   2118   switch (pc->check_contract.pos_alg)
   2119   {
   2120   case TALER_MCA_ECDSA_CHALLENGE:
   2121   case TALER_MCA_EDDSA_CHALLENGE:
   2122     if (NULL == pc->check_contract.pos_key)
   2123     {
   2124       GNUNET_break (0);
   2125       pay_end (pc,
   2126                TALER_MHD_reply_with_error (
   2127                  pc->connection,
   2128                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   2129                  TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   2130                  "order lacks the key needed to sign the POS confirmation"));
   2131       return;
   2132     }
   2133     pos_confirmation
   2134       = TALER_build_pos_confirmation_sig (pc->check_contract.pos_key,
   2135                                           pc->check_contract.pos_alg,
   2136                                           &pc->check_contract.pos_challenge);
   2137     if (NULL == pos_confirmation)
   2138     {
   2139       GNUNET_break (0);
   2140       pay_end (pc,
   2141                TALER_MHD_reply_with_error (
   2142                  pc->connection,
   2143                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   2144                  TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   2145                  "failed to sign the POS confirmation"));
   2146       return;
   2147     }
   2148     break;
   2149   case TALER_MCA_NONE:
   2150   case TALER_MCA_WITHOUT_PRICE:
   2151   case TALER_MCA_WITH_PRICE:
   2152     pos_confirmation = (NULL == pc->check_contract.pos_key)
   2153       ? NULL
   2154       : TALER_build_pos_confirmation (
   2155       pc->check_contract.pos_key,
   2156       pc->check_contract.pos_alg,
   2157       &pc->validate_tokens.brutto,
   2158       pc->check_contract.contract_terms->pc->timestamp);
   2159     break;
   2160   default:
   2161     GNUNET_break (0);
   2162     pay_end (pc,
   2163              TALER_MHD_reply_with_error (
   2164                pc->connection,
   2165                MHD_HTTP_INTERNAL_SERVER_ERROR,
   2166                TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   2167                "unknown POS confirmation algorithm"));
   2168     return;
   2169   }
   2170   pay_end (pc,
   2171            TALER_MHD_REPLY_JSON_PACK (
   2172              pc->connection,
   2173              MHD_HTTP_OK,
   2174              GNUNET_JSON_pack_allow_null (
   2175                GNUNET_JSON_pack_string ("pos_confirmation",
   2176                                         pos_confirmation)),
   2177              GNUNET_JSON_pack_allow_null (
   2178                GNUNET_JSON_pack_array_steal ("token_sigs",
   2179                                              build_token_sigs (pc))),
   2180              GNUNET_JSON_pack_data_auto ("sig",
   2181                                          &sig)));
   2182   GNUNET_free (pos_confirmation);
   2183 }
   2184 
   2185 
   2186 /**
   2187  * Use database to notify other clients about the
   2188  * payment being completed.
   2189  *
   2190  * @param[in,out] pc context to trigger notification for
   2191  */
   2192 static void
   2193 phase_payment_notification (struct PayContext *pc)
   2194 {
   2195   {
   2196     struct TMH_OrderPayEventP pay_eh = {
   2197       .header.size = htons (sizeof (pay_eh)),
   2198       .header.type = htons (TALER_DBEVENT_MERCHANT_ORDER_PAID),
   2199       .merchant_pub = pc->hc->instance->merchant_pub
   2200     };
   2201 
   2202     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2203                 "Notifying clients about payment of order %s\n",
   2204                 pc->order_id);
   2205     GNUNET_CRYPTO_hash (pc->order_id,
   2206                         strlen (pc->order_id),
   2207                         &pay_eh.h_order_id);
   2208     TALER_MERCHANTDB_event_notify (TMH_db,
   2209                                    &pay_eh.header,
   2210                                    NULL,
   2211                                    0);
   2212   }
   2213   {
   2214     struct TMH_OrderPayEventP pay_eh = {
   2215       .header.size = htons (sizeof (pay_eh)),
   2216       .header.type = htons (TALER_DBEVENT_MERCHANT_ORDER_STATUS_CHANGED),
   2217       .merchant_pub = pc->hc->instance->merchant_pub
   2218     };
   2219 
   2220     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2221                 "Notifying clients about status change of order %s\n",
   2222                 pc->order_id);
   2223     GNUNET_CRYPTO_hash (pc->order_id,
   2224                         strlen (pc->order_id),
   2225                         &pay_eh.h_order_id);
   2226     TALER_MERCHANTDB_event_notify (TMH_db,
   2227                                    &pay_eh.header,
   2228                                    NULL,
   2229                                    0);
   2230   }
   2231   if ( (NULL != pc->parse_pay.session_id) &&
   2232        (NULL != pc->check_contract.contract_terms->pc->base->fulfillment_url) )
   2233   {
   2234     struct TMH_SessionEventP session_eh = {
   2235       .header.size = htons (sizeof (session_eh)),
   2236       .header.type = htons (TALER_DBEVENT_MERCHANT_SESSION_CAPTURED),
   2237       .merchant_pub = pc->hc->instance->merchant_pub
   2238     };
   2239 
   2240     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2241                 "Notifying clients about session change to %s for %s\n",
   2242                 pc->parse_pay.session_id,
   2243                 pc->check_contract.contract_terms->pc->base->fulfillment_url);
   2244     GNUNET_CRYPTO_hash (pc->parse_pay.session_id,
   2245                         strlen (pc->parse_pay.session_id),
   2246                         &session_eh.h_session_id);
   2247     GNUNET_CRYPTO_hash (
   2248       pc->check_contract.contract_terms->pc->base->fulfillment_url,
   2249       strlen (pc->check_contract.contract_terms->pc->base->fulfillment_url),
   2250       &session_eh.h_fulfillment_url);
   2251     TALER_MERCHANTDB_event_notify (TMH_db,
   2252                                    &session_eh.header,
   2253                                    NULL,
   2254                                    0);
   2255   }
   2256   pc->phase = PP_SUCCESS_RESPONSE;
   2257 }
   2258 
   2259 
   2260 /**
   2261  * Phase to write all outputs to our database so we do
   2262  * not re-request them in case the client re-plays the
   2263  * request.
   2264  *
   2265  * @param[in,out] pc payment context
   2266  */
   2267 static void
   2268 phase_final_output_token_processing (struct PayContext *pc)
   2269 {
   2270   if (0 == pc->output_tokens_len)
   2271   {
   2272     pc->phase++;
   2273     return;
   2274   }
   2275   for (unsigned int retry = 0; retry < MAX_RETRIES; retry++)
   2276   {
   2277     enum GNUNET_DB_QueryStatus qs;
   2278 
   2279     TALER_MERCHANTDB_preflight (TMH_db);
   2280     if (GNUNET_OK !=
   2281         TALER_MERCHANTDB_start (TMH_db,
   2282                                 "insert_order_token_blinded_sig"))
   2283     {
   2284       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   2285                   "start insert_order_blinded_sigs_failed");
   2286       pc->phase++;
   2287       return;
   2288     }
   2289     if (pc->parse_wallet_data.num_bkps > 0)
   2290     {
   2291       qs = TALER_MERCHANTDB_update_donau_instance_receipts_amount (
   2292         TMH_db,
   2293         &pc->parse_wallet_data.donau_instance_serial,
   2294         &pc->parse_wallet_data.charity_receipts_to_date);
   2295       switch (qs)
   2296       {
   2297       case GNUNET_DB_STATUS_HARD_ERROR:
   2298         TALER_MERCHANTDB_rollback (TMH_db);
   2299         GNUNET_break (0);
   2300         pc->phase++;
   2301         return;
   2302       case GNUNET_DB_STATUS_SOFT_ERROR:
   2303         TALER_MERCHANTDB_rollback (TMH_db);
   2304         continue;
   2305       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   2306         /* weird for an update */
   2307         GNUNET_break (0);
   2308         break;
   2309       case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   2310         break;
   2311       }
   2312     }
   2313     for (unsigned int i = 0;
   2314          i < pc->output_tokens_len;
   2315          i++)
   2316     {
   2317       if (NULL == pc->output_tokens[i].sig.signature)
   2318         continue; /* must have been optional and not provided by wallet */
   2319       qs = TALER_MERCHANTDB_insert_order_token_blinded_sig (
   2320         TMH_db,
   2321         pc->order_id,
   2322         i,
   2323         &pc->output_tokens[i].h_issue.hash,
   2324         pc->output_tokens[i].sig.signature);
   2325 
   2326       switch (qs)
   2327       {
   2328       case GNUNET_DB_STATUS_HARD_ERROR:
   2329         TALER_MERCHANTDB_rollback (TMH_db);
   2330         pc->phase++;
   2331         return;
   2332       case GNUNET_DB_STATUS_SOFT_ERROR:
   2333         TALER_MERCHANTDB_rollback (TMH_db);
   2334         goto OUTER;
   2335       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   2336         /* weird for an update */
   2337         GNUNET_break (0);
   2338         break;
   2339       case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   2340         break;
   2341       }
   2342     } /* for i */
   2343     qs = TALER_MERCHANTDB_commit (TMH_db);
   2344     switch (qs)
   2345     {
   2346     case GNUNET_DB_STATUS_HARD_ERROR:
   2347       TALER_MERCHANTDB_rollback (TMH_db);
   2348       pc->phase++;
   2349       return;
   2350     case GNUNET_DB_STATUS_SOFT_ERROR:
   2351       TALER_MERCHANTDB_rollback (TMH_db);
   2352       continue;
   2353     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   2354       pc->phase++;
   2355       return; /* success */
   2356     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   2357       pc->phase++;
   2358       return; /* success */
   2359     }
   2360     GNUNET_break (0);
   2361     pc->phase++;
   2362     return; /* strange */
   2363 OUTER:
   2364   } /* for retry */
   2365   TALER_MERCHANTDB_rollback (TMH_db);
   2366   pc->phase++;
   2367   /* We continue anyway, as there is not much we can
   2368      do here: the Donau *did* issue us the receipts;
   2369      also, we'll eventually ask the Donau for the
   2370      balance and get the correct one. Plus, we were
   2371      paid by the client, so it's technically all still
   2372      OK. If the request fails anyway, the wallet will
   2373      most likely replay the request and then hopefully
   2374      we will succeed the next time */
   2375 }
   2376 
   2377 
   2378 /**
   2379  * Add donation receipt outputs to the output_tokens.
   2380  *
   2381  * Note that under the current (odd, bad) libdonau
   2382  * API *we* are responsible for freeing blinded_sigs,
   2383  * so we truly own that array!
   2384  *
   2385  * @param[in,out] pc payment context
   2386  * @param num_blinded_sigs number of signatures received
   2387  * @param blinded_sigs blinded signatures from Donau
   2388  * @return #GNUNET_OK on success,
   2389  *         #GNUNET_SYSERR on failure (state machine was
   2390  *          in that case already advanced)
   2391  */
   2392 static enum GNUNET_GenericReturnValue
   2393 add_donation_receipt_outputs (
   2394   struct PayContext *pc,
   2395   size_t num_blinded_sigs,
   2396   struct DONAU_BlindedDonationUnitSignature *blinded_sigs)
   2397 {
   2398   unsigned int i;
   2399   int donau_output_index = pc->validate_tokens.donau_output_index;
   2400 
   2401   GNUNET_assert (pc->parse_wallet_data.num_bkps ==
   2402                  num_blinded_sigs);
   2403   GNUNET_assert (donau_output_index >= 0);
   2404 
   2405   /* Find position where donau tokens start in output_tokens */
   2406   for (i = 0; i<pc->output_tokens_len; i++)
   2407   {
   2408     const struct SignedOutputToken *sot
   2409       = &pc->output_tokens[i];
   2410 
   2411     /* Only look at actual donau tokens. */
   2412     if (sot->output_index == donau_output_index)
   2413       break;
   2414   }
   2415 
   2416   /* copy donau signatures into output array */
   2417   for (unsigned int j = 0; j<pc->parse_wallet_data.num_bkps; j++)
   2418   {
   2419     struct SignedOutputToken *sot;
   2420 
   2421     GNUNET_assert (i + j < pc->output_tokens_len);
   2422     sot = &pc->output_tokens[i + j];
   2423     GNUNET_assert (sot->output_index == donau_output_index);
   2424     sot->sig.signature = GNUNET_CRYPTO_blind_sig_incref (
   2425       blinded_sigs[j].blinded_sig);
   2426     sot->h_issue.hash
   2427       = pc->parse_wallet_data.bkps[j].h_donation_unit_pub.hash;
   2428   }
   2429   return GNUNET_OK;
   2430 }
   2431 
   2432 
   2433 /**
   2434  * Callback to handle the result of a batch issue request.
   2435  *
   2436  * @param cls our `struct PayContext`
   2437  * @param resp the response from Donau
   2438  */
   2439 static void
   2440 merchant_donau_issue_receipt_cb (
   2441   void *cls,
   2442   const struct DONAU_BatchIssueResponse *resp)
   2443 {
   2444   struct PayContext *pc = cls;
   2445 
   2446   /* Donau replies asynchronously, so we expect the PayContext
   2447    * to be suspended. */
   2448   GNUNET_assert (GNUNET_YES == pc->suspended);
   2449   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   2450               "Donau responded with status=%u, ec=%u",
   2451               resp->hr.http_status,
   2452               resp->hr.ec);
   2453   switch (resp->hr.http_status)
   2454   {
   2455   case 0:
   2456     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2457                 "Donau batch issue request from merchant-httpd failed (http_status==0)");
   2458     resume_pay_with_error (pc,
   2459                            TALER_EC_MERCHANT_GENERIC_DONAU_INVALID_RESPONSE,
   2460                            resp->hr.hint);
   2461     return;
   2462   case MHD_HTTP_OK:
   2463     if (pc->parse_wallet_data.num_bkps !=
   2464         resp->details.ok.num_blinded_sigs)
   2465     {
   2466       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2467                   "Invalid number of signatures in batch issue response");
   2468       resume_pay_with_error (pc,
   2469                              TALER_EC_MERCHANT_GENERIC_DONAU_INVALID_RESPONSE,
   2470                              "invalid number of signatures");
   2471       return;
   2472     }
   2473     if (TALER_EC_NONE != resp->hr.ec)
   2474     {
   2475       /* Most probably, it is just some small flaw from
   2476        * donau so no point in failing, yet we have to display it */
   2477       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2478                   "Donau signalled error %u despite HTTP %u",
   2479                   resp->hr.ec,
   2480                   resp->hr.http_status);
   2481     }
   2482     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2483                 "Donau accepted donation receipts with total_issued=%s",
   2484                 TALER_amount2s (&resp->details.ok.issued_amount));
   2485     if (GNUNET_OK !=
   2486         add_donation_receipt_outputs (pc,
   2487                                       resp->details.ok.num_blinded_sigs,
   2488                                       resp->details.ok.blinded_sigs))
   2489       return; /* state machine was already advanced */
   2490     pc->phase = PP_FINAL_OUTPUT_TOKEN_PROCESSING;
   2491     pay_resume (pc);
   2492     return;
   2493 
   2494   case MHD_HTTP_BAD_REQUEST:
   2495   case MHD_HTTP_FORBIDDEN:
   2496   case MHD_HTTP_NOT_FOUND:
   2497   case MHD_HTTP_INTERNAL_SERVER_ERROR:
   2498   default: /* make sure that everything except 200/201 will end up here*/
   2499     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2500                 "Donau replied with HTTP %u (ec=%u)",
   2501                 resp->hr.http_status,
   2502                 resp->hr.ec);
   2503     resume_pay_with_error (pc,
   2504                            TALER_EC_MERCHANT_GENERIC_DONAU_INVALID_RESPONSE,
   2505                            resp->hr.hint);
   2506     return;
   2507   }
   2508 }
   2509 
   2510 
   2511 /**
   2512  * Parse a bkp encoded in JSON.
   2513  *
   2514  * @param[out] bkp where to return the result
   2515  * @param bkp_key_obj json to parse
   2516  * @return #GNUNET_OK if all is fine, #GNUNET_SYSERR if @a bkp_key_obj
   2517  * is malformed.
   2518  */
   2519 static enum GNUNET_GenericReturnValue
   2520 merchant_parse_json_bkp (struct DONAU_BlindedUniqueDonorIdentifierKeyPair *bkp,
   2521                          const json_t *bkp_key_obj)
   2522 {
   2523   struct GNUNET_JSON_Specification spec[] = {
   2524     GNUNET_JSON_spec_fixed_auto ("h_donation_unit_pub",
   2525                                  &bkp->h_donation_unit_pub),
   2526     DONAU_JSON_spec_blinded_donation_identifier ("blinded_udi",
   2527                                                  &bkp->blinded_udi),
   2528     GNUNET_JSON_spec_end ()
   2529   };
   2530 
   2531   if (GNUNET_OK !=
   2532       GNUNET_JSON_parse (bkp_key_obj,
   2533                          spec,
   2534                          NULL,
   2535                          NULL))
   2536   {
   2537     GNUNET_break_op (0);
   2538     return GNUNET_SYSERR;
   2539   }
   2540   return GNUNET_OK;
   2541 }
   2542 
   2543 
   2544 /**
   2545  * Generate a donation signature for the bkp and charity.
   2546  *
   2547  * @param[in,out] pc payment context containing the charity and bkps
   2548  */
   2549 static void
   2550 phase_request_donation_receipt (struct PayContext *pc)
   2551 {
   2552   if ( (NULL == pc->parse_wallet_data.donau.donau_url) ||
   2553        (0 == pc->parse_wallet_data.num_bkps) )
   2554   {
   2555     pc->phase++;
   2556     return;
   2557   }
   2558   pc->donau_receipt.birh =
   2559     DONAU_charity_issue_receipt (
   2560       TMH_curl_ctx,
   2561       pc->parse_wallet_data.donau.donau_url,
   2562       &pc->parse_wallet_data.charity_priv,
   2563       pc->parse_wallet_data.charity_id,
   2564       pc->parse_wallet_data.donau.donation_year,
   2565       pc->parse_wallet_data.num_bkps,
   2566       pc->parse_wallet_data.bkps,
   2567       &merchant_donau_issue_receipt_cb,
   2568       pc);
   2569   if (NULL == pc->donau_receipt.birh)
   2570   {
   2571     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   2572                 "Failed to create Donau receipt request");
   2573     pay_end (pc,
   2574              TALER_MHD_reply_with_error (pc->connection,
   2575                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
   2576                                          TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR,
   2577                                          "Donau request creation error"));
   2578     return;
   2579   }
   2580   MHD_suspend_connection (pc->connection);
   2581   pc->suspended = GNUNET_YES;
   2582 }
   2583 
   2584 
   2585 /**
   2586  * Increment the money pot @a pot_id in @a pc by @a increment.
   2587  *
   2588  * @param[in,out] pc context to update
   2589  * @param pot_id money pot to increment
   2590  * @param increment amount to add
   2591  */
   2592 static void
   2593 increment_pot (struct PayContext *pc,
   2594                uint64_t pot_id,
   2595                const struct TALER_Amount *increment)
   2596 {
   2597   for (unsigned int i = 0; i<pc->compute_money_pots.num_pots; i++)
   2598   {
   2599     if (pot_id == pc->compute_money_pots.pots[i])
   2600     {
   2601       struct TALER_Amount *p;
   2602 
   2603       p = &pc->compute_money_pots.increments[i];
   2604       GNUNET_assert (0 <=
   2605                      TALER_amount_add (p,
   2606                                        p,
   2607                                        increment));
   2608       return;
   2609     }
   2610   }
   2611   GNUNET_array_append (pc->compute_money_pots.pots,
   2612                        pc->compute_money_pots.num_pots,
   2613                        pot_id);
   2614   pc->compute_money_pots.num_pots--; /* do not increment twice... */
   2615   GNUNET_array_append (pc->compute_money_pots.increments,
   2616                        pc->compute_money_pots.num_pots,
   2617                        *increment);
   2618 }
   2619 
   2620 
   2621 /**
   2622  * Compute the total changes to money pots in preparation
   2623  * for the #PP_PAY_TRANSACTION phase.
   2624  *
   2625  * @param[in,out] pc payment context to transact
   2626  */
   2627 static void
   2628 phase_compute_money_pots (struct PayContext *pc)
   2629 {
   2630   const struct TALER_MERCHANT_Contract *contract
   2631     = pc->check_contract.contract_terms;
   2632   struct TALER_Amount assigned;
   2633 
   2634   if (0 == pc->parse_pay.coins_cnt)
   2635   {
   2636     /* Did not pay with any coins, so no currency/amount involved,
   2637        hence no money pot update possible. */
   2638     pc->phase++;
   2639     return;
   2640   }
   2641 
   2642   if (pc->compute_money_pots.pots_computed)
   2643   {
   2644     pc->phase++;
   2645     return;
   2646   }
   2647   /* reset, in case this phase is run a 2nd time */
   2648   GNUNET_free (pc->compute_money_pots.pots);
   2649   GNUNET_free (pc->compute_money_pots.increments);
   2650   pc->compute_money_pots.num_pots = 0;
   2651 
   2652   GNUNET_assert (GNUNET_OK ==
   2653                  TALER_amount_set_zero (pc->parse_pay.dc[0].cdd.amount.currency,
   2654                                         &assigned));
   2655   GNUNET_assert (NULL != contract);
   2656   for (size_t i = 0; i<contract->pc->products_len; i++)
   2657   {
   2658     const struct TALER_MERCHANT_ProductSold *product
   2659       = &contract->pc->products[i];
   2660     const struct TALER_Amount *price = NULL;
   2661 
   2662     /* find price in the right currency */
   2663     for (unsigned int j = 0; j<product->prices_length; j++)
   2664     {
   2665       if (GNUNET_OK ==
   2666           TALER_amount_cmp_currency (&assigned,
   2667                                      &product->prices[j]))
   2668       {
   2669         price = &product->prices[j];
   2670         break;
   2671       }
   2672     }
   2673     if (NULL == price)
   2674     {
   2675       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2676                   "Product `%s' has no price given in `%s'.\n",
   2677                   product->product_id,
   2678                   assigned.currency);
   2679       continue;
   2680     }
   2681     if (0 != product->product_money_pot)
   2682     {
   2683       GNUNET_assert (0 <=
   2684                      TALER_amount_add (&assigned,
   2685                                        &assigned,
   2686                                        price));
   2687       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2688                   "Contributing to product money pot %llu increment of %s\n",
   2689                   (unsigned long long) product->product_money_pot,
   2690                   TALER_amount2s (price));
   2691       increment_pot (pc,
   2692                      product->product_money_pot,
   2693                      price);
   2694     }
   2695   }
   2696 
   2697   {
   2698     /* Compute what is left from the order total and account for that.
   2699        Also sanity-check and handle the case where the overall order
   2700        is below that of the sum of the products. */
   2701     struct TALER_Amount left;
   2702 
   2703     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2704                 "Order brutto is %s\n",
   2705                 TALER_amount2s (&pc->validate_tokens.brutto));
   2706     if (0 >
   2707         TALER_amount_subtract (&left,
   2708                                &pc->validate_tokens.brutto,
   2709                                &assigned))
   2710     {
   2711       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2712                   "Total order brutto amount below sum from products, skipping per-product money pots\n");
   2713       GNUNET_free (pc->compute_money_pots.pots);
   2714       GNUNET_free (pc->compute_money_pots.increments);
   2715       pc->compute_money_pots.num_pots = 0;
   2716       left = pc->validate_tokens.brutto;
   2717     }
   2718 
   2719     if ( (! TALER_amount_is_zero (&left)) &&
   2720          (0 != contract->pc->base->default_money_pot) )
   2721     {
   2722       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   2723                   "Computing money pot %llu increment as %s\n",
   2724                   (unsigned long long) contract->pc->base->default_money_pot,
   2725                   TALER_amount2s (&left));
   2726       increment_pot (pc,
   2727                      contract->pc->base->default_money_pot,
   2728                      &left);
   2729     }
   2730   }
   2731   pc->compute_money_pots.pots_computed = true;
   2732   pc->phase++;
   2733 }
   2734 
   2735 
   2736 /**
   2737  * Function called with information about a coin that was deposited.
   2738  *
   2739  * @param cls closure
   2740  * @param exchange_url exchange where @a coin_pub was deposited
   2741  * @param coin_pub public key of the coin
   2742  * @param amount_with_fee amount the exchange will deposit for this coin
   2743  * @param deposit_fee fee the exchange will charge for this coin
   2744  * @param refund_fee fee the exchange will charge for refunding this coin
   2745  * @param wire_fee fee the exchange will charge for wiring this coin
   2746  */
   2747 static void
   2748 check_coin_paid (void *cls,
   2749                  const char *exchange_url,
   2750                  const struct TALER_CoinSpendPublicKeyP *coin_pub,
   2751                  const struct TALER_Amount *amount_with_fee,
   2752                  const struct TALER_Amount *deposit_fee,
   2753                  const struct TALER_Amount *refund_fee,
   2754                  const struct TALER_Amount *wire_fee)
   2755 {
   2756   struct PayContext *pc = cls;
   2757 
   2758   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   2759   {
   2760     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   2761 
   2762     if (dc->found_in_db)
   2763       continue; /* processed earlier, skip "expensive" memcmp() */
   2764     /* Get matching coin from results*/
   2765     if ( (0 != GNUNET_memcmp (coin_pub,
   2766                               &dc->cdd.coin_pub)) ||
   2767          (0 !=
   2768           strcmp (exchange_url,
   2769                   dc->exchange_url)) ||
   2770          (GNUNET_OK !=
   2771           TALER_amount_cmp_currency (amount_with_fee,
   2772                                      &dc->cdd.amount)) ||
   2773          (0 != TALER_amount_cmp (amount_with_fee,
   2774                                  &dc->cdd.amount)) )
   2775       continue; /* does not match, skip */
   2776     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   2777                 "Deposit of coin `%s' already in our DB.\n",
   2778                 TALER_B2S (coin_pub));
   2779     if ( (GNUNET_OK !=
   2780           TALER_amount_cmp_currency (&pc->pay_transaction.total_paid,
   2781                                      amount_with_fee)) ||
   2782          (GNUNET_OK !=
   2783           TALER_amount_cmp_currency (&pc->pay_transaction.total_fees_paid,
   2784                                      deposit_fee)) )
   2785     {
   2786       GNUNET_break_op (0);
   2787       pc->pay_transaction.deposit_currency_mismatch = true;
   2788       break;
   2789     }
   2790     GNUNET_assert (0 <=
   2791                    TALER_amount_add (&pc->pay_transaction.total_paid,
   2792                                      &pc->pay_transaction.total_paid,
   2793                                      amount_with_fee));
   2794     GNUNET_assert (0 <=
   2795                    TALER_amount_add (&pc->pay_transaction.total_fees_paid,
   2796                                      &pc->pay_transaction.total_fees_paid,
   2797                                      deposit_fee));
   2798     dc->deposit_fee = *deposit_fee;
   2799     dc->refund_fee = *refund_fee;
   2800     dc->wire_fee = *wire_fee;
   2801     dc->cdd.amount = *amount_with_fee;
   2802     dc->found_in_db = true;
   2803     pc->pay_transaction.pending--;
   2804   }
   2805 }
   2806 
   2807 
   2808 /**
   2809  * Function called with information about a refund.  Check if this coin was
   2810  * claimed by the wallet for the transaction, and if so add the refunded
   2811  * amount to the pc's "total_refunded" amount.
   2812  *
   2813  * @param cls closure with a `struct PayContext`
   2814  * @param coin_pub public coin from which the refund comes from
   2815  * @param refund_amount refund amount which is being taken from @a coin_pub
   2816  */
   2817 static void
   2818 check_coin_refunded (void *cls,
   2819                      const struct TALER_CoinSpendPublicKeyP *coin_pub,
   2820                      const struct TALER_Amount *refund_amount)
   2821 {
   2822   struct PayContext *pc = cls;
   2823 
   2824   /* We look at refunds here that apply to the coins
   2825      that the customer is currently trying to pay us with.
   2826 
   2827      Such refunds are not "normal" refunds, but abort-pay refunds, which are
   2828      given in the case that the wallet aborts the payment.
   2829      In the case the wallet then decides to complete the payment *after* doing
   2830      an abort-pay refund (an unusual but possible case), we need
   2831      to make sure that existing refunds are accounted for. */
   2832 
   2833   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   2834   {
   2835     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   2836 
   2837     /* Get matching coins from results.  */
   2838     if (0 != GNUNET_memcmp (coin_pub,
   2839                             &dc->cdd.coin_pub))
   2840       continue;
   2841     if (GNUNET_OK !=
   2842         TALER_amount_cmp_currency (&pc->pay_transaction.total_refunded,
   2843                                    refund_amount))
   2844     {
   2845       GNUNET_break (0);
   2846       pc->pay_transaction.refund_currency_mismatch = true;
   2847       break;
   2848     }
   2849     GNUNET_assert (0 <=
   2850                    TALER_amount_add (&pc->pay_transaction.total_refunded,
   2851                                      &pc->pay_transaction.total_refunded,
   2852                                      refund_amount));
   2853     break;
   2854   }
   2855 }
   2856 
   2857 
   2858 /**
   2859  * Check whether the amount paid is sufficient to cover the price.
   2860  *
   2861  * @param pc payment context to check
   2862  * @return true if the payment is sufficient, false if it is
   2863  *         insufficient
   2864  */
   2865 static bool
   2866 check_payment_sufficient (struct PayContext *pc)
   2867 {
   2868   struct TALER_Amount acc_fee;
   2869   struct TALER_Amount acc_amount;
   2870   struct TALER_Amount final_amount;
   2871   struct TALER_Amount total_wire_fee;
   2872   struct TALER_Amount total_needed;
   2873 
   2874   if (0 == pc->parse_pay.coins_cnt)
   2875     return TALER_amount_is_zero (&pc->validate_tokens.brutto);
   2876   GNUNET_assert (GNUNET_OK ==
   2877                  TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   2878                                         &total_wire_fee));
   2879   for (unsigned int i = 0; i < pc->parse_pay.num_exchanges; i++)
   2880   {
   2881     const struct ExchangeGroup *egsi = pc->parse_pay.egs[i];
   2882     const struct TALER_Amount *wire_fee = NULL;
   2883 
   2884     /* Note: we cannot just use egsi->wire_fee here, as that field
   2885        MAY not be initialized if the deposit for that exchange was
   2886        done earlier this is an idempotent request, for example
   2887        to deposit coins of another exchange or just because the
   2888        previous answer was lost; thus, we must get the fee from
   2889        the "dc" as that is guaranteed to be set! */
   2890     for (size_t j = 0; j < pc->parse_pay.coins_cnt; j++)
   2891     {
   2892       const struct DepositConfirmation *dc = &pc->parse_pay.dc[j];
   2893 
   2894       if (0 == strcmp (dc->exchange_url,
   2895                        egsi->exchange_url))
   2896       {
   2897         wire_fee = &dc->wire_fee;
   2898         break;
   2899       }
   2900     }
   2901     if (NULL == wire_fee)
   2902     {
   2903       /* Exchange group without a single deposit? Strange! */
   2904       GNUNET_break (0);
   2905       continue;
   2906     }
   2907 
   2908     if (GNUNET_OK !=
   2909         TALER_amount_cmp_currency (&total_wire_fee,
   2910                                    wire_fee))
   2911     {
   2912       GNUNET_break_op (0);
   2913       pay_end (pc,
   2914                TALER_MHD_reply_with_error (pc->connection,
   2915                                            MHD_HTTP_BAD_REQUEST,
   2916                                            TALER_EC_GENERIC_CURRENCY_MISMATCH,
   2917                                            total_wire_fee.currency));
   2918       return false;
   2919     }
   2920     if (0 >
   2921         TALER_amount_add (&total_wire_fee,
   2922                           &total_wire_fee,
   2923                           wire_fee))
   2924     {
   2925       GNUNET_break (0);
   2926       pay_end (pc,
   2927                TALER_MHD_reply_with_error (
   2928                  pc->connection,
   2929                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   2930                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_EXCHANGE_WIRE_FEE_ADDITION_FAILED,
   2931                  "could not add exchange wire fee to total"));
   2932       return false;
   2933     }
   2934   }
   2935 
   2936   /**
   2937    * This loops calculates what are the deposit fee / total
   2938    * amount with fee / and wire fee, for all the coins.
   2939    */
   2940   GNUNET_assert (GNUNET_OK ==
   2941                  TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   2942                                         &acc_fee));
   2943   GNUNET_assert (GNUNET_OK ==
   2944                  TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   2945                                         &acc_amount));
   2946   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   2947   {
   2948     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   2949 
   2950     GNUNET_assert (dc->found_in_db);
   2951     if ( (GNUNET_OK !=
   2952           TALER_amount_cmp_currency (&acc_fee,
   2953                                      &dc->deposit_fee)) ||
   2954          (GNUNET_OK !=
   2955           TALER_amount_cmp_currency (&acc_amount,
   2956                                      &dc->cdd.amount)) )
   2957     {
   2958       GNUNET_break_op (0);
   2959       pay_end (pc,
   2960                TALER_MHD_reply_with_error (
   2961                  pc->connection,
   2962                  MHD_HTTP_BAD_REQUEST,
   2963                  TALER_EC_GENERIC_CURRENCY_MISMATCH,
   2964                  dc->deposit_fee.currency));
   2965       return false;
   2966     }
   2967     if ( (0 >
   2968           TALER_amount_add (&acc_fee,
   2969                             &dc->deposit_fee,
   2970                             &acc_fee)) ||
   2971          (0 >
   2972           TALER_amount_add (&acc_amount,
   2973                             &dc->cdd.amount,
   2974                             &acc_amount)) )
   2975     {
   2976       GNUNET_break (0);
   2977       /* Overflow in these amounts? Very strange. */
   2978       pay_end (pc,
   2979                TALER_MHD_reply_with_error (
   2980                  pc->connection,
   2981                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   2982                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AMOUNT_OVERFLOW,
   2983                  "Overflow adding up amounts"));
   2984       return false;
   2985     }
   2986     if (1 ==
   2987         TALER_amount_cmp (&dc->deposit_fee,
   2988                           &dc->cdd.amount))
   2989     {
   2990       GNUNET_break_op (0);
   2991       pay_end (pc,
   2992                TALER_MHD_reply_with_error (
   2993                  pc->connection,
   2994                  MHD_HTTP_BAD_REQUEST,
   2995                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_FEES_EXCEED_PAYMENT,
   2996                  "Deposit fees exceed coin's contribution"));
   2997       return false;
   2998     }
   2999   } /* end deposit loop */
   3000 
   3001   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3002               "Amount received from wallet: %s\n",
   3003               TALER_amount2s (&acc_amount));
   3004   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3005               "Deposit fee for all coins: %s\n",
   3006               TALER_amount2s (&acc_fee));
   3007   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3008               "Total wire fee: %s\n",
   3009               TALER_amount2s (&total_wire_fee));
   3010   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3011               "Deposit fee limit for merchant: %s\n",
   3012               TALER_amount2s (&pc->validate_tokens.max_fee));
   3013   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
   3014               "Total refunded amount: %s\n",
   3015               TALER_amount2s (&pc->pay_transaction.total_refunded));
   3016 
   3017   /* Now compare exchange wire fee compared to what we are willing to pay */
   3018   if (GNUNET_YES !=
   3019       TALER_amount_cmp_currency (&total_wire_fee,
   3020                                  &acc_fee))
   3021   {
   3022     GNUNET_break (0);
   3023     pay_end (pc,
   3024              TALER_MHD_reply_with_error (
   3025                pc->connection,
   3026                MHD_HTTP_BAD_REQUEST,
   3027                TALER_EC_GENERIC_CURRENCY_MISMATCH,
   3028                total_wire_fee.currency));
   3029     return false;
   3030   }
   3031 
   3032   /* add wire fee to the total fees */
   3033   if (0 >
   3034       TALER_amount_add (&acc_fee,
   3035                         &acc_fee,
   3036                         &total_wire_fee))
   3037   {
   3038     GNUNET_break (0);
   3039     pay_end (pc,
   3040              TALER_MHD_reply_with_error (
   3041                pc->connection,
   3042                MHD_HTTP_INTERNAL_SERVER_ERROR,
   3043                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AMOUNT_OVERFLOW,
   3044                "Overflow adding up amounts"));
   3045     return false;
   3046   }
   3047   if (-1 == TALER_amount_cmp (&pc->validate_tokens.max_fee,
   3048                               &acc_fee))
   3049   {
   3050     /**
   3051      * Sum of fees of *all* the different exchanges of all the coins are
   3052      * higher than the fixed limit that the merchant is willing to pay.  The
   3053      * difference must be paid by the customer.
   3054      */
   3055     struct TALER_Amount excess_fee;
   3056 
   3057     /* compute fee amount to be covered by customer */
   3058     GNUNET_assert (TALER_AAR_RESULT_POSITIVE ==
   3059                    TALER_amount_subtract (&excess_fee,
   3060                                           &acc_fee,
   3061                                           &pc->validate_tokens.max_fee));
   3062     /* add that to the total */
   3063     if (0 >
   3064         TALER_amount_add (&total_needed,
   3065                           &excess_fee,
   3066                           &pc->validate_tokens.brutto))
   3067     {
   3068       GNUNET_break (0);
   3069       pay_end (pc,
   3070                TALER_MHD_reply_with_error (
   3071                  pc->connection,
   3072                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   3073                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AMOUNT_OVERFLOW,
   3074                  "Overflow adding up amounts"));
   3075       return false;
   3076     }
   3077   }
   3078   else
   3079   {
   3080     /* Fees are fully covered by the merchant, all we require
   3081        is that the total payment is not below the contract's amount */
   3082     total_needed = pc->validate_tokens.brutto;
   3083   }
   3084 
   3085   /* Do not count refunds towards the payment */
   3086   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3087               "Subtracting total refunds from paid amount: %s\n",
   3088               TALER_amount2s (&pc->pay_transaction.total_refunded));
   3089   if (0 >
   3090       TALER_amount_subtract (&final_amount,
   3091                              &acc_amount,
   3092                              &pc->pay_transaction.total_refunded))
   3093   {
   3094     GNUNET_break (0);
   3095     pay_end (pc,
   3096              TALER_MHD_reply_with_error (
   3097                pc->connection,
   3098                MHD_HTTP_INTERNAL_SERVER_ERROR,
   3099                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_REFUNDS_EXCEED_PAYMENTS,
   3100                "refunded amount exceeds total payments"));
   3101     return false;
   3102   }
   3103 
   3104   if (-1 == TALER_amount_cmp (&final_amount,
   3105                               &total_needed))
   3106   {
   3107     /* acc_amount < total_needed */
   3108     if (-1 < TALER_amount_cmp (&acc_amount,
   3109                                &total_needed))
   3110     {
   3111       GNUNET_break_op (0);
   3112       pay_end (pc,
   3113                TALER_MHD_reply_with_error (
   3114                  pc->connection,
   3115                  MHD_HTTP_PAYMENT_REQUIRED,
   3116                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_REFUNDED,
   3117                  "contract not paid up due to refunds"));
   3118       return false;
   3119     }
   3120     if (-1 < TALER_amount_cmp (&acc_amount,
   3121                                &pc->validate_tokens.brutto))
   3122     {
   3123       GNUNET_break_op (0);
   3124       pay_end (pc,
   3125                TALER_MHD_reply_with_error (
   3126                  pc->connection,
   3127                  MHD_HTTP_BAD_REQUEST,
   3128                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_INSUFFICIENT_DUE_TO_FEES,
   3129                  "contract not paid up due to fees (client may have calculated them badly)"));
   3130       return false;
   3131     }
   3132     GNUNET_break_op (0);
   3133     pay_end (pc,
   3134              TALER_MHD_reply_with_error (
   3135                pc->connection,
   3136                MHD_HTTP_BAD_REQUEST,
   3137                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_PAYMENT_INSUFFICIENT,
   3138                "payment insufficient"));
   3139     return false;
   3140   }
   3141   return true;
   3142 }
   3143 
   3144 
   3145 /**
   3146  * Execute the DB transaction.  If required (from
   3147  * soft/serialization errors), the transaction can be
   3148  * restarted here.
   3149  *
   3150  * @param[in,out] pc payment context to transact
   3151  */
   3152 static void
   3153 phase_execute_pay_transaction (struct PayContext *pc)
   3154 {
   3155   struct TMH_HandlerContext *hc = pc->hc;
   3156   const char *instance_id = hc->instance->settings.id;
   3157 
   3158   if (pc->batch_deposits.got_451)
   3159   {
   3160     pc->phase = PP_FAIL_LEGAL_REASONS;
   3161     return;
   3162   }
   3163   /* Avoid re-trying transactions on soft errors forever! */
   3164   if (pc->pay_transaction.retry_counter++ > MAX_RETRIES)
   3165   {
   3166     GNUNET_break (0);
   3167     pay_end (pc,
   3168              TALER_MHD_reply_with_error (pc->connection,
   3169                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
   3170                                          TALER_EC_GENERIC_DB_SOFT_FAILURE,
   3171                                          NULL));
   3172     return;
   3173   }
   3174 
   3175   /* Initialize some amount accumulators
   3176      (used in check_coin_paid(), check_coin_refunded()
   3177      and check_payment_sufficient()). */
   3178   GNUNET_break (GNUNET_OK ==
   3179                 TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   3180                                        &pc->pay_transaction.total_paid));
   3181   GNUNET_break (GNUNET_OK ==
   3182                 TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   3183                                        &pc->pay_transaction.total_fees_paid));
   3184   GNUNET_break (GNUNET_OK ==
   3185                 TALER_amount_set_zero (pc->validate_tokens.brutto.currency,
   3186                                        &pc->pay_transaction.total_refunded));
   3187   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   3188     pc->parse_pay.dc[i].found_in_db = false;
   3189   pc->pay_transaction.pending = pc->parse_pay.coins_cnt;
   3190 
   3191   /* First, try to see if we have all we need already done */
   3192   TALER_MERCHANTDB_preflight (TMH_db);
   3193   if (GNUNET_OK !=
   3194       TALER_MERCHANTDB_start (TMH_db,
   3195                               "run pay"))
   3196   {
   3197     GNUNET_break (0);
   3198     pay_end (pc,
   3199              TALER_MHD_reply_with_error (pc->connection,
   3200                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
   3201                                          TALER_EC_GENERIC_DB_START_FAILED,
   3202                                          NULL));
   3203     return;
   3204   }
   3205 
   3206   for (size_t i = 0; i<pc->parse_pay.tokens_cnt; i++)
   3207   {
   3208     struct TokenUseConfirmation *tuc = &pc->parse_pay.tokens[i];
   3209     enum GNUNET_DB_QueryStatus qs;
   3210     bool no_family;
   3211 
   3212     /* Insert used token into database, the unique constraint will
   3213        case an error if this token was used before. */
   3214     qs = TALER_MERCHANTDB_insert_used_token (TMH_db,
   3215                                              &pc->check_contract.h_contract_terms,
   3216                                              &tuc->h_issue,
   3217                                              &tuc->pub,
   3218                                              &tuc->sig,
   3219                                              &tuc->unblinded_sig,
   3220                                              &no_family);
   3221 
   3222     switch (qs)
   3223     {
   3224     case GNUNET_DB_STATUS_SOFT_ERROR:
   3225       TALER_MERCHANTDB_rollback (TMH_db);
   3226       return; /* do it again */
   3227     case GNUNET_DB_STATUS_HARD_ERROR:
   3228       /* Always report on hard error as well to enable diagnostics */
   3229       TALER_MERCHANTDB_rollback (TMH_db);
   3230       pay_end (pc,
   3231                TALER_MHD_reply_with_error (pc->connection,
   3232                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3233                                            TALER_EC_GENERIC_DB_STORE_FAILED,
   3234                                            "insert used token"));
   3235       return;
   3236     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   3237       TALER_MERCHANTDB_rollback (TMH_db);
   3238       if (no_family)
   3239       {
   3240         /* The token family key was deleted after the order was created,
   3241            so we cannot accept this token anymore. */
   3242         GNUNET_break_op (0);
   3243         pay_end (pc,
   3244                  TALER_MHD_reply_with_error (
   3245                    pc->connection,
   3246                    MHD_HTTP_NOT_FOUND,
   3247                    TALER_EC_MERCHANT_GENERIC_TOKEN_KEY_UNKNOWN,
   3248                    NULL));
   3249         return;
   3250       }
   3251       /* UNIQUE constraint violation, meaning this token was already used. */
   3252       pay_end (pc,
   3253                TALER_MHD_reply_with_error (pc->connection,
   3254                                            MHD_HTTP_CONFLICT,
   3255                                            TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_INVALID,
   3256                                            NULL));
   3257       return;
   3258     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   3259       /* Good, proceed! */
   3260       break;
   3261     }
   3262   } /* for all tokens */
   3263 
   3264   {
   3265     enum GNUNET_DB_QueryStatus qs;
   3266 
   3267     /* Check if some of these coins already succeeded for _this_ contract.  */
   3268     qs = TALER_MERCHANTDB_iterate_deposits (TMH_db,
   3269                                             instance_id,
   3270                                             &pc->check_contract.h_contract_terms,
   3271                                             &check_coin_paid,
   3272                                             pc);
   3273     if (0 > qs)
   3274     {
   3275       TALER_MERCHANTDB_rollback (TMH_db);
   3276       if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   3277         return; /* do it again */
   3278       /* Always report on hard error as well to enable diagnostics */
   3279       GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
   3280       pay_end (pc,
   3281                TALER_MHD_reply_with_error (
   3282                  pc->connection,
   3283                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   3284                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   3285                  "lookup deposits"));
   3286       return;
   3287     }
   3288     if (pc->pay_transaction.deposit_currency_mismatch)
   3289     {
   3290       TALER_MERCHANTDB_rollback (TMH_db);
   3291       GNUNET_break_op (0);
   3292       pay_end (pc,
   3293                TALER_MHD_reply_with_error (
   3294                  pc->connection,
   3295                  MHD_HTTP_BAD_REQUEST,
   3296                  TALER_EC_MERCHANT_GENERIC_CURRENCY_MISMATCH,
   3297                  pc->validate_tokens.brutto.currency));
   3298       return;
   3299     }
   3300   }
   3301 
   3302   {
   3303     enum GNUNET_DB_QueryStatus qs;
   3304 
   3305     /* Check if we refunded some of the coins */
   3306     qs = TALER_MERCHANTDB_iterate_refunds (TMH_db,
   3307                                            instance_id,
   3308                                            &pc->check_contract.h_contract_terms,
   3309                                            &check_coin_refunded,
   3310                                            pc);
   3311     if (0 > qs)
   3312     {
   3313       TALER_MERCHANTDB_rollback (TMH_db);
   3314       if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   3315         return; /* do it again */
   3316       /* Always report on hard error as well to enable diagnostics */
   3317       GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
   3318       pay_end (pc,
   3319                TALER_MHD_reply_with_error (pc->connection,
   3320                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3321                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
   3322                                            "lookup refunds"));
   3323       return;
   3324     }
   3325     if (pc->pay_transaction.refund_currency_mismatch)
   3326     {
   3327       TALER_MERCHANTDB_rollback (TMH_db);
   3328       pay_end (pc,
   3329                TALER_MHD_reply_with_error (pc->connection,
   3330                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3331                                            TALER_EC_GENERIC_DB_FETCH_FAILED,
   3332                                            "refund currency in database does not match order currency"));
   3333       return;
   3334     }
   3335   }
   3336 
   3337   /* Check if there are coins that still need to be processed */
   3338   if (0 != pc->pay_transaction.pending)
   3339   {
   3340     /* we made no DB changes, so we can just rollback */
   3341     TALER_MERCHANTDB_rollback (TMH_db);
   3342     /* Ok, we need to first go to the network to process more coins.
   3343        We that interaction in *tiny* transactions (hence the rollback
   3344        above). */
   3345     pc->phase = PP_BATCH_DEPOSITS;
   3346     return;
   3347   }
   3348 
   3349   /* 0 == pc->pay_transaction.pending: all coins processed, let's see if that was enough */
   3350   if (! check_payment_sufficient (pc))
   3351   {
   3352     /* check_payment_sufficient() will have queued an error already.
   3353        We need to still abort the transaction. */
   3354     TALER_MERCHANTDB_rollback (TMH_db);
   3355     return;
   3356   }
   3357   /* Payment succeeded, save in database */
   3358   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3359               "Order `%s' (%s) was fully paid\n",
   3360               pc->order_id,
   3361               GNUNET_h2s (&pc->check_contract.h_contract_terms.hash));
   3362   {
   3363     enum GNUNET_DB_QueryStatus qs;
   3364 
   3365     qs = TALER_MERCHANTDB_update_to_contract_terms_paid (TMH_db,
   3366                                                          instance_id,
   3367                                                          &pc->check_contract.h_contract_terms,
   3368                                                          pc->parse_pay.session_id,
   3369                                                          pc->parse_wallet_data.choice_index);
   3370     if (qs < 0)
   3371     {
   3372       TALER_MERCHANTDB_rollback (TMH_db);
   3373       if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   3374         return; /* do it again */
   3375       GNUNET_break (0);
   3376       pay_end (pc,
   3377                TALER_MHD_reply_with_error (pc->connection,
   3378                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3379                                            TALER_EC_GENERIC_DB_STORE_FAILED,
   3380                                            "mark contract paid"));
   3381       return;
   3382     }
   3383     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3384                 "Marked contract paid returned %d\n",
   3385                 (int) qs);
   3386 
   3387     if ( (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) &&
   3388          (0 < pc->compute_money_pots.num_pots) )
   3389     {
   3390       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3391                   "Incrementing %u money pots by %s\n",
   3392                   pc->compute_money_pots.num_pots,
   3393                   TALER_amount2s (&pc->compute_money_pots.increments[0]));
   3394       qs = TALER_MERCHANTDB_update_money_pot_totals (
   3395         TMH_db,
   3396         instance_id,
   3397         pc->compute_money_pots.num_pots,
   3398         pc->compute_money_pots.pots,
   3399         pc->compute_money_pots.increments);
   3400       switch (qs)
   3401       {
   3402       case GNUNET_DB_STATUS_SOFT_ERROR:
   3403         TALER_MERCHANTDB_rollback (TMH_db);
   3404         return; /* do it again */
   3405       case GNUNET_DB_STATUS_HARD_ERROR:
   3406         /* Always report on hard error as well to enable diagnostics */
   3407         TALER_MERCHANTDB_rollback (TMH_db);
   3408         pay_end (pc,
   3409                  TALER_MHD_reply_with_error (
   3410                    pc->connection,
   3411                    MHD_HTTP_INTERNAL_SERVER_ERROR,
   3412                    TALER_EC_GENERIC_DB_STORE_FAILED,
   3413                    "update_money_pot_totals"));
   3414         return;
   3415       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   3416         /* strange */
   3417         GNUNET_break (0);
   3418         break;
   3419       case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   3420         /* Good, proceed! */
   3421         break;
   3422       }
   3423     }
   3424   }
   3425 
   3426   {
   3427     const struct TALER_MERCHANT_ContractChoice *choice =
   3428       &pc->check_contract.contract_terms->pc->details.v1
   3429       .choices[pc->parse_wallet_data.choice_index];
   3430 
   3431     for (size_t i = 0; i<pc->output_tokens_len; i++)
   3432     {
   3433       unsigned int output_index;
   3434       enum TALER_MERCHANT_ContractOutputType type;
   3435 
   3436       output_index = pc->output_tokens[i].output_index;
   3437       GNUNET_assert (output_index < choice->outputs_len);
   3438       type = choice->outputs[output_index].type;
   3439       switch (type)
   3440       {
   3441       case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID:
   3442         /* Well, good luck getting here */
   3443         GNUNET_break (0);
   3444         pay_end (pc,
   3445                  TALER_MHD_reply_with_error (pc->connection,
   3446                                              MHD_HTTP_INTERNAL_SERVER_ERROR,
   3447                                              TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   3448                                              "invalid output type"));
   3449         break;
   3450       case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT:
   3451         /* We skip output tokens of donation receipts here, as they are handled in the
   3452          * phase_final_output_token_processing() callback from donau */
   3453         break;
   3454       case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN:
   3455         struct SignedOutputToken *output =
   3456           &pc->output_tokens[i];
   3457         enum GNUNET_DB_QueryStatus qs;
   3458         bool no_family;
   3459 
   3460         if (NULL == output->sig.signature)
   3461           continue; /* must have been optional and not provided by wallet */
   3462         qs = TALER_MERCHANTDB_insert_issued_token (
   3463           TMH_db,
   3464           &pc->check_contract.h_contract_terms,
   3465           &output->h_issue,
   3466           &output->sig,
   3467           &no_family);
   3468         switch (qs)
   3469         {
   3470         case GNUNET_DB_STATUS_HARD_ERROR:
   3471           TALER_MERCHANTDB_rollback (TMH_db);
   3472           GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
   3473           pay_end (pc,
   3474                    TALER_MHD_reply_with_error (
   3475                      pc->connection,
   3476                      MHD_HTTP_INTERNAL_SERVER_ERROR,
   3477                      TALER_EC_GENERIC_DB_STORE_FAILED,
   3478                      "insert output token"));
   3479           return;
   3480         case GNUNET_DB_STATUS_SOFT_ERROR:
   3481           /* Serialization failure, retry */
   3482           TALER_MERCHANTDB_rollback (TMH_db);
   3483           return;
   3484         case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   3485           TALER_MERCHANTDB_rollback (TMH_db);
   3486           if (no_family)
   3487           {
   3488             /* The token family key was deleted after the order was
   3489                created, so we cannot issue this token anymore. */
   3490             GNUNET_break_op (0);
   3491             pay_end (pc,
   3492                      TALER_MHD_reply_with_error (
   3493                        pc->connection,
   3494                        MHD_HTTP_NOT_FOUND,
   3495                        TALER_EC_MERCHANT_GENERIC_TOKEN_KEY_UNKNOWN,
   3496                        NULL));
   3497             return;
   3498           }
   3499           /* UNIQUE constraint violation, meaning this token was already used. */
   3500           pay_end (pc,
   3501                    TALER_MHD_reply_with_error (
   3502                      pc->connection,
   3503                      MHD_HTTP_INTERNAL_SERVER_ERROR,
   3504                      TALER_EC_GENERIC_DB_STORE_FAILED,
   3505                      "duplicate output token"));
   3506           return;
   3507         case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   3508           break;
   3509         }
   3510         break;
   3511       }
   3512     }
   3513   }
   3514 
   3515   TMH_notify_order_change (
   3516     hc->instance,
   3517     TMH_OSF_CLAIMED | TMH_OSF_PAID,
   3518     pc->check_contract.contract_terms->pc->timestamp,
   3519     pc->check_contract.order_serial);
   3520   {
   3521     enum GNUNET_DB_QueryStatus qs;
   3522     json_t *jhook;
   3523 
   3524     jhook = GNUNET_JSON_PACK (
   3525       GNUNET_JSON_pack_object_incref ("contract_terms",
   3526                                       pc->check_contract.contract_terms_json),
   3527       GNUNET_JSON_pack_string ("order_id",
   3528                                pc->order_id)
   3529       );
   3530     GNUNET_assert (NULL != jhook);
   3531     qs = TMH_trigger_webhook (pc->hc->instance->settings.id,
   3532                               "pay",
   3533                               jhook);
   3534     json_decref (jhook);
   3535     if (qs < 0)
   3536     {
   3537       TALER_MERCHANTDB_rollback (TMH_db);
   3538       if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   3539         return; /* do it again */
   3540       GNUNET_break (0);
   3541       pay_end (pc,
   3542                TALER_MHD_reply_with_error (pc->connection,
   3543                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3544                                            TALER_EC_GENERIC_DB_STORE_FAILED,
   3545                                            "failed to trigger webhooks"));
   3546       return;
   3547     }
   3548   }
   3549   {
   3550     enum GNUNET_DB_QueryStatus qs;
   3551 
   3552     /* Now commit! */
   3553     qs = TALER_MERCHANTDB_commit (TMH_db);
   3554     if (0 > qs)
   3555     {
   3556       /* commit failed */
   3557       TALER_MERCHANTDB_rollback (TMH_db);
   3558       if (GNUNET_DB_STATUS_SOFT_ERROR == qs)
   3559         return; /* do it again */
   3560       GNUNET_break (0);
   3561       pay_end (pc,
   3562                TALER_MHD_reply_with_error (pc->connection,
   3563                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   3564                                            TALER_EC_GENERIC_DB_COMMIT_FAILED,
   3565                                            NULL));
   3566       return;
   3567     }
   3568   }
   3569   pc->phase++;
   3570 }
   3571 
   3572 
   3573 /**
   3574  * Ensures that the expected number of tokens for a @e key
   3575  * are provided as inputs and have valid signatures.
   3576  *
   3577  * @param[in,out] pc payment context we are processing
   3578  * @param family family the tokens should be from
   3579  * @param index offset into parse_pay.tokens where the
   3580  *          input tokens for @a family should start
   3581  * @param expected_num number of tokens expected
   3582  * @return #GNUNET_YES on success
   3583  */
   3584 static enum GNUNET_GenericReturnValue
   3585 find_valid_input_tokens (
   3586   struct PayContext *pc,
   3587   const struct TALER_MERCHANT_ContractTokenFamily *family,
   3588   unsigned int index,
   3589   unsigned int expected_num)
   3590 {
   3591   unsigned int num_validated = 0;
   3592   struct GNUNET_TIME_Timestamp now
   3593     = GNUNET_TIME_timestamp_get ();
   3594   const struct TALER_MERCHANT_ContractTokenFamilyKey *kig = NULL;
   3595 
   3596   for (unsigned int j = 0; j < expected_num; j++)
   3597   {
   3598     struct TokenUseConfirmation *tuc;
   3599     const struct TALER_MERCHANT_ContractTokenFamilyKey *key = NULL;
   3600 
   3601     if (index + j >= pc->parse_pay.tokens_cnt)
   3602     {
   3603       /* There are not a sufficient number of input tokens left
   3604          to satisfy the request. Game over. */
   3605       GNUNET_break_op (0);
   3606       pay_end (pc,
   3607                TALER_MHD_reply_with_error (
   3608                  pc->connection,
   3609                  MHD_HTTP_BAD_REQUEST,
   3610                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_COUNT_MISMATCH,
   3611                  NULL));
   3612       return GNUNET_NO;
   3613     }
   3614     tuc = &pc->parse_pay.tokens[index + j];
   3615 
   3616     for (unsigned int i = 0; i<family->keys_len; i++)
   3617     {
   3618       const struct TALER_MERCHANT_ContractTokenFamilyKey *ki
   3619         = &family->keys[i];
   3620 
   3621       if (0 ==
   3622           GNUNET_memcmp (&ki->pub.public_key->pub_key_hash,
   3623                          &tuc->h_issue.hash))
   3624       {
   3625         if (GNUNET_TIME_timestamp_cmp (ki->valid_after,
   3626                                        >,
   3627                                        now) ||
   3628             GNUNET_TIME_timestamp_cmp (ki->valid_before,
   3629                                        <=,
   3630                                        now))
   3631         {
   3632           /* We have a match, but not in the current validity period */
   3633           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3634                       "Public key %s currently not valid\n",
   3635                       GNUNET_h2s (&ki->pub.public_key->pub_key_hash));
   3636           kig = ki;
   3637           continue;
   3638         }
   3639         key = ki;
   3640         break;
   3641       }
   3642     }
   3643     if (NULL == key)
   3644     {
   3645       if (NULL != kig)
   3646       {
   3647         char start_str[128];
   3648         char end_str[128];
   3649         char emsg[350];
   3650 
   3651         GNUNET_snprintf (start_str,
   3652                          sizeof (start_str),
   3653                          "%s",
   3654                          GNUNET_STRINGS_timestamp_to_string (kig->valid_after));
   3655         GNUNET_snprintf (end_str,
   3656                          sizeof (end_str),
   3657                          "%s",
   3658                          GNUNET_STRINGS_timestamp_to_string (kig->valid_before));
   3659         /* FIXME: use more specific EC */
   3660         GNUNET_snprintf (emsg,
   3661                          sizeof (emsg),
   3662                          "Token is only valid from %s to %s",
   3663                          start_str,
   3664                          end_str);
   3665         pay_end (pc,
   3666                  TALER_MHD_reply_with_error (
   3667                    pc->connection,
   3668                    MHD_HTTP_GONE,
   3669                    TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_OFFER_EXPIRED,
   3670                    emsg));
   3671         return GNUNET_NO;
   3672       }
   3673       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3674                   "Input token supplied for public key %s that is not acceptable\n",
   3675                   GNUNET_h2s (&tuc->h_issue.hash));
   3676       GNUNET_break_op (0);
   3677       pay_end (pc,
   3678                TALER_MHD_reply_with_error (
   3679                  pc->connection,
   3680                  MHD_HTTP_BAD_REQUEST,
   3681                  TALER_EC_MERCHANT_GENERIC_TOKEN_KEY_UNKNOWN,
   3682                  NULL));
   3683       return GNUNET_NO;
   3684     }
   3685     if (GNUNET_OK !=
   3686         TALER_token_issue_verify (&tuc->pub,
   3687                                   &key->pub,
   3688                                   &tuc->unblinded_sig))
   3689     {
   3690       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3691                   "Input token for public key with valid_after "
   3692                   "`%s' has invalid issue signature\n",
   3693                   GNUNET_TIME_timestamp2s (key->valid_after));
   3694       GNUNET_break (0);
   3695       pay_end (pc,
   3696                TALER_MHD_reply_with_error (
   3697                  pc->connection,
   3698                  MHD_HTTP_BAD_REQUEST,
   3699                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_ISSUE_SIG_INVALID,
   3700                  NULL));
   3701       return GNUNET_NO;
   3702     }
   3703 
   3704     if (GNUNET_OK !=
   3705         TALER_wallet_token_use_verify (&pc->check_contract.h_contract_terms,
   3706                                        &pc->parse_wallet_data.h_wallet_data,
   3707                                        &tuc->pub,
   3708                                        &tuc->sig))
   3709     {
   3710       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3711                   "Input token for public key with valid_before "
   3712                   "`%s' has invalid use signature\n",
   3713                   GNUNET_TIME_timestamp2s (key->valid_before));
   3714       GNUNET_break (0);
   3715       pay_end (pc,
   3716                TALER_MHD_reply_with_error (
   3717                  pc->connection,
   3718                  MHD_HTTP_BAD_REQUEST,
   3719                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_USE_SIG_INVALID,
   3720                  NULL));
   3721       return GNUNET_NO;
   3722     }
   3723     num_validated++;
   3724   }
   3725   GNUNET_assert (num_validated == expected_num);
   3726   return GNUNET_YES;
   3727 }
   3728 
   3729 
   3730 /**
   3731  * Check if an output token of the given @a tfk is mandatory, or if
   3732  * wallets are allowed to simply not support it and still proceed.
   3733  *
   3734  * @param tfk token family kind to check
   3735  * @return true if such outputs are mandatory and wallets must supply
   3736  *  the corresponding blinded input
   3737  */
   3738 /* FIXME: this function belongs into a lower-level lib! */
   3739 static bool
   3740 test_tfk_mandatory (enum TALER_MERCHANTDB_TokenFamilyKind tfk)
   3741 {
   3742   switch (tfk)
   3743   {
   3744   case TALER_MERCHANTDB_TFK_Discount:
   3745     return false;
   3746   case TALER_MERCHANTDB_TFK_Subscription:
   3747     return true;
   3748   }
   3749   GNUNET_break (0);
   3750   return false;
   3751 }
   3752 
   3753 
   3754 /**
   3755  * Sign the tokens provided by the wallet for a particular @a key.
   3756  *
   3757  * @param[in,out] pc reference for payment we are processing
   3758  * @param key token family data
   3759  * @param priv private key to use to sign with
   3760  * @param mandatory true if the token must exist, if false
   3761  *        and the client did not provide an envelope, that's OK and
   3762  *        we just also skimp on the signature
   3763  * @param wallet_index starting offset in the token envelopes array
   3764  * @param output_index starting offset into the output_tokens array
   3765  * @param expected_num number of tokens of this type that we should create
   3766  * @return #GNUNET_NO on failure
   3767  *         #GNUNET_OK on success
   3768  */
   3769 static enum GNUNET_GenericReturnValue
   3770 sign_token_envelopes (
   3771   struct PayContext *pc,
   3772   const struct TALER_MERCHANT_ContractTokenFamilyKey *key,
   3773   const struct TALER_TokenIssuePrivateKey *priv,
   3774   bool mandatory,
   3775   unsigned int wallet_index,
   3776   unsigned int output_index,
   3777   unsigned int expected_num)
   3778 {
   3779   unsigned int num_signed = 0;
   3780 
   3781   for (unsigned int j = 0; j<expected_num; j++)
   3782   {
   3783     unsigned int wallet_pos = wallet_index + j;
   3784     unsigned int output_pos = output_index + j;
   3785     const struct TokenEnvelope *env
   3786       = &pc->parse_wallet_data.token_envelopes[wallet_pos];
   3787     struct SignedOutputToken *output
   3788       = &pc->output_tokens[output_pos];
   3789 
   3790     if (wallet_pos >= pc->parse_wallet_data.token_envelopes_cnt)
   3791     {
   3792       if (! mandatory)
   3793         return GNUNET_OK; /* wallet input too short, we can live with it */
   3794 
   3795       /* mandatory token families require a token envelope, and
   3796          the wallet did not provide enough of them */
   3797       GNUNET_break_op (0);
   3798       pay_end (pc,
   3799                TALER_MHD_reply_with_error (
   3800                  pc->connection,
   3801                  MHD_HTTP_BAD_REQUEST,
   3802                  TALER_EC_GENERIC_PARAMETER_MALFORMED,
   3803                  "Token envelope for mandatory token family missing"));
   3804       return GNUNET_NO;
   3805     }
   3806     if (output_pos >= pc->output_tokens_len)
   3807     {
   3808       GNUNET_assert (0); /* this should not happen, we *computed*
   3809                             output_tokens_len to be big enough! */
   3810       return GNUNET_NO;
   3811     }
   3812     if (NULL == env->blinded_token.blinded_pub)
   3813     {
   3814       if (! mandatory)
   3815         continue;
   3816 
   3817       /* mandatory token families require a token envelope. */
   3818       GNUNET_break_op (0);
   3819       pay_end (pc,
   3820                TALER_MHD_reply_with_error (
   3821                  pc->connection,
   3822                  MHD_HTTP_BAD_REQUEST,
   3823                  TALER_EC_GENERIC_PARAMETER_MALFORMED,
   3824                  "Token envelope for mandatory token family missing"));
   3825       return GNUNET_NO;
   3826     }
   3827     TALER_token_issue_sign (priv,
   3828                             &env->blinded_token,
   3829                             &output->sig);
   3830     output->h_issue.hash
   3831       = key->pub.public_key->pub_key_hash;
   3832     num_signed++;
   3833   }
   3834 
   3835   if (mandatory &&
   3836       (num_signed != expected_num) )
   3837   {
   3838     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
   3839                 "Expected %d token envelopes for public key with valid_after "
   3840                 "'%s', but found %d\n",
   3841                 expected_num,
   3842                 GNUNET_TIME_timestamp2s (key->valid_after),
   3843                 num_signed);
   3844     GNUNET_break (0);
   3845     pay_end (pc,
   3846              TALER_MHD_reply_with_error (
   3847                pc->connection,
   3848                MHD_HTTP_BAD_REQUEST,
   3849                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_ENVELOPE_COUNT_MISMATCH,
   3850                NULL));
   3851     return GNUNET_NO;
   3852   }
   3853 
   3854   return GNUNET_OK;
   3855 }
   3856 
   3857 
   3858 /**
   3859  * Find the family entry for the family of the given @a slug
   3860  * in @a pc.
   3861  *
   3862  * @param[in] pc payment context to search
   3863  * @param slug slug to search for
   3864  * @return NULL if @a slug was not found
   3865  */
   3866 static const struct TALER_MERCHANT_ContractTokenFamily *
   3867 find_family (const struct PayContext *pc,
   3868              const char *slug)
   3869 {
   3870   for (unsigned int i = 0;
   3871        i < pc->check_contract.contract_terms->pc->details.v1.token_authorities_len;
   3872        i++)
   3873   {
   3874     const struct TALER_MERCHANT_ContractTokenFamily *tfi
   3875       = &pc->check_contract.contract_terms->pc->details.v1.token_authorities[i];
   3876 
   3877     if (0 == strcmp (tfi->slug,
   3878                      slug))
   3879     {
   3880       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   3881                   "Token family %s found with %u keys\n",
   3882                   slug,
   3883                   tfi->keys_len);
   3884       return tfi;
   3885     }
   3886   }
   3887   return NULL;
   3888 }
   3889 
   3890 
   3891 /**
   3892  * Handle contract output of type TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN.
   3893  * Looks up the token family, loads the matching private key,
   3894  * and signs the corresponding token envelopes from the wallet.
   3895  *
   3896  * @param[in,out] pc context for the pay request
   3897  * @param wallet_index start index of this output in the
   3898  *     ``parse_wallet_data.token_envelopes`` array
   3899  * @param output contract output we need to process
   3900  * @param output_index start index of this output in the
   3901  *     ``output_tokens`` array of @a pc
   3902  * @return #GNUNET_OK on success, #GNUNET_NO if an error was encountered
   3903  */
   3904 static enum GNUNET_GenericReturnValue
   3905 handle_output_token (struct PayContext *pc,
   3906                      unsigned int wallet_index,
   3907                      const struct TALER_MERCHANT_ContractOutput *output,
   3908                      unsigned int output_index)
   3909 {
   3910   const struct TALER_MERCHANT_ContractTokenFamily *family;
   3911   struct TALER_MERCHANT_ContractTokenFamilyKey *key;
   3912   struct TALER_MERCHANTDB_TokenFamilyKeyDetails details;
   3913   enum GNUNET_DB_QueryStatus qs;
   3914   bool mandatory;
   3915 
   3916   /* Locate token family in the contract.
   3917      This should ever fail as this invariant should
   3918      have been checked when the contract was created. */
   3919   family = find_family (pc,
   3920                         output->details.token.token_family_slug);
   3921   if (NULL == family)
   3922   {
   3923     /* This "should never happen", so treat it as an internal error */
   3924     GNUNET_break (0);
   3925     pay_end (pc,
   3926              TALER_MHD_reply_with_error (
   3927                pc->connection,
   3928                MHD_HTTP_INTERNAL_SERVER_ERROR,
   3929                TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   3930                "token family not found in order"));
   3931     return GNUNET_SYSERR;
   3932   }
   3933 
   3934   /* Check the key_index field from the output. */
   3935   if (output->details.token.key_index >= family->keys_len)
   3936   {
   3937     /* Also "should never happen", contract was presumably validated on insert */
   3938     GNUNET_break (0);
   3939     pay_end (pc,
   3940              TALER_MHD_reply_with_error (
   3941                pc->connection,
   3942                MHD_HTTP_INTERNAL_SERVER_ERROR,
   3943                TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   3944                "key index invalid for token family"));
   3945     return GNUNET_SYSERR;
   3946   }
   3947 
   3948   /* Pick the correct key inside that family. */
   3949   key = &family->keys[output->details.token.key_index];
   3950 
   3951   /* Fetch the private key from the DB for the merchant instance and
   3952    * this particular family/time interval. */
   3953   qs = TALER_MERCHANTDB_get_token_family_key (
   3954     TMH_db,
   3955     pc->hc->instance->settings.id,
   3956     family->slug,
   3957     pc->check_contract.contract_terms->pc->timestamp,
   3958     pc->check_contract.contract_terms->pc->pay_deadline,
   3959     &details);
   3960   switch (qs)
   3961   {
   3962   case GNUNET_DB_STATUS_HARD_ERROR:
   3963   case GNUNET_DB_STATUS_SOFT_ERROR:
   3964     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   3965                 "Database error looking up token-family key for %s\n",
   3966                 family->slug);
   3967     GNUNET_break (0);
   3968     pay_end (pc,
   3969              TALER_MHD_reply_with_error (
   3970                pc->connection,
   3971                MHD_HTTP_INTERNAL_SERVER_ERROR,
   3972                TALER_EC_GENERIC_DB_FETCH_FAILED,
   3973                NULL));
   3974     return GNUNET_NO;
   3975   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   3976     GNUNET_log (
   3977       GNUNET_ERROR_TYPE_ERROR,
   3978       "Token-family key for %s not found at [%llu,%llu]\n",
   3979       family->slug,
   3980       (unsigned long long)
   3981       pc->check_contract.contract_terms->pc->timestamp.abs_time.abs_value_us,
   3982       (unsigned long long)
   3983       pc->check_contract.contract_terms->pc->pay_deadline.abs_time.abs_value_us
   3984       );
   3985     GNUNET_break (0);
   3986     pay_end (pc,
   3987              TALER_MHD_reply_with_error (
   3988                pc->connection,
   3989                MHD_HTTP_NOT_FOUND,
   3990                TALER_EC_MERCHANT_GENERIC_TOKEN_KEY_UNKNOWN,
   3991                family->slug));
   3992     return GNUNET_NO;
   3993 
   3994   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   3995     break;
   3996   }
   3997   GNUNET_free (details.token_family.slug);
   3998   GNUNET_free (details.token_family.name);
   3999   GNUNET_free (details.token_family.description);
   4000   json_decref (details.token_family.description_i18n);
   4001   if (NULL != details.pub.public_key)
   4002     GNUNET_CRYPTO_blind_sign_pub_decref (details.pub.public_key);
   4003   GNUNET_free (details.token_family.cipher_spec);
   4004   if (NULL == details.priv.private_key)
   4005   {
   4006     /* The key must exist: the LEFT JOIN in get_token_family_key()
   4007        only yields a NULL private key if no key covers the validity
   4008        period *and* survives until the pay deadline, and POST /orders
   4009        guarantees exactly that before it commits the contract terms
   4010        (it extends the retention of an existing key or mints a new
   4011        one, see #11692). Kept as a safety net. */
   4012     GNUNET_break (0);
   4013     pay_end (pc,
   4014              TALER_MHD_reply_with_error (
   4015                pc->connection,
   4016                MHD_HTTP_INTERNAL_SERVER_ERROR,
   4017                TALER_EC_GENERIC_DB_INVARIANT_FAILURE,
   4018                "private token family key not found"));
   4019     return GNUNET_NO;
   4020 
   4021   }
   4022 
   4023   /* Depending on the token family, decide if the token envelope
   4024    * is mandatory or optional.  (Simplified logic here: adapt as needed.) */
   4025   mandatory = test_tfk_mandatory (details.token_family.kind);
   4026   /* Actually sign the number of token envelopes specified in 'count'.
   4027    * 'output_index' is the offset into the output_tokens while
   4028    * 'wallet_index' is the offset into parse_wallet_data.token_envelopes */
   4029   if (GNUNET_OK !=
   4030       sign_token_envelopes (pc,
   4031                             key,
   4032                             &details.priv,
   4033                             mandatory,
   4034                             wallet_index,
   4035                             output_index,
   4036                             output->details.token.count))
   4037   {
   4038     /* sign_token_envelopes() already queued up an error via pay_end() */
   4039     GNUNET_break_op (0);
   4040     GNUNET_CRYPTO_blind_sign_priv_decref (details.priv.private_key);
   4041     return GNUNET_NO;
   4042   }
   4043   GNUNET_CRYPTO_blind_sign_priv_decref (details.priv.private_key);
   4044   return GNUNET_OK;
   4045 }
   4046 
   4047 
   4048 /**
   4049  * Handle checks for contract output of type
   4050  * #TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT.
   4051  *
   4052  * @param pc context for the pay request
   4053  * @param output the contract output describing the donation receipt requirement
   4054  * @return #GNUNET_OK on success,
   4055  *         #GNUNET_NO if an error was already queued
   4056  */
   4057 static enum GNUNET_GenericReturnValue
   4058 handle_output_donation_receipt (
   4059   struct PayContext *pc,
   4060   const struct TALER_MERCHANT_ContractOutput *output)
   4061 {
   4062   enum GNUNET_GenericReturnValue ret;
   4063   bool listed = false;
   4064 
   4065   for (unsigned int i = 0;
   4066        i < output->details.donation_receipt.donau_urls_len;
   4067        i++)
   4068     if (0 == strcmp (output->details.donation_receipt.donau_urls[i],
   4069                      pc->parse_wallet_data.donau.donau_url))
   4070       listed = true;
   4071   if (! listed)
   4072   {
   4073     GNUNET_break_op (0);
   4074     pay_end (pc,
   4075              TALER_MHD_reply_with_error (
   4076                pc->connection,
   4077                MHD_HTTP_BAD_REQUEST,
   4078                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4079                "donau URL not accepted by contract"));
   4080     return GNUNET_NO;
   4081   }
   4082   ret = DONAU_get_donation_amount_from_bkps (
   4083     pc->parse_wallet_data.donau_keys,
   4084     pc->parse_wallet_data.bkps,
   4085     pc->parse_wallet_data.num_bkps,
   4086     pc->parse_wallet_data.donau.donation_year,
   4087     &pc->parse_wallet_data.donation_amount);
   4088   switch (ret)
   4089   {
   4090   case GNUNET_SYSERR:
   4091     GNUNET_break (0);
   4092     pay_end (pc,
   4093              TALER_MHD_reply_with_error (
   4094                pc->connection,
   4095                MHD_HTTP_INTERNAL_SERVER_ERROR,
   4096                TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   4097                NULL));
   4098     return GNUNET_NO;
   4099   case GNUNET_NO:
   4100     GNUNET_break_op (0);
   4101     pay_end (pc,
   4102              TALER_MHD_reply_with_error (
   4103                pc->connection,
   4104                MHD_HTTP_BAD_REQUEST,
   4105                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4106                "inconsistent bkps / donau keys"));
   4107     return GNUNET_NO;
   4108   case GNUNET_OK:
   4109     break;
   4110   }
   4111 
   4112   if (GNUNET_OK !=
   4113       TALER_amount_cmp_currency (&pc->parse_wallet_data.donation_amount,
   4114                                  &output->details.donation_receipt.amount))
   4115   {
   4116     GNUNET_break_op (0);
   4117     pay_end (pc,
   4118              TALER_MHD_reply_with_error (
   4119                pc->connection,
   4120                MHD_HTTP_BAD_REQUEST,
   4121                TALER_EC_GENERIC_CURRENCY_MISMATCH,
   4122                output->details.donation_receipt.amount.currency));
   4123     return GNUNET_NO;
   4124   }
   4125 
   4126   if (0 !=
   4127       TALER_amount_cmp (&pc->parse_wallet_data.donation_amount,
   4128                         &output->details.donation_receipt.amount))
   4129   {
   4130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   4131                 "Wallet amount: %s\n",
   4132                 TALER_amount2s (&pc->parse_wallet_data.donation_amount));
   4133     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
   4134                 "Donation receipt amount: %s\n",
   4135                 TALER_amount2s (&output->details.donation_receipt.amount));
   4136     GNUNET_break_op (0);
   4137     pay_end (pc,
   4138              TALER_MHD_reply_with_error (
   4139                pc->connection,
   4140                MHD_HTTP_CONFLICT,
   4141                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DONATION_AMOUNT_MISMATCH,
   4142                "donation amount mismatch"));
   4143     return GNUNET_NO;
   4144   }
   4145   {
   4146     struct TALER_Amount receipts_to_date;
   4147 
   4148     if (0 >
   4149         TALER_amount_add (&receipts_to_date,
   4150                           &pc->parse_wallet_data.charity_receipts_to_date,
   4151                           &pc->parse_wallet_data.donation_amount))
   4152     {
   4153       GNUNET_break (0);
   4154       pay_end (pc,
   4155                TALER_MHD_reply_with_error (pc->connection,
   4156                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
   4157                                            TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AMOUNT_OVERFLOW,
   4158                                            "adding donation amount"));
   4159       return GNUNET_NO;
   4160     }
   4161 
   4162     if (1 ==
   4163         TALER_amount_cmp (&receipts_to_date,
   4164                           &pc->parse_wallet_data.charity_max_per_year))
   4165     {
   4166       GNUNET_break_op (0);
   4167       pay_end (pc,
   4168                TALER_MHD_reply_with_error (pc->connection,
   4169                                            MHD_HTTP_CONFLICT,
   4170                                            TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_DONATION_AMOUNT_MISMATCH,
   4171                                            "donation limit exceeded"));
   4172       return GNUNET_NO;
   4173     }
   4174     pc->parse_wallet_data.charity_receipts_to_date = receipts_to_date;
   4175   }
   4176   return GNUNET_OK;
   4177 }
   4178 
   4179 
   4180 /**
   4181  * Count tokens produced by an output.
   4182  *
   4183  * @param pc pay context
   4184  * @param output output to consider
   4185  * @returns number of output tokens
   4186  */
   4187 static unsigned int
   4188 count_output_tokens (const struct PayContext *pc,
   4189                      const struct TALER_MERCHANT_ContractOutput *output)
   4190 {
   4191   switch (output->type)
   4192   {
   4193   case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID:
   4194     GNUNET_assert (0);
   4195     break;
   4196   case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN:
   4197     return output->details.token.count;
   4198   case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT:
   4199     return pc->parse_wallet_data.num_bkps;
   4200   }
   4201   /* Not reached. */
   4202   GNUNET_assert (0);
   4203 }
   4204 
   4205 
   4206 /**
   4207  * Validate tokens and token envelopes. First, we check if all tokens listed
   4208  * in the 'inputs' array of the selected choice are present in the 'tokens'
   4209  * array of the request. Then, we validate the signatures of each provided
   4210  * token.
   4211  *
   4212  * @param[in,out] pc context we use to handle the payment
   4213  */
   4214 static void
   4215 phase_validate_tokens (struct PayContext *pc)
   4216 {
   4217   /* We haven't seen a donau output yet. */
   4218   pc->validate_tokens.donau_output_index = -1;
   4219 
   4220   switch (pc->check_contract.contract_terms->pc->base->version)
   4221   {
   4222   case TALER_MERCHANT_CONTRACT_VERSION_0:
   4223     /* No tokens to validate */
   4224     pc->phase = PP_COMPUTE_MONEY_POTS;
   4225     pc->validate_tokens.max_fee
   4226       = pc->check_contract.contract_terms->pc->details.v0.max_fee;
   4227     pc->validate_tokens.brutto
   4228       = pc->check_contract.contract_terms->pc->details.v0.brutto;
   4229     break;
   4230   case TALER_MERCHANT_CONTRACT_VERSION_1:
   4231     {
   4232       const struct TALER_MERCHANT_ContractChoice *selected
   4233         = &pc->check_contract.contract_terms->pc->details.v1.choices[
   4234             pc->parse_wallet_data.choice_index];
   4235       unsigned int output_off;
   4236       unsigned int wallet_off;
   4237       unsigned int cnt;
   4238 
   4239       pc->validate_tokens.max_fee = selected->max_fee;
   4240       pc->validate_tokens.brutto = selected->amount;
   4241       wallet_off = 0;
   4242       for (unsigned int i = 0; i<selected->inputs_len; i++)
   4243       {
   4244         const struct TALER_MERCHANT_ContractInput *input
   4245           = &selected->inputs[i];
   4246         const struct TALER_MERCHANT_ContractTokenFamily *family;
   4247 
   4248         switch (input->type)
   4249         {
   4250         case TALER_MERCHANT_CONTRACT_INPUT_TYPE_INVALID:
   4251           GNUNET_break (0);
   4252           pay_end (pc,
   4253                    TALER_MHD_reply_with_error (
   4254                      pc->connection,
   4255                      MHD_HTTP_BAD_REQUEST,
   4256                      TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4257                      "input token type not valid"));
   4258           return;
   4259 #if FUTURE
   4260         case TALER_MERCHANT_CONTRACT_INPUT_TYPE_COIN:
   4261           GNUNET_break (0);
   4262           pay_end (pc,
   4263                    TALER_MHD_reply_with_error (
   4264                      pc->connection,
   4265                      MHD_HTTP_NOT_IMPLEMENTED,
   4266                      TALER_EC_MERCHANT_GENERIC_FEATURE_NOT_AVAILABLE,
   4267                      "token type not yet supported"));
   4268           return;
   4269 #endif
   4270         case TALER_MERCHANT_CONTRACT_INPUT_TYPE_TOKEN:
   4271           family = find_family (pc,
   4272                                 input->details.token.token_family_slug);
   4273           if (NULL == family)
   4274           {
   4275             /* this should never happen, since the choices and
   4276                token families are validated on insert. */
   4277             GNUNET_break (0);
   4278             pay_end (pc,
   4279                      TALER_MHD_reply_with_error (
   4280                        pc->connection,
   4281                        MHD_HTTP_INTERNAL_SERVER_ERROR,
   4282                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
   4283                        "token family not found in order"));
   4284             return;
   4285           }
   4286           if (GNUNET_NO ==
   4287               find_valid_input_tokens (pc,
   4288                                        family,
   4289                                        wallet_off,
   4290                                        input->details.token.count))
   4291           {
   4292             /* Error is already scheduled from find_valid_input_token. */
   4293             return;
   4294           }
   4295           wallet_off += input->details.token.count;
   4296         }
   4297       }
   4298 
   4299       /* calculate pc->output_tokens_len */
   4300       output_off = 0;
   4301       for (unsigned int i = 0; i<selected->outputs_len; i++)
   4302       {
   4303         const struct TALER_MERCHANT_ContractOutput *output
   4304           = &selected->outputs[i];
   4305 
   4306         switch (output->type)
   4307         {
   4308         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID:
   4309           GNUNET_assert (0);
   4310           break;
   4311         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN:
   4312           cnt = output->details.token.count;
   4313           if (output_off + cnt < output_off)
   4314           {
   4315             GNUNET_break_op (0);
   4316             pay_end (pc,
   4317                      TALER_MHD_reply_with_error (
   4318                        pc->connection,
   4319                        MHD_HTTP_BAD_REQUEST,
   4320                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4321                        "output token counter overflow"));
   4322             return;
   4323           }
   4324           output_off += cnt;
   4325           break;
   4326         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT:
   4327           /* check that this output type appears at most once */
   4328           if (pc->validate_tokens.donau_output_index >= 0)
   4329           {
   4330             /* This should have been prevented when the
   4331                contract was initially created */
   4332             GNUNET_break (0);
   4333             pay_end (pc,
   4334                      TALER_MHD_reply_with_error (
   4335                        pc->connection,
   4336                        MHD_HTTP_INTERNAL_SERVER_ERROR,
   4337                        TALER_EC_GENERIC_DB_INVARIANT_FAILURE,
   4338                        "two donau output sets in same contract"));
   4339             return;
   4340           }
   4341           pc->validate_tokens.donau_output_index = i;
   4342           if (output_off + pc->parse_wallet_data.num_bkps < output_off)
   4343           {
   4344             GNUNET_break_op (0);
   4345             pay_end (pc,
   4346                      TALER_MHD_reply_with_error (
   4347                        pc->connection,
   4348                        MHD_HTTP_BAD_REQUEST,
   4349                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4350                        "output token counter overflow"));
   4351             return;
   4352           }
   4353           output_off += pc->parse_wallet_data.num_bkps;
   4354           break;
   4355         }
   4356       }
   4357 
   4358 
   4359       pc->output_tokens_len = output_off;
   4360       pc->output_tokens
   4361         = GNUNET_new_array (pc->output_tokens_len,
   4362                             struct SignedOutputToken);
   4363 
   4364       /* calculate pc->output_tokens[].output_index */
   4365       output_off = 0; /* index into output_tokens */
   4366       for (unsigned int i = 0; i<selected->outputs_len; i++)
   4367       {
   4368         const struct TALER_MERCHANT_ContractOutput *output
   4369           = &selected->outputs[i];
   4370 
   4371         cnt = count_output_tokens (pc,
   4372                                    output);
   4373         for (unsigned int j = 0; j<cnt; j++)
   4374           pc->output_tokens[output_off + j].output_index = i;
   4375         output_off += cnt;
   4376       }
   4377 
   4378       /* compute non-donau outputs */
   4379       output_off = 0; /* index into output_tokens */
   4380       wallet_off = 0; /* index into parse_wallet_data.token_envelopes */
   4381       for (unsigned int i = 0; i<selected->outputs_len; i++)
   4382       {
   4383         const struct TALER_MERCHANT_ContractOutput *output
   4384           = &selected->outputs[i];
   4385 
   4386         switch (output->type)
   4387         {
   4388         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_INVALID:
   4389           GNUNET_assert (0);
   4390           break;
   4391         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_TOKEN:
   4392           cnt = output->details.token.count;
   4393           GNUNET_assert (output_off + cnt
   4394                          <= pc->output_tokens_len);
   4395           if (GNUNET_OK !=
   4396               handle_output_token (pc,
   4397                                    wallet_off,
   4398                                    output,
   4399                                    output_off))
   4400           {
   4401             /* Error is already scheduled from handle_output_token. */
   4402             return;
   4403           }
   4404           output_off += cnt;
   4405           wallet_off += cnt;
   4406           break;
   4407         case TALER_MERCHANT_CONTRACT_OUTPUT_TYPE_DONATION_RECEIPT:
   4408           if ( (0 != pc->parse_wallet_data.num_bkps) &&
   4409                (GNUNET_OK !=
   4410                 handle_output_donation_receipt (pc,
   4411                                                 output)) )
   4412           {
   4413             /* Error is already scheduled from handle_output_donation_receipt. */
   4414             return;
   4415           }
   4416           output_off += pc->parse_wallet_data.num_bkps;
   4417           /* Note: wallet_off NOT increased, as bkps are
   4418              separate from parse_wallet_data.token_envelopes */
   4419           continue;
   4420         } /* switch on output token */
   4421       } /* for all output token types */
   4422     } /* case contract v1 */
   4423     break;
   4424   } /* switch on contract type */
   4425 
   4426   if ( (0 != pc->parse_wallet_data.num_bkps) &&
   4427        (pc->validate_tokens.donau_output_index < 0) )
   4428   {
   4429     /* We must not have the charity issue receipts for a choice
   4430        that does not include a donation. */
   4431     GNUNET_break_op (0);
   4432     pay_end (pc,
   4433              TALER_MHD_reply_with_error (
   4434                pc->connection,
   4435                MHD_HTTP_BAD_REQUEST,
   4436                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   4437                "donation receipts requested, but choice has no donation output"));
   4438     return;
   4439   }
   4440 
   4441   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   4442   {
   4443     const struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   4444 
   4445     if (GNUNET_OK !=
   4446         TALER_amount_cmp_currency (&dc->cdd.amount,
   4447                                    &pc->validate_tokens.brutto))
   4448     {
   4449       GNUNET_break_op (0);
   4450       pay_end (pc,
   4451                TALER_MHD_reply_with_error (
   4452                  pc->connection,
   4453                  MHD_HTTP_CONFLICT,
   4454                  TALER_EC_MERCHANT_GENERIC_CURRENCY_MISMATCH,
   4455                  pc->validate_tokens.brutto.currency));
   4456       return;
   4457     }
   4458   }
   4459 
   4460   pc->phase = PP_COMPUTE_MONEY_POTS;
   4461 }
   4462 
   4463 
   4464 /**
   4465  * Function called with information about a coin that was deposited.
   4466  * Checks if this coin is in our list of deposits as well.
   4467  *
   4468  * @param cls closure with our `struct PayContext *`
   4469  * @param deposit_serial which deposit operation is this about
   4470  * @param exchange_url URL of the exchange that issued the coin
   4471  * @param h_wire hash of merchant's wire details
   4472  * @param deposit_timestamp when was the deposit made
   4473  * @param amount_with_fee amount the exchange will deposit for this coin
   4474  * @param deposit_fee fee the exchange will charge for this coin
   4475  * @param coin_pub public key of the coin
   4476  */
   4477 static void
   4478 deposit_paid_check (
   4479   void *cls,
   4480   uint64_t deposit_serial,
   4481   const char *exchange_url,
   4482   const struct TALER_MerchantWireHashP *h_wire,
   4483   struct GNUNET_TIME_Timestamp deposit_timestamp,
   4484   const struct TALER_Amount *amount_with_fee,
   4485   const struct TALER_Amount *deposit_fee,
   4486   const struct TALER_CoinSpendPublicKeyP *coin_pub)
   4487 {
   4488   struct PayContext *pc = cls;
   4489 
   4490   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   4491   {
   4492     struct DepositConfirmation *dci = &pc->parse_pay.dc[i];
   4493 
   4494     if (0 !=
   4495         GNUNET_memcmp (&dci->cdd.coin_pub,
   4496                        coin_pub))
   4497       continue;
   4498     dci->deposited_in_db = true;
   4499     if ( (0 ==
   4500           strcmp (dci->exchange_url,
   4501                   exchange_url)) &&
   4502          (GNUNET_YES ==
   4503           TALER_amount_cmp_currency (&dci->cdd.amount,
   4504                                      amount_with_fee)) &&
   4505          (0 ==
   4506           TALER_amount_cmp (&dci->cdd.amount,
   4507                             amount_with_fee)) )
   4508     {
   4509       dci->matched_in_db = true;
   4510       break;
   4511     }
   4512   }
   4513 }
   4514 
   4515 
   4516 /**
   4517  * Function called with information about a token that was spent.
   4518  * FIXME: Replace this with a more specific function for this cb
   4519  *
   4520  * @param cls closure with `struct PayContext *`
   4521  * @param spent_token_serial "serial" of the spent token unused
   4522  * @param h_contract_terms hash of the contract terms unused
   4523  * @param h_issue_pub hash of the token issue public key unused
   4524  * @param use_pub public key of the token
   4525  * @param use_sig signature of the token
   4526  * @param issue_sig signature of the token issue
   4527  */
   4528 static void
   4529 input_tokens_paid_check (
   4530   void *cls,
   4531   uint64_t spent_token_serial,
   4532   const struct TALER_PrivateContractHashP *h_contract_terms,
   4533   const struct TALER_TokenIssuePublicKeyHashP *h_issue_pub,
   4534   const struct TALER_TokenUsePublicKeyP *use_pub,
   4535   const struct TALER_TokenUseSignatureP *use_sig,
   4536   const struct TALER_TokenIssueSignature *issue_sig)
   4537 {
   4538   struct PayContext *pc = cls;
   4539 
   4540   for (size_t i = 0; i<pc->parse_pay.tokens_cnt; i++)
   4541   {
   4542     struct TokenUseConfirmation *tuc = &pc->parse_pay.tokens[i];
   4543 
   4544     if ( (0 ==
   4545           GNUNET_memcmp (&tuc->pub,
   4546                          use_pub)) &&
   4547          (0 ==
   4548           GNUNET_memcmp (&tuc->sig,
   4549                          use_sig)) &&
   4550          (0 ==
   4551           GNUNET_memcmp (&tuc->unblinded_sig,
   4552                          issue_sig)) )
   4553     {
   4554       tuc->found_in_db = true;
   4555       break;
   4556     }
   4557   }
   4558 }
   4559 
   4560 
   4561 /**
   4562  * Small helper function to append an output token signature from db
   4563  *
   4564  * @param cls closure with `struct PayContext *`
   4565  * @param h_issue hash of the token
   4566  * @param sig signature of the token
   4567  */
   4568 static void
   4569 append_output_token_sig (void *cls,
   4570                          struct GNUNET_HashCode *h_issue,
   4571                          struct GNUNET_CRYPTO_BlindedSignature *sig)
   4572 {
   4573   struct PayContext *pc = cls;
   4574   struct TALER_MERCHANT_ContractChoice *choice;
   4575   const struct TALER_MERCHANT_ContractOutput *output;
   4576   struct SignedOutputToken out;
   4577   unsigned int cnt;
   4578 
   4579   memset (&out,
   4580           0,
   4581           sizeof (out));
   4582   GNUNET_assert (TALER_MERCHANT_CONTRACT_VERSION_1 ==
   4583                  pc->check_contract.contract_terms->pc->base->version);
   4584   choice = &pc->check_contract.contract_terms->pc->details.v1
   4585            .choices[pc->parse_wallet_data.choice_index];
   4586   output = &choice->outputs[pc->output_index_gen];
   4587   cnt = count_output_tokens (pc,
   4588                              output);
   4589   out.output_index = pc->output_index_gen;
   4590   out.h_issue.hash = *h_issue;
   4591   out.sig.signature = sig;
   4592   GNUNET_CRYPTO_blind_sig_incref (sig);
   4593   GNUNET_array_append (pc->output_tokens,
   4594                        pc->output_tokens_len,
   4595                        out);
   4596   /* Go to next output once we've output all tokens for the current one. */
   4597   pc->output_token_cnt++;
   4598   if (pc->output_token_cnt >= cnt)
   4599   {
   4600     pc->output_token_cnt = 0;
   4601     pc->output_index_gen++;
   4602   }
   4603 }
   4604 
   4605 
   4606 /**
   4607  * Handle case where contract was already paid. Either decides
   4608  * the payment is idempotent, or refunds the excess payment.
   4609  *
   4610  * @param[in,out] pc context we use to handle the payment
   4611  */
   4612 static void
   4613 phase_contract_paid (struct PayContext *pc)
   4614 {
   4615   json_t *refunds;
   4616   bool unmatched = false;
   4617 
   4618   /* Just check if the choice provided with this payment round,
   4619      matches the previous one. Pretty much to tell the wallet, hey
   4620      you paid for another choice. */
   4621   if (TALER_MERCHANT_CONTRACT_VERSION_1 ==
   4622       pc->check_contract.contract_terms->pc->base->version)
   4623   {
   4624     enum GNUNET_DB_QueryStatus qs;
   4625     uint64_t order_serial;
   4626     bool paid;
   4627     bool wired;
   4628     bool session_matches;
   4629     int16_t paid_choice_index;
   4630 
   4631     qs = TALER_MERCHANTDB_get_contract_terms_status (
   4632       TMH_db,
   4633       pc->hc->instance->settings.id,
   4634       pc->order_id,
   4635       NULL,
   4636       NULL,
   4637       &order_serial,
   4638       &paid,
   4639       &wired,
   4640       &session_matches,
   4641       NULL,
   4642       &paid_choice_index);
   4643     if (0 > qs)
   4644     {
   4645       GNUNET_break (0);
   4646       pay_end (pc,
   4647                TALER_MHD_reply_with_error (
   4648                  pc->connection,
   4649                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   4650                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   4651                  "get_contract_terms_status"));
   4652       return;
   4653     }
   4654     if ( (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) &&
   4655          (paid_choice_index != pc->parse_wallet_data.choice_index) )
   4656     {
   4657       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4658                   "Order `%s' was paid with choice %d, not %d\n",
   4659                   pc->order_id,
   4660                   (int) paid_choice_index,
   4661                   (int) pc->parse_wallet_data.choice_index);
   4662       pay_end (pc,
   4663                TALER_MHD_REPLY_JSON_PACK (
   4664                  pc->connection,
   4665                  MHD_HTTP_CONFLICT,
   4666                  TALER_JSON_pack_ec (
   4667                    TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_CHOICE_INDEX_MISMATCH),
   4668                  GNUNET_JSON_pack_int64 ("choice_index",
   4669                                          paid_choice_index)));
   4670       return;
   4671     }
   4672   }
   4673 
   4674   {
   4675     enum GNUNET_DB_QueryStatus qs;
   4676 
   4677     qs = TALER_MERCHANTDB_iterate_deposits_by_order (TMH_db,
   4678                                                      pc->check_contract.order_serial,
   4679                                                      &deposit_paid_check,
   4680                                                      pc);
   4681     /* Since orders with choices can have a price of zero,
   4682        0 is also a valid query state */
   4683     if (qs < 0)
   4684     {
   4685       GNUNET_break (0);
   4686       pay_end (pc,
   4687                TALER_MHD_reply_with_error (
   4688                  pc->connection,
   4689                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   4690                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   4691                  "iterate_deposits_by_order"));
   4692       return;
   4693     }
   4694   }
   4695   for (size_t i = 0;
   4696        i<pc->parse_pay.coins_cnt && ! unmatched;
   4697        i++)
   4698   {
   4699     struct DepositConfirmation *dci = &pc->parse_pay.dc[i];
   4700 
   4701     if (! dci->matched_in_db)
   4702       unmatched = true;
   4703   }
   4704   /* Check if provided input tokens match token in the database */
   4705   {
   4706     enum GNUNET_DB_QueryStatus qs;
   4707 
   4708     /* FIXME-Optimization: Maybe use h_contract instead of order_serial here? */
   4709     qs = TALER_MERCHANTDB_iterate_used_tokens_by_order (TMH_db,
   4710                                                         pc->check_contract.order_serial,
   4711                                                         &input_tokens_paid_check,
   4712                                                         pc);
   4713 
   4714     if (qs < 0)
   4715     {
   4716       GNUNET_break (0);
   4717       pay_end (pc,
   4718                TALER_MHD_reply_with_error (
   4719                  pc->connection,
   4720                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   4721                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   4722                  "iterate_used_tokens_by_order"));
   4723       return;
   4724     }
   4725   }
   4726   for (size_t i = 0; i<pc->parse_pay.tokens_cnt && ! unmatched; i++)
   4727   {
   4728     struct TokenUseConfirmation *tuc = &pc->parse_pay.tokens[i];
   4729 
   4730     if (! tuc->found_in_db)
   4731       unmatched = true;
   4732   }
   4733 
   4734   /* In this part we are fetching token_sigs related output */
   4735   if (! unmatched)
   4736   {
   4737     /* Everything fine, idempotent request, generate response immediately */
   4738     enum GNUNET_DB_QueryStatus qs;
   4739 
   4740     pc->output_index_gen = 0;
   4741     qs = TALER_MERCHANTDB_iterate_order_token_blinded_sigs (
   4742       TMH_db,
   4743       pc->order_id,
   4744       &append_output_token_sig,
   4745       pc);
   4746     if (0 > qs)
   4747     {
   4748       GNUNET_break (0);
   4749       pay_end (pc,
   4750                TALER_MHD_reply_with_error (
   4751                  pc->connection,
   4752                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   4753                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   4754                  "iterate_order_token_blinded_sigs"));
   4755       return;
   4756     }
   4757 
   4758     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4759                 "Idempotent pay request for order `%s', signing again\n",
   4760                 pc->order_id);
   4761     pc->phase = PP_SUCCESS_RESPONSE;
   4762     return;
   4763   }
   4764   /* Conflict, double-payment detected! */
   4765   /* FIXME-#8674: What should we do with input tokens?
   4766      Currently there is no refund for tokens. */
   4767   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4768               "Client attempted to pay extra for already paid order `%s'\n",
   4769               pc->order_id);
   4770   refunds = json_array ();
   4771   GNUNET_assert (NULL != refunds);
   4772   for (size_t i = 0; i<pc->parse_pay.coins_cnt; i++)
   4773   {
   4774     struct DepositConfirmation *dci = &pc->parse_pay.dc[i];
   4775     struct TALER_MerchantSignatureP merchant_sig;
   4776 
   4777     if (dci->deposited_in_db)
   4778       continue; /* part of the payment on file, never refund it here */
   4779     TALER_merchant_refund_sign (&dci->cdd.coin_pub,
   4780                                 &pc->check_contract.h_contract_terms,
   4781                                 0, /* rtransaction id */
   4782                                 &dci->cdd.amount,
   4783                                 &pc->hc->instance->merchant_priv,
   4784                                 &merchant_sig);
   4785     GNUNET_assert (
   4786       0 ==
   4787       json_array_append_new (
   4788         refunds,
   4789         GNUNET_JSON_PACK (
   4790           GNUNET_JSON_pack_data_auto (
   4791             "coin_pub",
   4792             &dci->cdd.coin_pub),
   4793           GNUNET_JSON_pack_data_auto (
   4794             "merchant_sig",
   4795             &merchant_sig),
   4796           TALER_JSON_pack_amount ("amount",
   4797                                   &dci->cdd.amount),
   4798           GNUNET_JSON_pack_uint64 ("rtransaction_id",
   4799                                    0))));
   4800   }
   4801   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4802               "Generating JSON response with code %d\n",
   4803               (int) TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_ALREADY_PAID);
   4804   pay_end (pc,
   4805            TALER_MHD_REPLY_JSON_PACK (
   4806              pc->connection,
   4807              MHD_HTTP_CONFLICT,
   4808              TALER_MHD_PACK_EC (
   4809                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_ALREADY_PAID),
   4810              GNUNET_JSON_pack_array_steal ("refunds",
   4811                                            refunds)));
   4812 }
   4813 
   4814 
   4815 /**
   4816  * Check the database state for the given order.
   4817  * Schedules an error response in the connection on failure.
   4818  *
   4819  * @param[in,out] pc context we use to handle the payment
   4820  */
   4821 static void
   4822 phase_check_contract (struct PayContext *pc)
   4823 {
   4824   /* obtain contract terms */
   4825   enum GNUNET_DB_QueryStatus qs;
   4826   bool paid = false;
   4827 
   4828   if (NULL != pc->check_contract.contract_terms_json)
   4829   {
   4830     json_decref (pc->check_contract.contract_terms_json);
   4831     pc->check_contract.contract_terms_json = NULL;
   4832   }
   4833   if (NULL != pc->check_contract.contract_terms)
   4834   {
   4835     TALER_MERCHANT_contract_free (pc->check_contract.contract_terms);
   4836     pc->check_contract.contract_terms = NULL;
   4837   }
   4838   qs = TALER_MERCHANTDB_get_contract_terms_pos (
   4839     TMH_db,
   4840     pc->hc->instance->settings.id,
   4841     pc->order_id,
   4842     &pc->check_contract.contract_terms_json,
   4843     &pc->check_contract.order_serial,
   4844     &paid,
   4845     NULL,
   4846     &pc->check_contract.pos_key,
   4847     &pc->check_contract.pos_alg,
   4848     &pc->check_contract.pos_challenge);
   4849   if (0 > qs)
   4850   {
   4851     /* single, read-only SQL statements should never cause
   4852        serialization problems */
   4853     GNUNET_break (GNUNET_DB_STATUS_SOFT_ERROR != qs);
   4854     /* Always report on hard error to enable diagnostics */
   4855     GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR == qs);
   4856     pay_end (pc,
   4857              TALER_MHD_reply_with_error (
   4858                pc->connection,
   4859                MHD_HTTP_INTERNAL_SERVER_ERROR,
   4860                TALER_EC_GENERIC_DB_FETCH_FAILED,
   4861                "contract terms"));
   4862     return;
   4863   }
   4864   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
   4865   {
   4866     pay_end (pc,
   4867              TALER_MHD_reply_with_error (
   4868                pc->connection,
   4869                MHD_HTTP_NOT_FOUND,
   4870                TALER_EC_MERCHANT_GENERIC_ORDER_UNKNOWN,
   4871                pc->order_id));
   4872     return;
   4873   }
   4874   /* hash contract (needed later) */
   4875 #if DEBUG
   4876   json_dumpf (pc->check_contract.contract_terms_json,
   4877               stderr,
   4878               JSON_INDENT (2));
   4879 #endif
   4880   if (GNUNET_OK !=
   4881       TALER_JSON_contract_hash (pc->check_contract.contract_terms_json,
   4882                                 &pc->check_contract.h_contract_terms))
   4883   {
   4884     GNUNET_break (0);
   4885     pay_end (pc,
   4886              TALER_MHD_reply_with_error (
   4887                pc->connection,
   4888                MHD_HTTP_INTERNAL_SERVER_ERROR,
   4889                TALER_EC_GENERIC_FAILED_COMPUTE_JSON_HASH,
   4890                NULL));
   4891     return;
   4892   }
   4893 
   4894   /* Parse the contract terms even for paid orders,
   4895      as later phases need it. */
   4896 
   4897   pc->check_contract.contract_terms = TALER_MERCHANT_contract_parse (
   4898     pc->check_contract.contract_terms_json);
   4899 
   4900   if (NULL == pc->check_contract.contract_terms)
   4901   {
   4902     /* invalid contract */
   4903     GNUNET_break (0);
   4904     pay_end (pc,
   4905              TALER_MHD_reply_with_error (
   4906                pc->connection,
   4907                MHD_HTTP_INTERNAL_SERVER_ERROR,
   4908                TALER_EC_MERCHANT_GENERIC_DB_CONTRACT_CONTENT_INVALID,
   4909                pc->order_id));
   4910     return;
   4911   }
   4912 
   4913   if (paid)
   4914   {
   4915     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4916                 "Order `%s' paid, checking for double-payment\n",
   4917                 pc->order_id);
   4918     pc->phase = PP_CONTRACT_PAID;
   4919     return;
   4920   }
   4921   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4922               "Handling payment for order `%s' with contract hash `%s'\n",
   4923               pc->order_id,
   4924               GNUNET_h2s (&pc->check_contract.h_contract_terms.hash));
   4925 
   4926   /* Check fundamentals */
   4927   {
   4928     switch (pc->check_contract.contract_terms->pc->base->version)
   4929     {
   4930     case TALER_MERCHANT_CONTRACT_VERSION_0:
   4931       {
   4932         if (pc->parse_wallet_data.choice_index > 0)
   4933         {
   4934           GNUNET_break (0);
   4935           pay_end (pc,
   4936                    TALER_MHD_reply_with_error (
   4937                      pc->connection,
   4938                      MHD_HTTP_BAD_REQUEST,
   4939                      TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_CHOICE_INDEX_OUT_OF_BOUNDS,
   4940                      "contract terms v0 has no choices"));
   4941           return;
   4942         }
   4943       }
   4944       break;
   4945     case TALER_MERCHANT_CONTRACT_VERSION_1:
   4946       {
   4947         if (pc->parse_wallet_data.choice_index < 0)
   4948         {
   4949           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   4950                       "Order `%s' has non-empty choices array but"
   4951                       "request is missing 'choice_index' field\n",
   4952                       pc->order_id);
   4953           GNUNET_break (0);
   4954           pay_end (pc,
   4955                    TALER_MHD_reply_with_error (
   4956                      pc->connection,
   4957                      MHD_HTTP_BAD_REQUEST,
   4958                      TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_CHOICE_INDEX_MISSING,
   4959                      NULL));
   4960           return;
   4961         }
   4962         if (pc->parse_wallet_data.choice_index >=
   4963             pc->check_contract.contract_terms->pc->details.v1.choices_len)
   4964         {
   4965           GNUNET_log (
   4966             GNUNET_ERROR_TYPE_INFO,
   4967             "Order `%s' has choices array with %u elements but "
   4968             "request has 'choice_index' field with value %d\n",
   4969             pc->order_id,
   4970             pc->check_contract.contract_terms->pc->details.v1.choices_len,
   4971             pc->parse_wallet_data.choice_index);
   4972           GNUNET_break (0);
   4973           pay_end (pc,
   4974                    TALER_MHD_reply_with_error (
   4975                      pc->connection,
   4976                      MHD_HTTP_BAD_REQUEST,
   4977                      TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_CHOICE_INDEX_OUT_OF_BOUNDS,
   4978                      NULL));
   4979           return;
   4980         }
   4981       }
   4982       break;
   4983     default:
   4984       GNUNET_break (0);
   4985       pay_end (pc,
   4986                TALER_MHD_reply_with_error (
   4987                  pc->connection,
   4988                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   4989                  TALER_EC_GENERIC_DB_FETCH_FAILED,
   4990                  "contract 'version' in database not supported by this backend")
   4991                );
   4992       return;
   4993     }
   4994   }
   4995 
   4996   if (GNUNET_TIME_timestamp_cmp (
   4997         pc->check_contract.contract_terms->pc->wire_deadline,
   4998         <,
   4999         pc->check_contract.contract_terms->pc->refund_deadline))
   5000   {
   5001     /* This should already have been checked when creating the order! */
   5002     GNUNET_break (0);
   5003     pay_end (pc,
   5004              TALER_MHD_reply_with_error (
   5005                pc->connection,
   5006                MHD_HTTP_INTERNAL_SERVER_ERROR,
   5007                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_REFUND_DEADLINE_PAST_WIRE_TRANSFER_DEADLINE,
   5008                NULL));
   5009     return;
   5010   }
   5011   if (GNUNET_TIME_absolute_is_past (
   5012         pc->check_contract.contract_terms->pc->pay_deadline.abs_time))
   5013   {
   5014     /* too late */
   5015     pay_end (pc,
   5016              TALER_MHD_reply_with_error (
   5017                pc->connection,
   5018                MHD_HTTP_GONE,
   5019                TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_OFFER_EXPIRED,
   5020                NULL));
   5021     return;
   5022   }
   5023 
   5024 /* Make sure wire method (still) exists for this instance */
   5025   {
   5026     struct TMH_WireMethod *wm;
   5027 
   5028     wm = pc->hc->instance->wm_head;
   5029     while ( (NULL != wm) &&
   5030             (0 !=
   5031              GNUNET_memcmp (
   5032                &pc->check_contract.contract_terms->pc->h_wire,
   5033                &wm->h_wire)) )
   5034       wm = wm->next;
   5035     if (NULL == wm)
   5036     {
   5037       GNUNET_break (0);
   5038       pay_end (pc,
   5039                TALER_MHD_reply_with_error (
   5040                  pc->connection,
   5041                  MHD_HTTP_INTERNAL_SERVER_ERROR,
   5042                  TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_WIRE_HASH_UNKNOWN,
   5043                  NULL));
   5044       return;
   5045     }
   5046     pc->check_contract.wm = wm;
   5047   }
   5048   pc->phase = PP_VALIDATE_TOKENS;
   5049 }
   5050 
   5051 
   5052 /**
   5053  * Try to parse the wallet_data object of the pay request into
   5054  * the given context. Schedules an error response in the connection
   5055  * on failure.
   5056  *
   5057  * @param[in,out] pc context we use to handle the payment
   5058  */
   5059 static void
   5060 phase_parse_wallet_data (struct PayContext *pc)
   5061 {
   5062   const json_t *tokens_evs;
   5063   const json_t *donau_obj;
   5064 
   5065   struct GNUNET_JSON_Specification spec[] = {
   5066     GNUNET_JSON_spec_mark_optional (
   5067       GNUNET_JSON_spec_int16 ("choice_index",
   5068                               &pc->parse_wallet_data.choice_index),
   5069       NULL),
   5070     GNUNET_JSON_spec_mark_optional (
   5071       GNUNET_JSON_spec_array_const ("tokens_evs",
   5072                                     &tokens_evs),
   5073       NULL),
   5074     GNUNET_JSON_spec_mark_optional (
   5075       GNUNET_JSON_spec_object_const ("donau",
   5076                                      &donau_obj),
   5077       NULL),
   5078     GNUNET_JSON_spec_end ()
   5079   };
   5080 
   5081   pc->parse_wallet_data.choice_index = -1;
   5082   if (NULL == pc->parse_pay.wallet_data)
   5083   {
   5084     pc->phase = PP_CHECK_CONTRACT;
   5085     return;
   5086   }
   5087   {
   5088     enum GNUNET_GenericReturnValue res;
   5089 
   5090     res = TALER_MHD_parse_json_data (pc->connection,
   5091                                      pc->parse_pay.wallet_data,
   5092                                      spec);
   5093     if (GNUNET_YES != res)
   5094     {
   5095       GNUNET_break_op (0);
   5096       pay_end (pc,
   5097                (GNUNET_NO == res)
   5098              ? MHD_YES
   5099              : MHD_NO);
   5100       return;
   5101     }
   5102   }
   5103 
   5104   pc->parse_wallet_data.token_envelopes_cnt
   5105     = json_array_size (tokens_evs);
   5106   if (pc->parse_wallet_data.token_envelopes_cnt >
   5107       MAX_TOKEN_ALLOWED_OUTPUTS)
   5108   {
   5109     GNUNET_break_op (0);
   5110     pay_end (pc,
   5111              TALER_MHD_reply_with_error (
   5112                pc->connection,
   5113                MHD_HTTP_BAD_REQUEST,
   5114                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5115                "'tokens_evs' array too long"));
   5116     return;
   5117   }
   5118   pc->parse_wallet_data.token_envelopes
   5119     = GNUNET_new_array (pc->parse_wallet_data.token_envelopes_cnt,
   5120                         struct TokenEnvelope);
   5121 
   5122   {
   5123     unsigned int tokens_ev_index;
   5124     json_t *token_ev;
   5125 
   5126     json_array_foreach (tokens_evs,
   5127                         tokens_ev_index,
   5128                         token_ev)
   5129     {
   5130       struct TokenEnvelope *ev
   5131         = &pc->parse_wallet_data.token_envelopes[tokens_ev_index];
   5132       struct GNUNET_JSON_Specification ispec[] = {
   5133         TALER_JSON_spec_token_envelope (NULL,
   5134                                         &ev->blinded_token),
   5135         GNUNET_JSON_spec_end ()
   5136       };
   5137       enum GNUNET_GenericReturnValue res;
   5138 
   5139       if (json_is_null (token_ev))
   5140         continue;
   5141       res = TALER_MHD_parse_json_data (pc->connection,
   5142                                        token_ev,
   5143                                        ispec);
   5144       if (GNUNET_YES != res)
   5145       {
   5146         GNUNET_break_op (0);
   5147         pay_end (pc,
   5148                  (GNUNET_NO == res)
   5149                  ? MHD_YES
   5150                  : MHD_NO);
   5151         return;
   5152       }
   5153 
   5154       for (unsigned int j = 0; j<tokens_ev_index; j++)
   5155       {
   5156         const struct TokenEnvelope *pev
   5157           = &pc->parse_wallet_data.token_envelopes[j];
   5158 
   5159         if (NULL == pev->blinded_token.blinded_pub)
   5160           continue;
   5161         if (0 ==
   5162             GNUNET_CRYPTO_blinded_message_cmp (
   5163               ev->blinded_token.blinded_pub,
   5164               pev->blinded_token.blinded_pub))
   5165         {
   5166           GNUNET_break_op (0);
   5167           pay_end (pc,
   5168                    TALER_MHD_reply_with_error (
   5169                      pc->connection,
   5170                      MHD_HTTP_BAD_REQUEST,
   5171                      TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5172                      "duplicate token envelope in list"));
   5173           return;
   5174         }
   5175       }
   5176     }
   5177   }
   5178 
   5179   if (NULL != donau_obj)
   5180   {
   5181     const char *donau_url_tmp;
   5182     const json_t *budikeypairs;
   5183     json_t *donau_keys_json;
   5184 
   5185     /* Fetching and checking that all 3 are present in some way */
   5186     struct GNUNET_JSON_Specification dspec[] = {
   5187       TALER_JSON_spec_web_url      ("url",
   5188                                     &donau_url_tmp),
   5189       GNUNET_JSON_spec_uint64      ("year",
   5190                                     &pc->parse_wallet_data.donau.donation_year),
   5191       GNUNET_JSON_spec_array_const ("budikeypairs",
   5192                                     &budikeypairs),
   5193       GNUNET_JSON_spec_end ()
   5194     };
   5195     enum GNUNET_GenericReturnValue res;
   5196 
   5197     res = TALER_MHD_parse_json_data (pc->connection,
   5198                                      donau_obj,
   5199                                      dspec);
   5200     if (GNUNET_YES != res)
   5201     {
   5202       GNUNET_break_op (0);
   5203       pay_end (pc,
   5204                (GNUNET_NO == res)
   5205                ? MHD_YES
   5206                : MHD_NO);
   5207       return;
   5208     }
   5209 
   5210     /* Check if the needed data is present for the given donau URL */
   5211     {
   5212       enum GNUNET_DB_QueryStatus qs;
   5213 
   5214       qs = TALER_MERCHANTDB_get_donau_instance_by_url (
   5215         TMH_db,
   5216         pc->hc->instance->settings.id,
   5217         donau_url_tmp,
   5218         &pc->parse_wallet_data.charity_id,
   5219         &pc->parse_wallet_data.charity_max_per_year,
   5220         &pc->parse_wallet_data.charity_receipts_to_date,
   5221         &donau_keys_json,
   5222         &pc->parse_wallet_data.donau_instance_serial);
   5223 
   5224       switch (qs)
   5225       {
   5226       case GNUNET_DB_STATUS_HARD_ERROR:
   5227       case GNUNET_DB_STATUS_SOFT_ERROR:
   5228         TALER_MERCHANTDB_rollback (TMH_db);
   5229         pay_end (pc,
   5230                  TALER_MHD_reply_with_error (
   5231                    pc->connection,
   5232                    MHD_HTTP_INTERNAL_SERVER_ERROR,
   5233                    TALER_EC_GENERIC_DB_FETCH_FAILED,
   5234                    "get_donau_instance_by_url"));
   5235         return;
   5236       case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   5237         TALER_MERCHANTDB_rollback (TMH_db);
   5238         pay_end (pc,
   5239                  TALER_MHD_reply_with_error (
   5240                    pc->connection,
   5241                    MHD_HTTP_NOT_FOUND,
   5242                    TALER_EC_MERCHANT_GENERIC_DONAU_CHARITY_UNKNOWN,
   5243                    donau_url_tmp));
   5244         return;
   5245       case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
   5246         GNUNET_static_assert (sizeof (pc->parse_wallet_data.charity_priv) ==
   5247                               sizeof (pc->hc->instance->merchant_priv));
   5248         memcpy (&pc->parse_wallet_data.charity_priv,
   5249                 &pc->hc->instance->merchant_priv,
   5250                 sizeof (pc->hc->instance->merchant_priv));
   5251         pc->parse_wallet_data.donau.donau_url =
   5252           GNUNET_strdup (donau_url_tmp);
   5253         break;
   5254       }
   5255     }
   5256 
   5257     {
   5258       pc->parse_wallet_data.donau_keys =
   5259         DONAU_keys_from_json (donau_keys_json);
   5260       json_decref (donau_keys_json);
   5261       if (NULL == pc->parse_wallet_data.donau_keys)
   5262       {
   5263         GNUNET_break_op (0);
   5264         pay_end (pc,
   5265                  TALER_MHD_reply_with_error (pc->connection,
   5266                                              MHD_HTTP_BAD_REQUEST,
   5267                                              TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5268                                              "Invalid donau_keys"));
   5269         return;
   5270       }
   5271     }
   5272 
   5273     /* Stage to parse the budikeypairs from json to struct */
   5274     if (0 != json_array_size (budikeypairs))
   5275     {
   5276       size_t num_bkps = json_array_size (budikeypairs);
   5277       struct DONAU_BlindedUniqueDonorIdentifierKeyPair *bkps =
   5278         GNUNET_new_array (num_bkps,
   5279                           struct DONAU_BlindedUniqueDonorIdentifierKeyPair);
   5280 
   5281       /* Change to json for each */
   5282       for (size_t i = 0; i < num_bkps; i++)
   5283       {
   5284         const json_t *bkp_obj = json_array_get (budikeypairs,
   5285                                                 i);
   5286         if (GNUNET_SYSERR ==
   5287             merchant_parse_json_bkp (&bkps[i],
   5288                                      bkp_obj))
   5289         {
   5290           GNUNET_break_op (0);
   5291           for (size_t j = 0; j < i; j++)
   5292             GNUNET_CRYPTO_blinded_message_decref (
   5293               bkps[j].blinded_udi.blinded_message);
   5294           GNUNET_free (bkps);
   5295           pay_end (pc,
   5296                    TALER_MHD_reply_with_error (pc->connection,
   5297                                                MHD_HTTP_BAD_REQUEST,
   5298                                                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5299                                                "Failed to parse budikeypairs"));
   5300           return;
   5301         }
   5302       }
   5303 
   5304       pc->parse_wallet_data.num_bkps = num_bkps;
   5305       pc->parse_wallet_data.bkps = bkps;
   5306     }
   5307   }
   5308   TALER_json_hash (pc->parse_pay.wallet_data,
   5309                    &pc->parse_wallet_data.h_wallet_data);
   5310 
   5311   pc->phase = PP_CHECK_CONTRACT;
   5312 }
   5313 
   5314 
   5315 /**
   5316  * Try to parse the pay request into the given pay context.
   5317  * Schedules an error response in the connection on failure.
   5318  *
   5319  * @param[in,out] pc context we use to handle the payment
   5320  */
   5321 static void
   5322 phase_parse_pay (struct PayContext *pc)
   5323 {
   5324   const char *session_id = NULL;
   5325   const json_t *coins;
   5326   const json_t *tokens;
   5327   struct GNUNET_JSON_Specification spec[] = {
   5328     GNUNET_JSON_spec_array_const ("coins",
   5329                                   &coins),
   5330     GNUNET_JSON_spec_mark_optional (
   5331       TALER_JSON_spec_session_id ("session_id",
   5332                                   &session_id),
   5333       NULL),
   5334     GNUNET_JSON_spec_mark_optional (
   5335       GNUNET_JSON_spec_object_const ("wallet_data",
   5336                                      &pc->parse_pay.wallet_data),
   5337       NULL),
   5338     GNUNET_JSON_spec_mark_optional (
   5339       GNUNET_JSON_spec_array_const ("tokens",
   5340                                     &tokens),
   5341       NULL),
   5342     GNUNET_JSON_spec_end ()
   5343   };
   5344 
   5345 #if DEBUG
   5346   {
   5347     char *dump = json_dumps (pc->hc->request_body,
   5348                              JSON_INDENT (2)
   5349                              | JSON_ENCODE_ANY
   5350                              | JSON_SORT_KEYS);
   5351 
   5352     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   5353                 "POST /orders/%s/pay – request body follows:\n%s\n",
   5354                 pc->order_id,
   5355                 dump);
   5356 
   5357     free (dump);
   5358 
   5359   }
   5360 #endif /* DEBUG */
   5361 
   5362   GNUNET_assert (PP_PARSE_PAY == pc->phase);
   5363   {
   5364     enum GNUNET_GenericReturnValue res;
   5365 
   5366     res = TALER_MHD_parse_json_data (pc->connection,
   5367                                      pc->hc->request_body,
   5368                                      spec);
   5369     if (GNUNET_YES != res)
   5370     {
   5371       GNUNET_break_op (0);
   5372       pay_end (pc,
   5373                (GNUNET_NO == res)
   5374                ? MHD_YES
   5375                : MHD_NO);
   5376       return;
   5377     }
   5378   }
   5379 
   5380   /* copy session ID (if set) */
   5381   if (NULL != session_id)
   5382   {
   5383     pc->parse_pay.session_id = GNUNET_strdup (session_id);
   5384   }
   5385   else
   5386   {
   5387     /* use empty string as default if client didn't specify it */
   5388     pc->parse_pay.session_id = GNUNET_strdup ("");
   5389   }
   5390 
   5391   pc->parse_pay.coins_cnt = json_array_size (coins);
   5392   if (pc->parse_pay.coins_cnt > MAX_COIN_ALLOWED_COINS)
   5393   {
   5394     GNUNET_break_op (0);
   5395     pay_end (pc,
   5396              TALER_MHD_reply_with_error (
   5397                pc->connection,
   5398                MHD_HTTP_BAD_REQUEST,
   5399                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5400                "'coins' array too long"));
   5401     return;
   5402   }
   5403   /* note: 1 coin = 1 deposit confirmation expected */
   5404   pc->parse_pay.dc = GNUNET_new_array (pc->parse_pay.coins_cnt,
   5405                                        struct DepositConfirmation);
   5406 
   5407   /* This loop populates the array 'dc' in 'pc' */
   5408   {
   5409     unsigned int coins_index;
   5410     json_t *coin;
   5411 
   5412     json_array_foreach (coins, coins_index, coin)
   5413     {
   5414       struct DepositConfirmation *dc = &pc->parse_pay.dc[coins_index];
   5415       const char *exchange_url;
   5416       struct GNUNET_JSON_Specification ispec[] = {
   5417         GNUNET_JSON_spec_fixed_auto ("coin_sig",
   5418                                      &dc->cdd.coin_sig),
   5419         GNUNET_JSON_spec_fixed_auto ("coin_pub",
   5420                                      &dc->cdd.coin_pub),
   5421         TALER_JSON_spec_denom_sig ("ub_sig",
   5422                                    &dc->cdd.denom_sig),
   5423         GNUNET_JSON_spec_fixed_auto ("h_denom",
   5424                                      &dc->cdd.h_denom_pub),
   5425         TALER_JSON_spec_amount_any ("contribution",
   5426                                     &dc->cdd.amount),
   5427         TALER_JSON_spec_web_url ("exchange_url",
   5428                                  &exchange_url),
   5429         /* if a minimum age was required, the minimum_age_sig and
   5430          * age_commitment must be provided */
   5431         GNUNET_JSON_spec_mark_optional (
   5432           GNUNET_JSON_spec_fixed_auto ("minimum_age_sig",
   5433                                        &dc->minimum_age_sig),
   5434           &dc->no_minimum_age_sig),
   5435         GNUNET_JSON_spec_mark_optional (
   5436           TALER_JSON_spec_age_commitment ("age_commitment",
   5437                                           &dc->age_commitment),
   5438           &dc->no_age_commitment),
   5439         /* if minimum age was not required, but coin with age restriction set
   5440          * was used, h_age_commitment must be provided. */
   5441         GNUNET_JSON_spec_mark_optional (
   5442           GNUNET_JSON_spec_fixed_auto ("h_age_commitment",
   5443                                        &dc->cdd.h_age_commitment),
   5444           &dc->no_h_age_commitment),
   5445         GNUNET_JSON_spec_end ()
   5446       };
   5447       enum GNUNET_GenericReturnValue res;
   5448       struct ExchangeGroup *eg = NULL;
   5449 
   5450       res = TALER_MHD_parse_json_data (pc->connection,
   5451                                        coin,
   5452                                        ispec);
   5453       if (GNUNET_YES != res)
   5454       {
   5455         GNUNET_break_op (0);
   5456         pay_end (pc,
   5457                  (GNUNET_NO == res)
   5458                  ? MHD_YES
   5459                  : MHD_NO);
   5460         return;
   5461       }
   5462       for (unsigned int j = 0; j<coins_index; j++)
   5463       {
   5464         if (0 ==
   5465             GNUNET_memcmp (&dc->cdd.coin_pub,
   5466                            &pc->parse_pay.dc[j].cdd.coin_pub))
   5467         {
   5468           GNUNET_break_op (0);
   5469           pay_end (pc,
   5470                    TALER_MHD_reply_with_error (pc->connection,
   5471                                                MHD_HTTP_BAD_REQUEST,
   5472                                                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5473                                                "duplicate coin in list"));
   5474           return;
   5475         }
   5476       }
   5477 
   5478       dc->exchange_url = GNUNET_strdup (exchange_url);
   5479       dc->index = coins_index;
   5480       dc->pc = pc;
   5481 
   5482       /* Check the consistency of the (potential) age restriction
   5483        * information. */
   5484       if (dc->no_age_commitment != dc->no_minimum_age_sig)
   5485       {
   5486         GNUNET_break_op (0);
   5487         pay_end (pc,
   5488                  TALER_MHD_reply_with_error (
   5489                    pc->connection,
   5490                    MHD_HTTP_BAD_REQUEST,
   5491                    TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5492                    "inconsistent: 'age_commitment' vs. 'minimum_age_sig'"
   5493                    ));
   5494         return;
   5495       }
   5496 
   5497       /* Setup exchange group */
   5498       for (unsigned int i = 0; i<pc->parse_pay.num_exchanges; i++)
   5499       {
   5500         if (0 ==
   5501             strcmp (pc->parse_pay.egs[i]->exchange_url,
   5502                     exchange_url))
   5503         {
   5504           eg = pc->parse_pay.egs[i];
   5505           break;
   5506         }
   5507       }
   5508       if (NULL == eg)
   5509       {
   5510         eg = GNUNET_new (struct ExchangeGroup);
   5511         eg->pc = pc;
   5512         eg->exchange_url = dc->exchange_url;
   5513         eg->total = dc->cdd.amount;
   5514         GNUNET_array_append (pc->parse_pay.egs,
   5515                              pc->parse_pay.num_exchanges,
   5516                              eg);
   5517       }
   5518       else
   5519       {
   5520         if (0 >
   5521             TALER_amount_add (&eg->total,
   5522                               &eg->total,
   5523                               &dc->cdd.amount))
   5524         {
   5525           GNUNET_break_op (0);
   5526           pay_end (pc,
   5527                    TALER_MHD_reply_with_error (
   5528                      pc->connection,
   5529                      MHD_HTTP_INTERNAL_SERVER_ERROR,
   5530                      TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_AMOUNT_OVERFLOW,
   5531                      "Overflow adding up amounts"));
   5532           return;
   5533         }
   5534       }
   5535     }
   5536   }
   5537 
   5538   pc->parse_pay.tokens_cnt = json_array_size (tokens);
   5539   if (pc->parse_pay.tokens_cnt > MAX_TOKEN_ALLOWED_INPUTS)
   5540   {
   5541     GNUNET_break_op (0);
   5542     pay_end (pc,
   5543              TALER_MHD_reply_with_error (
   5544                pc->connection,
   5545                MHD_HTTP_BAD_REQUEST,
   5546                TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5547                "'tokens' array too long"));
   5548     return;
   5549   }
   5550 
   5551   pc->parse_pay.tokens = GNUNET_new_array (pc->parse_pay.tokens_cnt,
   5552                                            struct TokenUseConfirmation);
   5553 
   5554   /* This loop populates the array 'tokens' in 'pc' */
   5555   {
   5556     unsigned int tokens_index;
   5557     json_t *token;
   5558 
   5559     json_array_foreach (tokens, tokens_index, token)
   5560     {
   5561       struct TokenUseConfirmation *tuc = &pc->parse_pay.tokens[tokens_index];
   5562       struct GNUNET_JSON_Specification ispec[] = {
   5563         GNUNET_JSON_spec_fixed_auto ("token_sig",
   5564                                      &tuc->sig),
   5565         GNUNET_JSON_spec_fixed_auto ("token_pub",
   5566                                      &tuc->pub),
   5567         GNUNET_JSON_spec_fixed_auto ("h_issue",
   5568                                      &tuc->h_issue),
   5569         TALER_JSON_spec_token_issue_sig ("ub_sig",
   5570                                          &tuc->unblinded_sig),
   5571         GNUNET_JSON_spec_end ()
   5572       };
   5573       enum GNUNET_GenericReturnValue res;
   5574 
   5575       res = TALER_MHD_parse_json_data (pc->connection,
   5576                                        token,
   5577                                        ispec);
   5578       if (GNUNET_YES != res)
   5579       {
   5580         GNUNET_break_op (0);
   5581         pay_end (pc,
   5582                  (GNUNET_NO == res)
   5583                  ? MHD_YES
   5584                  : MHD_NO);
   5585         return;
   5586       }
   5587 
   5588       for (unsigned int j = 0; j<tokens_index; j++)
   5589       {
   5590         if (0 ==
   5591             GNUNET_memcmp (&tuc->pub,
   5592                            &pc->parse_pay.tokens[j].pub))
   5593         {
   5594           GNUNET_break_op (0);
   5595           pay_end (pc,
   5596                    TALER_MHD_reply_with_error (
   5597                      pc->connection,
   5598                      MHD_HTTP_BAD_REQUEST,
   5599                      TALER_EC_GENERIC_PARAMETER_MALFORMED,
   5600                      "duplicate token in list"));
   5601           return;
   5602         }
   5603       }
   5604     }
   5605   }
   5606 
   5607   pc->phase = PP_PARSE_WALLET_DATA;
   5608 }
   5609 
   5610 
   5611 /**
   5612  * Custom cleanup routine for a `struct PayContext`.
   5613  *
   5614  * @param cls the `struct PayContext` to clean up.
   5615  */
   5616 static void
   5617 pay_context_cleanup (void *cls)
   5618 {
   5619   struct PayContext *pc = cls;
   5620 
   5621   if (NULL != pc->batch_deposits.timeout_task)
   5622   {
   5623     GNUNET_SCHEDULER_cancel (pc->batch_deposits.timeout_task);
   5624     pc->batch_deposits.timeout_task = NULL;
   5625   }
   5626   if (NULL != pc->check_contract.contract_terms_json)
   5627   {
   5628     json_decref (pc->check_contract.contract_terms_json);
   5629     pc->check_contract.contract_terms_json = NULL;
   5630   }
   5631   for (unsigned int i = 0; i<pc->parse_pay.coins_cnt; i++)
   5632   {
   5633     struct DepositConfirmation *dc = &pc->parse_pay.dc[i];
   5634 
   5635     TALER_denom_sig_free (&dc->cdd.denom_sig);
   5636     GNUNET_free (dc->exchange_url);
   5637   }
   5638   GNUNET_free (pc->parse_pay.dc);
   5639   for (unsigned int i = 0; i<pc->parse_pay.tokens_cnt; i++)
   5640   {
   5641     struct TokenUseConfirmation *tuc = &pc->parse_pay.tokens[i];
   5642 
   5643     TALER_token_issue_sig_free (&tuc->unblinded_sig);
   5644   }
   5645   GNUNET_free (pc->parse_pay.tokens);
   5646   for (unsigned int i = 0; i<pc->parse_pay.num_exchanges; i++)
   5647   {
   5648     struct ExchangeGroup *eg = pc->parse_pay.egs[i];
   5649 
   5650     if (NULL != eg->fo)
   5651       TMH_EXCHANGES_keys4exchange_cancel (eg->fo);
   5652     if (NULL != eg->bdh)
   5653       TALER_EXCHANGE_post_batch_deposit_cancel (eg->bdh);
   5654     if (NULL != eg->keys)
   5655       TALER_EXCHANGE_keys_decref (eg->keys);
   5656     GNUNET_free (eg);
   5657   }
   5658   GNUNET_free (pc->parse_pay.egs);
   5659   if (NULL != pc->check_contract.contract_terms)
   5660   {
   5661     TALER_MERCHANT_contract_free (pc->check_contract.contract_terms);
   5662     pc->check_contract.contract_terms = NULL;
   5663   }
   5664   if (NULL != pc->response)
   5665   {
   5666     MHD_destroy_response (pc->response);
   5667     pc->response = NULL;
   5668   }
   5669   GNUNET_free (pc->parse_pay.session_id);
   5670   GNUNET_CONTAINER_DLL_remove (pc_head,
   5671                                pc_tail,
   5672                                pc);
   5673   GNUNET_free (pc->check_contract.pos_key);
   5674   GNUNET_free (pc->compute_money_pots.pots);
   5675   GNUNET_free (pc->compute_money_pots.increments);
   5676   if (NULL != pc->parse_wallet_data.bkps)
   5677   {
   5678     for (size_t i = 0; i < pc->parse_wallet_data.num_bkps; i++)
   5679       GNUNET_CRYPTO_blinded_message_decref (
   5680         pc->parse_wallet_data.bkps[i].blinded_udi.blinded_message);
   5681     GNUNET_array_grow (pc->parse_wallet_data.bkps,
   5682                        pc->parse_wallet_data.num_bkps,
   5683                        0);
   5684   }
   5685   if (NULL != pc->parse_wallet_data.donau_keys)
   5686   {
   5687     DONAU_keys_decref (pc->parse_wallet_data.donau_keys);
   5688     pc->parse_wallet_data.donau_keys = NULL;
   5689   }
   5690   GNUNET_free (pc->parse_wallet_data.donau.donau_url);
   5691   for (unsigned int i = 0; i<pc->parse_wallet_data.token_envelopes_cnt; i++)
   5692   {
   5693     struct TokenEnvelope *ev
   5694       = &pc->parse_wallet_data.token_envelopes[i];
   5695 
   5696     GNUNET_CRYPTO_blinded_message_decref (ev->blinded_token.blinded_pub);
   5697   }
   5698   GNUNET_free (pc->parse_wallet_data.token_envelopes);
   5699   if (NULL != pc->output_tokens)
   5700   {
   5701     for (unsigned int i = 0; i<pc->output_tokens_len; i++)
   5702       if (NULL != pc->output_tokens[i].sig.signature)
   5703         GNUNET_CRYPTO_blinded_sig_decref (pc->output_tokens[i].sig.signature);
   5704     GNUNET_free (pc->output_tokens);
   5705   }
   5706   GNUNET_free (pc);
   5707 }
   5708 
   5709 
   5710 enum MHD_Result
   5711 TMH_post_orders_ID_pay (const struct TMH_RequestHandler *rh,
   5712                         struct MHD_Connection *connection,
   5713                         struct TMH_HandlerContext *hc)
   5714 {
   5715   struct PayContext *pc = hc->ctx;
   5716 
   5717   GNUNET_assert (NULL != hc->infix);
   5718   if (NULL == pc)
   5719   {
   5720     pc = GNUNET_new (struct PayContext);
   5721     pc->connection = connection;
   5722     pc->hc = hc;
   5723     pc->order_id = hc->infix;
   5724     hc->ctx = pc;
   5725     hc->cc = &pay_context_cleanup;
   5726     GNUNET_CONTAINER_DLL_insert (pc_head,
   5727                                  pc_tail,
   5728                                  pc);
   5729   }
   5730   while (1)
   5731   {
   5732     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   5733                 "Processing /pay in phase %d\n",
   5734                 (int) pc->phase);
   5735     switch (pc->phase)
   5736     {
   5737     case PP_PARSE_PAY:
   5738       phase_parse_pay (pc);
   5739       break;
   5740     case PP_PARSE_WALLET_DATA:
   5741       phase_parse_wallet_data (pc);
   5742       break;
   5743     case PP_CHECK_CONTRACT:
   5744       phase_check_contract (pc);
   5745       break;
   5746     case PP_VALIDATE_TOKENS:
   5747       phase_validate_tokens (pc);
   5748       break;
   5749     case PP_CONTRACT_PAID:
   5750       phase_contract_paid (pc);
   5751       break;
   5752     case PP_COMPUTE_MONEY_POTS:
   5753       phase_compute_money_pots (pc);
   5754       break;
   5755     case PP_PAY_TRANSACTION:
   5756       phase_execute_pay_transaction (pc);
   5757       break;
   5758     case PP_REQUEST_DONATION_RECEIPT:
   5759       phase_request_donation_receipt (pc);
   5760       break;
   5761     case PP_FINAL_OUTPUT_TOKEN_PROCESSING:
   5762       phase_final_output_token_processing (pc);
   5763       break;
   5764     case PP_PAYMENT_NOTIFICATION:
   5765       phase_payment_notification (pc);
   5766       break;
   5767     case PP_SUCCESS_RESPONSE:
   5768       phase_success_response (pc);
   5769       break;
   5770     case PP_BATCH_DEPOSITS:
   5771       phase_batch_deposits (pc);
   5772       break;
   5773     case PP_RETURN_RESPONSE:
   5774       phase_return_response (pc);
   5775       break;
   5776     case PP_FAIL_LEGAL_REASONS:
   5777       phase_fail_for_legal_reasons (pc);
   5778       break;
   5779     case PP_END_YES:
   5780       return MHD_YES;
   5781     case PP_END_NO:
   5782       return MHD_NO;
   5783     default:
   5784       /* should not be reachable */
   5785       GNUNET_assert (0);
   5786       return MHD_NO;
   5787     }
   5788     switch (pc->suspended)
   5789     {
   5790     case GNUNET_SYSERR:
   5791       /* during shutdown, we don't generate any more replies */
   5792       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   5793                   "Processing /pay ends due to shutdown in phase %d\n",
   5794                   (int) pc->phase);
   5795       return MHD_NO;
   5796     case GNUNET_NO:
   5797       /* continue to next phase */
   5798       break;
   5799     case GNUNET_YES:
   5800       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
   5801                   "Processing /pay suspended in phase %d\n",
   5802                   (int) pc->phase);
   5803       return MHD_YES;
   5804     }
   5805   }
   5806   /* impossible to get here */
   5807   GNUNET_assert (0);
   5808   return MHD_YES;
   5809 }
   5810 
   5811 
   5812 /* end of taler-merchant-httpd_post-orders-ORDER_ID-pay.c */