PHP code example of serato / sqs-invoice-queue

1. Go to this page and download the library: Download serato/sqs-invoice-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serato / sqs-invoice-queue example snippets


use Serato\InvoiceQueue\InvoiceValidator;

$validator = new Serato\InvoiceQueue\InvoiceValidator;

# Validate an array against the root schema.
if ($validator->validateArray(['my' => 'data'])) {
  // Data conforms to schema
} else {
  // Data does not conform to schema
  foreach ($validator->getErrors() as $error) {
    print_r($error);
  }
}

# Validate a string against an named definition within the JSON schema
if ($validator->validateJsonString('{"my":"data"}', 'line_item')) {
  // Data conforms to schema
} else {
  // Data does not conform to schema
  foreach ($validator->getErrors('line_item') as $error) {
    print_r($error);
  }
}

use Serato\InvoiceQueue\Invoice;

# Constructor is private.
# Always create using Invoice::create() static method
$invoice = Invoice::create();

# Set individual properties
$invoice
  ->setInvoiceId('MyInvoiceId')
  ->setCurrency(Invoice::CURRENCY_EUR)
  ->setBillingAddressCompanyName('Acme Inc');
// ...etc

# Get individual properties
echo $invoice->getInvoiceId();
echo $invoice->getCurrency();
echo $invoice->getBillingAddressCompanyName();
// ...etc

# Create an Invoice Item
use Serato\InvoiceQueue\InvoiceItem;

$item = InvoiceItem::create();
$item
  ->setSku('MySkuCode')
  ->setQuantity(1)
  ->setAmountGross(2000)
  ->setAmountTax(0)
  ->setAmountNet(1000)
  ->setUnitPrice(1000)
  ->setTaxCode(Invoice::TAXCODE_V);

# Add the Item to an Invoice
$invoice->addItem($item);

# Gets all items in invoice (returns an array of InvoiceItem objects)
$invoice->getItems();

# Get complete invoice data that conforms to JSON schema
$data = $invoice->getData();

# Use the `Invoice::load` static method to populate model with data
# $data can be an array of string of JSON
# (the data will be validated against the JSON schema)
$invoice = Invoice::load($data);

# If loading multiple invoices, create a single InvoiceValidator
# instance and pass it to `Invoice::load` for better performance.
$validator = new Serato\InvoiceQueue\InvoiceValidator;
$invoice1 = Invoice::load($data1, $validator);
$invoice2 = Invoice::load($data2, $validator);


use Aws\Sdk;
use Serato\InvoiceQueue\SqsQueue;
use Monolog\Logger;
use Serato\InvoiceQueue\MonologLogFormatter;
use Serato\InvoiceQueue\InvoiceValidator;

# Create AWS SQS client instance
$awsSdk = new Sdk();
$awsSqsClient->createSqs();

# Create a PSR LogInterface instance.
# Monolog is recommended. Use in combination with a custom formatter
# that makes the log entries more legible in Cloudwatch Logs.
$logger = new Logger('My-App-Logger');
foreach ($logger->getHandlers() as $handler) {
    $handler->setFormatter(new MonologLogFormatter());
}

# Constructor ainst the JSON schema
# Batch will sent when interal batch size limit is reached or when
# SqsQueue instance is destroyed
$invoice1 = Invoice::create();
// ... populate $invoice1
$invoice2 = Invoice::create();
// ... populate $invoice2

# You MUST provide a InvoiceValidator when calling SqsQueue::sendInvoiceToBatch
$validator = new InvoiceValidator;

$queue
  ->sendInvoiceToBatch($invoice1, $validator)
  ->sendInvoiceToBatch($invoice2, $validator);

# A callback can be provided to the SqsQueue. This callback is called after every
# batch is sent to SQS.
#
# The callback as takes two arguments:
#
# - array $successfulInvoices      An array of Serato\InvoiceQueue\Invoice
#                                  instances that were successfully delivered to SQS.
# - array $failedInvoices          An array of Serato\InvoiceQueue\Invoice
#                                  instances that failed to deliver to SQS.

$queue->setOnSendMessageBatchCallback(function ($successfulInvoices, $failedInvoices ) {
  // Process invoices based on success or otherwise
});