# BTCPay Server Integration Fix - Deployment Guide

## Problem Summary
The BTCPay integration wasn't creating actual orders on BTCPay Server, resulting in no wallet addresses being provided to customers.

## Root Causes Identified
1. **Currency Mismatch**: Service was trying to create invoices in cryptocurrency (BTC) instead of fiat (GBP)
2. **Incorrect Address Extraction**: Code was looking for `CryptoInfo` property that doesn't exist in BTCPay v2 API
3. **Missing Payment Method Configuration**: Not specifying which payment methods to accept

## Solutions Implemented

### 1. Fixed Invoice Creation (BTCPayServerService.cs)
- Changed invoice currency from crypto (BTC) to fiat (GBP)
- BTCPay Server now handles automatic crypto conversion
- Added payment method specification to invoices
- Enhanced error logging for debugging

### 2. Updated Payment Flow (CryptoPaymentService.cs)
- Removed incorrect wallet address extraction logic
- Now uses BTCPay's CheckoutLink for customer payments
- BTCPay manages all wallet addresses internally
- Added comprehensive error handling

### 3. Added Diagnostic Endpoints (BTCPayTestController.cs)
- `GET /api/btcpay-test/connection` - Test BTCPay connection
- `GET /api/btcpay-test/stores` - List available stores
- `GET /api/btcpay-test/invoice/{invoiceId}` - Get invoice details
- `POST /api/btcpay-test/test-invoice` - Create test invoice
- `POST /api/btcpay-test/test-payment` - Create test payment with specific crypto

## Testing Instructions

### 1. Deploy the Updated Code
```bash
# Build the application
dotnet build --configuration Release

# Deploy to Hostinger
# (Use your existing deployment process)
```

### 2. Verify BTCPay Configuration
Check that your `appsettings.Hostinger.json` has:
```json
{
  "BTCPayServer": {
    "BaseUrl": "https://thebankofdebbie.giize.com",
    "ApiKey": "994589c8b514531f867dd24c83a02b6381a5f4a2",
    "StoreId": "AoxXjM9NJT6P9C1MErkaawXaSchz8sFPYdQ9FyhmQz33",
    "WebhookSecret": "your-webhook-secret-here"
  }
}
```

### 3. Test Connection
```bash
# Test BTCPay connection
curl -X GET https://your-app.com/api/btcpay-test/connection \
  -H "Cookie: your-admin-cookie"

# Response should show "Connected" status
```

### 4. Create Test Payment
```bash
# Create a test Bitcoin payment
curl -X POST https://your-app.com/api/btcpay-test/test-payment \
  -H "Content-Type: application/json" \
  -H "Cookie: your-admin-cookie" \
  -d '{"amount": 10.00, "cryptoCurrency": 0}'

# Response will include:
# - invoiceId: BTCPay invoice ID
# - checkoutLink: URL for customer to complete payment
# - paymentUrl: Direct link to payment page
```

### 5. Important Notes

#### How BTCPay v2 Works
- **No Raw Addresses**: BTCPay v2 doesn't expose raw wallet addresses via API
- **Checkout Links**: Customers must use the checkout link to pay
- **Managed Wallets**: BTCPay handles all wallet management internally
- **Automatic Conversion**: Invoices are created in GBP, BTCPay converts to crypto

#### What Customers See
1. Order is created in your system
2. BTCPay invoice is generated with checkout link
3. Customer clicks checkout link
4. BTCPay shows payment page with:
   - QR code for payment
   - Wallet address (visible on BTCPay page)
   - Amount in selected cryptocurrency
   - Payment timer

#### Monitoring Payments
- Check BTCPay dashboard at https://thebankofdebbie.giize.com
- View invoice status and payment confirmations
- Webhook notifications update order status automatically

## Troubleshooting

### No Checkout Link Returned
- Verify BTCPay Server has wallets configured
- Check API key has correct permissions
- Ensure store ID is correct

### Invoice Created but No Payment Options
- Login to BTCPay Server admin
- Go to Store Settings → Payment Methods
- Enable desired cryptocurrencies
- Configure wallet for each currency

### Connection Errors
- Check firewall allows HTTPS to BTCPay server
- Verify SSL certificates are valid
- Test with `curl https://thebankofdebbie.giize.com`

## Next Steps

1. **Test in Production**: Deploy and test with small amounts
2. **Configure Webhooks**: Set up webhook endpoint for payment notifications
3. **Monitor Logs**: Check application logs for any errors
4. **Customer Testing**: Have someone make a test purchase

## Support

If issues persist:
1. Check BTCPay Server logs
2. Review application logs for error details
3. Test with BTCPay's test network first
4. Verify all wallets are properly configured in BTCPay