# Webhooks & Notifications

### Introduction

Webhooks allow your application to receive real-time updates about payment statuses from Resmic Pro. This ensures that you are notified immediately when a payment is completed, pending, or failed. You can either provide an endpoint to receive webhook notifications or opt for email updates (not recommended for real-time processing).

### Configuring Webhooks

#### Option 1: Providing a Webhook URL (Recommended)

To receive automatic updates, configure a webhook endpoint in your Resmic Pro dashboard:

1. Just append the <kbd>`web_hook url`</kbd>  while initiating the payment session

Whenever a payment status is updated, Resmic Pro will send a `POST` request to the configured URL.

> You will required Webhook secret to verify the data you received is correct.
>
> To generate the webhook secret visit: <https://dashboard.resmic.com/developer>

**Example Server Webhook Response**

```json
{
    session_id,
    amount, 
    blockchain, 
    token,
    from_wallet_address,
    transaction_hash,
    txTime,
    status:true
}   
```

#### Option 2: Receiving Email Notifications (Not Recommended)

If you prefer, you can opt to receive payment updates via email instead of webhooks.&#x20;

To enable email notifications:

Just add email address at the <kbd>`webhook_url`</kbd> while initiating the payment

Whenever a payment status is updated, you will receive an email notification containing the payment details.

### Handling Webhook Notifications

When you receive a webhook notification, your server should:

1. Parse the JSON payload.
2. Verify the <kbd>`session_id`</kbd> and `status` fields.
3. Update your database accordingly.
4. Respond with a `200 OK` to acknowledge receipt.

### E.g. Success Payment response

\`req.body\`

```json
{
  session_id: '591abe63-aa61-411d-a419-0b546729bbd0',
  amount: '20',
  blockchain: 'Sepolia',
  token: 'DAI',
  from_wallet_address: '0x056397760b973BfB921Bc10Be9DA5034B1e921d7',
  transaction_hash: '0xa43df332c8d376fc14ffc992c4b87e04a5707a3bc7a92ec4eef7b90c66388db2',
  txTime: 'Mar 5, 2025, 03:36',
  status: true
}
```

### Failed Payment response

\`req.body\`

```json
{
        session_id:"session_id",
        txTime:"Tx_time",
        status:false
}
```

### Sample Webhook endpoint

```javascript
const express = require("express");
const crypto = require("crypto");

const app = express();
const port = 3000;

app.use(express.json()); // Middleware to parse JSON request bodies

// Your webhook secret (Get it from Resmic Dashboard)
const WEBHOOK_SECRET = "YOUR_WEBHOOK_SECRET";

// Middleware to verify the webhook signature
const verifyWebhookSignature = async (req, res, next) => {
  const clientSignature = req.headers["x-signature"]; // Get the signature from the request headers
  // Verify the request using HMAC
  const isSignatureValid = await verifyHMAC(req.body, clientSignature, WEBHOOK_SECRET);

  if (!isSignatureValid) {
    return res.status(401).json({ success: false, message: "Unauthorized access" });
  }

  next(); // Proceed to the next middleware or route handler
};

// Root route for testing
app.get("/", (req, res) => {
  res.send("Hello, Webhooks!");
});

// Webhook endpoint for payment updates
app.post("/payment-webhook", verifyWebhookSignature, async (req, res) => {
  const {
    session_id,
    amount,
    blockchain,
    token,
    from_wallet_address,
    transaction_hash,
    txTime,
    status,
  } = req.body;

  console.log("Webhook Received:", req.body);

  if (status) {
    // Handle successful payment logic here (e.g., updating database, sending confirmation emails, etc.)
    console.log("✅ Payment Confirmed for Session:", session_id);
  } else {
    console.log("⚠️ Payment Status:", status);
  }


  // Send a response back to acknowledge receipt of the webhook
  res.status(200).json({
    success: true,
    message: "Webhook processed successfully",
    data: {
      session_id,
      txTime,
      status,
    },
  });
});

// Start the Express server
app.listen(port, () => {
  console.log(`🚀 Webhook listener running on http://localhost:${port}`);
});

// Helper function to verify HMAC signature
function verifyHMAC(payload, signature, secretKey) {
  const data = JSON.stringify(payload);
  const computedSignature = crypto.createHmac("sha256", secretKey).update(data).digest("hex");
  return computedSignature === signature;
}

```

### Conclusion

By configuring webhooks, you can automatically track payment updates in real-time. If webhooks are not an option, email notifications can serve as an alternative.
