lighth7015/cashier-authorize-dot-net

A Laravel Cashier 接口,用于 Authorize.net 的订阅原生计费服务。

1.0.1 2017-03-06 16:23 UTC

This package is auto-updated.

Last update: 2024-09-17 18:43:23 UTC


README

简介

此包提供了一种兼容、表达性和流畅的接口,用于与 Authorize.net 的原生订阅计费服务进行交互。它处理(主要是)您否则必须自行管理的所有样板化订阅计费代码。除了基本的订阅管理外,此包还可以处理取消宽限期。

基本设置

请阅读以下内容以了解基本设置。

Composer 设置

composer require lighth7015/cashier-authorize-dot-net

.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"

配置

以下文件需要更新,如下所示。 config/services.php 需要更新以匹配您的用户账户的类名。

config/cashier.php

'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

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

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

基本用法

与 Authorize.net 相比,与其他服务(例如 Stripe)有一些不同;首先,Authorize.net 稍慢,并且是一个更受限的订阅提供商。您无法在不首先取消并创建新订阅的情况下执行某些操作(例如交换订阅、更改订阅数量)。

Laravel\CashierAuthorize\Billable 接口

interface Billable
{
	/**
	 * Charge a customer once.
	 *
	 * @param  int  $amount
	 * @param  array  $options
	 * @param  string $transactionType
	 *
	 * @return Charge
	 * @throws Error\Card
	 */
	public function charge(float $amount, array $options = [], string $transactionType = "authCaptureTransaction");

	/**
	 * Determines if the customer currently has a card on file.
	 *
	 * @return bool
	 */
	public function hasCardOnFile();

	/**
	 * Invoice the customer for the given amount.
	 *
	 * @param  string  $description
	 * @param  int  $amount
	 * @param  array  $options
	 * @return bool
	 *
	 * @throws \Stripe\Error\Card
	 */
	public function invoiceFor($description, $amount, array $options = []);

	/**
	 * Begin creating a new subscription.
	 *
	 * @param  string  $subscription
	 * @param  string  $plan
	 * @return \Laravel\CashierAuthorizeNet\SubscriptionBuilder
	 */
	public function newSubscription($subscription, $plan);

	/**
	 * Determine if the user is on trial.
	 *
	 * @param  string  $subscription
	 * @param  string|null  $plan
	 * @return bool
	 */
	public function onTrial(string $subscription = 'default', $plan = null);

	/**
	 * Determine if the user is on a "generic" trial at the user level.
	 *
	 * @return bool
	 */
	public function onGenericTrial();

	/**
	 * Determine if the user has a given subscription.
	 *
	 * @param  string  $subscription
	 * @param  string|null  $plan
	 * @return bool
	 */
	public function subscribed($subscription = 'default', $plan = null);

	/**
	 * Get a subscription instance by name.
	 *
	 * @param  string  $subscription
	 * @return \Laravel\Cashier\Subscription|null
	 */
	public function subscription($subscription = 'default');
	
	/**
	 * Get all of the subscriptions for the user.
	 *
	 * @return \Illuminate\Database\Eloquent\Collection
	 */
	public function subscriptions();
	
	/**
	 * Get the entity's upcoming invoice.
	 *
	 * @return \Laravel\Cashier\Invoice|null
	 */
	public function upcomingInvoice();
	
	/**
	 * Find an invoice by ID.
	 *
	 * @param  string  $id
	 * @return \Laravel\Cashier\Invoice|null
	 */
	public function findInvoice($invoiceId);
	
	/**
	 * Find an invoice or throw a 404 error.
	 *
	 * @param  string  $id
	 * @return \Laravel\Cashier\Invoice
	 */
	public function findInvoiceOrFail($id);
	
	/**
	 * Create an invoice download Response.
	 *
	 * @param  string  $id
	 * @param  array   $data
	 * @param  string  $storagePath
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function downloadInvoice($id, array $data, $storagePath = null);
	
	/**
	 * Get a subcription from Authorize
	 *
	 * @param  string $subscriptionId
	 * @return array
	 */
	public function getSubscriptionFromAuthorize($subscriptionId);

	/**
	 * Get a collection of the entity's invoices.
	 *
	 * @param  string  $plan
	 * @return \Illuminate\Support\Collection
	 */
	public function invoices($plan);
	
	/**
	 * Update customer's credit card.
	 *
	 * @param  string  $token
	 * @return void
	 */
	public function updateCard($card);
	
	/**
	 * Determine if the user is actively subscribed to one of the given plans.
	 *
	 * @param  array|string  $plans
	 * @param  string  $subscription
	 * @return bool
	 */
	public function subscribedToPlan($plans, $subscription = 'default');
	
	/**
	 * Determine if the entity is on the given plan.
	 *
	 * @param  string  $plan
	 * @return bool
	 */
	public function onPlan($plan);
	
	/**
	 * Determine if the entity has a Stripe customer ID.
	 *
	 * @return bool
	 */
	public function hasAuthorizeId() { return !!$this->authorize_id; }

	/**
	 * Create a Stripe customer for the given user.
	 *
	 * @param  array $creditCardDetails
	 * @return StripeCustomer
	 */
	public function createAsAuthorizeCustomer(Array $location, array $cardDetails);
	
	/**
	 * Delete an Authorize.net Profile
	 *
	 * @return
	 */
	public function deleteAuthorizeProfile();
	
	/**
	 * Get the Stripe supported currency used by the entity.
	 *
	 * @return string
	 */
	public function preferredCurrency();

	/**
	 * Get the tax percentage to apply to the subscription.
	 *
	 * @return int
	 */
	public function taxPercentage();

	/**
	 * Detect the brand cause Authorize wont give that to us
	 *
	 * @param  string $card Card number
	 * @return string
	 */
	public function cardBrandDetector($card);
}

交易详情

启用 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 的 API 使用 SOAP 结构,因此需要在将具有信用卡的客户添加到其系统以及将订阅添加到该用户之间添加时间延迟。这可以通过在您的应用程序中让用户输入他们的信用卡信息,然后允许用户执行确认他们想要购买的订阅的另一个操作来完成。此时间可以短至一秒,但迄今为止所有立即添加订阅的测试均失败,因此请在设计应用程序时注意此限制。

许可

Laravel Cashier-Authorize 是开源软件,根据 MIT 许可 许可。

新文档