# OnePay Payment Integration - Issues & Solutions

## Current Status: ⚠️ API Authentication Issue

### Problem
The OnePay API is returning **Status 200 with HTML content** instead of JSON. This indicates invalid API credentials or incorrect endpoint configuration.

### What's Working ✅
1. **Frontend Form** - Correctly collects ad data and payment type
2. **Payment Amount Calculation** - Properly maps ad types to prices:
   - Regular: 0.00 LKR
   - Urgent: 10.00 LKR  
   - Top: 10.00 LKR
   - Bump: 15.00 LKR
3. **Form Submission** - Data is correctly sent to backend via axios
4. **Session Storage** - Ad data properly stored during payment flow
5. **Hash Generation** - SHA256 hash correctly computed
6. **Logging** - Comprehensive logging in place

### What's NOT Working ❌
1. **OnePay API Response** - Returns HTML homepage instead of JSON payment link
2. **Payment Flow** - Cannot redirect user to payment gateway

---

## Root Cause

The OnePay API credentials in `.env` are **INVALID or EXPIRED**. The API is returning their website homepage (HTML) because:

1. **Wrong APP_ID** - The merchant application ID is incorrect
2. **Wrong APP_TOKEN** - The Bearer token is invalid/expired
3. **Wrong HASH_SALT** - The salt used for signature doesn't match
4. **Wrong API_URL** - The endpoint might be incorrect

---

## How to Fix

### Step 1: Get Correct OnePay Credentials

1. Login to your **OnePay Merchant Dashboard**
2. Navigate to **Settings → API Configuration** (or similar)
3. Copy these values:
   - `APP_ID` (Merchant Application ID)
   - `APP_TOKEN` (API Token/Secret Key)
   - `HASH_SALT` (Signature Salt)
   - `API_URL` (Integration Endpoint)

### Step 2: Update .env File

Open `d:\Projects\Laravel\hotads\hotads\.env` and update:

```env
# OnePay Payment Gateway Configuration
ONEPAY_APP_ID=YOUR_ACTUAL_APP_ID_HERE
ONEPAY_APP_TOKEN=YOUR_ACTUAL_TOKEN_HERE
ONEPAY_HASH_SALT=YOUR_ACTUAL_SALT_HERE
ONEPAY_API_URL=https://api.onepay.lk/api/ipg/gateway/request-payment-link/
```

### Step 3: Clear Config Cache

```bash
php artisan config:cache
```

### Step 4: Test Configuration

Visit: `http://127.0.0.1:8000/test-payment-config`

This endpoint will:
- Show your current configuration
- Test the API connection
- Display the actual API response
- Provide troubleshooting steps

**Expected Response:**
- ✅ `is_json: true` and `status: 200` = API working
- ❌ `is_json: false` and HTML content = Credentials still wrong

---

## Testing the Payment Flow

Once credentials are correct:

1. Go to **Create Ad** page
2. Fill in product details
3. Select **Urgent**, **Top**, or **Bump** ad type
4. Choose **Online** payment method
5. Click **Post Ad**
6. Should redirect to OnePay payment gateway
7. Complete payment
8. Should callback to `/payment/onepay/callback`
9. Ad should be created with payment record

---

## Files Modified

### Frontend
- `resources/js/Pages/Public/Ads/Create.tsx`
  - Added `getAdTypeAmount()` function
  - Modified `processOnePayPayment()` to send correct amount
  - Changed price inputs to number type with validation

### Backend
- `app/Http/Controllers/Public/Payment/OnePayController.php`
  - Enhanced error messages
  - Added comprehensive logging
  
- `app/Intergration/OnePay/OnePayIntegration.php`
  - Improved error detection (HTML vs JSON)
  - Added detailed diagnostic logging
  - Better error messages

- `config/services.php`
  - Added OnePay configuration array

- `.env`
  - Added OnePay environment variables

- `routes/web.php`
  - Added test configuration route

- `routes/test-payment.php` (NEW)
  - Diagnostic endpoint for testing API configuration

---

## API Request Format

The system sends this to OnePay:

```json
{
  "amount": "10.00",
  "currency": "LKR",
  "app_id": "YOUR_APP_ID",
  "reference": "AD-1234567890-AbCdEfGh",
  "customer_first_name": "John",
  "customer_last_name": "Doe",
  "customer_phone_number": "0771234567",
  "customer_email": "customer@example.com",
  "transaction_redirect_url": "http://127.0.0.1:8000/payment/onepay/callback",
  "hash": "sha256_hash_signature"
}
```

**Headers:**
```
Authorization: Bearer YOUR_APP_TOKEN
Content-Type: application/json
Accept: application/json
```

---

## Expected OnePay Response

**Success:**
```json
{
  "success": true,
  "data": {
    "gateway": {
      "redirect_url": "https://payment.onepay.lk/pay/xxx"
    }
  }
}
```

**Failure:**
```json
{
  "success": false,
  "message": "Error description"
}
```

---

## Contact OnePay Support

If you still get HTML responses after updating credentials:

1. Email: `support@onepay.lk` (verify actual support email)
2. Ask for:
   - API integration documentation
   - Test credentials (sandbox)
   - Correct endpoint URLs
   - Hash generation format
   - Example request/response

Provide them:
- Your merchant account ID
- The error you're seeing (HTML instead of JSON)
- Your integration attempt logs

---

## Logs Location

Check logs for detailed error information:
```bash
Get-Content "d:\Projects\Laravel\hotads\hotads\storage\logs\laravel.log" -Tail 50
```

Look for:
- `OnePay initiate request received` - Shows amount being sent
- `OnePay request data` - Shows full request payload
- `OnePay API raw response` - Shows API response status
- `OnePay API error` - Shows error details

---

## Security Notes

**⚠️ Remove test endpoint in production:**

Edit `routes/web.php` and comment out:
```php
// Test Payment Configuration (Remove in production!)
if (!app()->environment('production')) {
    require __DIR__.'/test-payment.php';
}
```

**Never commit:**
- Actual API credentials to version control
- Keep `.env` in `.gitignore`
- Use environment variables for all sensitive data

---

## Next Steps

1. ✅ Get correct OnePay credentials from merchant dashboard
2. ✅ Update `.env` file with real values
3. ✅ Run `php artisan config:cache`
4. ✅ Test using `/test-payment-config` endpoint
5. ✅ If still failing, contact OnePay support
6. ✅ Once working, test full payment flow
7. ✅ Remove test endpoint before production deploy

---

Generated: January 4, 2026  
Developer: JC (jagadchandana61@gmail.com)
