/*
  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 Affero General Public License as published by the Free Software
  Foundation; either version 3, 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file include/taler/exchange/post-coins-COIN_PUB-refund.h
 * @brief C interface for POST /coins/$COIN_PUB/refund
 * @author Christian Grothoff
 */
#ifndef _TALER_EXCHANGE__POST_COINS_COIN_PUB_REFUND_H
#define _TALER_EXCHANGE__POST_COINS_COIN_PUB_REFUND_H

#include <taler/exchange/common.h>

/**
 * Handle for an operation to POST /coins/$COIN_PUB/refund.
 */
struct TALER_EXCHANGE_PostCoinsRefundHandle;


/**
 * Set up POST /coins/$COIN_PUB/refund operation.
 * Note that you must explicitly start the operation after setup.
 *
 * @param ctx curl context
 * @param url exchange base URL
 * @param keys exchange keys
 * @param amount the amount to be refunded; must be larger than the refund fee
 *        (as that fee is still being subtracted), and smaller than the amount
 *        (with deposit fee) of the original deposit contribution of this coin
 * @param h_contract_terms hash of the contract of the merchant with the
 *        customer that is being refunded
 * @param coin_pub coin's public key of the coin from the original deposit
 * @param rtransaction_id transaction id for the refund operation; needed to
 *        distinguish multiple partial refunds for the same deposit
 * @param merchant_priv the private key of the merchant, used to generate
 *        signature for refund request
 * @return handle to operation, NULL on error
 */
struct TALER_EXCHANGE_PostCoinsRefundHandle *
TALER_EXCHANGE_post_coins_refund_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  struct TALER_EXCHANGE_Keys *keys,
  const struct TALER_Amount *amount,
  const struct TALER_PrivateContractHashP *h_contract_terms,
  const struct TALER_CoinSpendPublicKeyP *coin_pub,
  uint64_t rtransaction_id,
  const struct TALER_MerchantPrivateKeyP *merchant_priv);


/**
 * Response from the /refund API.
 */
struct TALER_EXCHANGE_PostCoinsRefundResponse
{
  /**
   * HTTP response data
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Response details depending on the HTTP status code.
   */
  union
  {
    /**
     * Details on #MHD_HTTP_OK.
     */
    struct
    {
      /**
       * Exchange key used to sign.
       */
      struct TALER_ExchangePublicKeyP exchange_pub;

      /**
       * The actual signature
       */
      struct TALER_ExchangeSignatureP exchange_sig;
    } ok;
  } details;
};


#ifndef TALER_EXCHANGE_POST_COINS_REFUND_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_EXCHANGE_PostCoinsRefundCallback.
 */
#define TALER_EXCHANGE_POST_COINS_REFUND_RESULT_CLOSURE void
#endif /* TALER_EXCHANGE_POST_COINS_REFUND_RESULT_CLOSURE */

/**
 * Type of the function that receives the result of a
 * POST /coins/$COIN_PUB/refund request.
 *
 * @param cls closure
 * @param result result returned by the HTTP server
 */
typedef void
(*TALER_EXCHANGE_PostCoinsRefundCallback)(
  TALER_EXCHANGE_POST_COINS_REFUND_RESULT_CLOSURE *cls,
  const struct TALER_EXCHANGE_PostCoinsRefundResponse *result);


/**
 * Start POST /coins/$COIN_PUB/refund operation.
 *
 * @param[in,out] pcrh operation to start
 * @param cb function to call with the exchange's result
 * @param cb_cls closure for @a cb
 * @return status code, #TALER_EC_NONE on success
 */
enum TALER_ErrorCode
TALER_EXCHANGE_post_coins_refund_start (
  struct TALER_EXCHANGE_PostCoinsRefundHandle *pcrh,
  TALER_EXCHANGE_PostCoinsRefundCallback cb,
  TALER_EXCHANGE_POST_COINS_REFUND_RESULT_CLOSURE *cb_cls);


/**
 * Cancel POST /coins/$COIN_PUB/refund operation.  This function must not be
 * called by clients after the TALER_EXCHANGE_PostCoinsRefundCallback has been
 * invoked (as in those cases it'll be called internally by the
 * implementation already).
 *
 * @param[in] pcrh operation to cancel
 */
void
TALER_EXCHANGE_post_coins_refund_cancel (
  struct TALER_EXCHANGE_PostCoinsRefundHandle *pcrh);


#endif /* _TALER_EXCHANGE__POST_COINS_COIN_PUB_REFUND_H */
