public static function createPreApprovalRequest(PagSeguroCredentials $credentials, PagSeguroPaymentRequest $paymentRequest)
 {
     LogPagSeguro::info("PagSeguroPreApprovalService.createPreApprovalRequest(" . $paymentRequest->toString() . ") - begin");
     $connectionData = new PagSeguroConnectionData($credentials, self::SERVICE_NAME);
     $data = array_merge($connectionData->getCredentials()->getAttributesMap(), PagSeguroPreApprovalParser::getData($paymentRequest));
     // On preApproval, credentials goes with data, not in the url query string
     try {
         $connection = new PagSeguroHttpConnection();
         $connection->post(self::buildCheckoutRequestUrl($connectionData), $data, $connectionData->getServiceTimeout(), $connectionData->getCharset());
         $httpStatus = new PagSeguroHttpStatus($connection->getStatus());
         switch ($httpStatus->getType()) {
             case 'OK':
                 $PaymentParserData = PagSeguroPreApprovalParser::readSuccessXml($connection->getResponse());
                 $paymentReturn = array('code' => $PaymentParserData->getCode(), 'cancelUrl' => self::buildCancelUrl($connectionData, $PaymentParserData->getCode()), 'checkoutUrl' => self::buildCheckoutUrl($connectionData, $PaymentParserData->getCode()));
                 LogPagSeguro::info("PagSeguroPreApprovalService.createPreApprovalRequest(" . $paymentRequest->toString() . ") - end {1}" . $PaymentParserData->getCode());
                 break;
             case 'BAD_REQUEST':
                 $errors = PagSeguroPreApprovalParser::readErrors($connection->getResponse());
                 $e = new PagSeguroServiceException($httpStatus, $errors);
                 LogPagSeguro::error("PagSeguroPreApprovalService.createPreApprovalRequest(" . $paymentRequest->toString() . ") - error " . $e->getOneLineMessage());
                 throw $e;
                 break;
             default:
                 $e = new PagSeguroServiceException($httpStatus);
                 LogPagSeguro::error("PagSeguroPreApprovalService.createPreApprovalRequest(" . $paymentRequest->toString() . ") - error " . $e->getOneLineMessage());
                 throw $e;
                 break;
         }
         return isset($paymentReturn) ? $paymentReturn : false;
     } catch (PagSeguroServiceException $e) {
         throw $e;
     } catch (Exception $e) {
         LogPagSeguro::error("Exception: " . $e->getMessage());
         throw $e;
     }
 }
 private static function buildPreApprovalCancelUrl(PagSeguroConnectionData $connectionData, $code)
 {
     $credentialsArray = $connectionData->getCredentials()->getAttributesMap();
     return $connectionData->getWebserviceUrl() . $connectionData->getResource('cancelUrl') . "{$code}?" . $connectionData->getCredentialsUrlQuery();
 }