/**
  * Generates the Express URL which is given to the user in order to approve
  * the payment
  * 
  * @param $data
  * @throws jmsPaymentUserActionRequiredException this is always thrown
  */
 private function generateExpressUrl(jmsPaymentMethodData $data)
 {
     $amount = PayPal::getType('BasicAmountType');
     $amount->setattr('currencyID', $data->getCurrency());
     $amount->setval(number_format($data->getAmount(), 2));
     $expressCheckout = PayPal::getType('SetExpressCheckoutRequestDetailsType');
     $expressCheckout->setNoShipping($data->getValue('no_shipping', 1));
     $expressCheckout->setCancelURL($data->getValue('cancel_url'));
     $expressCheckout->setReturnURL($data->getValue('return_url'));
     $expressCheckout->setPaymentAction('Order');
     $expressCheckout->setOrderDescription($data->getValue('subject'));
     $expressCheckout->setOrderTotal($amount);
     $expressCheckoutRequest = PayPal::getType('SetExpressCheckoutRequestType');
     $expressCheckoutRequest->setVersion(self::API_VERSION);
     $expressCheckoutRequest->setSetExpressCheckoutRequestDetails($expressCheckout);
     $request = $this->getCallerServices()->SetExpressCheckout($expressCheckoutRequest);
     if (Pear::isError($request)) {
         throw new jmsPaymentCommunicationException('Error when retrieving the express URL: ' . $request->getMessage());
     }
     if ($request->Ack !== 'Success') {
         $reasonCode = $this->extractErrors($request->Errors);
         $data->setReasonCode($reasonCode);
         $e = new jmsPaymentException('Error when retrieving the express url: ' . $reasonCode);
         $e->setPaymentMethodData($data);
         throw $e;
     }
     $host = !$this->isDebug() ? 'www.paypal.com' : 'www.sandbox.paypal.com';
     $data->setProcessedAmount($data->getAmount());
     $data->setValue('express_token', $request->Token);
     $data->setValue('express_url', sprintf('https://%s/cgi-bin/webscr?cmd=_express-checkout&token=%s', $host, $request->Token));
     // throw
     $exception = new jmsPaymentUserActionRequiredException(new jmsPaymentUserActionVisitURL($data->getValue('express_url')));
     $exception->setPaymentMethodData($data);
     throw $exception;
 }
 /**
  * Deposits a transaction. This method is not intended to call any
  * api belonging to 4b system as this api does not exist. The first
  * action for depositing has to be a POST submission to an URL outside
  * of the online shop pages where the purchaser has to fill payment info.
  * When this is done 4b server calls in return online shop url.
  * You have to connect that url with this method.
  * You can inform 4b about the url that leads to this method in the
  * field labeled: "URL que graba el resultado en la BD del comercio"
  * https://tpv.4b.es/config
  * 
  * @see plugins/jmsPaymentPlugin/lib/method/jmsPaymentMethod#deposit($data, $retry)
  */
 public function deposit(jmsPaymentMethodData $data, $retry = false)
 {
     //If this is not called from 4b systems we want to launch a user interaction exception
     //Check call originated from 4b simulation or production servers.
     $caller_address = sfContext::getInstance()->getRequest()->getRemoteAddress();
     //This call is far from the paymentDemo sfAction
     //to not taint the 'general' logic implemented there.
     $production_ip = $this->getProductionIp();
     $simulation_ip = $this->getSimulationIp();
     //This block is going to be executed when call is from 'shop' pages and
     //4b pages to be filled by the user are going to be shown as result.
     if ($caller_address != $production_ip && $caller_address != $simulation_ip) {
         //call to deposit not from 4b servers
         $production_url = $this->getProductionUrl();
         $simulation_url = $this->getSimulationUrl();
         $_4b_conection_url = !$this->isDebug() ? $production_url : $simulation_url;
         // throw
         $exception = new jmsPaymentUserActionRequiredException(new jmsPaymentUserActionVisitURL($_4b_conection_url));
         $data->setValue('transaction_from_ip', $caller_address);
         $exception->setPaymentMethodData($data);
         throw $exception;
     }
     //This block is going to be executed when call is from 4b server
     $amount_value = $data->getAmount();
     if ($data->getValue('transaction_result') == 1) {
         //Success
         $data->setResponseCode($data->getValue('transaction_id'));
         $data->setReasonCode('Approval with 4b approval code: ' . $data->getValue('transaction_approval_code'));
         $data->setProcessedAmount($amount_value);
         //Processed amount equals requested.
     } else {
         //Failed
         $data->setResponseCode($data->getValue('transaction_error_code'));
         $data->setReasonCode($data->getValue('transaction_error_description'));
         $e = new jmsPaymentException('Payment could not be completed. Reason: ' . $data->getReasonCode());
         $e->setPaymentMethodData($data);
         throw $e;
     }
 }