/** * @param int $projectId * @param Joomla\Registry\Registry $params * @param object $paymentSession * * @return stdClass * @throws UnexpectedValueException */ public function prepareItem($projectId, $params, $paymentSession) { $project = new Crowdfunding\Project(JFactory::getDbo()); $project->load($projectId); if (!$project->getId()) { throw new UnexpectedValueException(JText::_("COM_CROWDFUNDING_ERROR_INVALID_PROJECT")); } if ($project->isCompleted()) { throw new UnexpectedValueException(JText::_("COM_CROWDFUNDING_ERROR_COMPLETED_PROJECT")); } // Get currency $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $params->get("project_currency")); // Create amount object. $amount = new Crowdfunding\Amount($params); $amount->setCurrency($currency); $item = new stdClass(); $item->id = $project->getId(); $item->title = $project->getTitle(); $item->slug = $project->getSlug(); $item->catslug = $project->getCatSlug(); $item->rewardId = $paymentSession->rewardId; $item->starting_date = $project->getFundingStart(); $item->ending_date = $project->getFundingEnd(); $item->amount = $paymentSession->amount; $item->currencyCode = $currency->getCode(); $item->amountFormated = $amount->setValue($item->amount)->format(); $item->amountCurrency = $amount->setValue($item->amount)->formatCurrency(); return $item; }
protected function sendReportMail($report, $emailId) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ // Get website $uri = JUri::getInstance(); $website = $uri->toString(array('scheme', 'host')); $emailMode = $this->params->get('email_mode', 'plain'); // Get project $project = new Crowdfunding\Project(JFactory::getDbo()); $project->load($report->project_id); // Prepare data for parsing $data = array('site_name' => $app->get('sitename'), 'site_url' => JUri::root(), 'item_title' => $project->getTitle(), 'item_url' => $website . JRoute::_(CrowdfundingHelperRoute::getDetailsRoute($project->getSlug(), $project->getCatSlug())), 'report_subject' => $report->subject, 'report_description' => $report->description); $email = new Emailtemplates\Email(); $email->setDb(JFactory::getDbo()); $email->load($emailId); if (!$email->getSenderName()) { $email->setSenderName($app->get('fromname')); } if (!$email->getSenderEmail()) { $email->setSenderEmail($app->get('mailfrom')); } // Prepare recipient data. $componentParams = JComponentHelper::getParams('com_crowdfunding'); /** @var $componentParams Joomla\Registry\Registry */ $recipientId = (int) $componentParams->get('administrator_id'); if ($recipientId > 0) { $recipient = JFactory::getUser($recipientId); $recipientName = $recipient->get('name'); $recipientMail = $recipient->get('email'); } else { $recipientName = $app->get('fromname'); $recipientMail = $app->get('mailfrom'); } // Prepare data for parsing $data['sender_name'] = $email->getSenderName(); $data['sender_email'] = $email->getSenderEmail(); $data['recipient_name'] = $recipientName; $data['recipient_email'] = $recipientMail; $email->parse($data); $subject = $email->getSubject(); $body = $email->getBody($emailMode); $mailer = JFactory::getMailer(); if (strcmp('html', $emailMode) === 0) { // Send as HTML message $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML); } else { // Send as plain text. $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN); } // Log the error. if ($result !== true) { JLog::add($this->errorPrefix . $mailer->ErrorInfo, JLog::WARNING, 'com_crowdfunding'); return false; } return true; }
public function save($key = null, $urlVar = null) { JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $response = new Prism\Response\Json(); $data = $this->input->post->get('jform', array(), 'array'); $itemId = Joomla\Utilities\ArrayHelper::getValue($data, "project_id", 0, "int"); $terms = $this->input->post->getInt("terms", 0); $project = new Crowdfunding\Project(JFactory::getDbo()); $project->load($itemId); $returnUrl = CrowdfundingHelperRoute::getBackingRoute($project->getSlug(), $project->getCatSlug()); if (!$project->getId()) { // Send response to the browser $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(JText::_("COM_CROWDFUNDINGDATA_INVALID_ITEM"))->setRedirectUrl($returnUrl)->failure(); echo $response; JFactory::getApplication()->close(); } $model = $this->getModel(); /** @var $model CrowdfundingDataModelRecord */ $form = $model->getForm($data, false); /** @var $form JForm */ if (!$form) { throw new Exception(JText::_("COM_CROWDFUNDINGDATA_ERROR_FORM_CANNOT_BE_LOADED"), 500); } // Validate the form data $validData = $model->validate($form, $data); // Check for errors if ($validData === false) { $errors_ = $form->getErrors(); $errors = array(); /** @var $error RuntimeException */ foreach ($errors_ as $error) { $errors[] = $error->getMessage(); } // Send response to the browser $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(implode("\n", $errors))->setRedirectUrl($returnUrl)->failure(); echo $response; JFactory::getApplication()->close(); } // Get the payment session object and session ID. $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $project->getId(); $paymentSession = $app->getUserState($paymentSessionContext); try { $validData["session_id"] = $paymentSession->session_id; $model->save($validData); } catch (Exception $e) { $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(JText::_('COM_CROWDFUNDINGDATA_ERROR_SYSTEM'))->setRedirectUrl($returnUrl)->failure(); echo $response; JFactory::getApplication()->close(); } $componentParams = JComponentHelper::getParams("com_crowdfunding"); /** @var $componentParams Joomla\Registry\Registry */ $processUrl = JUri::base() . "index.php?option=com_crowdfunding&task=backing.process&id=" . (int) $project->getId() . "&rid=" . (int) $paymentSession->rewardId . "&amount=" . rawurldecode($paymentSession->amount) . "&" . JSession::getFormToken() . "=1"; // Set the value of terms of use condition. if ($componentParams->get("backing_terms", 0) and !empty($terms)) { $processUrl .= "&terms=1"; } $filter = JFilterInput::getInstance(); $processUrl = $filter->clean($processUrl); $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_SUCCESS"))->setText(JText::_("COM_CROWDFUNDINGDATA_DATA_SAVED_SUCCESSFULLY"))->setRedirectUrl($processUrl)->success(); echo $response; JFactory::getApplication()->close(); }
/** * Capture payments. * * @param string $context * @param object $item * @param Joomla\Registry\Registry $params * * @return array|null */ public function onPaymentsCapture($context, &$item, &$params) { $allowedContext = array("com_crowdfunding.payments.capture.paypal", "com_crowdfundingfinance.payments.capture.paypal"); if (!in_array($context, $allowedContext)) { 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; } // Load project object and set "memo". $project = new Crowdfunding\Project(JFactory::getDbo()); $project->load($item->project_id); $fundingType = $project->getFundingType(); $fees = $this->getFees($fundingType); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_FEES"), $this->debugType, $fees) : null; // Create transport object. $options = new Joomla\Registry\Registry(); /** @var $options Joomla\Registry\Registry */ $transport = new JHttpTransportCurl($options); $http = new JHttp($options, $transport); // Create payment object. $options = new Joomla\Registry\Registry(); /** @var $options Joomla\Registry\Registry */ $notifyUrl = $this->getCallbackUrl(); $cancelUrl = $this->getCancelUrl($project->getSlug(), $project->getCatSlug()); $returnUrl = $this->getReturnUrl($project->getSlug(), $project->getCatSlug()); $options->set("urls.notify", $notifyUrl); $options->set("urls.cancel", $cancelUrl); $options->set("urls.return", $returnUrl); $this->prepareCredentials($options); $options->set("payment.action_type", "PAY"); $options->set("payment.preapproval_key", $item->txn_id); $options->set("payment.fees_payer", $this->params->get("paypal_fees_payer")); $options->set("payment.currency_code", $item->txn_currency); $options->set("request.envelope", $this->envelope); $title = JText::sprintf($this->textPrefix . "_INVESTING_IN_S", htmlentities($project->getTitle(), ENT_QUOTES, "UTF-8")); $options->set("payment.memo", $title); // Get API url. $apiUrl = $this->getApiUrl(); try { // Calculate the fee. $fee = $this->calculateFee($fundingType, $fees, $item->txn_amount); // Get receiver list and set it to service options. $receiverList = $this->getReceiverList($item, $fee, $fundingType); $options->set("payment.receiver_list", $receiverList); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_OPTIONS"), $this->debugType, $options) : null; $adaptive = new Prism\Payment\PayPal\Adaptive($apiUrl, $options); $adaptive->setTransport($http); $response = $adaptive->doCapture(); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_RESPONSE"), $this->debugType, $response) : null; // Include extra data to transaction record. if ($response->isSuccess()) { $note = JText::_("PLG_CROWDFUNDINGPAYMENT_PAYPALADAPTIVE_RESPONSE_NOTE_CAPTURE_PREAPPROVAL"); $extraData = $this->prepareExtraData($response, $note); $transaction = new Crowdfunding\Transaction(JFactory::getDbo()); $transaction->load($item->id); $transaction->setFee($fee); $transaction->addExtraData($extraData); $transaction->store(); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_TRANSACTION"), $this->debugType, $transaction->getProperties()) : null; } } catch (Exception $e) { // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_ERROR_DOCAPTURE"), $this->debugType, $e->getMessage()) : null; $message = array("text" => JText::sprintf($this->textPrefix . "_CAPTURED_UNSUCCESSFULLY", $item->txn_id), "type" => "error"); return $message; } $message = array("text" => JText::sprintf($this->textPrefix . "_CAPTURED_SUCCESSFULLY", $item->txn_id), "type" => "message"); return $message; }