Skip to content

Instantly share code, notes, and snippets.

@aphilippi
Last active February 23, 2019 14:14
Show Gist options
  • Save aphilippi/8023f609e1556bb696009b00053a944b to your computer and use it in GitHub Desktop.
Save aphilippi/8023f609e1556bb696009b00053a944b to your computer and use it in GitHub Desktop.
Creare serii facturi proforme prin API-ul factureaza.ro
<?php
//set BASE_URL to https://factureaza.ro/api/v1/ for production
define('BASE_URL', 'https://sandbox.factureaza.ro/api/v1/');
//replace with the api key provided in the backend
define('API_KEY', '72543f4dc00474bc40a27916d172eb93339fae894ec7a6f2dceb4751d965');
$url = BASE_URL . 'proforma_invoice_series.xml';
$ch = curl_init();
$postdata_with_suffix = '<?xml version="1.0"?>
<proforma_invoice_series>
<counter_start>100</counter_start>
<counter_current>100</counter_current>
<prefix>PREFIXUSS</prefix>
<sepparator>-</sepparator>
<suffix>SUPREFIXUS</suffix>
<year>2021</year>
</proforma_invoice_series>';
$postdata_without_suffix = '<?xml version="1.0"?>
<proforma_invoice_series>
<counter_start>100</counter_start>
<counter_current>100</counter_current>
<prefix>PREFIXUSS</prefix>
<sepparator>-</sepparator>
<year>2021</year>
</proforma_invoice_series>';
# a little bit of randomness :)
$postdata = (mt_rand(0, 10) > 4) ? $postdata_without_suffix : $postdata_with_suffix;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_KEY . ":x");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$result = curl_exec($ch);
$header = curl_getinfo($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 201) {
// transform the invoice data from xml to a array
$invoice_data = json_decode(json_encode(simplexml_load_string($result)), TRUE);
echo 'Proforma Invoice Series created successfully. Series id: ' . $invoice_data['id'];
} else {
echo '<pre> Error occured: ' . $httpCode . ':' . $result . '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment