XF 2.1 Downgrading upgrades

tom3

Active member
I'm trying to extend the controller of an add-on in which an upgrade to the same group is checked and may need to be downgraded.
The AddOn itself extends the UserUpgrades with a group_id so that upgrades can be grouped together.

My extended class XF/Purchasable/UserUpgrade:
PHP:
<?php
use XF\Payment\CallbackState;
class UserUpgrade extends XFCP_UserUpgrade
{
    public function completePurchase(CallbackState $state)
    {
        parent::completePurchase($state);
        $user = $state->getPurchaser();
        $user = \XF::finder('XF:User')
                ->where('user_id', $user->user_id)
                ->fetchOne();
       
        $purchaseData = $state->getPurchaseRequest();
        /* save for later usage */
        //$purchaseData->extra_data['user_upgrade_record_id']
        //$purchaseData->extra_data['user_upgrade_id']
        //get group_id of purchased upgrade
        $purchasedUpgrade = \XF::finder('XF:UserUpgradeActive')
                ->where('user_upgrade_record_id', $purchaseData->extra_data['user_upgrade_record_id'])
                ->fetchOne();
        $groupID = $purchasedUpgrade->Upgrade->group_id;
        //get all active upgrades of purchaser first except just purchased one
        $activeUpgrades = \XF::finder('XF:UserUpgradeActive')
            ->where('user_id', $user->user_id)
            ->where('user_upgrade_record_id', '!=', $purchaseData->extra_data['user_upgrade_record_id'])
            ->fetch();
        if ($activeUpgrades)
        {
            foreach ($activeUpgrades as $activeUpgrade)
            {
                /* CHECK FOR ACTIVE UPGRADES IN SAME GROUP */
                if ($activeUpgrade->Upgrade->group_id == $groupID)
                {
                    //calculate and save end_date for extending before downgrading
                    if ($activeUpgrade->end_date != 0)
                    {
                        $endDate = min(
                            pow(2,32) - 1,
                            strtotime('+' . $purchasedUpgrade->Upgrade->length_amount . ' ' . $purchasedUpgrade->Upgrade->length_unit, $activeUpgrade->end_date)
                        );
                    }
                    else
                    {
                        $endDate = $activeUpgrade->end_date;
                    }
                    //extend active upgrade in same group
                    $upgradeService = \XF::app()->service('XF:User\Upgrade', $activeUpgrade->Upgrade, $user);
                    $upgradeService->setEndDate($endDate);
                    $upgradeService->ignoreUnpurchasable(FALSE);
                    $upgradeService->upgrade();
                    //downgrade just purchased upgrade
                    $downgradeService = \XF::app()->service('XF:User\Downgrade', $purchasedUpgrade->Upgrade, $user);
                    $downgradeService->setSendAlert(false);
                    $downgradeService->downgrade();
                    break;
                }
            }
        }
    }
}

Everything works fine except the downgrading. The error log is empty. Any ideas?
 
Last edited:
Fixed it myself. Seems like you need to pass the active upgrade as an 4th argument to the downgrade function:
PHP:
$downgradeService = \XF::app()->service('XF:User\Downgrade', $purchasedUpgrade->Upgrade, $user, $purchasedUpgrade);
$downgradeService->setSendAlert(false);
$downgradeService->downgrade();
 
Back
Top Bottom