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;
     }
 }
Ejemplo n.º 2
0
 public static function getData($payment)
 {
     $data = null;
     // pre-approval
     if (property_exists($payment, 'preApproval')) {
         if ($payment->getPreApproval() != null) {
             $data = PagSeguroPreApprovalParser::getData($payment->getPreApproval());
         }
     }
     // reference
     if ($payment->getReference() != null) {
         $data["reference"] = $payment->getReference();
     }
     // sender
     if ($payment->getSender() != null) {
         if ($payment->getSender()->getName() != null) {
             $data['senderName'] = $payment->getSender()->getName();
         }
         if ($payment->getSender()->getEmail() != null) {
             $data['senderEmail'] = $payment->getSender()->getEmail();
         }
         // phone
         if ($payment->getSender()->getPhone() != null) {
             if ($payment->getSender()->getPhone()->getAreaCode() != null) {
                 $data['senderAreaCode'] = $payment->getSender()->getPhone()->getAreaCode();
             }
             if ($payment->getSender()->getPhone()->getNumber() != null) {
                 $data['senderPhone'] = $payment->getSender()->getPhone()->getNumber();
             }
         }
         // documents
         /*** @var $document PagSeguroDocument */
         if ($payment->getSender()->getDocuments() != null) {
             $documents = $payment->getSender()->getDocuments();
             if (is_array($documents) && count($documents) == 1) {
                 foreach ($documents as $document) {
                     if (!is_null($document)) {
                         $document->getType() == "CPF" ? $data['senderCPF'] = $document->getValue() : ($data['senderCNPJ'] = $document->getValue());
                     }
                 }
             }
         }
         if ($payment->getSender()->getIP() != null) {
             $data['ip'] = $payment->getSender()->getIP();
         }
     }
     // currency
     if ($payment->getCurrency() != null) {
         $data['currency'] = $payment->getCurrency();
     }
     // items
     $items = $payment->getItems();
     if (count($items) > 0) {
         $i = 0;
         foreach ($items as $key => $value) {
             $i++;
             if ($items[$key]->getId() != null) {
                 $data["itemId{$i}"] = $items[$key]->getId();
             }
             if ($items[$key]->getDescription() != null) {
                 $data["itemDescription{$i}"] = $items[$key]->getDescription();
             }
             if ($items[$key]->getQuantity() != null) {
                 $data["itemQuantity{$i}"] = $items[$key]->getQuantity();
             }
             if ($items[$key]->getAmount() != null) {
                 $amount = PagSeguroHelper::decimalFormat($items[$key]->getAmount());
                 $data["itemAmount{$i}"] = $amount;
             }
             if ($items[$key]->getWeight() != null) {
                 $data["itemWeight{$i}"] = $items[$key]->getWeight();
             }
             if ($items[$key]->getShippingCost() != null) {
                 $data["itemShippingCost{$i}"] = PagSeguroHelper::decimalFormat($items[$key]->getShippingCost());
             }
         }
     }
     // extraAmount
     if ($payment->getExtraAmount() != null) {
         $data['extraAmount'] = PagSeguroHelper::decimalFormat($payment->getExtraAmount());
     }
     // shipping
     if ($payment->getShipping() != null) {
         if ($payment->getShipping()->getType() != null && $payment->getShipping()->getType()->getValue() != null) {
             $data['shippingType'] = $payment->getShipping()->getType()->getValue();
         }
         if ($payment->getShipping()->getCost() != null && $payment->getShipping()->getCost() != null) {
             $data['shippingCost'] = PagSeguroHelper::decimalFormat($payment->getShipping()->getCost());
         }
         // address
         if ($payment->getShipping()->getAddress() != null) {
             if ($payment->getShipping()->getAddress()->getStreet() != null) {
                 $data['shippingAddressStreet'] = $payment->getShipping()->getAddress()->getStreet();
             }
             if ($payment->getShipping()->getAddress()->getNumber() != null) {
                 $data['shippingAddressNumber'] = $payment->getShipping()->getAddress()->getNumber();
             }
             if ($payment->getShipping()->getAddress()->getComplement() != null) {
                 $data['shippingAddressComplement'] = $payment->getShipping()->getAddress()->getComplement();
             }
             if ($payment->getShipping()->getAddress()->getCity() != null) {
                 $data['shippingAddressCity'] = $payment->getShipping()->getAddress()->getCity();
             }
             if ($payment->getShipping()->getAddress()->getState() != null) {
                 $data['shippingAddressState'] = $payment->getShipping()->getAddress()->getState();
             }
             if ($payment->getShipping()->getAddress()->getDistrict() != null) {
                 $data['shippingAddressDistrict'] = $payment->getShipping()->getAddress()->getDistrict();
             }
             if ($payment->getShipping()->getAddress()->getPostalCode() != null) {
                 $data['shippingAddressPostalCode'] = $payment->getShipping()->getAddress()->getPostalCode();
             }
             if ($payment->getShipping()->getAddress()->getCountry() != null) {
                 $data['shippingAddressCountry'] = $payment->getShipping()->getAddress()->getCountry();
             }
         }
     }
     // maxAge
     if ($payment->getMaxAge() != null) {
         $data['maxAge'] = $payment->getMaxAge();
     }
     // maxUses
     if ($payment->getMaxUses() != null) {
         $data['maxUses'] = $payment->getMaxUses();
     }
     // redirectURL
     if ($payment->getRedirectURL() != null) {
         $data['redirectURL'] = $payment->getRedirectURL();
     }
     // notificationURL
     if ($payment->getNotificationURL() != null) {
         $data['notificationURL'] = $payment->getNotificationURL();
     }
     // metadata
     if (count($payment->getMetaData()->getItems()) > 0) {
         $i = 0;
         foreach ($payment->getMetaData()->getItems() as $item) {
             if ($item instanceof PagSeguroMetaDataItem) {
                 if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
                     $i++;
                     $data['metadataItemKey' . $i] = $item->getKey();
                     $data['metadataItemValue' . $i] = $item->getValue();
                     if (!PagSeguroHelper::isEmpty($item->getGroup())) {
                         $data['metadataItemGroup' . $i] = $item->getGroup();
                     }
                 }
             }
         }
     }
     // paymentMethodConfig
     if (count($payment->getPaymentMethodConfig()->getConfig()) > 0) {
         $i = 0;
         foreach ($payment->getPaymentMethodConfig()->getConfig() as $config) {
             if ($config instanceof PagSeguroPaymentMethodConfigItem) {
                 if (!PagSeguroHelper::isEmpty($config->getKey()) && !PagSeguroHelper::isEmpty($config->getValue())) {
                     $i++;
                     if (!PagSeguroHelper::isEmpty($config->getGroup())) {
                         $data['paymentMethodGroup' . $i] = $config->getGroup();
                     }
                     $data['paymentMethodConfigKey' . $i . "_1"] = $config->getKey();
                     $data['paymentMethodConfigValue' . $i . "_1"] = $config->getValue();
                 }
             }
         }
     }
     // AcceptedPaymentMethod
     if (count($payment->getAcceptedPaymentMethod()->getConfig()) > 0) {
         $i = 0;
         foreach ($payment->getAcceptedPaymentMethod()->getConfig() as $acceptedPayment) {
             if ($acceptedPayment instanceof PagSeguroAcceptPaymentMethod) {
                 $data['acceptPaymentMethodGroup'] = $acceptedPayment->getGroup();
                 $data['acceptPaymentMethodName'] = $acceptedPayment->getName();
             }
             if ($acceptedPayment instanceof PagSeguroExcludePaymentMethod) {
                 $data['excludePaymentMethodGroup'] = $acceptedPayment->getGroup();
                 $data['excludePaymentMethodName'] = $acceptedPayment->getName();
             }
         }
     }
     // parameter
     if (count($payment->getParameter()->getItems()) > 0) {
         foreach ($payment->getParameter()->getItems() as $item) {
             if ($item instanceof PagSeguroParameterItem) {
                 if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
                     if (!PagSeguroHelper::isEmpty($item->getGroup())) {
                         $data[$item->getKey() . '' . $item->getGroup()] = $item->getValue();
                     } else {
                         $data[$item->getKey()] = $item->getValue();
                     }
                 }
             }
         }
     }
     return $data;
 }
 /**
  * @param PagSeguroCredentials $credentials
  * @param PagSeguroPaymentRequest $request
  * @return null|PagSeguroParserData
  * @throws Exception
  * @throws PagSeguroServiceException
  */
 public static function createPreApprovalRequest(PagSeguroCredentials $credentials, PagSeguroPreApprovalRequest $request)
 {
     LogPagSeguro::info("PagSeguroPreApprovalService.PreApprovalRequest(" . $request->toString() . ") - begin");
     self::$connectionData = new PagSeguroConnectionData($credentials, self::SERVICE_NAME);
     $data = array_merge(self::$connectionData->getCredentials()->getAttributesMap(), PagSeguroPreApprovalParser::getData($request));
     try {
         $connection = new PagSeguroHttpConnection();
         $connection->post(self::buildPreApprovalUrl(self::$connectionData), $data, self::$connectionData->getServiceTimeout(), self::$connectionData->getCharset());
         self::$service = "PreApprovalRequest";
         return self::getResult($connection);
     } catch (PagSeguroServiceException $err) {
         //Logging
         LogPagSeguro::error("PagSeguroServiceException: " . $err->getMessage());
         //Exception
         throw $err;
     } catch (Exception $err) {
         //Logging
         LogPagSeguro::error("Exception: " . $err->getMessage());
         //Exception
         throw $err;
     }
 }