pcoelho/laravel-asaas

Asaas Http API 的 Laravel 封装

1.0.0-beta.1 2022-07-06 14:27 UTC

This package is auto-updated.

Last update: 2024-09-06 23:10:58 UTC


README

Total Downloads Latest Stable Version Latest Unstable Version License

安装

您可以通过 composer 安装此包

composer require "pcoelho/laravel-asaas"

配置包

您可以使用以下命令发布配置文件:

php artisan vendor:publish --provider="Laravel\Asaas\AsaasServiceProvider"

这是配置文件的内容

<?php

use App\Packages\Asaas\Job\DefaultWebhookProcessorJob;

return [

    /*
    |-------------------------------------------------------------------------
    | Access token
    |-------------------------------------------------------------------------
    |
    | This value is the access token needed to make request to
    | Asaas's Http Api.
    */
    "access_token" => env('ASAAS_TOKEN', ''),

    /*
    |-------------------------------------------------------------------------
    | Is Sandbox environment
    |-------------------------------------------------------------------------
    |
    | This value indicates if you want to request to sanbox environment
    | (TRUE) or production environment.
    |
    */
    "is_sandbox" => env('ASAAS_IS_SANDBOX', ''),

    /*
    |--------------------------------------------------------------------------
    | Sandbox URL
    |--------------------------------------------------------------------------
    |
    | This value is only used when "is_sandbox" is TRUE,
    | it indicate the sandbox url.
    */
    "sandbox_url" => env('ASAAS_SANDBOX_URL', ''),

    /*
    |--------------------------------------------------------------------------
    | Production URL
    |--------------------------------------------------------------------------
    |
    | This value is only used when "is_sandbox" is FALSE,
    | it indicate the production url.
    */
    "production_url" => env('ASAAS_PRODUCTION_URL', ''),

    /*
    |--------------------------------------------------------------------------
    | Webhook config
    |--------------------------------------------------------------------------
    |
    */
    "webhook" => [
        'processor' => DefaultWebhookProcessorJob::class
    ]
];

现在,您需要使用 Asaas 账户中的数据设置环境变量。

  • ASAAS_TOKEN - 您账户的 API 密钥。
  • ASAAS_IS_SANDBOX - 是否为沙箱环境。
  • ASAAS_SANDBOX_URL - 沙箱 URL。
  • ASAAS_PRODUCTION_URL - 生产 URL(仅在 ASAAS_IS_SANDBOXfalse 时使用)。

用法

目前,您可以管理客户和订阅。如下所示。

客户

您可以创建、查找或更新客户。

创建

  • 参数 - CustomerDto
  • 返回 - CustomerDto
$customer = Asaas::customer()->create(new CustomerDto(
    name: '<example>',
    cpf: '<example>'
))

查找

  • 参数 - CustomerDto
  • 返回 - CustomerDto
$customer = Asaas::customer()->find(new CustomerDto(
    name: '<example>',
    cpf: '<example>'
))

更新客户

  • 参数 - CustomerDto
  • 返回 - CustomerDto
$customer = Asaas::customer()->updateCustomer(new CustomerDto(
    name: '<example>',
    cpf: '<example>'
))

订阅

创建

  • 参数 - RecurrenceDto
  • 返回 - RecurrenceDto
$subscription = Asaas::subscription()->create(new RecurrenceDto(
    customerId: $customer->external_code,
    billingType: RecurrenceDto::CREDIT_CARD,
    value: $plan->price,
    description: $plan->description,
    cycle: RecurrenceDto::CYCLE_MONTHLY,
    creditCardHolderName: $card->holder,
    creditCardNumber: $card->number,
    creditCardExpiryMonth: $card->expiration_month,
    creditCardExpiryYear: $card->expiration_year,
    creditCardCvv: $card->cvv,
    creditCardHolderInfoName: $card->holder,
    creditCardHolderInfoEmail: $card->holder_email,
    creditCardHolderInfoCpfCnpj: $card->holder_document,
    creditCardHolderInfoPostalCode: $card->holder_postal_code,
    creditCardHolderInfoAddressNumber: $card->holder_address_number,
    creditCardHolderInfoPhone: $card->holder_phone,
    nextDueDate: Carbon::now()->format("Y-m-d"),
    remoteIp: Request::ip(),
))

删除

  • 参数 - string
  • 返回 - RecurrenceDto
$subscription = Asaas::subscription()->delete('<asaas-id-subscription>')

Webhook

要注册 webhook 路由,您需要添加以下路由:

Route::asaasWebhook();

幕后,这将注册一个名为 /webhook/asaasPOST 路由到控制器。然后,您必须将路由添加到 VerifyCsrfToken 中间件的 except 数组中。

protected $except = [
    '/webhook/asaas',
];

错误处理

可能抛出的异常有:

  • BadRequestException

    状态码:400

    在此异常中,您可以调用 $e->errors() 来查看更多关于错误的信息,查看哪些字段无效或缺失。

  • NotFoundException

    状态码:404

  • RequestForbiddenException

    状态码:403

  • ServerErrorException

    状态码:500

  • TooManyRequestsException

    状态码:429

  • UnauthorizedException

    状态码:401

许可证

MIT 许可证 (MIT)。