Setting up products with Spike
Products are non-recurring purchases. The user can add one or more products to their shopping cart and checkout with a single click. This makes it easy for you to sell bundles of credits, or other products from within Spike.
Displaying the products view
The page to buy products will show up automatically once there is at least one product set up.
You can either add it via config/spike.php
configuration file, or by providing a
custom product resolver.
Hiding the products view
If you would like to hide the products page completely, set the 'products'
to an empty array:
'products' => [],
Adding products via config
In order to make a product available for purchase, you simply add an associative array to the config/spike.php
configuration file. You can add as many products as you'd like.
use Opcodes\Spike\CreditAmount;
'products' => [
[
'id' => 'standard_pack',
'name' => 'Standard pack',
'short_description' => 'Great value for occasional use',
'payment_provider_price_id' => 'price_xxxxxxxx3',
'price_in_cents' => 1000,
'provides' => [
CreditAmount::make(5000)->expiresAfterMonths(6),
],
],
// other products...
],
Here's a rundown of the attributes:
Attributes | Required | Description |
---|---|---|
id | yes | Identifier of the product. Must be unique. |
name | yes | The name of the product that will be visible to the user. |
short_description | no | A short description of the product that will be visible to the user. |
payment_provider_price_id | yes | Stripe's or Paddle's price_id for this product. Learn how to set up Stripe products. |
price_in_cents | yes | The price of the product in cents. This will be visible to the user in a friendly format. |
provides | no | An array of providables that are provided upon purchase of this product. |
Archiving old products
It's OK to remove any outdated products from the config/spike.php
configuration. But if you would like to keep them there for reference, but hide them from the users, you can archive a product by setting the archived
flag to true
.
'products' => [
[
'id' => 'standard_pack',
'name' => 'Standard pack',
'archived' => true, // <== add this line
// other attributes...
],
],
Thank you page
When a user completes the purchase of credits, they will be shown a "Thank you" page in Spike. You can redirect from that page to a different page of your choice after any duration of your choice.
You can do so by adding this line to your AppServiceProvider::boot()
method:
// Will redirect the user to "/custom-thank-you-page" after 5 seconds.
Spike::redirectAfterProductPurchaseTo('/custom-thank-you-page', 5);
The first parameter even accepts a callback which will receive the Opcodes\Spike\Cart
instance where you can find the purchased items/products, if you need that to build the final redirect URL.
use Opcodes\Spike\Cart;
Spike::redirectAfterProductPurchaseTo(function (Cart $cart) {
if ($cart->billable->subscribed()) {
return "/special-subscriber-thank-you";
}
return null; // `null` value will not redirect anywhere.
}, 5);