/*
  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-private-transfers.h
 * @brief C interface for POST /private/transfers of the merchant backend
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__POST_PRIVATE_TRANSFERS_H
#define _TALER_MERCHANT__POST_PRIVATE_TRANSFERS_H

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


/**
 * Handle for a POST /private/transfers request.
 */
struct TALER_MERCHANT_PostTransfersHandle;


/**
 * Wire transfer information associated with a paid order.
 */
struct TALER_MERCHANT_WireTransfer
{

  /**
   * Base URL of the exchange that made the transfer.
   */
  const char *exchange_url;

  /**
   * When the transfer took place (note: may not be exact,
   * as the time at which the exchange initiated the transfer
   * may differ from the time the bank or the merchant observed).
   * Zero if not known.
   */
  struct GNUNET_TIME_Timestamp execution_time;

  /**
   * Wire transfer subject.
   */
  struct TALER_WireTransferIdentifierRawP wtid;

  /**
   * Total amount that was transferred.
   */
  struct TALER_Amount total_amount;

  /**
   * Whether this transfer was confirmed by the merchant using
   * the POST /transfers API, or whether it was merely claimed
   * by the exchange.
   */
  bool confirmed;

};


/**
 * Response details for a POST /private/transfers request.
 */
struct TALER_MERCHANT_PostTransfersResponse
{

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

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

    /**
     * Details on #MHD_HTTP_BAD_GATEWAY (exchange returned an error).
     */
    struct
    {

      /**
       * Error code returned by the exchange.
       */
      enum TALER_ErrorCode exchange_ec;

      /**
       * HTTP status code returned by the exchange.
       */
      unsigned int exchange_http_status;

    } bad_gateway;

  } details;

};


/**
 * Callback for a POST /private/transfers request.
 *
 * @param cls closure
 * @param ptr response details
 */
typedef void
(*TALER_MERCHANT_PostTransfersCallback)(
  void *cls,
  const struct TALER_MERCHANT_PostTransfersResponse *ptr);


/**
 * Issue a POST /private/transfers request to inform the merchant about
 * a wire transfer from an exchange.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param credit_amount amount credited in this transfer
 * @param wtid wire transfer identifier
 * @param payto_uri payto URI of the merchant account that received the funds
 * @param exchange_url base URL of the exchange that made the transfer
 * @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_PostTransfersHandle *
TALER_MERCHANT_transfers_post (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  const struct TALER_Amount *credit_amount,
  const struct TALER_WireTransferIdentifierRawP *wtid,
  struct TALER_FullPayto payto_uri,
  const char *exchange_url,
  TALER_MERCHANT_PostTransfersCallback cb,
  void *cb_cls);


/**
 * Cancel a POST /private/transfers request.
 *
 * @param[in] pth handle to cancel
 */
void
TALER_MERCHANT_transfers_post_cancel (
  struct TALER_MERCHANT_PostTransfersHandle *pth);


#endif
