示例#1
0
 /**
  * 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;
 }