brokenice/fatturaelettronica

此包的最新版本(dev-master)没有提供许可证信息。

用于通过 https://gestionefatture.brokenice.it 发送/接收电子发票的客户端

dev-master 2024-09-26 15:35 UTC

This package is auto-updated.

Last update: 2024-09-26 15:35:50 UTC


README

概述

Fattura Elettronica PHP 库 提供了与 Fattura Elettronica 服务交互的客户端。它允许您发送发票并处理 Fattura Elettronica API 的传入 webhook。

安装

  1. 安装依赖项

确保已安装 GuzzleHTTP。您可以在您的 composer.json 中包含它或运行

composer require guzzlehttp/guzzle
  1. 安装库

通过在 composer.json 中要求它或在运行中安装此库

composer require brokenice/fattura-elettronica

使用方法

发送发票

要发送发票,您需要创建一个 FatturaElettronicaClient 的实例并调用 sendInvoice 方法。

示例

<?php

require __DIR__ . '/vendor/autoload.php'; // Adjust the path as necessary

use Brokenice\FatturaElettronica\FatturaElettronicaClient;

// Instantiate the client
$fatturaClient = new FatturaElettronicaClient('your_email@example.com', 'your_password', 'your_signature');

try {
    // Call the sendInvoice method with XML content
    $fatturaClient->sendInvoice('<xml>...</xml>');
    echo 'Invoice sent successfully.';
} catch (Exception $e) {
    // Handle exceptions
    echo 'An error occurred: ' . $e->getMessage();
}

处理 webhook

要处理传入 webhook,您需要创建一个扩展 FatturaElettronicaWebhook 的类并实现 received 方法。

<?php

namespace Brokenice\Pulse\Http\Controllers\Invoices;

use Brokenice\FatturaElettronica\FatturaElettronicaWebhook as BaseWebhook;
use Brokenice\FatturaElettronica\FileSdI;

class FatturaElettronicaWebhook extends BaseWebhook
{
    public function received(FileSdI $fileSdI): void
    {
        // Handle the received FileSdI
        echo 'File received successfully: ' . $fileSdI;
    }
    
    public function failed(Exception $exception): void
    {
        // Handle webhook failed
    }

    public function signatureSecret(): string
    {
        return 'mysignaure';
    }
}

定义路由(Laravel 示例)

use Brokenice\Pulse\Http\Controllers\Invoices\FatturaElettronicaWebhook;

Route::post('/webhook', [FatturaElettronicaWebhook::class, 'handle']);

类参考

FatturaElettronicaClient

  • 属性
    • public string $endPoint - Fattura Elettronica 服务的端点 URL。
    • public string $email - 用于身份验证的电子邮件。
    • public string $password - 用于身份验证的密码。
    • public string $signature - 用于身份验证的签名。
  • 方法
    • public function __construct(string $email, string $password, string $signature) - 初始化凭据的构造函数。
    • public function sendInvoice(string $xml): void - 以 XML 格式发送发票。

FatturaElettronicaWebhook

  • 方法
    • public function handle(): ?bool - 处理传入 webhook 请求。
    • public abstract function received(FileSdI $fileSdI): void - 处理接收到的 FileSdI 对象的抽象方法。
  • FileSdI
    • 属性
      • public $IdentificativoSdI - SDI 的标识符。
    • 方法
      • public function __construct(\StdClass $parametersIn = null) - 使用参数初始化的构造函数。
      • public function __toString() - 对象的字符串表示。

错误处理

在 API 请求或 webhook 处理期间抛出的异常可以被捕获和处理。请确保在生产环境中正确处理异常以优雅地管理错误。

许可证

此库采用 MIT 许可证。有关详细信息,请参阅 LICENSE。

请根据您的具体配置和要求调整路径、详细信息和示例。此 README 应该为用户提供一个清晰且实用的指南,以便开始使用您的库。