/*
  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-refund.h
 * @brief C interface for the POST /orders/$ORDER_ID/refund endpoint
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_ORDERS_ORDER_ID_REFUND_H
#define _TALER_MERCHANT__POST_ORDERS_ORDER_ID_REFUND_H

#include <taler/merchant/common.h>


/**
 * Handle for a POST /orders/$ORDER_ID/refund request.
 */
struct TALER_MERCHANT_PostOrdersRefundHandle;


/**
 * Details about one refund from the exchange.
 */
struct TALER_MERCHANT_PostOrdersRefundDetail
{

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

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

  /**
   * Hash of the contract terms.
   */
  struct TALER_PrivateContractHashP h_contract_terms;

  /**
   * Merchant public key.
   */
  struct TALER_MerchantPublicKeyP merchant_pub;

  /**
   * Refund transaction ID.
   */
  uint64_t rtransaction_id;

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

  /**
   * Refund amount.
   */
  struct TALER_Amount refund_amount;

  /**
   * Refund fee charged by the exchange.
   */
  struct TALER_Amount refund_fee;

  /**
   * HTTP status returned by the exchange for this refund, 0 if not yet
   * obtained.
   */
  unsigned int exchange_http_status;

  /**
   * Taler error code returned by the exchange.
   */
  enum TALER_ErrorCode ec;

  /**
   * Full exchange reply (JSON), or NULL.
   */
  const json_t *exchange_reply;

  /**
   * HTTP response details from the exchange.
   */
  struct TALER_MERCHANT_HttpResponse hr;

};


/**
 * Response details for a POST /orders/$ORDER_ID/refund request.
 */
struct TALER_MERCHANT_PostOrdersRefundResponse
{

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

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

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

      /**
       * Number of refund details in @a refunds.
       */
      unsigned int num_refunds;

      /**
       * Array of refund details.
       */
      const struct TALER_MERCHANT_PostOrdersRefundDetail *refunds;

      /**
       * Total effective refund amount.
       */
      struct TALER_Amount refund_amount;

      /**
       * Merchant public key.
       */
      struct TALER_MerchantPublicKeyP merchant_pub;

    } ok;

  } details;

};


/**
 * Set up POST /orders/$ORDER_ID/refund 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
 * @param h_contract_terms hash of the contract terms
 * @return handle to operation
 */
struct TALER_MERCHANT_PostOrdersRefundHandle *
TALER_MERCHANT_post_orders_refund_create (
  struct GNUNET_CURL_Context *ctx,
  const char *url,
  const char *order_id,
  const struct TALER_PrivateContractHashP *h_contract_terms);


#ifndef TALER_MERCHANT_POST_ORDERS_REFUND_RESULT_CLOSURE
/**
 * Type of the closure used by
 * the #TALER_MERCHANT_PostOrdersRefundCallback.
 */
#define TALER_MERCHANT_POST_ORDERS_REFUND_RESULT_CLOSURE void
#endif /* TALER_MERCHANT_POST_ORDERS_REFUND_RESULT_CLOSURE */

/**
 * Callback for a POST /orders/$ORDER_ID/refund request.
 *
 * @param cls closure
 * @param orr response details
 */
typedef void
(*TALER_MERCHANT_PostOrdersRefundCallback)(
  TALER_MERCHANT_POST_ORDERS_REFUND_RESULT_CLOSURE *cls,
  const struct TALER_MERCHANT_PostOrdersRefundResponse *orr);


/**
 * Start POST /orders/$ORDER_ID/refund operation.
 *
 * @param[in,out] porh 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_refund_start (
  struct TALER_MERCHANT_PostOrdersRefundHandle *porh,
  TALER_MERCHANT_PostOrdersRefundCallback cb,
  TALER_MERCHANT_POST_ORDERS_REFUND_RESULT_CLOSURE *cb_cls);


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


#endif /* _TALER_MERCHANT__POST_ORDERS_ORDER_ID_REFUND_H */
