Laravel開發:如何使用Laravel Cashier和Authorize.net處理訂閱付款?

隨著電子商務的不斷發展,訂閱付款模式越來越受歡迎。laravel cashier是一個基于laravel框架的付款工具,它使得管理訂閱和收取付款變得非常簡單。 本文將介紹如何在laravel應用程序中使用laravel cashier以及authorize.net來處理訂閱付款。

  1. 安裝Laravel Cashier

在開始之前,需要確保你已經安裝了Laravel框架和Composer包管理器。在終端輸入以下命令來安裝Laravel Cashier:

composer require laravel/cashier

一旦它安裝成功,你需要為Cashier生成遷移表。可以在終端運行以下命令:

php artisan migrate

這將為付款相關的Cashier表生成所需的數據庫遷移文件。

  1. 配置Authorize.net API

在使用Authorize.net處理付款之前,需要安裝該服務并獲取API憑據(API Login ID和Transaction Key)。

可以通過以下步驟執行這些操作:

  • 前往https://www.authorize.net/注冊一個賬號
  • 登錄后,在左側菜單中選擇“Account”,再選擇“Settings”
  • 在“Security Settings”中,選擇“API Credentials & Keys”
  • 點擊“New Transaction Key”,輸入需要的信息,再點擊“Submit”
  • 在彈出窗口中會顯示API Login ID和Transaction Key。請務必復制并妥善保管。
  1. 配置Laravel Cashier

在開始之前,需要配置cashier.php文件中的參數。可以通過以下命令在config文件夾中創建該文件:

php artisan vendor:publish --tag="cashier-config"

接下來,需要在.env文件中設置Authorize.net API相關參數:

CASHIER_ENV=production CASHIER_CURRENCY=usd AUTHORIZE_API_LOGIN_ID=YOUR_API_LOGIN_ID AUTHORIZE_TRANSACTION_KEY=YOUR_TRANSACTION_KEY
  1. 創建訂閱計劃

在使用Laravel Cashier和Authorize.net處理訂閱付款之前,需要創建訂閱計劃。可以通過以下命令來創建訂閱計劃:

php artisan make:model Plan -m

該命令將在app文件夾中創建一個Plan模型,并為其生成遷移表。現在可以打開遷移文件進行編輯,并添加必要的字段。以下是一個可以參考的示例:

Schema::create('plans', function (Blueprint $table) {     $table->bigIncrements('id');     $table->string('name');     $table->string('stripe_id');     $table->string('authorizenet_id');     $table->integer('price');     $table->string('interval');     $table->integer('interval_count');     $table->integer('trial_period_days')->nullable();     $table->timestamps(); });

執行完遷移文件后,需要在數據庫中創建該表。在終端中運行以下命令:

php artisan migrate

接下來,需要在Plan模型中定義必要的屬性和方法。以下是一個示例:

use LaravelCashierSubscription;  class Plan extends Model {     public function subscriptions()     {         return $this->hasMany(Subscription::class);     }      public function getPrice()     {         return $this->price / 100;     }      public function getFormattedPrice()     {         return number_format($this->getPrice(), 2);     }      public function authorizeNetPlan()     {         return AuthorizeNet_Subscription::create([             'name'                   => $this->name,             'intervalLength'         => $this->interval_count,             'intervalUnit'           => $this->interval,             'startDate'              => date('Y-m-d'),             'totalOccurrences'       => '9999',             'trialOccurrences'       => '0',             'amount'                 => $this->price,             'trialAmount'            => '0.00',             'creditCardCardNumber'   => '',             'creditCardExpirationDate' => '',             'creditCardCardCode' => ''         ]);     } }

authorizeNetPlan方法將創建Authorize.net訂閱計劃并返回相關的信息。

  1. 處理訂閱付款

一旦訂閱計劃已經創建,現在就可以向訂閱者發送訂閱鏈接。接下來,訂閱者可以使用Link點擊鏈接進行訂閱付款。

在創建訂閱時,需要設置訂閱計劃和用戶相關信息。

以下是一個示例控制器方法:

public function subscribe(Request $request, Plan $plan) {     $user = $request->user();          $subscription = $user->newSubscription('default', $plan->stripe_id)->create($request->stripeToken);      $authorizeSubscription = $plan->authorizeNetPlan();      $subscription->authorize_net_id = $authorizeSubscription->getSubscriptionId();     $subscription->save();      return redirect()->route('home')->with('success', 'Subscription successful'); }

在該示例中,我們使用newSubscription方法為用戶創建新的訂閱。注意,$request->stripeToken是使用Stripe Checkout生成的令牌。getUserPlan方法在Plan模型中定義,用于獲取當前用戶的訂閱計劃。

在創建訂閱后,我們將創建的Authorize.net訂閱計劃的ID保存到Subscription模型中。

  1. 處理取消訂閱

當用戶想要取消訂閱時,需要執行以下操作:

public function cancel(Request $request) {     $user = $request->user();      $subscription = $user->subscription('default');      $authorizeSubscription = AuthorizeNet_Subscription::cancel($subscription->authorize_net_id);          $subscription->cancel();          return redirect()->route('home')->with('success', 'Subscription cancelled.'); }

在該示例中,我們使用cancel方法取消用戶的Laravel Cashier訂閱計劃,并使用Authorize.net中提供的方法取消訂閱計劃。

總結

使用Laravel Cashier和Authorize.net處理訂閱付款非常簡單。僅需按照以上步驟即可快速設置和實現。Laravel Cashier提供了便捷的付款工具,何不實現這樣一種新模式,以滿足日益變化的市場需求呢?

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享