
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Najpierw ustawiane jest wyświetlanie błędów tak abym mógł je widzieć podczas testów rozwiązania
function newInvoice($buyer_name, $buyer_email, int $buyer_nip, $buyer_post_code, $buyer_city, $buyer_street, $buyer_positions)
{
$kind = 'proforma';
$number = null;
$sell_date = date("Y-m-d");
$issue_date = date("Y-m-d");
$payment_to = date("Y-m-d", strtotime("$sell_date +7 day") );
/*
$seller_name = 'KRS francuski';
$seller_tax_no = '5342509726';
*/
$invoice = [
'kind' => $kind,
'number' => $number,
'sell_date' => $sell_date,
'issue_date' => $issue_date,
'payment_to' => $payment_to,
'seller_name' => $seller_name,
'seller_tax_no' => $seller_tax_no,
'buyer_name' => $buyer_name,
'buyer_email' => $buyer_email,
'buyer_tax_no' => $buyer_nip,
"buyer_post_code" => $buyer_post_code,
"buyer_city" => $buyer_city,
"buyer_street" => $buyer_street,
'positions' => $buyer_positions
];
$invoice_json = [
"api_token" => $GLOBALS['api_token'],
'invoice' => $invoice,
];
$invoice_json = json_encode($invoice_json);
$url = "https://frgw.fakturownia.pl/invoices.json?invoice={$invoice_json}";
return $invoice_json;
}
Funkcja new Invoice pobiera 7 parametrów. Są to dane osoby której faktura ma być wystawiona. Rodzaj faktury – proforma. Data sprzedaży, jeszcze jakaś data i czas zapłaty. W strukturze $invoice zapisane są dane kontrahenta. Struktura $invoice_josn zawiera token i fakturę – $invoice które zostaną zwrócone przez tą właśnie funkcje.
$nip = '5220000080';
$Pvalue = intval($_REQUEST['value']);
$GLOBALS['api_token'] = 'RriqIhxAIoueBsrqRZ';
$GLOBALS['buyer_name'] = $_POST['company_name'];
$GLOBALS['buyer_email'] = 'ttroczynski5@gmail.com';
$GLOBALS['buyer_nip'] = '5272184991';
$GLOBALS['buyer_post_code'] = '';
$GLOBALS['buyer_city'] = '';
$GLOBALS['buyer_street'] = '';
$GLOBALS['product'] = 'test';
$GLOBALS['cost'] = 100;
$GLOBALS['quantity'] =1;
Zmienne globalne zawierają dane testowe kupującego.
function do_post($url, $JSONObj){
$headers = array( "Content-Type: application/json; charset=utf-8" ,"Authorization: Basic USER_PASS" );
//Set Options for CURL
$curl = curl_init($url);
//Return Response to Application
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Set Content-Headers to JSON
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
//Execute call via http-POST
curl_setopt($curl, CURLOPT_POST, true);
//Set POST-Body
//convert DATA-Array into a JSON-Object
curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONObj);
//WARNING!!!!!
//This option should NOT be "false"
//Otherwise the connection is not secured
//You can turn it of if you're working on the test-system with no vital data
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$jsonResponse = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($jsonResponse, true);
return $response;
}
Funkcja do_post pobiera url oraz obiekt $invoice_josn zwrócony przez poprzednią funkcję. W funkcji ustawianę są parametry zapytania do serwera oraz zwracana jest odpowiedź
$invoiceData = newInvoice($GLOBALS['buyer_name'], $GLOBALS['buyer_email'], $GLOBALS['buyer_nip'],
$GLOBALS['buyer_post_code'], $GLOBALS['buyer_city'], $GLOBALS['buyer_street'],
['name' => $GLOBALS['product'], 'tax' => 23, 'total_price_gross' => $GLOBALS['cost'], 'quantity' => $GLOBALS['quantity'] ]);
$response = do_post('https://frgw.fakturownia.pl/invoices.json', $invoiceData);
Wywołanie funkcji newInvoice oraz do_post i zwrócenie rezultatu
Podsumowanie
Został przeprowadzony test działania API fakturowni i teraz wystarczy podmienić dane na dane produkcyjne.