> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oneclickdz.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Passer des Commandes

> Passer des commandes de cartes-cadeaux après avoir vérifié le stock et le solde

## Vue d'ensemble

Passez des commandes de cartes-cadeaux après avoir vérifié la disponibilité des produits. Les commandes sont traitées instantanément et retournent un `orderId` pour le suivi.

<Warning>Les commandes sont définitives une fois passées. Vérifiez le stock et le solde en premier.</Warning>

## Référence API

<Card title="POST /v3/gift-cards/placeOrder" icon="cart-shopping" href="/fr/api-reference/gift-cards/place-order">
  Documentation complète de l'endpoint
</Card>

## Passer une commande

<CodeGroup>
  ```python Python theme={null}
  import requests
  import os

  def place_order(product_id, type_id, quantity):
      response = requests.post(
          'https://api.oneclickdz.com/v3/gift-cards/placeOrder',
          headers={
              'Content-Type': 'application/json',
              'X-Access-Token': os.getenv('API_KEY')
          },
          json={
              'productId': product_id,
              'typeId': type_id,
              'quantity': quantity
          }
      )

      result = response.json()

      if not response.ok:
          raise Exception(result.get('message', 'Order failed'))

      return result['data']['orderId']

  # Usage
  order_id = place_order('507f1f77bcf86cd799439011', 'type_001', 2)
  print(f"Order placed: {order_id}")
  ```

  ```php PHP theme={null}
  <?php
  function placeOrder($productId, $typeId, $quantity, $apiKey) {
      $data = [
          'productId' => $productId,
          'typeId' => $typeId,
          'quantity' => $quantity
      ];

      $ch = curl_init('https://api.oneclickdz.com/v3/gift-cards/placeOrder');
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
          'Content-Type: application/json',
          'X-Access-Token: ' . $apiKey
      ]);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

      $response = curl_exec($ch);
      $result = json_decode($response, true);
      curl_close($ch);

      if (!$result['success']) {
          throw new Exception($result['message'] ?? 'Order failed');
      }

      return $result['data']['orderId'];
  }

  // Usage
  $orderId = placeOrder('507f1f77bcf86cd799439011', 'type_001', 2, getenv('API_KEY'));
  echo "Order placed: $orderId\n";
  ?>
  ```

  ```javascript Node.js theme={null}
  const axios = require("axios");

  async function placeOrder(productId, typeId, quantity) {
    try {
      const response = await axios.post(
        "https://api.oneclickdz.com/v3/gift-cards/placeOrder",
        {
          productId: productId,
          typeId: typeId,
          quantity: quantity,
        },
        {
          headers: {
            "Content-Type": "application/json",
            "X-Access-Token": process.env.API_KEY,
          },
        }
      );

      if (!response.data.success) {
        throw new Error(response.data.message || "Order failed");
      }

      return response.data.data.orderId;
    } catch (error) {
      if (error.response) {
        throw new Error(error.response.data.message || "Order failed");
      }
      throw error;
    }
  }

  // Usage
  (async () => {
    try {
      const orderId = await placeOrder("507f1f77bcf86cd799439011", "type_001", 2);
      console.log(`Order placed: ${orderId}`);
    } catch (error) {
      console.error("Error placing order:", error.message);
    }
  })();
  ```

  ```bash cURL theme={null}
  curl https://api.oneclickdz.com/v3/gift-cards/placeOrder \
    -X POST \
    -H "Content-Type: application/json" \
    -H "X-Access-Token: YOUR_API_KEY" \
    -d '{
      "productId": "507f1f77bcf86cd799439011",
      "typeId": "type_001",
      "quantity": 2
    }'
  ```
</CodeGroup>

## Réponse

```json theme={null}
{
  "success": true,
  "data": {
    "orderId": "6901616fe9e88196b4eb64b3"
  }
}
```

## Gestion des erreurs

<CodeGroup>
  ```python Python theme={null}
  try:
      order_id = place_order(product_id, type_id, quantity)
      print(f"Success: {order_id}")
  except Exception as error:
      if 'stock' in str(error):
          print("Out of stock")
      elif 'balance' in str(error):
          print("Insufficient balance")
      else:
          print(f"Order failed: {error}")
  ```

  ```php PHP theme={null}
  <?php
  try {
      $orderId = placeOrder($productId, $typeId, $quantity, $apiKey);
      echo "Success: {$orderId}\n";
  } catch (Exception $error) {
      $errorMsg = strtolower($error->getMessage());

      if (strpos($errorMsg, 'stock') !== false) {
          echo "Out of stock\n";
      } elseif (strpos($errorMsg, 'balance') !== false) {
          echo "Insufficient balance\n";
      } else {
          echo "Order failed: " . $error->getMessage() . "\n";
      }
  }
  ?>
  ```

  ```javascript Node.js theme={null}
  try {
    const orderId = await placeOrder(productId, typeId, quantity);
    console.log(`Success: ${orderId}`);
  } catch (error) {
    const errorMsg = error.message.toLowerCase();

    if (errorMsg.includes("stock")) {
      console.log("Out of stock");
    } else if (errorMsg.includes("balance")) {
      console.log("Insufficient balance");
    } else {
      console.log(`Order failed: ${error.message}`);
    }
  }
  ```
</CodeGroup>

## Étapes suivantes

Après avoir passé une commande :

1. **Sauvegardez l'orderId** - Vous en aurez besoin pour vérifier le statut
2. **Suivez la commande** - Vérifiez le statut jusqu'à l'exécution
3. **Livrez les cartes** - Envoyez les codes à votre client

<CardGroup cols={2}>
  <Card title="Suivre le statut de la commande" icon="chart-line" href="/fr/gift-card-guides/4-status-tracking">
    Surveiller la commande jusqu'à son achèvement
  </Card>

  <Card title="Vérifier les produits" icon="search" href="/fr/gift-card-guides/2-checking-products">
    Vérifier le stock avant de commander
  </Card>
</CardGroup>
