/*
  This file is part of TALER
  Copyright (C) 2014-2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 2.1, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  TALER; see the file COPYING.LGPL.  If not, see
  <http://www.gnu.org/licenses/>
*/
/**
 * @file src/include/taler/merchant/post-orders-ORDER_ID-abort.h
 * @brief C interface for the POST /orders/$ORDER_ID/abort endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_ORDERS_ORDER_ID_ABORT_H
#define _TALER_MERCHANT__POST_ORDERS_ORDER_ID_ABORT_H

#include <taler/merchant/common.h>


/**
 * Handle for a POST /orders/$ORDER_ID/abort request.
 */
struct TALER_MERCHANT_PostOrdersAbortHandle;


/**
 * One coin used in an abort request.
 */
struct TALER_MERCHANT_PostOrdersAbortCoin
{

  /**
   * Base URL of the exchange this coin was issued by.
   */
  const char *exchange_url;

  /**
   * Public key of the coin.
   */
  struct TALER_CoinSpendPublicKeyP coin_pub;

  /**
   * Total amount contributed including deposit fee.
   */
  struct TALER_Amount amount_with_fee;

  /**
   * Hash of the denomination public key.
   */
  struct TALER_DenominationHashP denom_pub_hash;

  /**
   * Denomination signature over the coin.
   */
  struct TALER_DenominationSignature denom_sig;

};


/**
 * Result for one coin from an abort operation.
 */
struct TALER_MERCHANT_PostOrdersAbortedCoin
{
  /**
   * Type of status for this coin, determines content of @e details
   */
  enum
  {
    TALER_MERCHANT_POAC_UNDEPOSITED = 0,
    TALER_MERCHANT_POAC_SUCCESS,
    TALER_MERCHANT_POAC_FAILURE,
  } type;

  /**
   * Public key of the coin.
   */
  struct TALER_CoinSpendPublicKeyP coin_pub;

  /**
   * Details depending on @e type
   */
  union
  {

    /**
     * Details on #TALER_MERCHANT_POAC_SUCCESS.
     */
    struct
    {

      /**
       * Exchange signature confirming the refund.
       */
      struct TALER_ExchangeSignatureP exchange_sig;

      /**
       * Exchange public key used for @e exchange_sig.
       */
      struct TALER_ExchangePublicKeyP exchange_pub;

      /**
       * HTTP status of the abort from the exchange.
       */
      uint32_t exchange_status;
    } success;

    /**
     * Details on #TALER_MERCHANT_POAC_FAILURE.
     */
    struct
    {

      /**
       * HTTP status of the abort from the exchange.
       */
      uint32_t exchange_status;

      /**
       * Error code from the exchange.
       */
      enum TALER_ErrorCode exchange_code;

      /**
       * Full exchange reply, could be NULL.
       */
      const json_t *exchange_reply;

    } failure;

  } details;

};


/**
 * Response details for a POST /orders/$ORDER_ID/abort request.
 */
struct TALER_MERCHANT_PostOrdersAbortResponse
{

  /**
   * HTTP response details.
   */
  struct TALER_MERCHANT_HttpResponse hr;

  /**
   * Details depending on the HTTP status code.
   */
  union
  {

    /**
     * Details on #MHD_HTTP_OK.
     */
    struct
    {

      /**
       * Number of aborted coins in @a aborts.
       */
      unsigned int num_aborts;

      /**
       * Array of aborted coin results.
       */
      const struct TALER_MERCHANT_PostOrdersAbortedCoin *aborts;

    } ok;

    /**
     * Details on #MHD_HTTP_BAD_GATEWAY.
     * The response body has the same refunds array as on
     * #MHD_HTTP_OK, but at least one coin had an exchange error.
     */
    struct
    {

      /**
       * Number of aborted coins in @a aborts.
       */
      unsigned int num_aborts;

      /**
       * Array of aborted coin results.
       */
      const struct TALER_MERCHANT_PostOrdersAbortedCoin *aborts;

    } bad_gateway;

    /**
     * Details on #MHD_HTTP_GATEWAY_TIMEOUT.
     */
    struct
    {

      /**
       * Taler error code from the exchange, if available.
       */
      enum TALER_ErrorCode exchange_ec;

      /**
       * HTTP status code returned by the exchange, if available.
       * 0 if no exchange was involved or the request timed out
       * before a response was received.
       */
      unsigned int exchange_http_status;

    } gateway_timeout;

  } details;

};


/**
 * Set up POST /orders/$ORDER_ID/abort operation.
 * Note that you must explicitly start the operation after
 * possibly setting options.
 *
 * @param ctx the context
 * @param url base URL of the merchant backend
 * @param order_id identifier of the order to abort
 * @param merchant_pub merchant public key
 * @param h_contract hash of the contract terms
 * @param num_coins number of coins in @a coins
 * @param coins array of coins used in the aborted payment
 * @return handle to operation
 */
struct TALER_MERCHANT_PostOrdersAbortHandle *
TALER_MERCHANT_post_orders_abort_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *order_id,
  const struct TALER_MerchantPublicKeyP *merchant_pub,
  const struct TALER_PrivateContractHashP *h_contract,
  unsigned int num_coins,
  const struct TALER_MERCHANT_PostOrdersAbortCoin coins[static num_coins]);


#ifndef TALER_MERCHANT_POST_ORDERS_ABORT_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostOrdersAbortCallback.
 */
#define TALER_MERCHANT_POST_ORDERS_ABORT_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_ORDERS_ABORT_RESULT_CLOSURE */

/**
 * Callback for a POST /orders/$ORDER_ID/abort request.
 *
 * @param cls closure
 * @param oar response details
 */
typedef void
(*TALER_MERCHANT_PostOrdersAbortCallback)(
  TALER_MERCHANT_POST_ORDERS_ABORT_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostOrdersAbortResponse *oar);


/**
 * Start POST /orders/$ORDER_ID/abort operation.
 *
 * @param[in,out] poah operation to start
 * @param cb function to call with the merchant's result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_MERCHANT_post_orders_abort_start (
  struct TALER_MERCHANT_PostOrdersAbortHandle *poah,
  TALER_MERCHANT_PostOrdersAbortCallback cb,
  TALER_MERCHANT_POST_ORDERS_ABORT_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /orders/$ORDER_ID/abort operation.  This function must not be
 * called by clients after the TALER_MERCHANT_PostOrdersAbortCallback has
 * been invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] poah operation to cancel
 */
void
TALER_MERCHANT_post_orders_abort_cancel (
  struct TALER_MERCHANT_PostOrdersAbortHandle *poah);


#endif /* _TALER_MERCHANT__POST_ORDERS_ORDER_ID_ABORT_H */
