# BWDK Callback Diagnostic Report

## TASK 1 — Plugin Callback Endpoints

### Endpoint 1: Rewrite Rule Callback Handler
- **File**: `src/Integrations/Digify/Routes/CallbackRoute.php`
- **Class**: `BWDK\Integrations\Digify\Routes\CallbackRoute`
- **Method**: `handle()`
- **Registration**: Lines 39-57
- **URL/Route**: 
  - Rewrite rule: `^bwdk-callback/?$` → `index.php?__bwdk_callback=1`
  - Full URL: `https://{site}/bwdk-callback/?order_uuid={uuid}&reason={reason}&phone_number={phone}&merchant_order_id={id}`
  - Hook: `template_redirect` (checks for `__bwdk_callback` query var)
- **Status**: ✅ ACTIVE (REST API route is commented out, rewrite rule is active)

### Endpoint 2: REST API Route (COMMENTED OUT)
- **File**: `src/Integrations/Digify/Routes/CallbackRoute.php`
- **Class**: `BWDK\Integrations\Digify\Routes\CallbackRoute`
- **Method**: `handle()`
- **Registration**: Lines 29-36 (commented out)
- **URL/Route**: Would be `wp-json/bwdk/v1/digify/callback` if enabled
- **Status**: ❌ DISABLED (commented out)

### Registration Location
- **File**: `src/Core/ServiceProvider.php`
- **Line**: 35-36
- **Code**: 
  ```php
  App::bind(CallbackRoute::class, new CallbackRoute());
  App::get(CallbackRoute::class)->register();
  ```

---

## TASK 2 — Where callback_url is SENT to Digify

### Location 1: Main Cart/Checkout Flow
- **File**: `src/Integrations/Digify/Client.php`
- **Class**: `BWDK\Integrations\Digify\Client`
- **Method**: `buildPayload()`
- **Line**: 571
- **Code**:
  ```php
  $callbackUrl = home_url( Constant::BWDK_CB_URL );
  // Constant::BWDK_CB_URL = '/bwdk-callback/'
  ```
- **Sent in payload**: Line 587
  ```php
  'callback_url' => $callbackUrl,
  ```
- **Full URL sent**: `https://{wordpress-site}/bwdk-callback/`

### Location 2: Single Buy Flow
- **File**: `src/Woo/SingleBuyEndpoint.php`
- **Note**: Single buy flow uses `Client::createRemoteOrder()` which also calls `buildPayload()`, so it uses the same callback URL from `Client.php`

### API Endpoint Called
- **File**: `src/Integrations/Digify/Client.php`
- **Method**: `createRemoteOrder()`
- **Line**: 205
- **Endpoint**: `{base_url}/orders/api/v1/create-order/`
- **Base URL**: From plugin settings (typically `https://bwdk-backend.digify.shop`)

### Diagnostic Logging Added
- **File**: `src/Integrations/Digify/Client.php`
- **Line**: ~571-583
- **Logs**: 
  - `BWDK DIAGNOSTIC: CALLBACK_URL SENT TO DIGIFY`
  - Includes: order_id, callback_url, home_url, BWDK_CB_URL constant
  - Source: `bwdk_callback_diagnostic`

---

## TASK 3 — Runtime Behavior Tracing

### Diagnostic Logging Added

#### 1. Callback Handler Entry Point
- **File**: `src/Integrations/Digify/Routes/CallbackRoute.php`
- **Method**: `handle()`
- **Line**: ~64-75
- **Logs**: 
  - `BWDK DIAGNOSTIC: CALLBACK HANDLER HIT`
  - Includes: REQUEST_URI, GET params, POST params
  - Source: `bwdk_callback_diagnostic`

#### 2. Callback URL Sent to Digify
- **File**: `src/Integrations/Digify/Client.php`
- **Method**: `buildPayload()`
- **Line**: ~571-583
- **Logs**: 
  - `BWDK DIAGNOSTIC: CALLBACK_URL SENT TO DIGIFY`
  - Includes: order_id, full callback_url value
  - Source: `bwdk_callback_diagnostic`

### Expected Flow

1. **User clicks BWDK button** → `AjaxHandler::create_order()`
2. **Order created** → `OrderService::createFromCurrentCart()`
3. **Payload built** → `Client::buildPayload()` 
   - Sets `callback_url = home_url('/bwdk-callback/')`
   - **LOG**: "CALLBACK_URL SENT TO DIGIFY = https://site.com/bwdk-callback/"
4. **Sent to Digify** → `Client::createRemoteOrder()`
   - POST to `https://bwdk-backend.digify.shop/orders/api/v1/create-order/`
   - Payload includes `callback_url`
5. **Digify processes payment** → User redirected to Digify
6. **User returns/cancels** → Digify redirects to `callback_url`
7. **Callback handler hit** → `CallbackRoute::handle()`
   - **LOG**: "CALLBACK HANDLER HIT"
   - Reads: `order_uuid`, `reason`, `phone_number`, `merchant_order_id` from GET params

### When Callback Should Be Hit

- ✅ **back_from_shipping_page**: When user clicks back from Digify shipping page
- ✅ **payment failure**: When payment fails
- ✅ **payment success**: When payment succeeds (before verify)
- ✅ **cancellation**: When user cancels payment

---

## TASK 4 — Final Answer

### **YES — callback_url reaches the plugin**

**Evidence:**

1. **Callback URL Construction** (Client.php:571):
   ```php
   $callbackUrl = home_url( Constant::BWDK_CB_URL );
   // Results in: https://{wordpress-site}/bwdk-callback/
   ```

2. **Callback URL Sent to Digify** (Client.php:587):
   ```php
   'callback_url' => $callbackUrl,  // Sent in create-order payload
   ```

3. **Callback Handler Registration** (CallbackRoute.php:39-57):
   ```php
   add_rewrite_rule('^bwdk-callback/?$', 'index.php?__bwdk_callback=1', 'top');
   add_action('template_redirect', function() {
       if ( get_query_var('__bwdk_callback') ) {
           $this->handle();
           exit;
       }
   });
   ```

4. **Handler Method** (CallbackRoute.php:64):
   ```php
   public function handle(): WP_REST_Response
   {
       // Reads: order_uuid, reason, phone_number, merchant_order_id from $_GET
   }
   ```

### Exact Code Path

**Sending callback_url:**
- File: `src/Integrations/Digify/Client.php`
- Method: `buildPayload()` (line 571)
- Method: `createRemoteOrder()` (line 205) - sends payload to Digify

**Receiving callback:**
- File: `src/Integrations/Digify/Routes/CallbackRoute.php`
- Method: `handle()` (line 64)
- Route: `https://{site}/bwdk-callback/?order_uuid={uuid}&reason={reason}&...`
- Hook: `template_redirect` (line 51)

### Verification Method

Check WooCommerce logs (wp-content/uploads/wc-logs/) for:
- `BWDK DIAGNOSTIC: CALLBACK_URL SENT TO DIGIFY` - confirms URL sent
- `BWDK DIAGNOSTIC: CALLBACK HANDLER HIT` - confirms handler received callback

If handler is NOT hit, possible reasons:
1. Digify not redirecting to callback_url (check Digify dashboard/logs)
2. Rewrite rules not flushed (run: Settings → Permalinks → Save)
3. Server blocking the callback URL
4. Digify using different callback mechanism (webhook vs redirect)

---

## Summary

- **Callback URL**: `https://{wordpress-site}/bwdk-callback/`
- **Handler**: `CallbackRoute::handle()` via rewrite rule
- **Status**: ✅ Properly configured and should receive callbacks
- **Diagnostic Logs**: Added to trace both sending and receiving

