# Pesapal Payment Integration

This document describes the Pesapal payment gateway integration that has been added to the events project.

## Files Created/Modified

### Controllers Created
1. **app/Http/Controllers/FrontEnd/PaymentGateway/PesapalController.php**
   - Handles event booking payments via Pesapal
   - Includes methods: `makePayment()`, `callback()`, `ipn()`
   - Automatically registers IPN URL on first use

2. **app/Http/Controllers/FrontEnd/Shop/PaymentGateway/PesapalController.php**
   - Handles shop/product order payments via Pesapal
   - Includes methods: `purchaseProcess()`, `callback()`, `ipn()`
   - Automatically registers IPN URL on first use

### Controllers Modified
1. **app/Http/Controllers/FrontEnd/Event/BookingController.php**
   - Added Pesapal import and payment gateway option

2. **app/Http/Controllers/FrontEnd/Shop/OrderController.php**
   - Added Pesapal import and payment gateway option

3. **app/Http/Controllers/BackEnd/PaymentGateway/OnlineGatewayController.php**
   - Added `updatePesapalInfo()` method for admin configuration
   - Added Pesapal to gateway list in `index()` method

4. **app/Http/Controllers/BackEnd/MobileInterfaceController.php**
   - Added 'pesapal' to mobile gateway list

### Routes Added
**routes/web.php:**
- `GET /pesapal/callback` - Event booking callback
- `GET /pesapal/ipn` - Event booking IPN endpoint
- `GET /product-order/pesapal/callback` - Shop order callback
- `GET /product-order/pesapal/ipn` - Shop order IPN endpoint

**routes/admin.php:**
- `POST /admin/payment-gateways/update-pesapal-info` - Admin configuration update

### Views Modified
1. **resources/views/backend/payment-gateways/online-gateways.blade.php**
   - Added Pesapal configuration form section

### Config Files Created
1. **config/pesapal.php**
   - Configuration file for Pesapal settings

## Database Setup

You need to add Pesapal to the `online_gateways` table. Run this SQL:

```sql
INSERT INTO `online_gateways` (`name`, `keyword`, `information`, `status`, `mobile_status`, `mobile_information`) 
VALUES ('Pesapal', 'pesapal', '{"sandbox_status":"1","consumer_key":"","consumer_secret":"","ipn_id":null}', 0, 0, '{"sandbox_status":"1","consumer_key":"","consumer_secret":""}');
```

## Configuration Steps

1. **Access Admin Panel:**
   - Go to Payment Gateways > Online Gateways
   - Find the Pesapal section

2. **Configure Pesapal:**
   - Set Status: Active/Deactive
   - Set Sandbox Status: Active (for testing) or Deactive (for production)
   - Enter Consumer Key (from your Pesapal merchant account)
   - Enter Consumer Secret (from your Pesapal merchant account)
   - Click Update

3. **IPN Registration:**
   - The IPN URL is automatically registered on first payment attempt
   - The IPN ID will be saved in the database after first registration
   - IPN URLs:
     - Event Bookings: `https://yourdomain.com/pesapal/ipn`
     - Shop Orders: `https://yourdomain.com/pesapal/shop/ipn`

## Environment Variables (Optional)

You can also set these in your `.env` file:

```
PESAPAL_CONSUMER_KEY=your_consumer_key
PESAPAL_CONSUMER_SECRET=your_consumer_secret
PESAPAL_SANDBOX_STATUS=1
```

## Features Implemented

1. ✅ Authentication with Pesapal API
2. ✅ Automatic IPN URL registration
3. ✅ Event booking payments
4. ✅ Shop/product order payments
5. ✅ Payment callback handling
6. ✅ IPN notification handling
7. ✅ Admin configuration interface
8. ✅ Sandbox and Production mode support
9. ✅ Error handling and logging

## API Endpoints Used

- **Authentication:** `/api/Auth/RequestToken`
- **IPN Registration:** `/api/URLSetup/RegisterIPN`
- **Submit Order:** `/api/Transactions/SubmitOrderRequest`
- **Get Transaction Status:** `/api/Transactions/GetTransactionStatus`

## Testing

1. Use sandbox credentials from Pesapal
2. Set Sandbox Status to Active
3. Make a test payment
4. Verify callback and IPN are working
5. Check payment status updates in database

## Notes

- The IPN ID is automatically generated and stored after first payment attempt
- Both GET and POST IPN methods are supported (currently configured for GET)
- Payment status is verified via GetTransactionStatus API before completing orders
- Country codes are automatically mapped (defaults to KE if not found)

## Support

For Pesapal API documentation, visit:
- Postman Collection: https://documenter.getpostman.com/view/6715320/UyxepTv1
- Sandbox URL: https://cybqa.pesapal.com/pesapalv3
- Production URL: https://pay.pesapal.com/v3
