Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
1
Indexable
Never
<?php

$curl_handle = curl_init();
$ordered_products = array();

foreach($customer->products_ordered as $product){
  if($product->amount > 0){
    $productObj = (object)array('name' => $product->name, 'quantity' => $product->amount, 'unit_cost' => $product->price);
    array_push($ordered_products, $productObj);
  }
}

if(!empty($customer->billing_address)){
  $bill_to = $customer->personal_information->first_name.' '.$customer->personal_information->last_name.PHP_EOL.
  $customer->billing_address->address_line1.' '.$customer->billing_address->address_line2.PHP_EOL.
  $customer->billing_address->postal_code.' '.$customer->billing_address->city.PHP_EOL.
  $customer->billing_address->country;
} else{
  $bill_to = ''.PHP_EOL.PHP_EOL. // full name
  ''.PHP_EOL. // address line 1
  ''.' '.''.PHP_EOL. // postail code + city
  '' // country
}

$ship_to = $customer->shipping_information->address_line1.' '.$customer->shipping_information->address_line2.PHP_EOL.
$customer->shipping_information->postal_code.' '.$customer->shipping_information->city.PHP_EOL.
$customer->shipping_information->country;

$custom_field1 = (object)array('name' => 'Order ID', 'value' => '');
$custom_field2 = (object)array('name' => 'Email address', 'value' => '');
$custom_field3 = (object)array('name' => 'Payment method', 'value' => '');
$fields = (object)array('tax' => 'false', 'discounts' => 'false', 'shipping' => 'true');

$json_data = [
  'logo' => 'https://ps5-store.net/assets/images/logos/logo-complete.png',
  'from' => 'PS5 Store.NET',
  'to' => $bill_to,
  'ship_to' => $ship_to,
  'number' => 'B5738F-C29762AA-A9CFE3',
  'currency' => 'EUR',
  'custom_fields' => [
    $custom_field1,
    $custom_field2,
    $custom_field3
  ],
  'date' => $customer->order_datetime,
  'items' => $ordered_products,
  'fields' => $fields,
  'shipping' => 11,
  'amount_paid' => $total_due + 11,
  'notes' => 'This invoice is already paid.'.PHP_EOL.
    'Please use this invoice for any form of administration.'.PHP_EOL.
    'This invoice will be saved by us too.'.PHP_EOL.PHP_EOL.
    'Thank you for shopping at PS5 Store.NET'
];

curl_setopt_array($curl_handle, [
  CURLOPT_URL => 'https://invoice-generator.com',
  CURLOPT_POST => 1,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
  CURLOPT_POSTFIELDS => json_encode($json_data)
]);

$response = curl_exec($curl_handle);
curl_close($curl_handle);

if(empty($response)){
  echo '<script>console.log("cURL Error: '.curl_error($curl_handle).' - Code: ' . curl_errno($curl_handle).'")</script>';
}
else{
  $file_path = 'invoice1.pdf';
  $dirname = dirname($file_path);
  if(!is_dir($dirname)) mkdir($dirname);

  $file_handler = fopen($file_path, 'w');
  fwrite($file_handler, $response);
  fclose($file_handler);

}