Skip to content

Commit

Permalink
Call ReceiptException for fail createReceipt. Change owner data.
Browse files Browse the repository at this point in the history
  • Loading branch information
arhitov committed May 12, 2024
1 parent 277b323 commit 8b18c60
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/Models/Traits/ModelOwnerExpandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
trait ModelOwnerExpandTrait
{
protected array $ownerData = [];

public static function bootDeleteCascadeBalance(): void
{
if (config('billing.database.delete_cascade')) {
Expand All @@ -60,28 +62,47 @@ final public function getOwnerIdentifier(): array
];
}

/**
* @return ?string
*/
public function getOwnerUuid(): ?string
{
return $this->ownerData['uuid'] ?? null;
}

/**
* @return string
*/
public function getOwnerName(): string
{
return $this->name;
return $this->ownerData['name'] ?? $this->name;
}

/**
* @return ?string
*/
public function getOwnerEmail(): ?string
{
return $this->email;
return $this->ownerData['email'] ?? $this->email;
}

/**
* @return ?string
*/
public function getOwnerPhone(): ?string
{
return null;
return $this->ownerData['phone'] ?? null;
}

/**
* Setting owner data
*
* @param array $data
* @return void
*/
public function setOwnerData(array $data): void
{
$this->ownerData = $data;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/OmnireceiptGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function receiptFactory(BillableInterface $owner, array $parameters = [],
$receipt->setCustomer(
$this->getGateway()->customerFactory(
array_filter([
'name' => $owner->getOwnerName(),
'uuid' => $owner->getOwnerUuid(),
'name' => $owner->getOwnerName(),
'email' => $owner->getOwnerEmail(),
'phone' => $owner->getOwnerPhone(),
])
Expand Down
14 changes: 14 additions & 0 deletions src/Services/FiscalReceipt/FiscalReceiptCheckState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Arhitov\LaravelBilling\Services\FiscalReceipt;

use Arhitov\LaravelBilling\Enums\ReceiptStateEnum;
use Arhitov\LaravelBilling\Exceptions\Receipt\ReceiptException;
use Arhitov\LaravelBilling\Models\Receipt;
use Arhitov\LaravelBilling\OmnireceiptGateway;
use Carbon\Carbon;
Expand Down Expand Up @@ -42,6 +43,12 @@ public function countActive(): int
return $this->builder()->count();
}

/**
* @return void
* @throws \Arhitov\LaravelBilling\Exceptions\Receipt\ReceiptStateException
* @throws \Omnireceipt\Common\Exceptions\Parameters\ParameterValidateException
* @throws \Throwable
*/
public function execute(): void
{
// First we make a request to get a receipt from the API for the last X amount of time.
Expand Down Expand Up @@ -79,6 +86,13 @@ public function execute(): void
$response = $this->gateway->getGateway()->createReceipt($receipt->getReceipt());
if ($response->isSuccessful()) {
$receipt->changeStateOrFail(ReceiptStateEnum::Send);
} else {
throw new ReceiptException($receipt, implode(
'; ',
method_exists($response, 'getPayload')
? $response->getPayload()['warning'] ?? []
: null,
));
}
} else {
$unknownReceipt[] = $receipt;
Expand Down

0 comments on commit 8b18c60

Please sign in to comment.