Showing posts with label myob. Show all posts
Showing posts with label myob. Show all posts

Saturday, May 6, 2017

Connect to MYOB Api Using Php Application

At first you need to create an application in MYOB, follow the below link to create an application:

https://my.myob.com.au/au/bd/DevAppList.aspx








Next step is to authorize your MYOB, to do so you have to visit below link:

https://secure.myob.com/oauth2/account/authorize?client_id=xxxxx&redirect_uri=http://localhost/ci/myob/&response_type=code&scope=CompanyFile

This will redirect to MYOB login page if you not logged in and then Authorization page.




After authorize you will back to you application with a code. 

You need to get access token and refresh token from MYOB using the code.

http://localhost/ci/myob/?code=.....

After you get access token and refresh token store that, its required to get company data. To get company related data it may need username and password for that company file.

Output of contact list will be below:


Array
(
    [code] => 200
    [error] => 
    [response] => stdClass Object
        (
            [Items] => Array
                (
                    [0] => stdClass Object
                        (
                            [UID] => 6cacb476-u6fd-451e-bb93-575d971c7189
                            [CompanyName] => Pritom
                            [IsIndividual] => 
                            [DisplayID] => OK-0019
                            [IsActive] => 1
                            [Addresses] => Array()
                            [Notes] => 
                            [Identifiers] => 
                            [CustomList1] => 
                            [CustomList2] => 
                            [CustomList3] => 
                            [CustomField1] => 
                            [CustomField2] => 
                            [CustomField3] => 
                            [CurrentBalance] => 750.82
                            [SellingDetails] => stdClass Object
                            [PaymentDetails] => stdClass Object
                            [LastModified] => 2016-02-26T06:06:37.277
                            [PhotoURI] => 
                            [URI] => https://ar2.api.myob.com/.....
                            [RowVersion] => -629937699343564800
                        )

                )

            [NextPageLink] => https://.../accountright/.../Contact/Customer?$top=5&$skip=5
            [Count] => 760
        )

)

Full PHP script below:


<?php
session_start();

$configs = array(
    "api_key" => "qv5s8qga.....hkhacwkn",
    "api_secret" => "aZJCySs.....vaDBD",
    "redirect_uri" => "http://localhost/myob.php"
);
$myob_connector = new MyobConnector($configs);
if (isset($_GET["expire"])) {
    unset($_SESSION["access_token"]);
    unset($_SESSION["company"]);
    unset($_SESSION["state"]);
}
if (isset($_GET["contacts"])) {
    $contacts = $myob_connector->execute("Contact/Customer?\$top=5&\$skip=0", "GET", "Administrator");
    $myob_connector->prettyPrint($contacts);
}
elseif (isset($_GET["company"])) {
    echo "<a href='myob.php?contacts=true'>List Contacts</a>";
    $contacts = $myob_connector->execute("Company", "GET", "Administrator");
    $myob_connector->prettyPrint($contacts);
}
elseif (isset($_SESSION["access_token"])) {
    $company_list = $myob_connector->getCompanyList();
    if ($company_list["code"] == 200) {
        $_SESSION["company"] = $company_list["response"][0]->Id;
        header("Location: " . $configs["redirect_uri"] . "?company=true");
    }
    else {
        $myob_connector->prettyPrint($company_list);
    }
}
elseif (isset($_GET["code"])) {
    $myob_connector->getAccessToken($_GET["code"]);
    header("Location: " . $configs["redirect_uri"] . "?company");
}
else {
    $_SESSION["state"] = uniqid();
    $myob_connector->authorize();
}

class MyobConnector {
    private $conigs = null;

    function __construct($configs) {
        $this->conigs = $configs;
    }

    public function authorize() {
        $authorize_url = "https://secure.myob.com/oauth2/account/authorize?" .
            "client_id=" . $this->conigs["api_key"] .
            "&redirect_uri=" . rawurlencode($this->conigs['redirect_uri']) .
            "&response_type=code&scope=CompanyFile&state=".$_SESSION["state"];
        header("Location: $authorize_url");
        exit();
    }

    public function getAccessToken($access_code) {
        $params = array(
            'client_id' => $this->conigs["api_key"],
            'client_secret' => $this->conigs["api_secret"],
            'scope' => "CompanyFile",
            'code' => rawurlencode($access_code),
            'redirect_uri' => rawurlencode($this->conigs["redirect_uri"]),
            'grant_type' => 'authorization_code'
        );
        $post = "";
        foreach ($params as $k => $v) {
            $post .= "$k=$v&";
        }
        $headers[] = "Content-Type: application/x-www-form-urlencoded";
        $response = $this->getToken(substr($post, 0, strlen($post) - 1), $headers);
        if ($response["code"] == 200) {
            $_SESSION["access_token"] = $response["response"]->access_token;
            $_SESSION["refresh_token"] = $response["response"]->refresh_token;
            $_SESSION["expires_in"] = $response["response"]->expires_in + time();
        }
        else {
            $this->prettyPrint($response);
            die();
        }
    }

    public function refreshAccessToken() {
        $params = array(
            'client_id' => $this->conigs["api_key"],
            'client_secret' => $this->conigs["api_secret"],
            'refresh_token' => $_SESSION["refresh_token"],
            'grant_type' => 'refresh_token',
        );
        $post = "";
        foreach ($params as $k => $v) {
            $post .= "$k=$v&";
        }
        $headers[] = "Content-Type: application/x-www-form-urlencoded";
        $response = $this->getToken(substr($post, 0, strlen($post) - 1), $headers);
        if ($response["code"] == 200) {
            $_SESSION["access_token"] = $response["response"]->access_token;
            $_SESSION["refresh_token"] = $response["response"]->refresh_token;
            $_SESSION["expires_in"] = $response["response"]->expires_in + time();
        }
        else {
            $this->prettyPrint($response);
            die();
        }
    }

    private function getToken($params, $headers = null) {
        return $this->getURL("https://secure.myob.com/oauth2/v1/authorize", "POST", $params, $headers);
    }

    public function execute($end_point, $method = "GET", $userName = null, $password = "") {
        $company = $_SESSION["company"];
        $end_point = "https://api.myob.com/accountright/$company/$end_point";
        $headers[] = "Authorization: Bearer " . $_SESSION["access_token"];
        if ($userName) {
            $headers[] = "x-myobapi-cftoken: " . base64_encode("$userName:$password");
        }
        $headers[] = "x-myobapi-key: " . $this->conigs["api_key"];
        $headers[] = "x-myobapi-version: v2";
        $response = $this->getURL($end_point, $method, null, $headers);
        if ($response["code"] == 401) {
            $this->refreshAccessToken();
        }
        return $this->getURL($end_point, $method, null, $headers);
    }

    public function getCompanyList() {
        $end_point = "https://api.myob.com/accountright";
        $headers[] = "Authorization: Bearer " . $_SESSION["access_token"];
        $headers[] = "x-myobapi-cftoken: ";
        $headers[] = "x-myobapi-key: " . $this->conigs["api_key"];
        $headers[] = "x-myobapi-version: v2";
        return $this->getURL($end_point, "GET", null, $headers);
    }

    private function getURL($url, $method = "GET", $params = null, $headers = null) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_USERAGENT, "Test APP");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        if ($method == "POST") {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }

        if (isset($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        $response = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);
        $response2 = json_decode($response);
        if (strlen($response) > 0 && is_null($response2)) {
            $response2 = $response;
        }
        return array(
            "code" => $code,
            "error" => $error,
            "response" => $response2
        );
    }

    public function prettyPrint($o) {
        echo "<pre>";
        print_r($o);
        echo "</pre>";
    }
}
  

Wednesday, September 30, 2015

Myob Cloud Integration: Use Filter In Myob Integration


Equal Method (String)
{domain}/{cf guid}/Sale/Invoice/?$filter=PaymentDetails/Method eq 'Cash'

Equal Method (UID)
{domain}/{cf guid}/Sale/Invoice/?$filter=Customer/UID eq guid'd61a6a86-453a-48bf-9402-6eb6b4ea23cf'

Equal Method (Boolean)
{domain}/{cf guid}/Contact/?$filter=IsIndividual eq true

Filtering in List
{domain}/{cf guid}/Contact/Customer/?$filter=Addresses/any(x: x/Email eq null) 

Equal Method (Date)
{domain}/{cf guid}/Purchase/Bill/?$filter=Date ge datetime'2014-07-01' and Date le datetime'2014-09-24'