# REST API

1\. **RestAPI\Administration**

* **Purpose**: Manages administrative functionalities of the Payment Page plugin, including settings and configurations.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers API endpoints related to the admin settings.
    * **Example Endpoints**:
      * **`GET /wp-json/payment-page/v1/settings`**
        * **Description**: Retrieves the current settings of the Payment Page.
        * **Response**:

          ```json
          {
              "currency": "USD",
              "payment_gateway": "stripe",
              "api_key": "sk_test_...",
              "debug_mode": false
          }
          ```
      * **`POST /wp-json/payment-page/v1/settings`**
        * **Description**: Updates the plugin settings.
        * **Request Body**:

          ```json
          {
              "currency": "EUR",
              "payment_gateway": "paypal",
              "api_key": "sk_test_...",
              "debug_mode": true
          }
          ```
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Settings updated successfully."
          }
          ```
      * **`DELETE /wp-json/payment-page/v1/settings`**
        * **Description**: Resets settings to default.
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Settings reset to default."
          }
          ```

#### 2. **RestAPI\Payment**

* **Purpose**: Handles payment-related functionalities, including processing transactions and managing payment history.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers endpoints for managing payment processes.
    * **Example Endpoints**:
      * **`POST /wp-json/payment-page/v1/payment/process`**
        * **Description**: Processes a new payment.
        * **Request Body**:

          ```json
          {
              "amount": 100,
              "currency": "USD",
              "payment_method": "credit_card",
              "description": "Order #12345"
          }
          ```
        * **Response**:

          ```json
          {
              "transaction_id": "abc123",
              "status": "success",
              "message": "Payment processed successfully."
          }
          ```
      * **`GET /wp-json/payment-page/v1/payment/status`**
        * **Description**: Retrieves the status of a specific payment using its transaction ID.
        * **Request Query**: `?transaction_id=abc123`
        * **Response**:

          ```json
          {
              "transaction_id": "abc123",
              "status": "completed",
              "amount": 100,
              "currency": "USD",
              "date": "2024-10-01T12:00:00Z"
          }
          ```
      * **`GET /wp-json/payment-page/v1/payment/history`**
        * **Description**: Retrieves a list of past payments.
        * **Response**:

          ```json
          [
              {
                  "transaction_id": "abc123",
                  "amount": 100,
                  "currency": "USD",
                  "status": "completed",
                  "date": "2024-10-01T12:00:00Z"
              },
              {
                  "transaction_id": "def456",
                  "amount": 50,
                  "currency": "USD",
                  "status": "pending",
                  "date": "2024-10-02T15:30:00Z"
              }
          ]
          ```

#### 3. **RestAPI\PaymentGateway**

* **Purpose**: Interfaces with various payment gateways, allowing users to configure and manage gateway settings.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers API endpoints for interacting with different payment gateways.
    * **Example Endpoints**:
      * **`GET /wp-json/payment-page/v1/gateway/list`**
        * **Description**: Lists all available payment gateways.
        * **Response**:

          ```json
          [
              {
                  "gateway": "paypal",
                  "enabled": true
              },
              {
                  "gateway": "stripe",
                  "enabled": true
              },
              {
                  "gateway": "authorize.net",
                  "enabled": false
              }
          ]
          ```
      * **`POST /wp-json/payment-page/v1/gateway/configure`**
        * **Description**: Configures a selected payment gateway.
        * **Request Body**:

          ```json
          {
              "gateway": "stripe",
              "api_key": "sk_test_...",
              "webhook_url": "https://yourwebsite.com/webhook"
          }
          ```
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Stripe gateway configured successfully."
          }
          ```
      * **`DELETE /wp-json/payment-page/v1/gateway/:gateway`**
        * **Description**: Deletes a configured payment gateway.
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Payment gateway removed."
          }
          ```

#### 4. **RestAPI\Plugin**

* **Purpose**: Manages plugin-level functionalities, including activation, deactivation, and updates.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers endpoints that provide information about the plugin.
    * **Example Endpoints**:
      * **`GET /wp-json/payment-page/v1/plugin/info`**
        * **Description**: Retrieves information about the plugin.
        * **Response**:

          ```json
          {
              "name": "Payment Page",
              "version": "1.3.9",
              "author": "Gaucho Plugins",
              "license": "GPLv3"
          }
          ```
      * **`POST /wp-json/payment-page/v1/plugin/update`**
        * **Description**: Handles updates for the plugin.
        * **Request Body**:

          ```json
          {
              "version": "1.4.0"
          }
          ```
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Plugin updated to version 1.4.0."
          }
          ```
      * **`GET /wp-json/payment-page/v1/plugin/status`**
        * **Description**: Checks the status of the plugin.
        * **Response**:

          ```json
          {
              "active": true,
              "version": "1.3.9"
          }
          ```

#### 5. **RestAPI\Stripe**

* **Purpose**: Specifically manages Stripe integration for processing payments.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers endpoints for handling Stripe payments and configurations.
    * **Example Endpoints**:
      * **`POST /wp-json/payment-page/v1/stripe/payment`**
        * **Description**: Initiates a payment through Stripe.
        * **Request Body**:

          ```json
          {
              "amount": 100,
              "currency": "USD",
              "source": "tok_visa",
              "description": "Test payment"
          }
          ```
        * **Response**:

          ```json
          {
              "transaction_id": "xyz789",
              "status": "success",
              "message": "Payment processed through Stripe."
          }
          ```
      * **`GET /wp-json/payment-page/v1/stripe/webhook`**
        * **Description**: Handles Stripe webhooks for payment confirmations.
        * **Request Body**: Automatically handled by Stripe.
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Webhook received and processed."
          }
          ```
      * **`GET /wp-json/payment-page/v1/stripe/config`**
        * **Description**: Retrieves the current Stripe configuration.
        * **Response**:

          ```json
          {
              "api_key": "sk_test_...",
              "webhook_url": "https://yourwebsite.com/webhook"
          }
          ```

#### 6. **RestAPI\Tagging**

* **Purpose**: Manages tagging and categorization of payments or payment forms.
* **Expected Methods**:
  * \*\*\`register\_routes

()\`\*\*: - **Functionality**: Registers endpoints for managing tags.

````
- **Example Endpoints**:
  - **`GET /wp-json/payment-page/v1/tags`**
    - **Description**: Lists all available tags.
    - **Response**:
      ```json
      [
          {
              "tag_id": 1,
              "tag": "featured"
          },
          {
              "tag_id": 2,
              "tag": "new"
          }
      ]
      ```
  - **`POST /wp-json/payment-page/v1/tags`**
    - **Description**: Creates a new tag for categorization.
    - **Request Body**:
      ```json
      {
          "tag": "sale"
      }
      ```
    - **Response**:
      ```json
      {
          "success": true,
          "message": "Tag created successfully."
      }
      ```
  - **`DELETE /wp-json/payment-page/v1/tags/:tag_id`**
    - **Description**: Deletes a specified tag.
    - **Response**:
      ```json
      {
          "success": true,
          "message": "Tag deleted successfully."
      }
      ```
````

#### 7. **RestAPI\Webhook**

* **Purpose**: Handles incoming webhooks from payment processors to keep track of transaction statuses.
* **Expected Methods**:
  * **`register_routes()`**:
    * **Functionality**: Registers webhook endpoints for processing notifications from payment gateways.
    * **Example Endpoints**:
      * **`POST /wp-json/payment-page/v1/webhook/receive`**
        * **Description**: Receives and processes webhook notifications.
        * **Request Body**:

          ```json
          {
              "event": "payment_intent.succeeded",
              "data": {
                  "transaction_id": "xyz789",
                  "amount": 100,
                  "currency": "USD"
              }
          }
          ```
        * **Response**:

          ```json
          {
              "success": true,
              "message": "Webhook processed successfully."
          }
          ```
      * **`GET /wp-json/payment-page/v1/webhook/status`**
        * **Description**: Checks the current webhook status configuration.
        * **Response**:

          ```json
          {
              "webhook_url": "https://yourwebsite.com/webhook",
              "status": "active"
          }
          ```

#### How to Utilize `register_rest_route()`

Each of the `register_routes()` methods in the classes above typically employs the `register_rest_route()` function to define the endpoints. This function is vital for creating a custom REST API in WordPress.

**Basic Syntax:**

```php
register_rest_route('payment-page/v1', '/endpoint-name', [
    'methods' => 'GET', // Can be 'GET', 'POST', 'PUT', 'DELETE', etc.
    'callback' => 'callback_function_name', // Function that handles the request.
    'permission_callback' => 'permission_check_function' // Function to check permissions.
]);
```

* **Parameters**:
  * **Namespace**: The first parameter (e.g., `'payment-page/v1'`) specifies the namespace for the REST API, which helps in organizing routes.
  * **Route**: The second parameter specifies the endpoint (e.g., `'/settings'`).
  * **Arguments**: The third parameter is an associative array that defines:
    * **`methods`**: Allowed HTTP methods (GET, POST, etc.).
    * **`callback`**: The function that processes the request.
    * **`permission_callback`**: A function to validate whether the user has permission to access the endpoint.

### Constants

The plugin defines several constants that are utilized throughout the codebase, allowing developers to reference them easily. These constants are defined in `definitions.php`:

* **`PAYMENT_PAGE_NAME`**: The name of the payment page plugin.
* **`PAYMENT_PAGE_ALIAS`**: A short alias for the payment page, used in various settings.
* **`PAYMENT_PAGE_PREFIX`**: A prefix used in the plugin's identifiers, useful for namespacing custom functionality.
* **`PAYMENT_PAGE_REST_API_PREFIX`**: The prefix used for REST API routes.
* **`PAYMENT_PAGE_ADMIN_CAP`**: The capability required for accessing admin settings.
* **`PAYMENT_PAGE_VERSION`**: The current version of the plugin.
* **`PAYMENT_PAGE_TABLE_STRIPE_CUSTOMERS`**: The database table name for storing Stripe customer records.

#### Example Usage of Constants

```php
if ( current_user_can(PAYMENT_PAGE_ADMIN_CAP) ) {
    // Code to show settings page
}
```

***

This detailed documentation provides an extensive overview and actionable examples for developers working with the Payment Page plugin. If you have any additional requests or need further modifications, feel free to [contact support](https://paymentpageplugin.com/support/).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.paymentpageplugin.com/advanced-configuration/rest-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
