jrmypttrsn's avatar

Get plan price from config/spark.php?

I'm trying to build a pricing section on my landing page. I'm able to loop through the plans I've specified in my config file but I'm at a loss as to how to access the prices for each plan, I can only return the ID for the monthly or yearly plan from Paddle.

I've tried looking at the Vue files in vendor/laravel/spark-paddle/resources/js/components but it's still not clear to me.

Any ideas on how to access these values?

0 likes
4 replies
MartinLindalHansen's avatar

Here is what is done in the FrontendState.php of spark-paddle:

/** * Get the subscription plans. * * @param string $type * @param Model $billable * @return Collection */ protected function getPlans($type, $billable) { $plans = Spark::plans($type);

    $previews = $this->getPricePreviews($plans);

    /** @var PricePreview $preview */
    foreach ($previews as $preview) {
        if ($sparkPlan = $plans->where('id', $preview->price['id'])->first()) {
            $sparkPlan->priceIncludesVat = $preview->price['tax_mode'] !== 'external';

            $sparkPlan->price = $sparkPlan->priceIncludesVat ? $preview->total() : $preview->subtotal();

            $sparkPlan->currency = $preview->currency()->getCode();
        }
    }

    return $plans;
}

/**
 * Get the price previews from Paddle.
 *
 * @param  Collection  $plans
 * @return Collection
 */
protected function getPricePreviews($plans)
{
    return Cashier::previewPrices($plans->map->id->toArray(), [
        'customer_ip_address' => request()->ip(),
    ]);
}
imrandevbd's avatar

If you look at how Spark is designed, the static config file (config/spark.php) only holds the configuration and the Paddle Price IDs. It doesn't store the actual price values because those are fetched dynamically from Paddle's API to ensure currency localization and tax/VAT calculations are accurate based on the user's IP.

As Martin pointed out above, Spark uses Cashier::previewPrices() under the hood to fetch these values dynamically.

Please or to participate in this conversation.