Laravel Türkiye Discord Kanalı Forumda kod paylaşılırken dikkat edilmesi gerekenler!Birlikte proje geliştirmek ister misiniz?
  • YardımLaravel
  • Paraşüt Entegrasyonu Hakkında Yardımcı Olabilir misiniz ?

  • koti42

      Seviye 246

    Kodum bu şekilde

    array:4 [▼ // app\Services\InvoiceService.php:23
      "success" => false
      "error" => true
      "body" => {#2513 ▼
        +"errors": array:1 [▼
          0 => {#2468 ▼
            +"detail": "Fatura türü kabul edilen bir kelime değil"
          }
        ]
      }
      "status" => 422
    ]

    bu şekilde bir hata alıyorum ama ne demek istediğini anlamadım.

    <?php
    
    namespace App\Services;
    
    use Theposeidonas\LaravelParasutApi\Facades\Parasut;
    use App\Models\Invoice;
    use Illuminate\Support\Facades\Log;
    
    class InvoiceService
    {
        public function createAndSendInvoice($data)
        {
            try {
                // Müşteri oluştur veya getir
                $customer = $this->getOrCreateCustomer($data['user_email'], $data);
    
                // Ürün verisini hazırla
                $items = $this->prepareInvoiceItems($data);
    
                // Fatura oluştur
                $invoice = $this->createInvoice($customer['data']['id'], $items, $data);
    
                dd($invoice);
                // E-Fatura/E-Arşiv kontrolü ve oluşturma
                $this->createEDocument($invoice['data']['id'], $customer['data']['attributes']['tax_number'] ?? null);
    
                return $invoice;
            } catch (\Exception $e) {
                Log::error('Paraşüt fatura oluşturma hatası: ' . $e->getMessage());
                throw $e;
            }
        }
    
        private function getOrCreateCustomer($email, $data)
        {
            // Önce müşteriyi ara
            $customers = Parasut::Customer()->index(['filter' => ['email' => $email]]);
    
            if (!empty($customers['body']->data)) {
                return ['data' => (array)$customers['body']->data[0]];
            }
    
            // Müşteri yoksa yeni oluştur
            $customerData = [
                'data' => [
                    'type' => 'contacts',
                    'attributes' => [
                        'email' => $email,
                        'name' => $data['user_name'] ?? 'Müşteri',
                        'contact_type' => 'person',
                        'tax_number' => $data['tax_number'] ?? null,
                        'account_type' => 'customer'
                    ]
                ]
            ];
    
            return Parasut::Customer()->create($customerData);
        }
    
        private function prepareInvoiceItems($data)
        {
            $items = [];
            foreach ($data['items'] as $item) {
                $productId = $this->getOrCreateProduct($item);
    
                $items[] = [
                    'type' => 'sales_invoice_details',
                    'attributes' => [
                        'quantity' => $item['quantity'],
                        'unit_price' => $item['price'],
                        'vat_rate' => 18,
                        'description' => $item['name']
                    ],
                    'relationships' => [
                        'product' => [
                            'data' => [
                                'id' => $productId,
                                'type' => 'products'
                            ]
                        ]
                    ]
                ];
            }
            return $items;
        }
        private function getOrCreateProduct($orderItem)
        {
            try {
                // Ürün ara
                $searchResult = Parasut::Product()->index([
                    'filter' => [
                        'name' => $orderItem['name']
                    ]
                ]);
    
                if (!empty($searchResult['body']->data)) {
                    return $searchResult['body']->data[0]->id;
                }
    
                // Ürün yoksa oluştur
                $productData = [
                    'data' => [
                        'type' => 'products',
                        'attributes' => [
                            'name' => $orderItem['name'],
                            'vat_rate' => 18,
                            'unit' => 'Adet',
                            'currency' => 'TRL',
                            'list_price' => $orderItem['price'],
                            'inventory_tracking' => false
                        ]
                    ]
                ];
    
                $result = Parasut::Product()->create($productData);
                return $result['body']->data->id;
    
            } catch (\Exception $e) {
                Log::error('Parasut ürün oluşturma hatası: ' . $e->getMessage());
                throw $e;
            }
        }
    
        private function createInvoice($customerId, $items, $data)
        {
            $invoiceData = [
                'data' => [
                    'type' => 'sales_invoices',
                    'attributes' => [
                        'invoice_type' => 'sale',
                        'description' => 'Online Satış - Sipariş #' . $data['order_id'],
                        'issue_date' => date('Y-m-d'),
                        'due_date' => date('Y-m-d', strtotime('+1 month')),
                        'currency' => 'TRL',
                        'shipment_included' => false,
                    ],
                    'relationships' => [
                        'contact' => [
                            'data' => [
                                'id' => $customerId,
                                'type' => 'contacts'
                            ]
                        ],
                        'details' => [
                            'data' => $items
                        ]
                    ]
                ]
            ];
    
            return Parasut::Bill()->create($invoiceData);
        }
    
        private function createEDocument($invoiceId, $taxNumber)
        {
            if ($taxNumber) {
                // E-Fatura kullanıcısı kontrolü
                $inbox = Parasut::Inbox()->index(['filter' => ['vkn' => $taxNumber]]);
    
                if (!empty($inbox['body']->data)) {
                    // E-Fatura oluştur
                    $eInvoice = Parasut::EBill()->create([
                        'data' => [
                            'type' => 'e_invoices',
                            'relationships' => [
                                'sales_invoice' => [
                                    'data' => [
                                        'id' => $invoiceId,
                                        'type' => 'sales_invoices'
                                    ]
                                ]
                            ]
                        ]
                    ]);
    
                    return $this->trackEDocumentStatus($eInvoice['body']->data->id, 'e_invoice');
                }
            }
    
            // E-Arşiv oluştur
            $eArchive = Parasut::EArchive()->create([
                'data' => [
                    'type' => 'e_archives',
                    'relationships' => [
                        'sales_invoice' => [
                            'data' => [
                                'id' => $invoiceId,
                                'type' => 'sales_invoices'
                            ]
                        ]
                    ]
                ]
            ]);
    
            return $this->trackEDocumentStatus($eArchive['body']->data->id, 'e_archive');
        }
    
        private function trackEDocumentStatus($jobId, $type)
        {
            $maxAttempts = 10;
            $attempt = 0;
    
            do {
                sleep(3);
                $job = Parasut::TrackableJob()->show($jobId);
    
                if ($job['body']->data->attributes->status === 'done') {
                    return true;
                }
    
                if ($job['body']->data->attributes->status === 'error') {
                    throw new \Exception("E-belge oluşturma hatası: " . $job['body']->data->attributes->errors);
                }
    
                $attempt++;
            } while ($attempt < $maxAttempts);
    
            throw new \Exception("E-belge oluşturma zaman aşımı");
        }
    }
    • koti42

        Seviye 246

      createInvoice Fonksiyonun da invoice_type göndermişim arkadaşlar item_type olarak gönderecekmişiz.