As more and more companies begin to provide subscription services to consumers, subscription payments have become an important part of modern Internet commerce. If you want to build a subscription service, or want to add subscription payment functionality to an existing service, the Laravel Cashier and Paddle combination will provide you with a suitable solution.
Laravel Cashier is an extension package developed by Laravel that provides a simple and easy-to-use interface for subscription processing and payment gateway integration. Paddle is a global payments solution that allows you to process payments and subscriptions in a standards-compliant way.
In this article, we will explore how to use Laravel Cashier and Paddle to handle subscription payments, including how to set up a Paddle account and Laravel project environment, how to handle subscription payments, and how to test Paddle.
Set up a Paddle account
In order to use Paddle as a payment gateway, you need to first create a Paddle account. After creating a Paddle account, you need to create a product in Paddle. This product can be the main product of your subscription service, or it can be an add-on product or service.
Next, you need to set up shipping locations and tax rules. These rules will depend on the localization scope of your service and other important factors. Finally, you need to set up a price plan or subscription plan for your product in Paddle. This plan will define the price, payment cycle, and other relevant information for your subscription service.
Set up the Laravel environment
Before setting up the Laravel environment, you need to determine the requirements for the Laravel version and PHP version. Laravel Cashier needs to run on Laravel 5.1 and above, and Paddle needs to run on PHP 7.0 and above.
After you confirm the Laravel and PHP version requirements, you need to introduce Laravel Cashier into the Laravel project. You can install Laravel Cashier by running the following command:
composer require laravel/cashier
After successful installation, you need to add the service provider and alias in config/app.php. Add the following service provider in the providers array:
LaravelCashierCashierServiceProvider::class,
Add the following alias in the aliases array:
'Cashier' => LaravelCashierFacadesCashier:: class,
Next, you need to configure your Paddle information. Add the following information to config/services.php:
'paddle' => [
'vendor_id' => env('PADDLE_VENDOR_ID'), 'vendor_auth_code' => env('PADDLE_VENDOR_AUTH_CODE'),
],
You need to replace the above information with your Paddle account Information. You can find this information in the settings page of your Paddle account.
Processing Subscription Payments
Now that you have set up your Paddle account and Laravel environment, you can start using Laravel Cashier and Paddle to process subscription payments.
In your application, you can create a simple route to handle payment requests. For example:
Route::get('/subscribe', function () {
$plan = request('plan'); $user = auth()->user(); $user->newSubscription('main', $plan)->create(request()->query('email'), [ 'metadata' => [ 'name' => $user->name, 'company' => $user->company, ] ]); return redirect('/');
});
In the above code, we use Cashier to create a new of subscriptions. We first get the user's email address and then create a new subscription. When creating a subscription, we add some user information to the metadata array.
You can also use the defaultPaymentMethod method to specify the user's default payment method:
$user->defaultPaymentMethod('pm_1234567890');
When a payment is received , you can use Stripe events to automatically update the user's payment status, for example:
Route::post('/stripe/webhook', function (Request $request) {
$payload = $request->all(); $event = null; if (isset($payload['type'])) { $event = StripeEvent::constructFrom($payload); } if ($event && $event->type === 'customer.subscription.updated') { $subscription = $event->data->object->id; $customer = $event->data->object->customer; $user = User::where('stripe_id', $customer)->first(); $user->subscriptions()->where('stripe_id', $subscription)->update([ 'stripe_status' => $event->data->object->status, ]); } return response()->json(['success' => true]);
}) ;
In the above code, we use Stripe events to update the user's subscription status. For specific usage, please refer to Stripe documentation.
Testing
After you complete the above steps and implement subscription payment, you should test Paddle to ensure that all functions are working properly.
Before you test, you need to configure a Paddle account and a test product for the test environment. To ensure that your code works smoothly against the test environment, you need to use test amounts and test subscription loops when testing.
After you complete the test, you can view the test data in the Paddle dashboard to make sure everything is working properly. If you encounter any problems, you can find more information in the Laravel Cashier or Paddle documentation.
Conclusion
The integration of Laravel Cashier and Paddle provides you with a simple, easy-to-configure solution for implementing subscription payment functionality. Whether you are building a new subscription service or adding subscription payment functionality to an existing service, this combination will provide you with the right solution.
The above is the detailed content of Laravel Development: How to handle subscription payments using Laravel Cashier and Paddle?. For more information, please follow other related articles on the PHP Chinese website!