Jump to content

Matthew Barraud

Members
  • Posts

    8
  • Joined

  • Last visited

Reputation

0 Neutral
  1. Hi all In Sage Accounting, when creating an invoice, we can save & email it. I couldn't see in the API documentation (https://developer.sage.com/accounting/guides/concepts/invoicing/ or https://developer.sage.com/accounting/reference/invoicing-sales/#tag/Sales-Invoices/operation/postSalesInvoices) if this is possible. I think we can download the PDF worse case, but it would be good to be able to trigger this rather than add extra complexity if possible. Could anyone point me in the right direction?
  2. Ok, schoolboy error, docs stated its an array of ids, all sorted!
  3. Hi all I'm starting out on a website integration for our business, I've got my PHP class working with authentication now dipping my toes into creating a contact. When I send the request to create a contact, I get this returned. array ( 0 => (object) array( '$severity' => 'error', '$dataCode' => 'Validation', '$message' => 'contact[contact_type_ids] is invalid', '$source' => 'contact[contact_type_ids]', ), ) When I pull out all my contact_types, I get the following returned which suggests that I have two setup in my Sage Accounting package (live) with CUSTOMER & SUPPLIER as their ids. (object) array( '$total' => 2, '$page' => 1, '$next' => NULL, '$back' => NULL, '$itemsPerPage' => 20, '$items' => array ( 0 => (object) array( 'id' => 'CUSTOMER', 'displayed_as' => 'Customer', '$path' => '/contact_types/CUSTOMER', ), 1 => (object) array( 'id' => 'VENDOR', 'displayed_as' => 'Supplier', '$path' => '/contact_types/VENDOR', ), ), ) I had assumed therefore I'd be creating an array to send the data like the below: $data = array( 'contact' => [ 'name' => 'Test Name', 'contact_type_ids' => 'CUSTOMER', ] ); But I get the error above. Can anyone point me in the right direction, am I looking in the wrong place for my contact types? Many thanks! Matt
  4. I think I have it! This was an encoding / decoding issue in the URL. With the edition of the following to the $authCode variable I was able to get a token returned. function handleCallback() { if (isset($_GET['code'])) { // Initialize cURL $curl = curl_init(); // Create the URL $url = 'https://oauth.accounting.sage.com/token'; $clientSecret = $this->clientSecret; $authCode = $_GET['code']; $authCode = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($authCode)); $authCode = html_entity_decode($authCode,null,'UTF-8'); $data = array( 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $authCode, 'grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/sailadventure-new/tests/callback' ); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded', 'Accept: application/json', ]); // Execute the request $response = curl_exec($curl); if (curl_errno($curl)) { $error = curl_error($curl); return $error; } curl_close($curl); echo '<pre>' . var_export($response, true) . '</pre>'; } }
  5. Sorry, forgot to add I downloaded Postman and created a flow through there and it gave me a token, so obviously something is wrong in my code somewhere.
  6. Hi Mark Thanks for replying 🙂 In response ... You've read the Authentication Guide. Yep, loads, he he Your application has been registered in the developer self service portal and the correct callback url's have been added. **Note** - callback url's are case sensitive. I believe so., see pic below. A Sage Business Cloud Accounting instance has been created to test the oAuth flow https://developer.sage.com/accounting/quick-start/set-up-the-basics/. This one, I'm not sure, I already have a Sage Accounting account for my business. Do I need to setup a trial account as well? You're able to sign into the Sage Business Cloud Accounting instance and view the welcome dashboard. Yep, on my business account, see above ref, developer. The parameters passed in the auth url match those registered in the developer portal and the country filter(optional) passed matches the country the business is registered for. For example if GB is passed and the business is registered for the US the auth will fail. Understood all GB used. ----- When I visit http://localhost/sailadventure-new/tests/ it opens a new instance of my class and runs the authorizeSageAccountingApp() function. This redirects the page to https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&country=gb&locale=en_GB&response_type=code&client_id=XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX&redirect_uri=http%3A%2F%2Flocalhost%2Fsailadventure-new%2Ftests%2Fcallback&scope=full_access&state=159786325 it then prompts me with the following clicking allow then returns '{"$severity":"error","$dataCode":"DataParsingError","$message":"The data you sent could not be processed.","$source":"Proxy"}' I've been updating the code and working through issues as I learn/understand them. This is the class currently in PHP <?php class Sage { private $clientId; private $clientSecret; private $accessToken; public function __construct() { $this->clientId = 'XXXX'; $this->clientSecret = 'XXXX'; } function authorizeSageAccountingApp() { $authorizationUrl = 'https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&country=gb&locale=en_GB'; $clientId = $this->clientId; $redirectUri = 'http://localhost/sailadventure-new/tests/callback'; $scopes = 'full_access'; // or 'readonly' if applicable $authorizationUrl .= '&response_type=code'; $authorizationUrl .= '&client_id=' . urlencode($clientId); $authorizationUrl .= '&redirect_uri=' . urlencode($redirectUri); $authorizationUrl .= '&scope=' . urlencode($scopes); $authorizationUrl .= '&state=159786325'; header('Location: ' . $authorizationUrl); exit(); } function handleCallback() { if (isset($_GET['code'])) { // Initialize cURL $curl = curl_init(); // Create the URL $url = 'https://oauth.accounting.sage.com/token'; $clientSecret = $this->clientSecret; $authCode = $_GET['code']; $data = array( 'grant_type' => 'authorization_code', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $authCode, 'redirect_uri' => 'http://localhost/sailadventure-new/tests/callback' ); http_build_query($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded', 'Accept: application/json', ]); // Execute the request $response = curl_exec($curl); if (curl_errno($curl)) { $error = curl_error($curl); return $error; } curl_close($curl); echo '<pre>' . var_export($response, true) . '</pre>'; } } }
  7. I just tidied the code a little, and I am still getting the same response. <?php class Sage { private $clientId; private $clientSecret; private $accessToken; public function __construct() { $this->clientId = '37d7XXXXXXXXXXXXXe064ec9'; $this->clientSecret = 'eieXXXXXXXXXXXIz'; } function authorizeSageAccountingApp() { $authorizationUrl = 'https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&country=gb&locale=en_GB'; $clientId = $this->clientId; $redirectUri = 'http://localhost/sailadventure-new/tests/callback'; $scopes = 'full_access'; // or 'readonly' if applicable $authorizationUrl .= '&response_type=code'; $authorizationUrl .= '&client_id=' . urlencode($clientId); $authorizationUrl .= '&redirect_uri=' . urlencode($redirectUri); $authorizationUrl .= '&scope=' . urlencode($scopes); header('Location: ' . $authorizationUrl); exit(); } function handleCallback() { if (isset($_GET['code'])) { // Initialize cURL $curl = curl_init(); // Create the URL $url = 'https://oauth.accounting.sage.com/token'; $clientSecret = $this->clientSecret; $authCode = urlencode($_GET['code']); $data = array( 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $authCode, 'grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/sailadventure-new/tests/continue' ); $jsonData = json_encode($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); // Execute the request $response = curl_exec($curl); if (curl_errno($curl)) { $error = curl_error($curl); return $error; } curl_close($curl); echo '<pre>' . var_export($response, true) . '</pre>'; } } }
  8. Hi All New here, had a quick search and I believe that my issue is related to encoding but really not sure, feels like I've tried so much now. I have an app created and the beginnings of a Sage Class. When calling authorizeSageAccountingApp() from a PHP page, it redirects to authorise my app, I am redirected to a page with handleCallback() and then receiving the following: '{"$severity":"error","$dataCode":"DataParsingError","$message":"The data you sent could not be processed.","$source":"Proxy"}' I am clearly doing something wrong but I may not have a good understanding of what's happening here. Any pointers are greatly received. Eventually, I would like to code the ability to create invoices in our Sage Accounting account rather than using Stripe. <?php class Sage { public function __construct() { $this->clientId = 'XXXX'; $this->clientSecret = 'XXXX'; } function authorizeSageAccountingApp() { $authorizationUrl = 'https://www.sageone.com/oauth2/auth/central?filter=apiv3.1&country=gb&locale=en_GB'; $clientId = $this->clientId; $redirectUri = 'http://localhost/sailadventure-new/tests/callback'; $scopes = 'full_access'; // or 'readonly' if applicable $authorizationUrl .= '&response_type=code'; $authorizationUrl .= '&client_id=' . urlencode($clientId); $authorizationUrl .= '&redirect_uri=' . urlencode($redirectUri); $authorizationUrl .= '&scope=' . urlencode($scopes); header('Location: ' . $authorizationUrl); exit(); } function handleCallback() { if (isset($_GET['code'])) { $authorizationCode = $_GET['code']; $clientId = $this->clientId; $clientSecret = $this->clientSecret; $redirectUri = 'http://localhost/sailadventure-new/tests/continue'; $tokenUrl = 'https://oauth.accounting.sage.com/token'; $header = array('Accept: application/json','Content-Type: application/x-www-form-urlencoded'); $content = "client_id=$clientId&client_secret=$clientSecret&grant_type=authorization_code"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $tokenUrl, CURLOPT_HTTPHEADER => $header, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $content )); $response = curl_exec($curl); curl_close($curl); echo '<pre>' . var_export($response, true) . '</pre>'; } else { echo "Authorization code not found."; } } }
×
×
  • Create New...