# Order Webhook Extension for Magento 2.4.7

This extension provides webhook functionality to send order data to external APIs when orders are placed or updated.

## Features

- Sends order data to external API endpoints via webhook
- Configurable through Magento Admin
- Retry mechanism with exponential backoff
- Comprehensive logging
- Support for custom API authentication
- Console command for testing webhooks

## Installation

1. Copy the extension files to `app/code/PortalPeople/OrderWebhook/`
2. Run the following commands:

```bash
php bin/magento module:enable PortalPeople_OrderWebhook
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush
```

## Configuration

1. Navigate to **Stores > Configuration > Custom Extensions > Order Webhook Settings**
2. Configure the following settings:
	 - **Enable Webhook**: Enable/disable the webhook functionality
	 - **API Endpoint URL**: The URL where order data will be sent
	 - **API Key**: Optional API key for authentication (sent as Bearer token)
	 - **Request Timeout**: HTTP request timeout in seconds (default: 30)
	 - **Retry Attempts**: Number of retry attempts on failure (default: 3)
	 - **Enable Logging**: Enable/disable logging of webhook activities

## Webhook Events

The extension triggers webhooks on the following events:
- `order_placed`: When an order is successfully placed
- `order_updated`: When an order status changes to processing, complete, or shipped

## Webhook Payload

The webhook sends a JSON payload with the following structure:

```json
{
	"event_type": "order_placed",
	"timestamp": "2025-08-01T10:30:00+00:00",
	"order": {
		"id": 123,
		"increment_id": "000000123",
		"status": "processing",
		"state": "processing",
		"store_id": 1,
		"created_at": "2025-08-01 10:30:00",
		"updated_at": "2025-08-01 10:30:00",
		"currency_code": "USD",
		"grand_total": 150.00,
		"subtotal": 130.00,
		"tax_amount": 10.00,
		"shipping_amount": 10.00,
		"discount_amount": 0.00,
		"total_qty_ordered": 2,
		"weight": 5.5,
		"coupon_code": null
	},
	"customer": {
		"id": 456,
		"email": "customer@example.com",
		"firstname": "John",
		"lastname": "Doe",
		"group_id": 1,
		"is_guest": false
	},
	"billing_address": { ... },
	"shipping_address": { ... },
	"payment": {
		"method": "checkmo",
		"additional_information": []
	},
	"items": [
		{
			"item_id": 789,
			"product_id": 101,
			"sku": "PRODUCT-SKU",
			"name": "Product Name",
			"qty_ordered": 1,
			"price": 100.00,
			"row_total": 100.00
		}
	],
	"shipping_method": {
		"method": "flatrate_flatrate",
		"description": "Flat Rate - Fixed"
	}
}
```

## Testing

Use the console command to test webhook functionality:

```bash
php bin/magento order:webhook:test [ORDER_ID]
```

Replace `[ORDER_ID]` with an existing order ID to test the webhook.

## Logs

When logging is enabled, webhook activities are logged to:
- `var/log/system.log`
- Look for entries prefixed with `[OrderWebhook]`

## Security Considerations

- Use HTTPS endpoints for production
- Implement proper authentication on your webhook endpoint
- Validate webhook signatures if needed (can be extended)
- Consider rate limiting on your webhook endpoint

## Troubleshooting

1. **Webhook not sending**: Check if the extension is enabled and properly configured
2. **Request timeout**: Increase the timeout value in configuration
3. **Authentication issues**: Verify the API key and endpoint URL
4. **Check logs**: Enable logging and check `var/log/system.log` for errors

## Customization

To extend the webhook payload or add custom fields:
1. Modify `PortalPeople\OrderWebhook\Model\OrderDataBuilder`
2. Add custom fields to the `build()` method
3. Recompile and clear cache

## Support

This extension is provided as-is. For customizations or support, please contact your development team.