webwizo / cashier-authorizenet

Laravel Cashier-Authorize为Authorize.net的订阅计费服务提供了一个表达性、流畅的接口。

v1.1 2020-12-15 11:57 UTC

This package is auto-updated.

Last update: 2024-09-15 19:50:09 UTC


README

简介

Laravel Cashier-Authorize为Authorize.net的订阅计费服务提供了一个表达性、流畅的接口。它处理了您不希望编写的几乎所有样板订阅计费代码。除了基本的订阅管理外,Cashier-Authorize还可以处理取消宽限期,甚至生成发票PDF。

基本设置

请阅读以下基本设置。

.env

ADN_ENV= ADN_LOG=authorize.log

ADN_API_LOGIN_ID= ADN_TRANSACTION_KEY= ADN_SECRET_KEY=Simon

ADN_ENV应设置为:sandbox(沙盒)、production(生产)

迁移

您需要创建包含以下内容的迁移

Schema::table('users', function ($table) {
    $table->string('authorize_id')->nullable();
    $table->string('authorize_payment_id')->nullable();
    $table->string('card_brand')->nullable();
    $table->string('card_last_four')->nullable();
});
Schema::create('subscriptions', function ($table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->string('name');
    $table->string('authorize_id');
    $table->string('authorize_payment_id');
    $table->text('metadata');
    $table->string('authorize_plan');
    $table->integer('quantity');
    $table->timestamp('trial_ends_at')->nullable();
    $table->timestamp('ends_at')->nullable();
    $table->timestamps();
});

发布

您需要发布此包的资产。

php artisan vendor:publish --provider="Laravel\CashierAuthorizeNet\CashierServiceProvider"

配置

以下是具有Authorize.net兼容性的订阅示例配置。您可以在该配置中定义您的订阅。

'monthly-10-1' => [
    'name' => 'main',
    'interval' => [
        'length' => 1, // number of instances for billing
        'unit' => 'months' //months, days, years
    ],
    'total_occurances' => 9999, // 9999 means without end date
    'trial_occurances' => 0,
    'amount' => 9.99,
    'trial_amount' => 0,
    'trial_days' => 0,
    'trial_delay' => 0, // days you wish to delay the start of billing
]

'config/services.php'

您需要将以下内容添加到您的'config/services.php'文件中,请确保模型与您的应用程序的User类匹配

'authorize' => [
    'model'  => App\User::class,
],

您还可以使用以下.env变量设置此值:ADN_MODEL

基本用法

与Stripe等服务相比,Authorize.net的订阅提供商稍慢且限制更多。这意味着您不能像切换订阅或更改订阅数量那样执行某些操作。您需要取消并创建新的订阅来处理这些变化。

您可以进行以下操作

用户:

  • charge($amount, array $options = [])
  • hasCardOnFile
  • newSubscription($subscription, $plan)
  • onTrial($subscription = 'default', $plan = null)
  • onGenericTrial()
  • subscribed($subscription = 'default', $plan = null)
  • subscription($subscription = 'default')
  • subscriptions()
  • updateCard($card) // $card = ['number' => '', 'expriation' => '']
  • subscribedToPlan($plans, $subscription = 'default')
  • onPlan($plan)
  • hasAuthorizeId()
  • createAsAuthorizeCustomer($creditCardDetails)
  • upcomingInvoice($plan)
  • findInvoice($invoiceId)
  • findInvoiceOrFail($id)
  • downloadInvoice($id, array $data, $storagePath = null)
  • getSubscriptionFromAuthorize($subscriptionId)
  • invoices($plan)
  • deleteAuthorizeProfile()
  • preferredCurrency()
  • taxPercentage() (此方法应添加到User模型中并定义该用户的税率,例如:return 10;)

交易详情

启用API以启用交易详情API

  1. 登录https://account.authorize.net上的商户界面。
  2. 在左侧主菜单下选择“设置”。
  3. 在安全设置部分单击“交易详情API”链接。打开交易详情API屏幕。
  4. 如果您尚未启用交易详情API,请输入您的秘密问题答案,然后单击“启用交易详情API”。
  5. 成功启用交易详情API后,设置页面将显示。

CRON作业

您需要启用以下CRON作业来检查您用户订阅的状态。这可以按您喜欢的频率运行,并会确认您的用户订阅是否处于活动状态。如果状态被更改为客户已取消或暂停 - 系统将本地禁用其订阅。您的团队需要与Authorize.net解决支付问题,然后继续前进。

protected $commands = [
    \Laravel\CashierAuthorizeNet\Console\SubscriptionUpdates::class,
];
$schedule->command('subscription:update')->hourly();

限制

另一个限制是时间相关的。由于Authorize.net使用SOAP结构为其API,在将具有信用卡的客户添加到其系统和向该用户添加订阅之间需要一定的时间延迟。这可以通过让用户输入他们的信用卡信息,并允许确认他们希望购买的订阅作为另一个操作来轻松地在您的应用中完成。这个时间可能短至一秒,但迄今为止所有立即添加订阅的测试都未能成功,因此在设计您的应用时请注意此限制。

许可

Laravel Cashier-Authorize是开源软件,根据MIT许可授权 "# cashier-authorize"。