1. Go to this page and download the library: Download aenzenith/laravel-birfatura 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/ */
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\TokenResolver;
use App\Models\Setting;
use Illuminate\Support\Facades\Crypt;
final class SettingsTokenResolver implements TokenResolver
{
public function resolve(): ?string
{
$stored = Setting::query()->where('key', 'birfatura_token')->value('value');
// null dönerse entegrasyon kapalı olur (bütün uçlar 404).
return $stored ? Crypt::decryptString($stored) : null;
}
}
// AppServiceProvider::boot()
use Aenzenith\BirFatura\Facades\BirFatura;
BirFatura::resolveTokenUsing(fn (): ?string => app(Vault::class)->get('birfatura'));
// Örnek — durumlar ve eşlemeleri projenize göre tanımlanır.
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\HasBirFaturaLabel;
enum BirFaturaOrderStatus: int implements HasBirFaturaLabel
{
case Approved = 1;
case Shipped = 2;
case Cancelled = 3;
public function birFaturaLabel(): string
{
return match ($this) {
self::Approved => 'Onaylandı',
self::Shipped => 'Kargolandı',
self::Cancelled => 'İptal Edildi',
};
}
/** Projenin kendi sipariş durumlarına eşleme — tamamen projeye özgü. */
public function orderStatuses(): array
{
return match ($this) {
self::Approved => ['paid', 'completed'],
self::Shipped => ['shipped'],
self::Cancelled => ['cancelled', 'refunded'],
};
}
}
// Örnek
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\HasBirFaturaLabel;
enum BirFaturaPaymentMethod: int implements HasBirFaturaLabel
{
case CreditCard = 1;
case BankTransfer = 2;
case CashOnDelivery = 3;
public function birFaturaLabel(): string
{
return match ($this) {
self::CreditCard => 'Kredi Kartı',
self::BankTransfer => 'Banka EFT-Havale',
self::CashOnDelivery => 'Kapıda Ödeme Nakit',
};
}
}
// Örnek — sorgu, alan adları ve kurallar projenin modeline göre yazılır.
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\OrderProvider;
use Aenzenith\BirFatura\Data\BillingParty;
use Aenzenith\BirFatura\Data\Order;
use Aenzenith\BirFatura\Data\OrderLine;
use Aenzenith\BirFatura\Data\OrdersQuery;
use Aenzenith\BirFatura\Data\PricePair;
use Aenzenith\BirFatura\Data\ShippingParty;
use Aenzenith\BirFatura\Data\Totals;
use App\Models\Order as ShopOrder;
final class OrderSource implements OrderProvider
{
public function orders(OrdersQuery $query): iterable
{
$status = BirFaturaOrderStatus::from($query->statusId);
$orders = ShopOrder::query()
->with('items')
->whereIn('status', $status->orderStatuses())
->whereBetween('updated_at', [$query->from, $query->to])
->lazy(); // iterable: bellek sabit kalır
foreach ($orders as $order) {
yield $this->toBirFatura($order);
}
}
private function toBirFatura(ShopOrder $order): Order
{
$billing = $order->is_corporate
? BillingParty::corporate(
name: $order->company_name,
taxOffice: $order->tax_office,
taxNumber: $order->tax_number,
address: $order->billing_address,
town: $order->billing_town,
city: $order->billing_city,
mobilePhone: $order->phone,
email: $order->email,
)
: BillingParty::individual(
name: $order->full_name,
identityNumber: $order->tckn,
address: $order->billing_address,
town: $order->billing_town,
city: $order->billing_city,
mobilePhone: $order->phone,
email: $order->email,
);
$lines = $order->items->map(fn ($item): OrderLine => new OrderLine(
productId: $item->product_id,
productCode: $item->sku,
productName: $item->name,
vatRate: $item->vat_rate,
unitPrice: PricePair::fromTaxIncluding($item->unit_price, $item->vat_rate), // BİRİM fiyat
quantity: $item->quantity,
))->all();
return new Order(
id: $order->id,
code: $order->number,
date: $order->paid_at,
billing: $billing,
shipping: ShippingParty::fromBilling($billing), // fiziksel teslimat varsa kendi adresiyle kurun
paymentTypeId: $order->payment_method_id, // ödeme sözlüğündeki id
totals: new Totals(
paid: PricePair::fromTaxIncluding($order->total, 20),
products: PricePair::fromTaxIncluding($order->subtotal, 20),
discount: PricePair::fromTaxIncluding($order->discount, 20),
),
lines: $lines,
currency: 'TRY',
customerId: $order->user_id,
);
}
}
// Örnek — nereye ve nasıl kaydedileceği projeye özgüdür.
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\InvoiceLinkHandler;
use Aenzenith\BirFatura\Data\HandlerResult;
use Aenzenith\BirFatura\Data\InvoiceLinkUpdate;
use App\Models\Order;
final class StoreInvoiceLink implements InvoiceLinkHandler
{
public function handle(InvoiceLinkUpdate $update): HandlerResult
{
$order = Order::find($update->orderId); // string olarak gelir
if ($order === null) {
return HandlerResult::notFound(); // 404 "Sipariş bulunamadı."
}
$order->update([
'invoice_url' => $update->url,
'invoice_number' => $update->number, // ?string
'invoice_date' => $update->date, // ?CarbonImmutable
]);
return HandlerResult::ok(); // 200 "Fatura bağlantısı güncellendi."
}
}
// Örnek
namespace App\Support\BirFatura;
use Aenzenith\BirFatura\Contracts\CargoUpdateHandler;
use Aenzenith\BirFatura\Data\CargoUpdate;
use Aenzenith\BirFatura\Data\HandlerResult;
use App\Models\Shipment;
final class StoreCargo implements CargoUpdateHandler
{
public function handle(CargoUpdate $update): HandlerResult
{
Shipment::updateOrCreate(
['order_id' => $update->orderId],
[
'status_id' => $update->statusId,
'tracking_code' => $update->trackingCode,
'tracking_url' => $update->trackingUrl,
'company' => $update->company,
],
);
return HandlerResult::ok();
}
}