# 🚀 MYSOFT Production Deployment Guide

## Environment Comparison

### TEST Environment (Development)

```env
MYSOFT_BASE_URL=https://edocumentapi.mytest.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mytest.tr/oauth/token
```

**Use for:**

-   Development and testing
-   Initial integration setup
-   Training and demos
-   QA testing

### PRODUCTION Environment (Live)

```env
MYSOFT_BASE_URL=https://edocumentapi.mysoft.com.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mysoft.com.tr/oauth/token
```

**Use for:**

-   Live customer invoices
-   Real financial transactions
-   Production data

---

## Pre-Deployment Checklist

Before deploying to production, ensure:

-   [ ] Integration working perfectly in test environment
-   [ ] All test invoices created successfully
-   [ ] No errors in logs
-   [ ] Team trained on system usage
-   [ ] MYSOFT production account credentials obtained
-   [ ] Production API access confirmed by MYSOFT
-   [ ] Backup strategy in place
-   [ ] Rollback plan prepared

---

## Deployment Steps

### Step 1: Update .env File

**Change these lines in `.env`:**

```env
# FROM (Test)
# MYSOFT_BASE_URL=https://edocumentapi.mytest.tr/api
# MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mytest.tr/oauth/token

# TO (Production)
MYSOFT_BASE_URL=https://edocumentapi.mysoft.com.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mysoft.com.tr/oauth/token
```

### Step 2: Clear Configuration Cache

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

### Step 3: Update Integration Record (Optional)

If you want to use different credentials for production:

```bash
php artisan tinker
```

```php
\App\Models\Integration::where('type', 'mysoft')->update([
    'url' => 'https://edocumentapi.mysoft.com.tr/api',
    'username' => 'production-username@example.com',
    'password' => 'production-password',
    'api_key' => 'production-api-key',
]);
```

Or create a new production integration:

```php
\App\Models\Integration::create([
    'type' => 'mysoft',
    'tool' => 'mysoft',
    'title' => 'MYSOFT E-Invoice PRODUCTION',
    'url' => 'https://edocumentapi.mysoft.com.tr/api',
    'username' => 'production-username@example.com',
    'password' => 'production-password',
    'api_key' => 'production-api-key',
    'is_active' => true,
    'current_team_id' => 1,
    'user_id' => 1,
]);
```

### Step 4: Test Connection

```bash
php artisan mysoft:test
```

Expected output:

```
✅ Token acquired successfully!
✅ Connection test successful!
✅ Invoice document built successfully!
```

### Step 5: Create Test Invoice

Create one test invoice to verify everything works:

**Via API:**

```bash
curl -X POST http://your-domain/api/mysoft/create-invoice \
  -H "Content-Type: application/json" \
  -d '{"sale_id": 1}'
```

**Via Dashboard:**

1. Go to `/mysoft`
2. Enter a test sale ID
3. Click "Create E-Invoice"

### Step 6: Monitor Logs

Watch for any errors:

```bash
tail -f storage/logs/laravel.log | grep MYSOFT
```

Look for:

-   ✅ Successful token acquisition
-   ✅ Invoice creation success
-   ✅ No connection errors

### Step 7: Verify in MYSOFT Portal

1. Login to MYSOFT portal
2. Check if invoices appear correctly
3. Verify ETTN numbers match
4. Confirm document structure is correct

---

## Post-Deployment Verification

### Database Checks

```sql
-- Check integration status
SELECT id, title, url, is_active, token_expires_at
FROM integrations
WHERE type = 'mysoft';

-- View recent invoices
SELECT document_no, ettn, status, sent_at, created_at
FROM eadisyon
ORDER BY created_at DESC
LIMIT 10;
```

### Log Analysis

```bash
# Check for errors
grep "MYSOFT.*error" storage/logs/laravel.log | tail -20

# Check successful operations
grep "MYSOFT.*successfully" storage/logs/laravel.log | tail -20
```

### Performance Monitoring

Monitor for first few days:

-   Invoice creation success rate (should be >99%)
-   Average response time (should be <3 seconds)
-   Token refresh frequency (every ~23 hours)
-   Error frequency (should be minimal)

---

## Rollback Plan

If issues occur in production:

### Quick Rollback

1. **Revert .env changes:**

```env
MYSOFT_BASE_URL=https://edocumentapi.mytest.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mytest.tr/oauth/token
```

2. **Clear cache:**

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

3. **Test:**

```bash
php artisan mysoft:test
```

### Partial Rollback

If only some features have issues:

1. Keep production URLs
2. Disable problematic features in code
3. Use test environment for those features temporarily
4. Fix issues and redeploy incrementally

---

## Common Production Issues

### Issue 1: Connection Timeout

**Symptoms:** Request hangs or times out

**Solutions:**

-   Check firewall settings
-   Verify SSL certificates
-   Test endpoint accessibility:
    ```bash
    curl https://edocumentapi.mysoft.com.tr/oauth/token
    ```

### Issue 2: Authentication Failed

**Symptoms:** 401 Unauthorized errors

**Solutions:**

-   Verify production credentials
-   Check if account is active in MYSOFT portal
-   Ensure API access is enabled
-   Contact MYSOFT support

### Issue 3: Schema Validation Errors

**Symptoms:** Invoice rejected by MYSOFT

**Solutions:**

-   Review MYSOFT documentation
-   Check required fields
-   Verify data formats
-   Compare with test environment responses

### Issue 4: Token Not Caching

**Symptoms:** New token fetched every request

**Solutions:**

-   Check cache configuration
-   Verify cache driver is working
-   Clear cache: `php artisan cache:clear`
-   Check file permissions on cache directory

---

## Security Best Practices

### In Production

1. **Use HTTPS only**

    - Force SSL on all routes
    - Update APP_URL in .env to https://

2. **Encrypt sensitive data**

    - Consider encrypting credentials in database
    - Use Laravel encryption services

3. **Limit access**

    - Restrict /mysoft routes to admin users
    - Add middleware for authorization

4. **Monitor access**

    - Enable detailed logging
    - Set up alerts for failures
    - Regular security audits

5. **Rotate credentials**
    - Change passwords periodically
    - Update API keys as needed
    - Revoke old tokens

---

## Maintenance Schedule

### Daily

-   [ ] Check logs for errors
-   [ ] Monitor invoice success rate
-   [ ] Verify token cache is working

### Weekly

-   [ ] Review all created invoices
-   [ ] Check token expiration dates
-   [ ] Analyze error patterns

### Monthly

-   [ ] Rotate API credentials
-   [ ] Review user access logs
-   [ ] Performance optimization review

### Quarterly

-   [ ] Full system audit
-   [ ] Security review
-   [ ] Update documentation
-   [ ] Team training refresher

---

## Support Contacts

### Internal

-   **Developer:** [Your contact]
-   **System Admin:** [Admin contact]
-   **Support Email:** support@yourcompany.com

### External

-   **MYSOFT Support:** support@mysoft.com.tr
-   **MYSOFT Documentation:** https://mysoft.com.tr/docs
-   **Emergency Contact:** [MYSOFT emergency number]

---

## Success Criteria

Production deployment is successful when:

1. ✅ All test invoices created without errors
2. ✅ Invoices visible in MYSOFT portal
3. ✅ ETTN numbers assigned correctly
4. ✅ No errors in application logs
5. ✅ Token caching working (23-hour cycles)
6. ✅ Response times acceptable (<3s)
7. ✅ Success rate >99%
8. ✅ Team can use system confidently

---

## Environment Variables Reference

### Required for Production

```env
# Application
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

# MYSOFT Production
MYSOFT_BASE_URL=https://edocumentapi.mysoft.com.tr/api
MYSOFT_TOKEN_ENDPOINT=https://edocumentapi.mysoft.com.tr/oauth/token
MYSOFT_TOKEN_CACHE_DURATION=82800
MYSOFT_DEFAULT_PREFIX=APP
MYSOFT_LOGGING=true
MYSOFT_RETRY_COUNT=3
MYSOFT_RETRY_DELAY=1000
```

### Optional Enhancements

```env
# Enhanced logging
LOG_LEVEL=info
LOG_MYSOFT_DETAILED=true

# Performance tuning
MYSOFT_TIMEOUT=30
MYSOFT_MAX_RETRIES=5
```

---

## Final Checklist

Before going live:

-   [ ] All pre-deployment tests passed
-   [ ] Production credentials obtained
-   [ ] .env updated with production URLs
-   [ ] Cache cleared
-   [ ] Test invoice created successfully
-   [ ] Logs monitored for 1 hour
-   [ ] Team trained
-   [ ] Rollback plan tested
-   [ ] Monitoring alerts configured
-   [ ] Support contacts available
-   [ ] Documentation updated

---

**🎉 You're ready for production!**

Deploy with confidence and monitor closely for the first 24-48 hours.

---

**Last Updated:** March 11, 2026  
**Version:** 1.0.0  
**Status:** Production Ready ✅
