定制 c-delouvencourt/pennylane-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

c-delouvencourt/pennylane-laravel

Composer 安装命令:

composer require c-delouvencourt/pennylane-laravel

包简介

Pennylane API wrapper for Laravel 11

README 文档

README

Latest Version on Packagist

A comprehensive Laravel wrapper for the Pennylane API v2. Covers all ~127 endpoints with typed Response DTOs, cursor-based pagination, OAuth 2.0 support, and automatic retry.

Built on Laravel's HTTP client (Illuminate\Http\Client).

Requirements: PHP 8.1+, Laravel 8+

Installation

composer require c-delouvencourt/laravel-pennylane

Publish the configuration file:

php artisan vendor:publish --provider="CLDT\PennylaneLaravel\PennylaneLaravelServiceProvider" --tag="config"

Authentication

Bearer token (default)

PENNYLANE_API_KEY=your_api_key

OAuth 2.0

PENNYLANE_AUTH_METHOD=oauth2
PENNYLANE_OAUTH_CLIENT_ID=your_client_id
PENNYLANE_OAUTH_CLIENT_SECRET=your_client_secret
PENNYLANE_OAUTH_REDIRECT_URI=https://your-app.com/callback
PENNYLANE_OAUTH_TOKEN=your_access_token
PENNYLANE_OAUTH_REFRESH_TOKEN=your_refresh_token

Retry

All HTTP requests are automatically retried on recoverable failures (5xx server errors, 429 rate limiting, and connection timeouts). Client errors (400, 401, 403, 404, 422) are not retried since they would never succeed. You can configure this behavior via environment variables:

PENNYLANE_RETRY_TIMES=3      # Number of attempts (default: 3)
PENNYLANE_RETRY_SLEEP=500     # Delay in ms between attempts (default: 500)
PENNYLANE_RETRY_THROW=true    # Throw exception after all attempts fail (default: true)

Or directly in config/pennylane-laravel.php:

'retry' => [
    'times' => 3,
    'sleep' => 500,
    'throw' => true,
],

Error Handling

All API calls throw typed exceptions on failure. Every exception extends PennylaneApiException, so you can catch specific errors or handle them all at once:

use CLDT\PennylaneLaravel\Exceptions\PennylaneApiException;
use CLDT\PennylaneLaravel\Exceptions\PennylaneNotFoundException;
use CLDT\PennylaneLaravel\Exceptions\PennylaneBadRequestException;
use CLDT\PennylaneLaravel\Exceptions\PennylaneAuthenticationException;

try {
    $invoice = $pennylane->customerInvoices()->get(999);
} catch (PennylaneNotFoundException $e) {
    // 404 — resource not found
    $e->getStatusCode();    // 404
    $e->getErrorMessage();  // Error message from the API
    $e->getResponseBody();  // Full decoded JSON response
} catch (PennylaneBadRequestException $e) {
    // 400 — includes structured error details
    $e->getField();          // ?string — the field that caused the error
    $e->getBadRequestCode(); // ?BadRequestCode enum — e.g. BadRequestCode::NotExistRequiredKey
    $e->getPayload();        // ?string
} catch (PennylaneAuthenticationException $e) {
    // 401 — invalid or missing token
} catch (PennylaneApiException $e) {
    // Catch-all for any other API error (403, 422, 429, 5xx...)
    $e->getStatusCode();
    $e->getErrorMessage();
}

Exception hierarchy:

Exception HTTP Status
PennylaneBadRequestException 400
PennylaneAuthenticationException 401
PennylaneAuthorizationException 403
PennylaneNotFoundException 404
PennylaneValidationException 422
PennylaneRateLimitException 429
PennylaneServerException 5xx
PennylaneApiException Any other non-2xx

All exceptions provide: getStatusCode(), getErrorMessage(), getResponseBody(), getResponse().

Usage

All methods return typed Response DTOs with readonly properties. List endpoints return a PaginatedResponse with cursor-based pagination.

Pagination

use CLDT\PennylaneLaravel\PennylaneLaravel;

$result = app(PennylaneLaravel::class)->customers()->list();

$result->items;       // array of CustomerResponse
$result->has_more;    // bool
$result->next_cursor; // ?string

// Fetch next page
$nextPage = app(PennylaneLaravel::class)->customers()->list(['cursor' => $result->next_cursor]);

Available Resources

Customers

$pennylane = app(PennylaneLaravel::class);

// List all customers (company + individual)
$customers = $pennylane->customers()->list();

// Get a customer
$customer = $pennylane->customers()->get('123');

// Customer categories
$categories = $pennylane->customers()->categories('123');
$pennylane->customers()->updateCategories('123', $data);

// Customer contacts
$contacts = $pennylane->customers()->contacts('123');

Company Customers

$customer = $pennylane->companyCustomers()->create([
    'name' => 'Acme Corp',
    'emails' => ['contact@acme.com'],
    'billing_address' => [
        'address' => '1 rue de la Paix',
        'postal_code' => '75001',
        'city' => 'Paris',
        'country_alpha2' => 'FR',
    ],
]);

$customer = $pennylane->companyCustomers()->get('123');
$customer = $pennylane->companyCustomers()->update('123', $data);

Individual Customers

$customer = $pennylane->individualCustomers()->create([
    'first_name' => 'John',
    'last_name' => 'Doe',
    'emails' => ['john@example.com'],
]);

$customer = $pennylane->individualCustomers()->get('123');
$customer = $pennylane->individualCustomers()->update('123', $data);

Products

$products = $pennylane->products()->list();
$product = $pennylane->products()->get('1');

$product = $pennylane->products()->create([
    'label' => 'Product 1',
    'unit' => 'piece',
    'price_before_tax' => '10.00',
    'price' => '12.00',
    'vat_rate' => 'FR_200',
    'currency' => 'EUR',
]);

$product = $pennylane->products()->update('1', ['description' => 'Updated']);

Customer Invoices

// List & CRUD
$invoices = $pennylane->customerInvoices()->list();
$invoice = $pennylane->customerInvoices()->create($data);
$invoice = $pennylane->customerInvoices()->get(1);
$invoice = $pennylane->customerInvoices()->update(1, $data);
$pennylane->customerInvoices()->delete(1);

// Actions
$invoice = $pennylane->customerInvoices()->finalize(1);
$invoice = $pennylane->customerInvoices()->markAsPaid(1);
$pennylane->customerInvoices()->sendByEmail(1, ['emails' => ['client@example.com']]);
$invoice = $pennylane->customerInvoices()->linkCreditNote(1, $data);
$invoice = $pennylane->customerInvoices()->createFromQuote($data);
$invoice = $pennylane->customerInvoices()->updateImported(1, $data);

// Import with file upload (multipart/form-data)
$invoice = $pennylane->customerInvoices()->import(
    fields: ['create_customer' => true],
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/invoice.pdf'), 'filename' => 'invoice.pdf'],
    ],
);

// Sub-resources
$sections = $pennylane->customerInvoices()->invoiceLineSections(1);
$lines = $pennylane->customerInvoices()->invoiceLines(1);
$payments = $pennylane->customerInvoices()->payments(1);
$matched = $pennylane->customerInvoices()->matchedTransactions(1);
$pennylane->customerInvoices()->matchTransaction(1, $data);
$pennylane->customerInvoices()->unmatchTransaction(1, 5);
$appendices = $pennylane->customerInvoices()->appendices(1);

// Upload appendix (multipart/form-data)
$pennylane->customerInvoices()->uploadAppendix(1,
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/appendix.pdf'), 'filename' => 'appendix.pdf'],
    ],
);

$categories = $pennylane->customerInvoices()->categories(1);
$pennylane->customerInvoices()->updateCategories(1, $data);
$fields = $pennylane->customerInvoices()->customHeaderFields(1);

Supplier Invoices

$invoices = $pennylane->supplierInvoices()->list();
$invoice = $pennylane->supplierInvoices()->get(1);
$invoice = $pennylane->supplierInvoices()->update(1, $data);
$invoice = $pennylane->supplierInvoices()->validateAccounting(1);

// Import with file upload (multipart/form-data)
$invoice = $pennylane->supplierInvoices()->import(
    fields: ['create_supplier' => true],
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/invoice.pdf'), 'filename' => 'invoice.pdf'],
    ],
);

// Sub-resources
$lines = $pennylane->supplierInvoices()->invoiceLines(1);
$payments = $pennylane->supplierInvoices()->payments(1);
$pennylane->supplierInvoices()->updatePaymentStatus(1, $data);
$matched = $pennylane->supplierInvoices()->matchedTransactions(1);
$pennylane->supplierInvoices()->matchTransaction(1, $data);
$pennylane->supplierInvoices()->unmatchTransaction(1, 5);
$pennylane->supplierInvoices()->linkPurchaseRequest(1, $data);
$categories = $pennylane->supplierInvoices()->categories(1);
$pennylane->supplierInvoices()->updateCategories(1, $data);

Quotes

$quotes = $pennylane->quotes()->list();
$quote = $pennylane->quotes()->create($data);
$quote = $pennylane->quotes()->get(1);
$quote = $pennylane->quotes()->update(1, $data);
$quote = $pennylane->quotes()->updateStatus(1, ['status' => 'accepted']);
$pennylane->quotes()->sendByEmail(1, $data);

// Sub-resources
$lines = $pennylane->quotes()->invoiceLines(1);
$sections = $pennylane->quotes()->invoiceLineSections(1);
$appendices = $pennylane->quotes()->appendices(1);
$pennylane->quotes()->uploadAppendix(1, $data);

Suppliers

$suppliers = $pennylane->suppliers()->list();
$supplier = $pennylane->suppliers()->create($data);
$supplier = $pennylane->suppliers()->get(1);
$supplier = $pennylane->suppliers()->update(1, $data);
$categories = $pennylane->suppliers()->categories(1);
$pennylane->suppliers()->updateCategories(1, $data);

Billing Subscriptions

$subscriptions = $pennylane->billingSubscriptions()->list();
$subscription = $pennylane->billingSubscriptions()->create($data);
$subscription = $pennylane->billingSubscriptions()->get(1);
$subscription = $pennylane->billingSubscriptions()->update(1, $data);
$sections = $pennylane->billingSubscriptions()->invoiceLineSections(1);
$lines = $pennylane->billingSubscriptions()->invoiceLines(1);

Bank Accounts & Transactions

// Bank accounts
$accounts = $pennylane->bankAccounts()->list();
$account = $pennylane->bankAccounts()->create($data);
$account = $pennylane->bankAccounts()->get(1);

// Bank establishments
$establishments = $pennylane->bankEstablishments()->list();

// Transactions
$transactions = $pennylane->transactions()->list();
$transaction = $pennylane->transactions()->create($data);
$transaction = $pennylane->transactions()->get(1);
$transaction = $pennylane->transactions()->update(1, $data);
$categories = $pennylane->transactions()->categories(1);
$pennylane->transactions()->updateCategories(1, $data);
$matched = $pennylane->transactions()->matchedInvoices(1);

Ledger (Accounts, Entries, Lines, Attachments)

// Ledger accounts
$accounts = $pennylane->ledgerAccounts()->list();
$account = $pennylane->ledgerAccounts()->create($data);
$account = $pennylane->ledgerAccounts()->get(1);
$account = $pennylane->ledgerAccounts()->update(1, $data);

// Ledger entries
$entries = $pennylane->ledgerEntries()->list();
$entry = $pennylane->ledgerEntries()->create($data);
$entry = $pennylane->ledgerEntries()->get(1);
$entry = $pennylane->ledgerEntries()->update(1, $data);
$lines = $pennylane->ledgerEntries()->ledgerEntryLines(1);

// Ledger entry lines
$lines = $pennylane->ledgerEntryLines()->list();
$line = $pennylane->ledgerEntryLines()->get(1);
$pennylane->ledgerEntryLines()->letter($data);
$pennylane->ledgerEntryLines()->unletter($data);
$lettered = $pennylane->ledgerEntryLines()->letteredLines(1);
$categories = $pennylane->ledgerEntryLines()->categories(1);
$pennylane->ledgerEntryLines()->updateCategories(1, $data);

// Ledger attachments
$attachments = $pennylane->ledgerAttachments()->list();

// Upload (multipart/form-data)
$pennylane->ledgerAttachments()->upload(
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/attachment.pdf'), 'filename' => 'attachment.pdf'],
    ],
);

Categories & Category Groups

$categories = $pennylane->categories()->list();
$category = $pennylane->categories()->create($data);
$category = $pennylane->categories()->get(1);
$category = $pennylane->categories()->update(1, $data);

$groups = $pennylane->categoryGroups()->list();
$group = $pennylane->categoryGroups()->get(1);
$categories = $pennylane->categoryGroups()->categories(1);

Journals

$journals = $pennylane->journals()->list();
$journal = $pennylane->journals()->create($data);
$journal = $pennylane->journals()->get(1);

Commercial Documents

$documents = $pennylane->commercialDocuments()->list();
$document = $pennylane->commercialDocuments()->get(1);
$appendices = $pennylane->commercialDocuments()->appendices(1);
$pennylane->commercialDocuments()->uploadAppendix(1, $data);
$lines = $pennylane->commercialDocuments()->invoiceLines(1);
$sections = $pennylane->commercialDocuments()->invoiceLineSections(1);

SEPA & GoCardless Mandates

// SEPA mandates
$mandates = $pennylane->sepaMandates()->list();
$mandate = $pennylane->sepaMandates()->create($data);
$mandate = $pennylane->sepaMandates()->get(1);
$mandate = $pennylane->sepaMandates()->update(1, $data);
$pennylane->sepaMandates()->delete(1);

// GoCardless mandates
$mandates = $pennylane->gocardlessMandates()->list();
$mandate = $pennylane->gocardlessMandates()->get(1);
$pennylane->gocardlessMandates()->sendMailRequest($data);
$pennylane->gocardlessMandates()->cancel(1);
$associations = $pennylane->gocardlessMandates()->associations(1);

// Pro account mandates
$mandates = $pennylane->proAccountMandates()->list();

Exports

$export = $pennylane->exports()->createAnalyticalGeneralLedger($data);
$status = $pennylane->exports()->getAnalyticalGeneralLedger(1);

$export = $pennylane->exports()->createFec($data);
$status = $pennylane->exports()->getFec(1);

File Attachments

$files = $pennylane->fileAttachments()->list();

// Upload (multipart/form-data)
$file = $pennylane->fileAttachments()->upload(
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/document.pdf'), 'filename' => 'document.pdf'],
    ],
);

Purchase Requests

$requests = $pennylane->purchaseRequests()->list();
$request = $pennylane->purchaseRequests()->get(1);
$request = $pennylane->purchaseRequests()->update(1, $data);
$pennylane->purchaseRequests()->import($data);

Other Resources

// Users
$user = $pennylane->users()->create($data);
$user = $pennylane->users()->find(['email' => 'user@example.com']);
$user = $pennylane->users()->update(1, $data);
$me = $pennylane->users()->me();
// Or directly:
$me = $pennylane->me();

// Companies
$pennylane->companies()->create($data);
$pennylane->companies()->completeRegistration(1, $data);

// Webhooks
$webhook = $pennylane->webhooks()->get();

// Customer invoice templates
$templates = $pennylane->customerInvoiceTemplates()->list();

// E-Invoices - Import with file upload (multipart/form-data)
$pennylane->eInvoices()->import(
    fields: ['create_customer' => true],
    attachments: [
        ['name' => 'file', 'contents' => file_get_contents('/path/to/invoice.pdf'), 'filename' => 'invoice.pdf'],
    ],
);

// Fiscal years
$years = $pennylane->fiscalYears()->list();

// Trial balance
$balance = $pennylane->trialBalance()->get(['start_date' => '2024-01-01', 'end_date' => '2024-12-31']);

// Changelogs
$changes = $pennylane->changelogs()->customerInvoices(['since' => '2024-01-01']);
$changes = $pennylane->changelogs()->supplierInvoices($params);
$changes = $pennylane->changelogs()->customers($params);
$changes = $pennylane->changelogs()->suppliers($params);
$changes = $pennylane->changelogs()->products($params);
$changes = $pennylane->changelogs()->ledgerEntryLines($params);
$changes = $pennylane->changelogs()->transactions($params);
$changes = $pennylane->changelogs()->quotes($params);

Migrating from v2.0 to v2.1

HTTP client

The underlying HTTP client has been migrated from GuzzleHTTP to Laravel's HTTP facade (Illuminate\Http\Client). This change is transparent for most usages, but if you were injecting GuzzleHttp\ClientInterface directly, you need to update to Illuminate\Http\Client\PendingRequest.

File uploads

The import() and upload() methods now use multipart/form-data and accept two parameters instead of one:

Method Before After
customerInvoices()->import() import(array $data) import(array $fields = [], array $attachments = [])
customerInvoices()->uploadAppendix() uploadAppendix(int $id, array $data) uploadAppendix(int $id, array $fields = [], array $attachments = [])
supplierInvoices()->import() import(array $data) import(array $fields = [], array $attachments = [])
fileAttachments()->upload() upload(array $data) upload(array $fields = [], array $attachments = [])
ledgerAttachments()->upload() upload(array $data) upload(array $fields = [], array $attachments = [])
eInvoices()->import() import(array $data) import(array $fields = [], array $attachments = [])

Each attachment is an array with the following keys:

['name' => 'file', 'contents' => $fileContents, 'filename' => 'invoice.pdf']

Error handling

API errors now throw typed exceptions (PennylaneApiException and subclasses) instead of silently returning partial data. If your code relied on checking response arrays for errors, wrap your calls in try/catch blocks instead. See the Error Handling section.

Retry

Automatic retry is now configured by default (3 attempts, 500ms delay) and only retries recoverable errors (5xx, 429, connection failures). Client errors (4xx) are thrown immediately. See the Retry section to customize.

Dependencies

  • Removed: guzzlehttp/guzzle
  • Added: illuminate/http

Migrating from v1 to v2

v1 v2 Notes
invoices() customerInvoices() invoices() still works as deprecated alias
estimates() quotes() estimates() still works as deprecated alias
enums()->get('unit') Removed Use PHP enums in Dto\Enums\* instead
Array returns DTO returns All methods return typed Response DTOs
Offset pagination Cursor pagination Use PaginatedResponse with next_cursor
PENNYLANE_API_KEY PENNYLANE_API_KEY Same env var, new config structure

Response DTOs

All responses use readonly properties and can be created from arrays:

use CLDT\PennylaneLaravel\Dto\Responses\ProductResponse;

$product = $pennylane->products()->get('1');
$product->id;                // int
$product->label;             // string
$product->price_before_tax;  // ?string
$product->currency;          // ?string
$product->created_at;        // ?string

PHP Enums

All API enum values are available as PHP 8.1 backed enums:

use CLDT\PennylaneLaravel\Dto\Enums\InvoiceStatus;
use CLDT\PennylaneLaravel\Dto\Enums\Currency;
use CLDT\PennylaneLaravel\Dto\Enums\VatRate;
use CLDT\PennylaneLaravel\Dto\Enums\PaymentConditions;

InvoiceStatus::Draft->value;          // 'draft'
Currency::EUR->value;                 // 'EUR'
VatRate::FR200->value;                // 'FR_200'
PaymentConditions::Days30->value;     // '30_days'

Available enums: AccountType, BadRequestCode, BillingSubscriptionMode, BillingSubscriptionOccurrenceRuleType, BillingSubscriptionPaymentMethod, BillingSubscriptionStatus, CategoryDirection, CommercialDocumentType, Currency, CustomerBillingLanguage, DiscountType, ExportStatus, InvoiceAccountingStatus, InvoicePaymentStatus, InvoiceStatus, MandateStatus, PaymentConditions, PaymentStatus, ProductUnit, PurchaseRequestStatus, QuoteStatus, SepaSequenceType, SupplierDueDateRule, SupplierPaymentMethod, VatRate.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email clement@meilleursbiens.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

c-delouvencourt/pennylane-laravel 适用场景与选型建议

c-delouvencourt/pennylane-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.73k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「invoices」 「customers」 「Pennylane」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 c-delouvencourt/pennylane-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 c-delouvencourt/pennylane-laravel 我们能提供哪些服务?
定制开发 / 二次开发

基于 c-delouvencourt/pennylane-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-08