QuickBooks Online V3 PHP SDK

repository·master·Indexed 43 days ago

https://github.com/intuit/quickbooks-v3-php-sdk

A PHP client library for interacting with the QuickBooks Online V3 REST API. It enables developers to perform CRUD operations on QuickBooks data and provides tools for managing OAuth 1.0 and OAuth 2.0 authentication, including the OAuth2LoginHelper and OAuth2AccessToken classes for token lifecycle management.

Tokens
9K
Snippets
23
Records
34
Agent score
54%

What's inside QuickBooks PHP SDK

  1. What is Legko XML (XSD2PHP)

    master

    Legko XML (also known as XSD2PHP) is a library used to compile PHP bindings from XML Schema (XSD) files. Once compiled, these bindings allow you to:

    1. Unmarshalling: Bind XML files to PHP models.
    2. Marshalling: Save PHP bindings back into XML format.
    3. WSDL Generation: Generate WSDL files utilizing existing mappings in classes.
  2. Use the OAuth2AccessToken class to manage credentials

    master

    The OAuth2AccessToken class is a data object used to store and manage OAuth 2.0 credentials required for QuickBooks Online API requests. It holds the access token, refresh token, client credentials, and expiration metadata.

    Important Note on Refresh Tokens: QuickBooks Online access tokens are valid for 1 hour. While refresh tokens are technically valid for longer, the refresh token returned by QBO changes every 24 hours. You must always record the latest refresh token returned from your most recent refresh token API call to ensure continued access.

    use QuickBooksOnline//API/Core/OAuth/OAuth2/OAuth2AccessToken;
    
    // Initialize with client credentials and token details
    $accessToken = new OAuth2AccessToken(
        'YOUR_CLIENT_ID', 
        'YOUR_CLIENT_SECRET', 
        'ACCESS_TOKEN', 
        'REFRESH_TOKEN', 
        3600, 
        8726400
    );
  3. Complete the OAuth 2.0 Authorization flow

    master

    The OAuth 2.0 flow using OAuth2LoginHelper follows these steps:

    1. Get Authorization URL: Call getAuthorizationCodeURL() to get the URL where you redirect the user to log in.
    2. Exchange Code for Token: After the user is redirected back to your redirectUri with a code parameter, call exchangeAuthorizationCodeForToken($code, $realmID) to obtain an OAuth2AccessToken object.

    Note: The $realmID (Company ID) is used to associate the token with the specific QuickBooks company.

    // 1. Redirect user to this URL
    $authUrl = $helper->getAuthorizationCodeURL();
    
    // 2. After redirect, catch the 'code' and 'realmId' from the query string
    $accessToken = $helper->exchangeAuthorizationCodeForToken($code, $realmID);
  4. Initialize OAuth2LoginHelper for the OAuth 2.0 flow

    master

    Use OAuth2LoginHelper to manage the lifecycle of OAuth 2.0 authentication, including generating authorization URLs, exchanging codes for tokens, and refreshing tokens.

    There are two ways to initialize the helper:

    1. New Authentication Flow: Pass your application credentials directly.
    2. Token Refresh Flow: Pass an existing ServiceContext containing an OAuth2AccessToken to allow the helper to perform refresh operations.

    Parameters for new flows:

    • $clientID: Your App's Client ID.
    • $clientSecret: Your App's Client Secret.
    • $redirectUri: The redirect URI specified in your App settings.
    • $scope: The requested scope for the app.
    • $state: (Optional) A string to prevent CSRF. If not provided, a random 5-character string is generated.
    // For a new authentication flow
    $helper = new OAuth2LoginHelper(
        'YOUR_CLIENT_ID',
        'YOUR_CLIENT_SECRET',
        'https://your-redirect-uri.com',
        'com.intuit.quickbooks.accounting'
    );
    
    // For refreshing an existing token using a ServiceContext
    // $serviceContext must contain an OAuth2AccessToken in its requestValidator
    $helper = new OAuth2LoginHelper(
        null, // clientID not used when serviceContext is provided
        null, // clientSecret not used when serviceContext is provided
        null, 
        null, 
        null, 
        $serviceContext
    );
  5. Configure the SDK using IppConfiguration

    master

    The IppConfiguration class is the central configuration object used to set up the QuickBooks Online PHP SDK environment. It allows you to define security mechanisms, message settings, retry policies, base URLs, and authentication modes.

    Key properties include:

    • $Security: Handles authentication (e.g., RequestValidator or OAuth2AccessToken).
    • $OAuthMode: Specifies whether to use CoreConstants::OAUTH1Mode or CoreConstants::OAUTH2Mode.
    • $RealmID: The Company ID (Realm ID) associated with the QuickBooks account.
    • $minorVersion: An integer specifying the API minor version.
    • $RetryPolicy: An instance of IntuitRetryPolicy to manage service call retries.
    • $Logger: The mechanism used for diagnostics and logging.
    • $BaseUrl: Defines the API endpoints (e.g., Pre-Production URLs).
    • $SSLCheckStatus: A boolean to enable or disable SSL verification.
  6. Initialize OAuth2AccessToken

    master

    Construct a new OAuth2AccessToken instance with your application credentials and the tokens received from the OAuth 2.0 flow.

    Parameters:

    • $cID (String): The Client ID associated with your app.
    • $cS (String): The Client Secret associated with your app.
    • $atk (String, optional): The OAuth 2 Access Token.
    • $refreshtk (String, optional): The OAuth 2 Refresh Token.
    • $atei (int, optional): Access token expiration in seconds (typically 3600).
    • $refreshtei (int, optional): Refresh token expiration in seconds (typically 8726400).
    • $tk (String, optional): The token type. Defaults to "bearer".
    $token = new OAuth2AccessToken($clientID, $clientSecret, $accessToken, $refreshToken, $accessTokenExpiresIn, $refreshTokenExpiresIn);
  7. Extract OAuth credentials from settings arrays

    master

    The CoreConstants class provides helper methods to safely extract OAuth credentials from configuration arrays, accounting for common naming variations (e.g., camelCase vs PascalCase).

    • getAccessTokenFromArray(array $settings): Returns the access token. Looks for accessTokenKey, accessToken, or AccessToken.
    • getRefreshTokenFromArray(array $settings): Returns the refresh token. Looks for refreshTokenKey, refreshToken, or RefreshToken.
    • getRedirectURL(array $settings): Returns the redirect URL. Handles numerous variations like redirectURL, RedirectUrl, redirecturi, etc.
    use QuickBooksOnline\API\Core\CoreConstants;
    
    $settings = [
        'accessToken' => 'your_access_token',
        'refreshToken' => 'your_refresh_token',
        'redirectUrl' => 'https://your-app.com/callback'
    ];
    
    $token = CoreConstants::getAccessTokenFromArray($settings);
    $refresh = CoreConstants::getRefreshTokenFromArray($settings);
    $redirect = CoreConstants::getRedirectURL($settings);
  8. Manually sign a request with sign()

    master

    If you need to perform the signing step separately from header generation, use the sign method. This updates the internal oauthParameters array with a new oauth_signature.

    Method Signature: public function sign($uri, $queryParameters, $httpMethod)

    Parameters:

    • $uri (String): The complete URL.
    • $queryParameters (Array): Query parameters to include in the signature.
    • $httpMethod (String): The HTTP method (e.g., 'GET', 'POST').
    $oauth->sign($uri, $queryParams, 'GET');
    // The $oauth->oauthParameters property now contains the signature.