Saskaitos.lt API sujungimo pavyzdys

< ?
// Don't foget: 

// Open your company and you will see tab "API", here is api key and company id number
$config = array(
    'apiKey'    => '',
    'companyId' => 1,
);

# -- Create customer
$customerParams = array(
    'type'      => 'customer',
    'action'    => 'create', // search (by any param in array) | get | create | update | delete
    'Customer' => array(
//        'ID'        => '', // for actions: create | update | delete
        'Name'      => 'Vardas',
        'Surename'  => 'Pavarde',
        'Address'   => 'Ukmerges g., Vilnius',
        'Phone'     => '+3706XXXXXXX',
        'Email'     => 'el.pastas@gmail.com',
        'Bank' => array(
            'Account' => 'LT1234567890',
        ),
        'Company' => array(
            'Name'  => 'UAB "Imones pavadinimas"',
            'Code'  => 'LT123456',
            'Tax'   => '123456',
        ),
        'Test'      => '1',
    ),
);
$respondCustomer = json_decode(UseCURLSocket('https://www.saskaitos.lt/api', array_merge($config, $customerParams)));

# -- Get company details
$companyParams = array(
    'type'      => 'company',
    'action'    => 'get',
    'Company' => array(
        'ID' => $config['companyId']
    ),
);
$respondCompany = json_decode(UseCURLSocket('https://www.saskaitos.lt/api', array_merge($config, $companyParams)));

# -- Get new seria for bill
$newSeriaParams = array(
    'type'      => 'bill',
    'action'    => 'seria',
    'Bill' => array(
        'Type' => 'bill', // pre | bill | other
        'Test' => '1',
    ),
);

$respondNewSeria = json_decode(UseCURLSocket('https://www.saskaitos.lt/api', array_merge($config, $newSeriaParams)));

# -- Create new bill
$billParams = array(
    'type'      => 'bill',
    'action'    => 'create', // get | create | update | delete
    'Bill' => array(
//        'ID' => '', // for actions: create | update | delete
        'Customer' => array(
            'ID' => $respondCustomer->Company->Customer->ID,
        ),
        'Prefix'        => $respondNewSeria->Seria->Prefix,
        'Number'        => $respondNewSeria->Seria->Number,
        'Date'          => date("Y-m-d"),
        'PayDate'       => date("Y-m-d", strtotime("+14 days")),
        'Currency'      => 'EUR',
        'PaymentAmount' => '', // empty if not paid
        'PaymentDate'   => '', // empty if not paid
        'Recipient'     => 'customer@domain.co', // email recipient
        'TaxPercentage' => $respondCompany->Tax->Percentage,
        'Type'          => 'bill', // pre | bill | other
        'Wrote'         => 'Vardas `Pavarde`',
        'Comment'       => 'Netikra saskaita, tikrinama.',
        'Item' => array(
            'Count' => array(
                3, 1, 1
            ),
            'Name' => array(
                'Uz atliktas paslaugas', 'Kita paslauga', 'Paskutine paslauga',
            ),
            'Price' => array(
                '10.10', '21.00', '0.00',
            ),
            'Type' => array(
                'vnt', 'vnt', 'vnt',
            ),
        ),
        'Test'          => '1',
    ),
);

$respondBill = json_decode(UseCURLSocket('https://www.saskaitos.lt/api', array_merge($config, $billParams)));

if ($respondBill->Type == 'success') {
    exit('Bill created!');
}
else {
    exit('Error message: ' . $respondBill->Message);
}

// ----------------------------------------------------------------------------------------------------------------------------------------------------------

function UseCURLSocket( $url, $post_fields = '', $timeout=10, $cookie = null, $UA = "XML Sender", $proxy = null, $extended = false ) {
    GLOBAL $curlHeaders;
    $curlHeaders = array();
    $post_fields = http_build_query($post_fields);
    $cu = curl_init();
    curl_setopt($cu, CURLOPT_URL, 				$url);

    if( $UA ){
        curl_setopt($cu, CURLOPT_USERAGENT, 		$UA);
    }

    curl_setopt($cu, CURLOPT_RETURNTRANSFER, 	1);
    curl_setopt($cu, CURLOPT_SSL_VERIFYHOST,    false);
    curl_setopt($cu, CURLOPT_SSL_VERIFYPEER,    false);
    @curl_setopt($cu, CURLOPT_FOLLOWLOCATION, 	true);

    if( $_SERVER['SERVER_ADDR'] && $_SERVER['SERVER_ADDR'] != '127.0.0.1' &&
        !strstr($url, 'localhost') && !strstr($url, '127.0.0.1') &&
        !strstr($url, $_SERVER['SERVER_ADDR']) )
        @curl_setopt($cu, CURLOPT_INTERFACE, $_SERVER['SERVER_ADDR']);


    $proxy = explode(":", $proxy);
    if( $proxy[0] && $proxy[1] ) {
        if( $proxy[2] == 'SOCK5' )
            curl_setopt($cu, CURLOPT_PROXYTYPE,     CURLPROXY_SOCKS5);
        curl_setopt($cu, CURLOPT_PROXY,         trim($proxy[0]));
        curl_setopt($cu, CURLOPT_PROXYPORT,     trim($proxy[1]));
    }

    if(strlen($post_fields) > 0) {
        curl_setopt($cu, CURLOPT_POST, 1);
        curl_setopt($cu, CURLOPT_POSTFIELDS, $post_fields);
    }

    curl_setopt($cu, CURLOPT_HEADERFUNCTION,	'curlHeaderCallback');
    curl_setopt($cu, CURLOPT_TIMEOUT, 			$timeout);
    curl_setopt($cu, CURLOPT_CONNECTTIMEOUT, 	$timeout);

    if( strstr($cookie, '=') ){
        curl_setopt($cu, CURLOPT_COOKIE, 			$cookie);
    }

    $err_no = curl_errno($cu); $err_msg = null;
    $succeeded  = $err_no == 0 ? true : false;
    if (!$succeeded) {
        $err_msg = curl_error($cu);
        report_error("SOCKET", "\ncURL unable to connect to ".$url.": $err_no $err_msg
", '', true); }elseif($cu){ $output = curl_exec($cu); if(false === $output) { $err_no = curl_errno($cu); $err_msg = curl_error($cu); } } if($extended) return array( 'data' => $output, 'error' => $err_msg, 'errno' => $err_no ); else return $output; } function curlHeaderCallback($resURL, $strHeader) { GLOBAL $curlHeaders; $curlHeaders[] = trim($strHeader); return strlen($strHeader); } ?>