/** * Boleto transaction **/ public function boletoTransaction($order, $data, Uecommerce_Mundipagg_Model_Standard $standard) { try { // Get Webservice URL $url = $standard->getURL(); // Set Data $_request = array(); $_request["Order"] = array(); $_request["Order"]["OrderReference"] = $order->getIncrementId(); if ($standard->getEnvironment() != 'production') { $_request["Order"]["OrderReference"] = md5(date('Y-m-d H:i:s')); // Identificação do pedido na loja } $_request["BoletoTransactionCollection"] = array(); $boletoTransactionCollection = new stdclass(); for ($i = 1; $i <= $data['boleto_parcelamento']; $i++) { $boletoTransactionData = new stdclass(); if (!empty($data['boleto_dates'])) { $datePagamentoBoleto = $data['boleto_dates'][$i - 1]; $now = strtotime(date('Y-m-d')); $yourDate = strtotime($datePagamentoBoleto); $datediff = $yourDate - $now; $daysToAddInBoletoExpirationDate = floor($datediff / (60 * 60 * 24)); } else { $daysToAddInBoletoExpirationDate = $standard->getDiasValidadeBoleto(); } $baseGrandTotal = str_replace(',', '.', $order->getBaseGrandTotal()); $amountInCentsVar = intval(strval($baseGrandTotal / $data['boleto_parcelamento'] * 100)); $boletoTransactionData->AmountInCents = $amountInCentsVar; $boletoTransactionData->Instructions = $standard->getInstrucoesCaixa(); if ($standard->getEnvironment() != 'production') { $boletoTransactionData->BankNumber = $standard->getBankNumber(); } $boletoTransactionData->DocumentNumber = ''; $boletoTransactionData->Options = new stdclass(); $boletoTransactionData->Options->CurrencyIso = 'BRL'; $boletoTransactionData->Options->DaysToAddInBoletoExpirationDate = $daysToAddInBoletoExpirationDate; $addy = $this->buyerBillingData($order, $data, $_request, $standard); $boletoTransactionData->BillingAddress = $addy['AddressCollection'][0]; $boletoTransactionCollection = array($boletoTransactionData); } $_request["BoletoTransactionCollection"] = $this->ConvertBoletoTransactionCollectionFromRequest($boletoTransactionCollection); // Buyer data $_request["Buyer"] = array(); $_request["Buyer"] = $this->buyerBillingData($order, $data, $_request, $standard); // Cart data $_request["ShoppingCartCollection"] = array(); $_request["ShoppingCartCollection"] = $this->cartData($order, $data, $_request, $standard); // Data $dataToPost = json_encode($_request); if ($standard->getDebug() == 1) { Mage::log('Uecommerce_Mundipagg: ' . Mage::helper('mundipagg')->getExtensionVersion(), null, 'Uecommerce_Mundipagg.log'); Mage::log(print_r($dataToPost, 1), null, 'Uecommerce_Mundipagg.log'); } // Send payment data to MundiPagg $ch = curl_init(); // Header curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'MerchantKey: ' . $standard->getMerchantKey() . '')); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToPost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute post $_response = curl_exec($ch); // Close connection curl_close($ch); if ($standard->getDebug() == 1) { Mage::log('Uecommerce_Mundipagg: ' . Mage::helper('mundipagg')->getExtensionVersion(), null, 'Uecommerce_Mundipagg.log'); Mage::log(print_r($_response, 1), null, 'Uecommerce_Mundipagg.log'); } // Is there an error? $xml = simplexml_load_string($_response); $json = json_encode($xml); $data = array(); $data = json_decode($json, true); if ($standard->getDebug() == 1) { Mage::log('Uecommerce_Mundipagg: ' . Mage::helper('mundipagg')->getExtensionVersion(), null, 'Uecommerce_Mundipagg.log'); Mage::log(print_r($data, 1), null, 'Uecommerce_Mundipagg.log'); } // Error if (isset($data['ErrorReport']) && !empty($data['ErrorReport'])) { $_errorItemCollection = $data['ErrorReport']['ErrorItemCollection']; foreach ($_errorItemCollection as $errorItem) { $errorCode = $errorItem['ErrorCode']; $ErrorDescription = $errorItem['Description']; } return array('error' => 1, 'ErrorCode' => $errorCode, 'ErrorDescription' => Mage::helper('mundipagg')->__($ErrorDescription), 'result' => $data); } // False if (isset($data['Success']) && (string) $data['Success'] == 'false') { return array('error' => 1, 'ErrorCode' => 'WithError', 'ErrorDescription' => 'WithError', 'result' => $data); } else { // Success return array('success' => true, 'message' => 0, 'OrderKey' => $data['OrderResult']['OrderKey'], 'OrderReference' => $data['OrderResult']['OrderReference'], 'result' => $data); } } catch (Exception $e) { //Redirect to Cancel page Mage::getSingleton('checkout/session')->setApprovalRequestSuccess('cancel'); //Log error Mage::logException($e); //Mail error $this->mailError(print_r($e->getMessage(), 1)); // Return error $approvalRequest['error'] = 'Error WS'; $approvalRequest['ErrorCode'] = 'ErrorCode WS'; $approvalRequest['ErrorDescription'] = 'ErrorDescription WS'; $approvalRequest['OrderKey'] = ''; $approvalRequest['OrderReference'] = ''; return $approvalRequest; } }