Skip to content

Instantly share code, notes, and snippets.

@alexgarrettsmith
Last active October 8, 2019 09:02
Show Gist options
  • Save alexgarrettsmith/2155cec162c7a2e400f4b6f5b01daa07 to your computer and use it in GitHub Desktop.
Save alexgarrettsmith/2155cec162c7a2e400f4b6f5b01daa07 to your computer and use it in GitHub Desktop.
<?php
namespace Codecourse\Domain\Invoice;
use Octobat\Invoice;
use Octobat\Customer;
use Octobat\Transaction;
use Braintree\Transaction as BraintreeTransaction;
use Octobat\TaxEvidenceRequest;
use Codecourse\Domain\Users\User;
class InvoiceGenerator
{
/**
* Undocumented variable
*
* @var [type]
*/
protected $user;
/**
* Undocumented variable
*
* @var [type]
*/
protected $transaction;
/**
* Undocumented function
*
* @param User $user
* @param Transaction $transaction
*/
public function __construct(User $user, BraintreeTransaction $transaction)
{
$this->user = $user;
$this->transaction = $transaction;
}
/**
* Undocumented function
*
* @return void
*/
public function generate()
{
if (!$this->user->octobat_id) {
$customer = Customer::create([
'email' => $this->user->email,
'name' => $this->user->fullName(),
'billing_address_country' => $this->user->address->country->code,
'billing_address_line1' => $this->user->address->company,
'billing_address_line2' => $this->user->address->address,
'business_type' => $this->user->address->vat_number ? 'B2B' : 'B2C',
]);
$this->user->update([
'octobat_id' => $customer->id
]);
} else {
Customer::update($this->user->octobat_id, [
'billing_address_country' => $this->user->address->country->code,
'billing_address_line1' => $this->user->address->company ?: '',
'billing_address_line2' => $this->user->address->address,
'business_type' => $this->user->address->vat_number ? 'B2B' : 'B2C',
]);
}
$invoice = Invoice::create([
'customer' => $this->user->octobat_id,
'payment_recipients' => [
'oc_pr_15254636581d9me2426e06'
],
'description' => 'Pro membership',
'currency' => $this->transaction->currencyIsoCode
]);
$taxEvidence = TaxEvidenceRequest::create([
'customer' => $this->user->octobat_id,
'customer_billing_address_country' => $this->user->address->country->code,
'payment_source_country' => $this->user->address->country->code,
'transaction_date' => $this->transaction->createdAt->format('Y-m-d'),
'product_type' => 'eservice',
'customer_tax_number' => $this->user->address->vat_number
]);
$invoiceItem = $invoice->items()->create([
'tax_evidence' => $taxEvidence->id,
'quantity' => 1,
'currency' => $this->transaction->currencyIsoCode,
'unit_gross_amount' => $this->transaction->amount * 100,
'description' => 'Pro membership',
]);
$invoice->confirm([
'confirmed_on' => $this->transaction->createdAt->format('Y-m-d')
]);
$transaction = Transaction::create([
'customer' => $this->user->octobat_id,
'payment_recipient' => 'oc_pr_15254636581d9me2426e06',
'transaction_date' => $this->transaction->createdAt->format('Y-m-d'),
'invoice' => $invoice->id,
'status' => 'succeeded',
'flow_type' => 'payment',
'amount' => $this->transaction->amount * 100,
'currency' => $this->transaction->currencyIsoCode
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment