Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swissmant's avatar

Extending Spark Subscribe functionality using Spark::swap

I have a situation where I need to customize the Subscribe class of Spark. In my particular case, I need to modify trial days based upon the day of the month, and make a one-off charge to the customer.

Within my SparkServiceProvider->booted method, I have added Spark::swap to replace the Subscribe handle method. This works and my code is called, however, there is a line which references $this->token and I get an error because $this is not working.

Here's my booted method (I've added a comment after the problematic line): ` public function booted() { Spark::useStripe();

    Spark::swap('Subscribe@handle', function ($user, $plan, $fromRegistration, array $data) {
        $subscription = $user->newSubscription('default', $plan->id);

        // Here we will check if we need to skip trial or set trial days on the subscription
        // when creating it on the provider. By default, we will skip the trial when this
        // interaction is not from egistration since they have already usually trialed.
        if (! $fromRegistration) {
            $subscription->skipTrial();
        } elseif ($plan->trialDays > 0) {
            $subscription->trialDays($plan->trialDays);
        }
            //$subscription->trialDays(1);

        if (isset($data['coupon'])) {
            $subscription->withCoupon($data['coupon']);
        }

        // Next, we need to check if this application is storing billing addresses and if so
        // we will update the billing address in the database so that any tax information
        // on the user will be up to date via the taxPercentage method on the billable.
        if (Spark::collectsBillingAddress()) {
            Spark::call(
                UserRepository::class.'@updateBillingAddress',
                [$user, $data]
            );
        }

        // If this application collects European VAT, we will store the VAT ID that was sent
        // with the request. It is used to determine if the VAT should get charged at all
        // when billing the customer. When it is present, VAT is not typically charged.
        if (Spark::collectsEuropeanVat()) {
            Spark::call(
                UserRepository::class.'@updateVatId',
                [$user, array_get($data, 'vat_id')]
            );
        }

        // Here we will create the actual subscription on the service and fire off the event
        // letting other listeners know a user has subscribed, which will allow any hooks
        // to fire that need to send the subscription data to any external metrics app.
        $subscription->create($data[$this->token]); /***************** Here's the problem! **************/

        event(new UserSubscribed(
            $user = $user->fresh(), $plan, $fromRegistration
        ));
        $user->invoiceFor('First month pro-rated', 1333);

        return $user;
    });
    
   Spark::plan('Tier 1', 'tier-1')
        ->price(100)
        ->features([
            'First', 'Second', 'Third'
        ]);

    }`
0 likes
2 replies
poms32's avatar

Hi did you manage to fix it ? I have the same issue !

Please or to participate in this conversation.