# FamGateway PHP SDK 🚀

Welcome to the **FamGateway PHP SDK**! This lightweight drop-in module makes integrating our automated UPI payment gateway into your PHP website incredibly simple. You can process secure payments and verify transactions in just a few minutes.

## What's Included?

1. **`FamGateway.php`** 
   - The core SDK library. It securely communicates with the FamGateway API so you don't have to write any cURL or JSON parsing code.
2. **`pay_now.php`** 
   - An example of how to generate an order and redirect the customer to the secure checkout page.
3. **`webhook.php`** 
   - An example of how to securely receive automated payment success notifications (Webhooks) from our server.

---

## 🛠️ Step 1: Create a Payment (Checkout Flow)

When a customer wants to pay, use the SDK to generate an order and redirect them to our checkout page. 

**Example (pay_now.php):**
```php
<?php
require 'FamGateway.php';

// 1. Initialize the Gateway with your API Key
$fam = new FamGateway("YOUR_API_KEY_HERE");

// 2. Set the amount and success URL
$amount = 150.00; // Amount in INR
$redirectUrl = "https://yourwebsite.com/success.php"; // Where user goes after payment

// 3. Create the payment and automatically redirect the user
$fam->createPayment($amount, $redirectUrl);
?>
```
*Note: Make sure your `redirectUrl` starts with `http://` or `https://`.*

---

## 🔐 Step 2: Handle Webhooks (Automated Fulfillment)

Customers might close the browser before returning to your success page. To guarantee you never miss a successful payment, set up a **Webhook URL** in your FamGateway Dashboard.

When an order is paid, we will send an automated `POST` request to your webhook URL.

**Example (webhook.php):**
```php
<?php
require 'FamGateway.php';

$fam = new FamGateway("YOUR_API_KEY_HERE");

// 1. Get raw request data and signature header
$rawPostData = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_FAMGATEWAY_SIGNATURE'] ?? '';

// 2. Verify the webhook's authenticity
$data = $fam->verifyWebhook($rawPostData, $signature);

if ($data !== false) {
    if ($data['event'] === 'payment.success') {
        $orderId = $data['data']['order_id'];
        $amount = $data['data']['amount'];
        $transactionId = $data['data']['transaction_id'];
        
        // TODO: Update your database!
        // Example: $db->query("UPDATE orders SET status='paid' WHERE id='$orderId'");
        
        http_response_code(200);
        echo "Webhook processed.";
    }
} else {
    http_response_code(403);
    echo "Invalid signature!";
}
?>
```

---

## ❓ Need Help?
Check out the full Developer Documentation in your FamGateway dashboard. Happy Coding! 💻
