MartinLindalHansen's avatar

MartinLindalHansen wrote a reply+100 XP

6h ago

Get plan price from config/spark.php?

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(),
    ]);
}