Using products in Spike

Products are great for one-time sales. The most common use case is to purchase a set amount of credits or other providables without locking in to a recurring subscription. This gives the users the freedom and allows you to reach more customers.

Opcodes\Spike\Product is the Spike's representation object of a product. It contains all the necessary information about a particular product being offered, including its name, cost, and configuration values.

When interacting with Spike's product methods, you'll be receiving and providing instances of Product.

class Product
{
    public function __construct(
        public string $id,
        public string $name,
        public ?string $short_description = null,
        public ?array $features = [],
        public ?string $payment_provider_price_id = null,
        public ?int $price_in_cents = null,
        public array $provides = [],
    ) {}

    public static function fromArray(array $config): Product;

The Product instance has these methods available for convenience:

class Product
{
    /** Get the formatted price, e.g. "€10,00" */
    public function priceFormatted(): string;
}

Getting a list of products

Spike::products() returns a collection of Products available to purchase for the default billable. As always, you can provide a custom billable instance to get the products available for them.

use App\Models\Team;
use Opcodes\Spike\Facades\Spike;

// get the products available for the default billable
$products = Spike::products();

// get the products available for the team billable type
$products = Spike::billable(Team::class)->products();

Finding a specific product

Spike::findProduct(string $id) returns a Product instance with the given ID, if found. As always, you can provide a custom billable instance to find the product available for them.

use App\Models\Team;
use Opcodes\Spike\Facades\Spike;

// find the product with ID of 'basic_credit_pack'
$product = Spike::findProducts('basic_credit_pack');

// find the product with ID of 'basic_credit_pack' for the team billable type
$product = Spike::billable(Team::class)->findProduct('basic_credit_pack');

Other notable methods

Here are a few other, notable, available methods related to products.

use App\Models\Team;

// returns a boolean whether there are any products available for purchase
Spike::productsAvailable();
Spike::billable(Team::class)->productsAvailable();

// provide a custom resolver for products
Spike::resolveProductsUsing(callable $callback);
Spike::billable(Team::class)->resolveProductsUsing(callable $callback);

Past purchases

You can get a list of the user's past purchases. This can be extremely helpful when you want to figure out whether a user has purchased a specific product already:

// Did the user purchase a specific product?
$billable->hasPurchased('product_id');

// You can also provide a Product instance
$product = Spike::findProducts('basic_credit_pack');
$billable->hasPurchased($product);

You can also get the full history of which and how many products the user has purchased:

// Get a collection of purchases
// Collection of \Opcodes\Spike\ProductPurchase
$purchases = $billable->purchases();
$purchases->first()->product;       // The Spike Product instance
$purchases->first()->quantity;      // How many of this product was purchased
$purchases->first()->purchased_at;  // When was this purchased.

// Get grouped purchases (quantities combined, grouped by product ID)
// Collection of \Opcodes\Spike\GroupedProductPurchase
$billable->groupedPurchases();

Support

If you have any questions, feedback, or need any help setting up Spike within your project, feel free to reach out to me.