Download the PHP package veeqtoh/cashier-paystack without Composer
On this page you can find all versions of the php package veeqtoh/cashier-paystack. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download veeqtoh/cashier-paystack
More information about veeqtoh/cashier-paystack
Files in veeqtoh/cashier-paystack
Package cashier-paystack
Short Description A complete re-write of the abandoned webong-cashier which provides an expressive, fluent interface to Paystack's subscription billing services.
License MIT
Homepage https://github.com/veeqtoh/cashier-paystack
Informations about the package cashier-paystack
Laravel Cashier - Paystack Edition
Table of Contents
- Overview
- Installation
- Requirements
- Install the Package
- Publish the Config and Migrations
- Migrate the Database
- Configuration
- Usage
- Billable Model
- Currency Configuration
- Subscriptions
- Creating Subscriptions
- Additional User Details
- Checking Subscription Status
- Cancelled Subscription Status
- Cancelling Subscription
- Resuming Subscription
- Subscription Trials
- Without Billing Up Front
- Customers
- Creating Customers
- Payment Methods
- Retrieving Authenticated Payment Methods
- Deleting Payment Methods
- Handling Paystack Webhooks
- Webhooks & CSRF Protection
- Defining Webhook Event Handlers
- Single Charges
- Simple Charge
- Charge With Invoice
- Refunding Charges
- Invoices
- Generating Invoice PDFs
- Testing
- Security
- Contribution
- Changelog
- Upgrading
- License
Overview
This library is a modern & complete re-write of the abandoned webong's cashier-paystack library which provides an expressive, fluent interface to Paystack's subscription billing services for Laravel > 10.x apps.
Installation
Requirements
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.x
- Laravel 10.x
Install the Package
You can install the package via Composer:
Publish the Config and Migrations
You can then publish the package's config file and database migrations by using the following command:
Migrate the Database
Before using Cashier, we'll also need to prepare the database. We need to add several columns to your users table and create a new subscriptions table to hold all of our customer's subscriptions: Run the following command.
Configuration
Upon publishing the configuration and migration files, a configuration-file named paystack
.php with some sensible defaults will be placed in your config directory. Update them or set their corresponding values in your .env
file as follows.
Usage
Billable Model
Next, add the Billable trait to your model definition. This trait provides various methods to allow you to perform common billing tasks, such as creating subscriptions, applying coupons, and updating credit card information:
Currency Configuration
The default Cashier currency is Nigeria Naira (NGN). You can change the default currency by calling the Cashier::useCurrency method from within the boot method of one of your service providers. The useCurrency method accepts two string parameters: the currency and the currency's symbol:
Subscriptions
Creating Subscriptions
To create a subscription, first retrieve an instance of your billable model, which typically will be an instance of App\User
. Once you have retrieved the model instance, you may use the newSubscription
method to create the model's subscription:
The first argument passed to the newSubscription
method is the specific Paystack Paystack code the user is subscribing to. This value should correspond to the Paystack's code identifier in Paystack. The second argument should be the name of the subscription. If your application only offers a single subscription, you might call this main or primary.
The create
method, which accepts a Paystack authorization token, will begin the subscription as well as update your database with the customer/user ID and other relevant billing information.
The charge
method, initializes a transaction which returns a response containing an authorization url for payment and an access code.
Additional User Details
If you would like to specify additional customer details, you may do so by passing them as the second argument to the create
method:
To learn more about the additional fields supported by Paystack, check out paystack's documentation on customer creation or the corresponding Paystack documentation.
Checking Subscription Status
Once a user is subscribed to your application, you may easily check their subscription status using a variety of convenient methods. First, the subscribed
method returns true
if the user has an active subscription, even if the subscription is currently within its trial period:
The subscribed
method also makes a great candidate for a route middleware, allowing you to filter access to routes and controllers based on the user's subscription status:
If you would like to determine if a user is still within their trial period, you may use the onTrial
method. This method can be useful for displaying a warning to the user that they are still on their trial period:
The subscribedToPaystack
method may be used to determine if the user is subscribed to a given Paystack based on a given Paystack Paystack code. In this example, we will determine if the user's main subscription is actively subscribed to the monthly Paystack:
Cancelled Subscription Status
To determine if the user was once an active subscriber, but has cancelled their subscription, you may use the cancelled
method:
You may also determine if a user has cancelled their subscription, but are still on their "grace period" until the subscription fully expires. For example, if a user cancels a subscription on March 5th that was originally scheduled to expire on March 10th, the user is on their "grace period" until March 10th. Note that the subscribed method still returns true during this time:
Cancelling Subscriptions
To cancel a subscription, call the cancel method on the user's subscription:
When a subscription is cancelled, Cashier will automatically set the ends_at
column in your database. This column is used to know when the subscribed method should begin returning false. For example, if a customer cancels a subscription on March 1st, but the subscription was not scheduled to end until March 5th, the subscribed method will continue to return true until March 5th.
You may determine if a user has cancelled their subscription but are still on their "grace period" using the onGracePeriod method:
If you wish to cancel a subscription immediately, call the cancelNow
method on the user's subscription:
Resuming Subscriptions
If a user has cancelled their subscription and you wish to resume it, use the resume
method. The user must still be on their grace period in order to resume a subscription:
If the user cancels a subscription and then resumes that subscription before the subscription has fully expired, they will not be billed immediately. Instead, their subscription will be re-activated, and they will be billed on the original billing cycle.
Subscription Trials
With Billing Up Front
If you would like to offer trial periods to your customers while still collecting payment method information up front, you should use the trialDays
method when creating your subscriptions:
This method will set the trial period ending date on the subscription record within the database, as well as instruct Paystack to not begin billing the customer until after this date.
If the customer's subscription is not cancelled before the trial ending date they will be charged as soon as the trial expires, so you should be sure to notify your users of their trial ending date.
You may determine if the user is within their trial period using either the onTrial method of the user instance, or the onTrial method of the subscription instance. The two examples below are identical:
Without Billing Up Front
If you would like to offer trial periods without collecting the user's payment method information up front, you may set the trial_ends_at
column on the user record to your desired trial ending date. This is typically done during user registration:
Be sure to add a date mutator for trial_ends_at
to your model definition.
Cashier refers to this type of trial as a "generic trial", since it is not attached to any existing subscription. The onTrial method on the User instance will return true if the current date is not past the value of trial_ends_at:
You may also use the onGenericTrial
method if you wish to know specifically that the user is within their "generic" trial period and has not created an actual subscription yet:
Once you are ready to create an actual subscription for the user, you may use the newSubscription method as usual:
Customers
Creating Customers
Occasionally, you may wish to create a Paystack customer without beginning a subscription. I would recommend you even do this at sign up. You may accomplish this using the createAsPaystackCustomer
method:
Once the customer has been created in Paystack, you may begin a subscription at a later date.
Payment Methods
Retrieving Authenticated Payment Methods
The cards method on the billable model instance returns a collection of Veeqtoh\Cashier\Card
instances:
Deleting Payment Methods
To delete a card, you should first retrieve the customer's authentications with the card method. Then, you may call the delete method on the instance you wish to delete:
To delete all card payment authentication for a customer
Handling Paystack Webhooks
Paystack can notify your application of a variety of events via webhooks. By default, a route that points to Cashier's webhook controller is automatically registered by the Cashier service provider. This controller will handle all incoming webhook requests.
By default, this controller will automatically handle cancelling subscriptions that have too many failed charges (as defined by your paystack settings), charge success, transfer success or fail, invoice updates and subscription changes; however, as we'll soon discover, you can extend this controller to handle any webhook event you like.
To ensure your application can handle Paystack webhooks, be sure to configure the webhook URL in your Paystack dashboard settings. By default, Cashier's webhook controller responds to the /paystack/webhook URL path. The full list of all webhooks supported by Paystack are listed here
Make sure you protect incoming requests with Cashier's included webhook signature verification middleware.
Webhooks & CSRF Protection
Since Paystack webhooks need to bypass Laravel's CSRF protection, you should ensure that Laravel does not attempt to validate the CSRF token for incoming Stripe webhooks. To accomplish this, you should exclude paystack/*
from CSRF protection in your application's bootstrap/app.php
file:
Defining Webhook Event Handlers
Cashier automatically handles subscription cancellations for failed charges and other common Paystack webhook events. However, if you have additional webhook events you would like to handle, you may do so by listening to the following events that are dispatched by Cashier:
Veeqtoh\Cashier\Events\WebhookReceived
Veeqtoh\Cashier\Events\WebhookHandled
Both events contain the full payload of the Paystack webhook. For example, if you wish to handle the invoice.payment_failed
webhook, you may register a listener that will handle the event:
Another approach to handling additional Paystack webhook events is to extend the Webhook controller. Your method names should correspond to Cashier's expected convention, specifically, methods should be prefixed with handle and the "camel case" name of the Paystack webhook event you wish to handle.
Next, define a route to your Cashier controller within your routes/web.php file to overwrite the default webhook route that came out-of-the-box with this package:
Single Charges
Simple Charge
When using Paystack, the charge
method accepts the amount you would like to charge in the lowest denominator of the currency used by your application.
If you would like to make a "one off" charge against a subscribed customer's credit card, you may use the charge method on a billable model instance.
The charge
method accepts an array as its second argument, allowing you to pass any options you wish to the underlying Paystack charge creation. Consult the Paystack documentation regarding the options available to you when creating charges:
The charge method will throw an exception if the charge fails. If the charge is successful, the full Paystack response will be returned from the method:
Charge With Invoice
Sometimes you may need to make a one-time charge but also generate an invoice for the charge so that you may offer a PDF receipt to your customer. The invoiceFor method lets you do just that. For example, let's invoice the customer ₦2,000.00 for a "One Time Fee":
The invoice will be charged immediately against the user's credit card. The invoiceFor method also accepts an array as its third argument. This array contains the billing options for the invoice item. The fourth argument accepted by the method is also an array. This final argument accepts the billing options for the invoice itself:
To learn more about the additional fields supported by Paystack, check out paystack's documentation on customer creation or the corresponding Paystack documentation.
Refunding Charges
If you need to refund a Paystack charge, you may use the refund method. This method accepts the Paystack charge ID as its only argument:
Invoices
You may easily retrieve an array of a billable model's invoices using the invoices method:
When listing the invoices for the customer, you may use the invoice's helper methods to display the relevant invoice information. For example, you may wish to list every invoice in a table, allowing the user to easily download any of them:
Generating Invoice PDFs
From within a route or controller, use the downloadInvoice
method to generate a PDF download of the invoice. This method will automatically generate the proper HTTP response to send the download to the browser:
All versions of cashier-paystack with dependencies
ext-json Version *
nesbot/carbon Version ^2.0|^3.0
illuminate/view Version ^10.0|^11.0|^12.0
illuminate/http Version ^10.0|^11.0|^12.0
illuminate/support Version ^10.0|^11.0|^12.0
illuminate/routing Version ^10.0|^11.0|^12.0
illuminate/contracts Version ^10.0|^11.0|^12.0
illuminate/container Version ^10.0|^11.0|^12.0
moneyphp/money Version ^4.0
unicodeveloper/laravel-paystack Version ^1.0
symfony/http-kernel Version ^6.0|^7.0
symfony/polyfill-intl-icu Version ^1.22.1