Download the PHP package bigdropinc/yii2-cashier without Composer

On this page you can find all versions of the php package bigdropinc/yii2-cashier. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-cashier

Cashier Extension for Yii 2


This extension is the port of Laravel's Cashier package

Installation

The preferred way to install this extension is through composer.

Either run

or add

to the require section of your composer.json file.

Stripe Configuration

This project is inspired by the Laravel's Cashier package and tries to bring it's simplicity to the Yii framework.

Database Migrations

Before using Cashier, we'll also need to prepare the database.

Also you can apply migration by the following command:

Model Setup

Next, add the Billable trait to your User model definition:

Provider Keys

Next, you should configure your params section in your configuration file:

Subscriptions

Creating Subscriptions

To create a subscription, first retrieve an instance of your billable model, which typically will be an instance of models\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 should be the name of the subscription. If your application only offers a single subscription, you might call this main or primary. The second argument is the specific Stripe plan the user is subscribing to. This value should correspond to the plan's identifier in Stripe.

The create method will automatically create the subscription, as well as update your database with the customer ID and other relevant billing information.

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 Stripe, check out Stripe's documentation on customer creation

Coupons

If you would like to apply a coupon when creating the subscription, you may use the withCoupon method:

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:

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 subscribedToPlan method may be used to determine if the user is subscribed to a given plan based on a given Stripe plan ID. In this example, we will determine if the user's main subscription is actively subscribed to the monthly plan:

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.

Changing Plans

After a user is subscribed to your application, they may occasionally want to change to a new subscription plan. To swap a user to a new subscription, use the swap method. For example, we may easily switch a user to the premium subscription:

If the user is on trial, the trial period will be maintained. Also, if a "quantity" exists for the subscription, that quantity will also be maintained:

If you would like to swap plans but skip the trial period on the plan you are swapping to, you may use the skipTrial method:

Subscription Quantity

Sometimes subscriptions are affected by "quantity". For example, your application might charge $10 per month per user on an account. To easily increment or decrement your subscription quantity, use the incrementQuantity and decrementQuantity methods:

Alternatively, you may set a specific quantity using the updateQuantity method:

For more information on subscription quantities, consult the Stripe documentation.

Subscription Taxes

With Cashier, it's easy to provide the tax_percent value sent to Stripe. To specify the tax percentage a user pays on a subscription, implement the taxPercentage method on your billable model, and return a numeric value between 0 and 100, with no more than 2 decimal places.

This enables you to apply a tax rate on a model-by-model basis, which may be helpful for a user base that spans multiple countries.

Cancelling Subscriptions

To cancel a subscription, simply call the cancel method on the user's subscription:

When a subscription is cancelled, Cashier will automatically set the 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:

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 simply be re-activated, and they will be billed on the original billing cycle.

Subscription Trials

With Credit Card 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 Stripe 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 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 essentially identical in purpose:

Without Credit Card Up Front

If you would like to offer trial periods without collecting the user's payment method information up front, you may simply set the trial_end_at column on the user record to your desired trial ending date. For example, this is typically done during user registration:

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_end_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:

Handling Stripe Webhooks

Failed Subscriptions

Just add the WebhookController to the in your configuration file

That's it! Failed payments will be captured and handled by the controller. The controller will cancel the customer's subscription when Stripe determines the subscription has failed (normally after three failed payment attempts). Don't forget: you will need to configure the webhook URI, for example: in your Stripe control panel settings.

Single Charges

Simple Charge

When using Stripe, 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 method accepts an array as its second argument, allowing you to pass any options you wish to the underlying Stripe charge creation:

The charge method will throw an exception if the charge fails. If the charge is successful, the full Stripe 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 $5.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, allowing you to pass any options you wish to the underlying Stripe charge creation:

The invoiceFor method will create a Stripe invoice which will retry failed billing attempts. If you do not want invoices to retry failed charges, you will need to close them using the Stripe API after the first failed charge.

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 GridView, allowing the user to easily download any of them:

Generating Invoice PDFs

Create the action in the your controller, for example:

Refunds

Refund objects allow you to refund a charge that has previously been created but not yet refunded. Funds will be refunded to the credit or debit card that was originally charged. The fees you were originally charged are also refunded.


All versions of yii2-cashier with dependencies

PHP Build Version
Package Version
Requires yiisoft/yii2 Version >=2.0.8
yii2mod/yii2-behaviors Version *
stripe/stripe-php Version ~4.0
nesbot/carbon Version ~1.0
dompdf/dompdf Version ^0.8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bigdropinc/yii2-cashier contains the following files

Loading the files please wait ....