/** * @param PagSeguroHttpConnection $connection * @param PagSeguroAuthorizationRequest $authorizationRequest * @param PagSeguroConnectionData $connectionData * @param null $onlyAuthorizationCode * @return bool|mixed|string * @throws PagSeguroServiceException */ private static function authorizationReturn(PagSeguroHttpConnection $connection, PagSeguroAuthorizationRequest $authorizationRequest, PagSeguroConnectionData $connectionData, $onlyAuthorizationCode = null) { $httpStatus = new PagSeguroHttpStatus($connection->getStatus()); switch ($httpStatus->getType()) { case 'OK': $authorization = PagSeguroAuthorizationParser::readSuccessXml($connection->getResponse()); if ($onlyAuthorizationCode) { $authorizationReturn = $authorization->getCode(); } else { $authorizationReturn = self::buildAuthorizationApprovalUrl($connectionData, $authorization->getCode()); } LogPagSeguro::info("PagSeguroAuthorizationService.Register(" . $authorizationRequest->toString() . ") - end {1}" . $authorization->getCode()); break; case 'BAD_REQUEST': $errors = PagSeguroPaymentParser::readErrors($connection->getResponse()); $err = new PagSeguroServiceException($httpStatus, $errors); LogPagSeguro::error("PagSeguroAuthorizationService.Register(" . $authorizationRequest->toString() . ") - error " . $err->getOneLineMessage()); throw $err; break; default: $err = new PagSeguroServiceException($httpStatus); LogPagSeguro::error("PagSeguroAuthorizationService.Register(" . $authorizationRequest->toString() . ") - error " . $err->getOneLineMessage()); throw $err; break; } return isset($authorizationReturn) ? $authorizationReturn : false; }
/** * */ public static function main() { // Instantiate a new authorization request $authorizationRequest = new PagSeguroAuthorizationRequest(); $authorizationRequest->setReference('REF123'); $authorizationRequest->setRedirectURL('http://www.lojamodelo.com.br'); $authorizationRequest->setNotificationURL('http://www.lojamodelo.com.br'); /** * @enum "CREATE_CHECKOUTS", * @enum "RECEIVE_TRANSACTION_NOTIFICATIONS", * @enum "SEARCH_TRANSACTIONS", * @enum "MANAGE_PAYMENT_PRE_APPROVALS", * @enum "DIRECT_PAYMENT", * @enum "REFUND_TRANSACTIONS", * @enum "CANCEL_TRANSACTIONS" */ $authorizationRequest->setPermissions(array("CREATE_CHECKOUTS", "RECEIVE_TRANSACTION_NOTIFICATIONS", "SEARCH_TRANSACTIONS", "MANAGE_PAYMENT_PRE_APPROVALS", "DIRECT_PAYMENT", "REFUND_TRANSACTIONS", "CANCEL_TRANSACTIONS")); try { /** * #### Credentials ##### * Replace the parameters below with your credentials * You can also get your credentials from a config file. See an example: * $credentials = PagSeguroConfig::getApplicationCredentials(); */ $credentials = new PagSeguroApplicationCredentials("appId", "appKey"); // Register this payment request in PagSeguro to obtain the payment URL to redirect your customer. $return = $authorizationRequest->register($credentials); self::printAuthorizationReturn($return); } catch (PagSeguroServiceException $e) { die($e->getMessage()); } }