Пример #1
0
 /**
  * Check if the user access token connected to payout exists. If it does not exist, returns null.
  *
  * If it exist, check for its expiration. If the token has been expired, refresh it with new one.
  *
  * @param array   $apiKeys
  * @param Payout  $payout
  * @param int     $expires
  *
  * @return Registry|AccessToken|null
  */
 public static function getPayoutAccessToken($apiKeys, Payout $payout, $expires = 7)
 {
     try {
         $token = $payout->getStripe();
         // Try to get an access token (using the authorization code grant)
         $alias = !$apiKeys['test'] ? 'production' : 'test';
         if ($token === null or !$token->get('stripeconnect.' . $alias . '.access_token')) {
             return null;
         }
         $options = array('access_token' => $token->get('stripeconnect.' . $alias . '.access_token'), 'refresh_token' => $token->get('stripeconnect.' . $alias . '.refresh_token'), 'expires' => $token->get('stripeconnect.' . $alias . '.expires'));
         $accessToken = new AccessToken($options);
         if ($accessToken->hasExpired()) {
             $provider = new Stripe(['clientId' => $apiKeys['client_id'], 'clientSecret' => $apiKeys['secret_key']]);
             $accessToken = $provider->getAccessToken('refresh_token', ['refresh_token' => $token->get('stripeconnect.' . $alias . '.refresh_token')]);
             // Prepare expiration date.
             $date = new \JDate();
             $date->add(new \DateInterval('P' . $expires . 'D'));
             $token->set('stripeconnect.' . $alias . '.access_token', $accessToken->getToken());
             $token->set('stripeconnect.' . $alias . '.refresh_token', $accessToken->getRefreshToken());
             $token->set('stripeconnect.' . $alias . '.expires', $date->getTimestamp());
             $payout->setStripe($token);
             $payout->storeStripe();
         }
     } catch (\Exception $e) {
         \JLog::add($e->getMessage());
         return null;
     }
     return $accessToken;
 }
Пример #2
0
 /**
  * Prepare filter by date.
  *
  * @param JDatabaseQuery $query
  */
 protected function prepareFilterDate(&$query)
 {
     $db = $this->getDbo();
     // Filter by date.
     $filter = (int) $this->getState($this->context . '.filter_date');
     switch ($filter) {
         case 1:
             // Starting soon
             jimport('joomla.date.date');
             $date = new JDate();
             $today = $date->toSql();
             $date->sub(new DateInterval('P7D'));
             $query->where('a.funding_start >= ' . $db->quote($date->toSql()) . ' AND a.funding_start <= ' . $db->quote($today));
             break;
         case 2:
             // Ending soon
             jimport('joomla.date.date');
             $date = new JDate();
             $today = $date->toSql();
             $date->add(new DateInterval('P7D'));
             $query->where('a.funding_end >= ' . $db->quote($today) . ' AND a.funding_start <= ' . $db->quote($date->toSql()));
             break;
     }
 }
 /**
  * Authorize user to payment gateway.
  *
  * @param string                   $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPayoutsAuthorize($context, $params)
 {
     if (strcmp('com_crowdfundingfinance.payouts.authorize.stripeconnect', $context) !== 0) {
         return null;
     }
     if ($this->app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp('html', $docType) !== 0) {
         return null;
     }
     // Prepare output data.
     $output = array('redirect_url' => '', 'message' => '');
     $errorOutput = array('redirect_url' => JRoute::_(CrowdfundingHelperRoute::getDiscoverRoute()), 'message' => '');
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_GET_RESPONSE_AUTHORIZE'), $this->debugType, $_GET) : null;
     $userId = JFactory::getUser()->get('id');
     if (!$userId) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_NOT_REGISTERED_USER');
         return $errorOutput;
     }
     // Get token
     $code = $this->app->input->get('code');
     $state = $this->app->input->get('state');
     if (!$code or !$state) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     // Get project ID and redirect URL from the session.
     $stateData = $this->app->getUserState($state);
     if (count($stateData) === 0 or (!$stateData['redirect_url'] or !$stateData['project_id'])) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     $cfFinanceParams = JComponentHelper::getParams('com_crowdfundingfinance');
     $apiKeys = Crowdfundingfinance\Stripe\Helper::getKeys($cfFinanceParams);
     if (!$apiKeys['client_id'] or !$apiKeys['secret_key']) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_CONFIGURATION');
         return $errorOutput;
     }
     // Prepare expiration date.
     $date = new JDate();
     $date->add(new DateInterval('P' . $cfFinanceParams->get('stripe_expiration_period', '7') . 'D'));
     // Prepare Stripe data.
     $provider = new AdamPaterson\OAuth2\Client\Provider\Stripe(['clientId' => $apiKeys['client_id'], 'clientSecret' => $apiKeys['secret_key']]);
     $token = $provider->getAccessToken('authorization_code', ['code' => $code]);
     // Get resource owner.
     $resourceOwner = $provider->getResourceOwner($token);
     $alias = !$apiKeys['test'] ? 'production' : 'test';
     $stripe = new \Joomla\Registry\Registry(array('stripeconnect' => array($alias => array('access_token' => $token->getToken(), 'refresh_token' => $token->getRefreshToken(), 'expires' => $date->getTimestamp(), 'account_id' => $resourceOwner->getId()))));
     $payout = new Crowdfundingfinance\Payout(JFactory::getDbo());
     $payout->setSecretKey($this->app->get('secret'));
     $payout->load(array('project_id' => (int) $stateData['project_id']));
     // Create user record.
     if (!$payout->getId()) {
         $payout->setProjectId((int) $stateData['project_id']);
     }
     $payout->setStripe($stripe);
     $payout->store();
     // Get next URL.
     $output['redirect_url'] = base64_decode($stateData['redirect_url']);
     return $output;
 }
 protected function isBruteForceAttack($userData)
 {
     if ((int) $userData['attempts'] >= (int) $this->params->get('allowed_failures', 10)) {
         $today = new JDate();
         $bannedTo = new JDate($userData['date']);
         $bannedTo->add(new DateInterval('P' . (int) $this->params->get('ban_period', 7) . 'D'));
         if ($today <= $bannedTo) {
             return true;
         }
     }
     return false;
 }
Пример #5
0
 /**
  * Get the number of ending soon projects.
  *
  * <code>
  * $options = array(
  *     "interval" => 7, // The number of next days when the campaigns is going to finish.
  *     "state"    => 1, // The state of the campaign - published or unpublished.
  *     "approved" => 1, // The approved state - approved or not approved.
  * );
  * $statistics   = new Crowdfunding\Statistics\Basic(\JFactory::getDbo());
  * $numberOfProjects = $statistics->getEndingSoonProjects();
  * </code>
  *
  * @param array $options Options used to be aggregated data.
  *
  * @return int
  */
 public function getEndingSoonProjects(array $options = array())
 {
     $query = $this->db->getQuery(true);
     $query->select('COUNT(*)')->from($this->db->quoteName('#__crowdf_projects', 'a'));
     // Filter by date interval.
     if (array_key_exists('interval', $options)) {
         $days = (int) $options['interval'];
         if ($days > 0) {
             jimport('joomla.date.date');
             $date = new \JDate();
             $today = $date->toSql();
             $date->add(new \DateInterval('P' . $days . 'D'));
             $query->where('a.funding_end >= ' . $this->db->quote($today) . ' AND a.funding_start <= ' . $this->db->quote($date->toSql()));
         }
     }
     // Prepare filters.
     $this->prepareFilters($query, $options);
     $this->db->setQuery($query);
     return (int) $this->db->loadResult();
 }
Пример #6
0
 public static function monthize($days)
 {
     $now = new JDate();
     $end = new JDate();
     $duration = new DateInterval("P{$days}D");
     $diff = $end->add($duration)->diff($now);
     // handle years
     if ($diff->y > 0) {
         $diff->m = $diff->m + 12 * $diff->y;
     }
     return $diff->m;
 }
Пример #7
0
 function onPaymentForOrder($paylog, $order)
 {
     if (!$order->status == 'C') {
         return;
     }
     $modelorder = JTheFactoryPricingHelper::getModel('orders');
     $items = $modelorder->getOrderItems($order->id, self::getItemName());
     if (!is_array($items) || !count($items)) {
         return;
     }
     //no Listing items in order
     $cfg = BidsHelperTools::getConfig();
     $nowDate = new JDate();
     $auction = JTable::getInstance('auction');
     foreach ($items as $item) {
         if (!$item->iteminfo) {
             continue;
         }
         //AuctionID is stored in iteminfo
         if ($item->itemname != self::getItemName()) {
             continue;
         }
         if (!$auction->load($item->iteminfo)) {
             continue;
         }
         //auction no longer exists
         $auction->modified = $nowDate->toMySQL();
         $auction->published = 1;
         if (!$cfg->bid_opt_enable_date) {
             $startDate = new JDate($auction->start_date);
             $diff = $nowDate->toUnix() - $startDate->toUnix();
             if ($diff > 0) {
                 $auction->start_date = $nowDate->toMySQL();
                 $endDate = new JDate($auction->end_date);
                 $endDate->add(new DateInterval('PT' . $diff . 'S'));
                 $auction->end_date = $endDate->toMySQL();
             }
         }
         $auction->store();
         JTheFactoryEventsHelper::triggerEvent('onAfterSaveAuctionSuccess', array($auction));
         //for email notifications
     }
 }
Пример #8
0
 /**
  * Get the number of ending soon projects.
  *
  * <code>
  * $options = array(
  *     "interval" => 7, // The number of next days when the campaigns is going to finish.
  *     "state"    => 1, // The state of the campaign - published or unpublished.
  *     "approved" => 1, // The approved state - approved or not approved.
  * );
  * $statistics   = new Crowdfunding\Statistics\Basic(\JFactory::getDbo());
  * $numberOfProjects = $statistics->getEndingSoonProjects();
  * </code>
  *
  * @param array $options Options used to be aggregated data.
  *
  * @return int
  */
 public function getEndingSoonProjects($options = array())
 {
     $query = $this->db->getQuery(true);
     $query->select("COUNT(*)")->from($this->db->quoteName("#__crowdf_projects", "a"));
     // Filter by date interval.
     if (isset($options["interval"])) {
         $days = (int) $options["interval"];
         if ($days > 0) {
             jimport("joomla.date.date");
             $date = new \JDate();
             $today = $date->toSql();
             $date->add(new \DateInterval("P" . $days . "D"));
             $query->where("a.funding_end >= " . $this->db->quote($today) . " AND a.funding_start <= " . $this->db->quote($date->toSql()));
         }
     }
     // Prepare filters.
     $this->prepareFilters($query, $options);
     $this->db->setQuery($query);
     $result = $this->db->loadResult();
     if (!$result) {
         $result = 0;
     }
     return $result;
 }