# Payment Plugin Setup Guide

## โœ… Plugin Setup Complete!

Your Adyen payment plugin is now ready to use with token authentication from your main app.

## ๐Ÿ”ง How It Works

The plugin uses **Dio** with an **interceptor** that automatically adds your auth token to every API request.

### Architecture

```
Main App โ†’ Token Provider โ†’ Plugin (Dio + Interceptor) โ†’ API with Token
```

## ๐Ÿ“ Usage in Your Main App

### 1. Add the Plugin to Your Main App's `pubspec.yaml`

```yaml
dependencies:
  payment_plugin:
    path: ../payment_plugin  # Adjust path as needed
```

### 2. Initialize the AdyenRepository

```dart
import 'package:payment_plugin/payment_plugin.dart';

// Initialize with your token provider
final adyenRepo = AdyenRepository(
  tokenProvider: () async {
    // Get token from your auth service in main app
    // This function is called every time the plugin makes an API call
    return await yourAuthService.getToken();
  },
  baseUrl: 'https://your-api.com',
);
```

### 3. Use the Repository

```dart
// Create a payment session for checkout
final config = await adyenRepo.sessionCheckout(
  'hotel123',      // hotelCode
  'booking456',    // bookingId
  false,           // usePoints
);

// Add a new card
final cardConfig = await adyenRepo.fetchSessionForAddingCard();

// Get stored payment methods
final storedMethods = await adyenRepo.mockStoredPaymentData;
```

## ๐Ÿ” Token Authentication Flow

1. **Main app** calls a payment method on `adyenRepo`
2. **Plugin** needs to make an API call
3. **Dio interceptor** calls your `tokenProvider()` function
4. **Main app** returns the current auth token
5. **Interceptor** adds `Authorization: Bearer {token}` header
6. **API request** is sent with authentication

### Benefits

โœ… Token stays in your main app (never stored in plugin)
โœ… Works with token refresh (always gets latest token)
โœ… Automatic authentication on all requests
โœ… Clean separation of concerns

## ๐Ÿ“ฆ Available API Methods

The plugin includes the following API endpoints:

- `getPaymentMethods()` - Get stored payment methods
- `createAdyenSession()` - Create session for booking payment
- `createAdyenSessionForCards()` - Create session for adding cards
- `listAvailablePaymentMethods()` - List available payment methods
- `storePaymentMethod()` - Store a new payment method
- `removePaymentMethod()` - Remove a payment method
- `submitPayment()` - Submit a payment

## ๐Ÿ—๏ธ Plugin Structure

```
payment_plugin/
โ”œโ”€โ”€ lib/
โ”‚   โ”œโ”€โ”€ adyen/
โ”‚   โ”‚   โ”œโ”€โ”€ adyen_repository.dart     # Main repository with business logic
โ”‚   โ”‚   โ”œโ”€โ”€ api_client.dart           # Retrofit API client
โ”‚   โ”‚   โ””โ”€โ”€ models/                   # Adyen-specific models
โ”‚   โ”œโ”€โ”€ data/
โ”‚   โ”‚   โ””โ”€โ”€ remote/
โ”‚   โ”‚       โ””โ”€โ”€ models/               # API response models
โ”‚   โ””โ”€โ”€ payment_plugin.dart           # Main export file
โ”œโ”€โ”€ android/                          # Android-specific code
โ”œโ”€โ”€ ios/                              # iOS-specific code
โ””โ”€โ”€ example/                          # Example usage app
```

## ๐Ÿงช Testing

Run the example app to test the plugin:

```bash
cd example
flutter run
```

## ๐Ÿ”„ Development Workflow

When you make changes to models with `@JsonSerializable` or `@RestApi`:

```bash
# Regenerate code
flutter pub run build_runner build --delete-conflicting-outputs

# Or watch for changes
flutter pub run build_runner watch --delete-conflicting-outputs
```

## ๐Ÿ“ฑ AndroidManifest Configuration

The plugin already includes necessary permissions in:
`android/src/main/AndroidManifest.xml`

For Adyen-specific configuration, you may need to add activities or permissions to your **main app's** AndroidManifest.

## ๐ŸŽฏ Next Steps

1. โœ… Plugin structure created
2. โœ… Token authentication configured
3. โœ… Code generation setup
4. โœ… API client with Retrofit
5. ๐Ÿ”œ Add plugin to your main app
6. ๐Ÿ”œ Test with real API endpoints
7. ๐Ÿ”œ Implement payment flows

## ๐Ÿ’ก Tips

- The `tokenProvider` function is called for **every API request**, so make sure it's efficient
- If you need to refresh tokens, do it in your main app's auth service, not in the plugin
- The plugin uses **Dio** which supports interceptors, retries, and more advanced features
- All API responses are wrapped in `ApiResponse<T>` for consistent error handling

## ๐Ÿ“š Key Files to Know

- **`lib/adyen/adyen_repository.dart`** - Your main entry point
- **`lib/adyen/api_client.dart`** - REST API definitions
- **`lib/payment_plugin.dart`** - Public API exports
- **`pubspec.yaml`** - Dependencies (Dio, Retrofit, Adyen SDK)

---

**Ready to integrate!** ๐Ÿš€

For questions or issues, check the example app in `example/lib/main.dart`.