Commit bc3e1db3 authored by Mark Harding's avatar Mark Harding
Browse files

(feat): introduce pro billing for wire

parent e9072cc6
Loading
Loading
Loading
Loading
+163 −0
Original line number Diff line number Diff line
<?php
/**
 * Upgrades Delegate
 */
namespace Minds\Core\Wire\Delegates;

use Minds\Core\Config;
use Minds\Core\Di\Di;
use Minds\Core\Wire\Wire;
use Minds\Core\Pro\Manager as ProManager;

class UpgradesDelegate
{
    /** @var Config */
    private $config;

    /** @var EntitiesBuilder */
    private $entitiesBuilder;

    /** @var ProManager */
    private $proManager;

    public function __construct($config = null, $entitiesBuilder = null, $proManager = null)
    {
        $this->config = $config ?: Di::_()->get('Config');
        $this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
        $this->proManager = $proManager ?? Di::_()->get('Pro\Manager');
    }

    /**
     * On Wire
     * @param Wire $wire
     * @param string $receiver_address
     * @return Wire $wire
     */
    public function onWire($wire, $receiver_address): Wire
    {
        switch ($wire->getReceiver()->guid) {
            case $this->config->get('blockchain')['contracts']['wire']['plus_guid']:
                return $this->onPlusUpgrade($wire, $receiver_address);
                break;
            case $this->config->get('pro')['handler']:
                return $this->onProUpgrade($wire, $receiver_address);
                break;
        }
        return $wire; // Not expected
    }

    private function onPlusUpgrade($wire, $receiver_address): Wire
    {
        if (
            !(
                $receiver_address == 'offchain'
                || $receiver_address == $this->config->get('blockchain')['contracts']['wire']['plus_address']
            )
        ) {
            return $wire; //not offchain or potential onchain fraud
        }

        // 20 tokens
        if ($wire->getAmount() != "20000000000000000000") {
            return $wire; //incorrect wire amount sent
        }

        //set the plus period for this user
        $user = $wire->getSender();

        // rebuild the user as we can't trust upstream
        $user = $this->entitiesBuilder->single($user->getGuid(), [
            'cache' => false,
        ]);

        if (!$user) {
            return $wire;
        }

        $days = 30;
        $monthly = $this->config->get('upgrades')['plus']['monthly'];
        $yearly = $this->config->get('upgrades')['plus']['yearly'];
        
        switch ($wire->getMethod()) {
            case 'tokens':
                if ($monthly['tokens'] == $wire->getAmount()) {
                    $days = 30;
                } elseif ($yearly['tokens'] == $wire->getAmount()) {
                    $days = 365;
                } else {
                    return $wire;
                }
                break;
            case 'usd':
                if ($monthly['usd'] == $wire->getAmount() / 100) {
                    $days = 30;
                } elseif ($yearly['usd'] == $wire->getAmount() / 100) {
                    $days = 365;
                } else {
                    return $wire;
                }
                break;
            default:
                return $wire;
        }

        $expires = strtotime("+{$days} days", $wire->getTimestamp());

        $user->setPlusExpires($expires);
        $user->save();

        //$wire->setSender($user);
        return $wire;
    }

    private function onProUpgrade($wire, $receiver_address): Wire
    {
        //set the plus period for this user
        $user = $wire->getSender();

        // rebuild the user as we can't trust upstream
        $user = $this->entitiesBuilder->single($user->getGuid(), [
            'cache' => false,
        ]);

        if (!$user) {
            return $wire;
        }

        $days = 30;
        $monthly = $this->config->get('upgrades')['pro']['monthly'];
        $yearly = $this->config->get('upgrades')['pro']['yearly'];

        error_log($wire->getMethod());
        switch ($wire->getMethod()) {
            case 'tokens':
                error_log($wire->getAmount());
                  if ($monthly['tokens'] == $wire->getAmount() / (10 ** 18)) {
                      $days = 30;
                  } elseif ($yearly['tokens'] == $wire->getAmount() / (10 ** 18)) {
                      $days = 365;
                  } else {
                      return $wire;
                  }
                break;
            case 'usd':
                if ($monthly['usd'] == $wire->getAmount() / 100) {
                    $days = 30;
                } elseif ($yearly['usd'] == $wire->getAmount() / 100) {
                    $days = 365;
                } else {
                    return $wire;
                }
                break;
            default:
                return $wire;
        }

        $expires = strtotime("+{$days} days", $wire->getTimestamp());

        $this->proManager->setUser($user)
            ->enable($expires);

        return $wire;
    }
}
+12 −7
Original line number Diff line number Diff line
@@ -61,8 +61,8 @@ class Manager
    /** @var Core\Blockchain\Wallets\OffChain\Cap $cap */
    protected $cap;
    
    /** @var Delegates\Plus $plusDelegate */
    protected $plusDelegate;
    /** @var Delegates\UpgradesDelegate */
    protected $upgradesDelegate;

    /** @var Delegates\RecurringDelegate $recurringDelegate */
    protected $recurringDelegate;
@@ -87,7 +87,7 @@ class Manager
        $client = null,
        $token = null,
        $cap = null,
        $plusDelegate = null,
        $upgradesDelegate = null,
        $recurringDelegate = null,
        $notificationDelegate = null,
        $cacheDelegate = null,
@@ -101,7 +101,8 @@ class Manager
        $this->client = $client ?: Di::_()->get('Blockchain\Services\Ethereum');
        $this->token = $token ?: Di::_()->get('Blockchain\Token');
        $this->cap = $cap ?: Di::_()->get('Blockchain\Wallets\OffChain\Cap');
        $this->plusDelegate = $plusDelegate ?: new Delegates\Plus();
        $this->upgradesDelegate = $upgradesDelegate ?? new Delegates\UpgradesDelegate();
        ;
        $this->recurringDelegate = $recurringDelegate ?: new Delegates\RecurringDelegate();
        $this->notificationDelegate = $notificationDelegate ?: new Delegates\NotificationDelegate();
        $this->cacheDelegate = $cacheDelegate ?: new Delegates\CacheDelegate();
@@ -248,8 +249,8 @@ class Manager

                $wire->setAddress('offchain');

                // Notify plus
                $this->plusDelegate
                // Notify plus/pro
                $this->upgradesDelegate
                    ->onWire($wire, 'offchain');

                // Send notification
@@ -287,6 +288,10 @@ class Manager
                // Save the wire to the Repository
                $this->repository->add($wire);

                // Notify plus/pro
                $this->upgradesDelegate
                    ->onWire($wire, 'usd');

                // Send notification
                $this->notificationDelegate->onAdd($wire);

@@ -330,7 +335,7 @@ class Manager
            ->setCompleted(true);
        $this->txRepo->add($transaction);

        $this->plusDelegate
        $this->upgradesDelegate
            ->onWire($wire, $data['receiver_address']);

        $this->notificationDelegate->onAdd($wire);