/*
  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/get-private-orders-ORDER_ID.h
 * @brief C interface for GET /private/orders/$ORDER_ID of the merchant backend
 * @author Christian Grothoff
 */
#ifndef _TALER_MERCHANT__GET_PRIVATE_ORDERS_ORDER_ID_H
#define _TALER_MERCHANT__GET_PRIVATE_ORDERS_ORDER_ID_H

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


/**
 * Handle for a GET /private/orders/$ORDER_ID request.
 */
struct TALER_MERCHANT_OrderMerchantGetHandle;


/**
 * Order status code.
 */
enum TALER_MERCHANT_OrderStatusCode
{
  /**
   * The order is unpaid.
   */
  TALER_MERCHANT_OSC_UNPAID = 0,

  /**
   * The order has been claimed by a wallet.
   */
  TALER_MERCHANT_OSC_CLAIMED = 1,

  /**
   * The order has been paid.
   */
  TALER_MERCHANT_OSC_PAID = 2
};


/**
 * Refund detail in a paid order (merchant view).
 */
struct TALER_MERCHANT_RefundOrderDetail
{

  /**
   * Human-readable reason given for the refund.
   */
  const char *reason;

  /**
   * Time when the refund was granted.
   */
  struct GNUNET_TIME_Timestamp refund_time;

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

};


/**
 * Response details for a GET /private/orders/$ORDER_ID request.
 */
struct TALER_MERCHANT_OrderStatusResponse
{

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

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

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

      /**
       * Current status of the order.
       */
      enum TALER_MERCHANT_OrderStatusCode status;

      /**
       * Details depending on @a status.
       */
      union
      {

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_UNPAID.
         */
        struct
        {

          /**
           * Total amount of the order.
           */
          struct TALER_Amount contract_amount;

          /**
           * Order ID of a previously paid order that covers this one,
           * or NULL if none.
           */
          const char *already_paid_order_id;

          /**
           * Taler pay URI for this order.
           */
          const char *taler_pay_uri;

          /**
           * Short summary of the order.
           */
          const char *summary;

          /**
           * Timestamp when the order was created.
           */
          struct GNUNET_TIME_Timestamp creation_time;

        } unpaid;

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_CLAIMED.
         */
        struct
        {

          /**
           * Contract terms (JSON).
           */
          const json_t *contract_terms;

        } claimed;

        /**
         * Details when @a status is #TALER_MERCHANT_OSC_PAID.
         */
        struct
        {

          /**
           * True if a refund has been granted.
           */
          bool refunded;

          /**
           * True if a refund is still pending at the exchange.
           */
          bool refund_pending;

          /**
           * True if the payment has been wired to the merchant.
           */
          bool wired;

          /**
           * Total amount deposited.
           */
          struct TALER_Amount deposit_total;

          /**
           * Error code from the exchange, or TALER_EC_NONE.
           */
          enum TALER_ErrorCode exchange_ec;

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

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

          /**
           * Contract terms (JSON).
           */
          const json_t *contract_terms;

          /**
           * Number of wire transfers in @a wts.
           */
          unsigned int wts_len;

          /**
           * Array of wire transfer details.
           */
          struct TALER_MERCHANT_WireTransfer *wts;

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

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

          /**
           * Timestamp of the last payment.
           */
          struct GNUNET_TIME_Timestamp last_payment;

        } paid;

      } details;

    } ok;

  } details;

};


/**
 * Callback for a GET /private/orders/$ORDER_ID request.
 *
 * @param cls closure
 * @param osr response details
 */
typedef void
(*TALER_MERCHANT_OrderMerchantGetCallback)(
  void *cls,
  const struct TALER_MERCHANT_OrderStatusResponse *osr);


/**
 * Issue a GET /private/orders/$ORDER_ID request.
 *
 * @param ctx CURL context to use
 * @param backend_url base URL of the merchant backend
 * @param order_id identifier of the order to retrieve
 * @param session_id session ID for repurchase detection, or NULL
 * @param timeout long-polling timeout
 * @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_OrderMerchantGetHandle *
TALER_MERCHANT_merchant_order_get (
  struct GNUNET_CURL_Context *ctx,
  const char *backend_url,
  const char *order_id,
  const char *session_id,
  struct GNUNET_TIME_Relative timeout,
  TALER_MERCHANT_OrderMerchantGetCallback cb,
  void *cb_cls);


/**
 * Cancel a GET /private/orders/$ORDER_ID request.
 *
 * @param[in] omgh handle to cancel
 */
void
TALER_MERCHANT_merchant_order_get_cancel (
  struct TALER_MERCHANT_OrderMerchantGetHandle *omgh);


#endif
