/*
  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 include/taler/taler-merchant/post-orders-ORDER_ID-refund.h
 * @brief C interface for POST /orders/$ORDER_ID/refund (wallet-facing) of the merchant backend
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_ORDERS_ORDER_ID_REFUND_H
#define _TALER_MERCHANT__POST_ORDERS_ORDER_ID_REFUND_H

#include <taler/taler-merchant/common.h>


/**
 * Handle for a POST /orders/$ORDER_ID/refund request (wallet-facing).
 */
struct TALER_MERCHANT_WalletOrderRefundHandle;


/**
 * Refund detail in a wallet refund response.
 */
struct TALER_MERCHANT_RefundDetail
{

  /**
   * Exchange response details.  Full details are only included
   * upon failure (HTTP status is not #MHD_HTTP_OK).
   */
  struct TALER_EXCHANGE_HttpResponse hr;

  /**
   * Coin this detail is about.
   */
  struct TALER_CoinSpendPublicKeyP coin_pub;

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

  /**
   * Amount to be refunded for this coin.
   */
  struct TALER_Amount refund_amount;

  /**
   * Details depending on exchange HTTP status.
   */
  union
  {
    /**
     * Details if exchange status is #MHD_HTTP_OK.
     */
    struct
    {
      /**
       * Public key of the exchange affirming the refund,
       * only valid if the @e hr http_status is #MHD_HTTP_OK.
       */
      struct TALER_ExchangePublicKeyP exchange_pub;

      /**
       * Signature of the exchange affirming the refund,
       * only valid if the @e hr http_status is #MHD_HTTP_OK.
       */
      struct TALER_ExchangeSignatureP exchange_sig;
    } ok;
  } details;
};


/**
 * Response details for a POST /orders/$ORDER_ID/refund request (wallet-facing).
 */
struct TALER_MERCHANT_WalletRefundResponse
{

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

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

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

      /**
       * Total amount refunded.
       */
      struct TALER_Amount refund_amount;

      /**
       * Public key of the merchant.
       */
      struct TALER_MerchantPublicKeyP merchant_pub;

      /**
       * Number of per-coin refund details in @a refunds.
       */
      unsigned int refunds_length;

      /**
       * Array of per-coin refund details.
       */
      const struct TALER_MERCHANT_RefundDetail *refunds;

    } ok;

  } details;

};


/**
 * Callback for a POST /orders/$ORDER_ID/refund request (wallet-facing).
 *
 * @param cls closure
 * @param wrr response details
 */
typedef void
(*TALER_MERCHANT_WalletRefundCallback)(
  void *cls,
  const struct TALER_MERCHANT_WalletRefundResponse *wrr);


/**
 * Issue a POST /orders/$ORDER_ID/refund request (wallet-facing) to
 * obtain refund signatures.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param order_id identifier of the order to refund
 * @param h_contract_terms hash of the contract terms
 * @param cb callback to invoke with the result
 * @param cb_cls closure for @a cb
 * @return handle for the request, NULL on hard error
 */
struct TALER_MERCHANT_WalletOrderRefundHandle *
TALER_MERCHANT_wallet_post_order_refund (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  const char *order_id,
  const struct TALER_PrivateContractHashP *h_contract_terms,
  TALER_MERCHANT_WalletRefundCallback cb,
  void *cb_cls);


/**
 * Cancel a POST /orders/$ORDER_ID/refund request (wallet-facing).
 *
 * @param[in] orh handle to cancel
 */
void
TALER_MERCHANT_wallet_post_order_refund_cancel (
  struct TALER_MERCHANT_WalletOrderRefundHandle *orh);


#endif
