Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Last active October 6, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccullagh/1821351e1a4ca654bc1a8f51cce12435 to your computer and use it in GitHub Desktop.
Save rmccullagh/1821351e1a4ca654bc1a8f51cce12435 to your computer and use it in GitHub Desktop.
<?php
require_once 'vendor/autoload.php';
class Invoice
{
/**
* @var \Stripe\Invoice;
*/
private $invoice;
/**
* Represents a Stripe invoice. Pass in the result from `\Stripe\Invoice::retrieve`
*
* @param string $stripeObj stripe api Invoice
*
* @see https://stripe.com/docs/api/invoices/retrieve?lang=php
*/
public function __construct(Stripe\Invoice $invoice)
{
$this->invoice = $invoice;
}
/**
* Sum all line items in this invoice
*
* @return int
*/
public function getTotal()
{
$amount = 0;
foreach ($this->invoice->lines as $line) {
$amount += $line->amount;
}
return $amount;
}
/**
* Get the invoice amount
*
* @return string
*/
public function getPresentableAmount()
{
$formatter = new \NumberFormatter(
'en_US',
\NumberFormatter::CURRENCY
);
return $formatter->formatCurrency(
$this->getTotal() / 100,
$this->getCurrency()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment