# Setup: Receiver Terpisah untuk SALES_INVOICE dan SALES_RECEIPT

## Perubahan

**Sebelum:** Satu file `receiver.php` handle kedua type berdasarkan header `X-Webhook-Type`.

**Sesudah:** Dua file terpisah:
- `receiver-invoice.php` → Hanya handle SALES_INVOICE
- `receiver-receipt.php` → Hanya handle SALES_RECEIPT

**Keuntungan:**
- ✅ Isolasi yang lebih baik (tidak saling blocking)
- ✅ Mudah di-scale terpisah
- ✅ Mudah debug (log terpisah)
- ✅ Bisa di-deploy di server berbeda jika perlu

---

## Langkah Setup

### 1. Upload File ke Shared Hosting

**Upload 2 file ke shared hosting:**

- `receiver-invoice.php` → `https://po.persadalab.com/webhook/receiver-invoice.php`
- `receiver-receipt.php` → `https://po.persadalab.com/webhook/receiver-receipt.php`

**Via cPanel File Manager atau FTP/SCP.**

---

### 2. Update .env di VPS (Golang)

**Edit `.env` di VPS:**

```bash
sudo nano /opt/webhook-service/.env
```

**Tambahkan/update:**

```env
# Shared Hosting Configuration
SHARED_HOSTING_URL=https://po.persadalab.com/webhook/receiver.php  # Fallback (opsional)
SHARED_HOSTING_URL_INVOICE=https://po.persadalab.com/webhook/receiver-invoice.php
SHARED_HOSTING_URL_RECEIPT=https://po.persadalab.com/webhook/receiver-receipt.php
SHARED_HOSTING_SECRET=8f7b2c5e4d1a9b0c3f5e9d8a7b6c5a4b3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a
```

**Simpan (Ctrl+O, Enter, Ctrl+X).**

---

### 3. Restart Service Golang

```bash
sudo systemctl restart webhook-service
sudo systemctl status webhook-service
```

---

### 4. Test Manual

**Test SALES_INVOICE:**

```bash
curl -X POST https://po.persadalab.com/webhook/receiver-invoice.php \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Secret: 8f7b2c5e4d1a9b0c3f5e9d8a7b6c5a4b3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a" \
  -H "X-Webhook-Source: VPS-GOLANG-RELAY" \
  -d '{
    "payload": {
      "type": "SALES_INVOICE",
      "databaseId": 1293289,
      "uuid": "test-invoice-001",
      "data": [{"salesInvoiceNo": "INV-001"}]
    },
    "timestamp": "2026-02-02T10:00:00Z"
  }'
```

**Expected:** `{"status":"success","message":"Processed"}`

**Test SALES_RECEIPT:**

```bash
curl -X POST https://po.persadalab.com/webhook/receiver-receipt.php \
  -H "Content-Type: application/json" \
  -H "X-Webhook-Secret: 8f7b2c5e4d1a9b0c3f5e9d8a7b6c5a4b3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a" \
  -H "X-Webhook-Source: VPS-GOLANG-RELAY" \
  -d '{
    "payload": {
      "type": "SALES_RECEIPT",
      "databaseId": 1293289,
      "uuid": "test-receipt-001",
      "data": [{"salesReceiptNo": "BKB-001"}]
    },
    "timestamp": "2026-02-02T10:00:00Z"
  }'
```

**Expected:** `{"status":"success","message":"Processed"}`

---

## Verifikasi Setelah Deploy

### 1. Cek Log Golang

```bash
sudo journalctl -u webhook-service -f
```

**Yang harus muncul:**
- `🔄 [Queue X] Forwarding SALES_INVOICE to shared hosting...`
- `✅ [Forward] Shared hosting responded OK` → URL invoice
- `🔄 [Queue Y] Forwarding SALES_RECEIPT to shared hosting...`
- `✅ [Forward] Shared hosting responded OK` → URL receipt

### 2. Cek Log Shared Hosting

**Invoice log:**
- `[Invoice] Processing SALES_INVOICE (DB ID: ...)`
- `[Invoice] Success for PO: ...`

**Receipt log:**
- `[Receipt] Processing SALES_RECEIPT (DB ID: ...)`
- `[Receipt] Success Recorded!`

---

## Alur Setelah Setup

### SALES_INVOICE
```
Golang → receiver-invoice.php → handleSalesInvoice() → Update t_po.nomor_invoice
```

### SALES_RECEIPT
```
Golang → receiver-receipt.php → handleSalesReceipt() → Insert t_po_pembayaran + Update PO
```

**Keduanya tidak saling blocking!** ✅

---

## Fallback Behavior

**Jika URL khusus kosong:**
- Golang akan pakai `SHARED_HOSTING_URL` (fallback)
- Ini untuk backward compatibility

**Tapi lebih baik set URL khusus untuk isolasi yang lebih baik.**

---

## Checklist

- [ ] `receiver-invoice.php` sudah di-upload ke shared hosting
- [ ] `receiver-receipt.php` sudah di-upload ke shared hosting
- [ ] `.env` di VPS sudah di-update dengan URL terpisah
- [ ] Service Golang sudah di-restart
- [ ] Test manual kedua receiver berhasil
- [ ] Monitor log untuk verifikasi

---

**Setelah setup ini, SALES_INVOICE dan SALES_RECEIPT akan punya jalur sendiri yang tidak saling blocking!** 🎉
