This page explains how to track and manage cryptocurrency orders created through the TwoCoin widget. Understanding the order lifecycle and implementing proper order tracking is essential for a complete integration.

Order Lifecycle

Each order created through the widget goes through several states during its lifecycle:

1

Created

Order is initially created when the user submits the exchange form

2

Payment Pending

Waiting for the user to complete their payment

3

Payment Received

Payment has been received but is being processed

4

Final Status

Completed
Failed
Expired

Tracking Orders with Events

The widget provides several events that you can listen to for order tracking. This is the recommended way to monitor orders in real-time:

<script>
  // Listen for order creation
  TwoCoin.on('orderCreated', function(order) {
    console.log('New order created:', order.orderId);
    // Store the order ID in your system
    saveOrderToDatabase(order);
  });

  // Listen for order status changes
  TwoCoin.on('orderStatusChanged', function(order) {
    console.log('Order status changed:', order.orderId, order.status);
    // Update the order status in your system
    updateOrderStatus(order.orderId, order.status);
  });

  // Listen for order completion
  TwoCoin.on('orderCompleted', function(order) {
    console.log('Order completed:', order.orderId);
    // Process the successful order
    completeOrder(order);
  });
</script>

Order Data Structure

The order object contains the following properties:

{
  "orderId": "ord_123456",
  "status": "completed",
  "createdAt": "2023-04-15T14:23:45Z",
  "updatedAt": "2023-04-15T14:30:12Z",
  "expiresAt": "2023-04-15T15:23:45Z",
  "sourceAmount": 100,
  "sourceCurrency": "USD",
  "targetAmount": 0.00345,
  "targetCurrency": "BTC",
  "fee": 3.5,
  "rate": 28985.5,
  "paymentMethod": "credit_card",
  "walletAddress": "bc1q...",
  "transactionHash": "0x...",  // Only available for completed orders
  "customer": {
    "email": "[email protected]"
  }
}

Retrieving Order Details

You can retrieve order details using the widget's JavaScript API:

TwoCoin.getOrder('ord_123456')
  .then(order => {
    console.log('Order details:', order);
  })
  .catch(error => {
    console.error('Error retrieving order:', error);
  });

Displaying Order History

If you want to display a user's order history, you can use the widget's order history component:

<div id="twocoin-order-history"></div>
<script>
  TwoCoin.renderOrderHistory({
    merchantId: 'YOUR_MERCHANT_ID',
    containerId: 'twocoin-order-history',
    customerEmail: '[email protected]', // Optional - limits to specific customer
    limit: 10, // Optional - number of orders to display
    theme: 'dark'
  });
</script>

Order Webhooks

For server-side tracking of orders, you should use webhooks in addition to the client-side events. Webhooks ensure you don't miss any updates even if the user closes their browser.

See the API Webhooks documentation for details on setting up and receiving webhook notifications for orders.

Order Verification

For security purposes, you may want to verify order details on your server. You can use the TwoCoin API to verify order information:

// Server-side code (Node.js example)
const axios = require('axios');
const crypto = require('crypto');

async function verifyOrder(orderId) {
  const timestamp = Math.floor(Date.now() / 1000);
  const path = `/v1/orders/${orderId}`;

  // Generate signature
  const signature = generateSignature(
    process.env.MERCHANT_ID,
    process.env.API_SECRET,
    timestamp,
    'GET',
    path
  );

  // Make API request
  const response = await axios.get(`https://api.two-coin.com${path}`, {
    headers: {
      'X-Merchant-ID': process.env.MERCHANT_ID,
      'X-Timestamp': timestamp,
      'X-Signature': signature
    }
  });

  return response.data;
}

function generateSignature(merchantId, apiSecret, timestamp, method, path, body = '') {
  const message = `${merchantId}|${timestamp}|${method}|${path}|${body}`;
  return crypto
    .createHmac('sha256', apiSecret)
    .update(message)
    .digest('hex');
}

Next Steps

Support

If you encounter any issues or have questions not addressed in this documentation, please contact our support team on Telegram at https://t.me/cs_2coin (@cs_2coin).