<commit_before><?php

abstract class Ebanx_Gateway_Model_Payment extends Mage_Payment_Model_Method_Abstract
{
	static protected $redirect_url;
	protected $gateway;
	protected $payment;
	protected $ebanx;
	protected $adapter;
	protected $data;
	protected $result;
	protected $customer;
	protected $paymentData;
	protected $_isGateway = true;
	protected $_canUseFormMultishipping = false;
	protected $_isInitializeNeeded = true;
	protected $_canRefund = true;

	public function __construct()
	{
		parent::__construct();

		$this->ebanx = Mage::getSingleton('ebanx/api')->ebanx();
		$this->adapter = Mage::getModel('ebanx/adapters_paymentAdapter');
		$this->helper = Mage::helper('ebanx');
	}

	public function initialize($paymentAction, $stateObject)
	{
		try {
			parent::initialize($paymentAction, $stateObject);

			$this->payment = $this->getInfoInstance();
			$this->order = $this->payment->getOrder();
			$this->customer = Mage::getModel('sales/order')->load($this->order->getId());

			$this->setupData();

			$this->transformPaymentData();

			$this->processPayment();

			$this->persistPayment();
		} catch (Exception $e) {
			Mage::throwException($e->getMessage());
		}
	}

	public function setupData()
	{
		// Create payment data
		$id = $this->payment->getOrder()->getIncrementId();
		$time = time();
		$merchantPaymentCode = "$id-$time";

		$this->data = new Varien_Object();
		$this->data->setMerchantPaymentCode($merchantPaymentCode)
			->setDueDate($this->helper->getDueDate())
			->setEbanxMethod($this->getCode())
			->setStoreCurrency(Mage::app()->getStore()->getCurrentCurrencyCode())
			->setAmountTotal($this->order->getGrandTotal())
			->setPerson($this->customer)
			->setItems($this->order->getAllVisibleItems())
			->setRemoteIp($this->order->getRemoteIp())
			->setBillingAddress($this->order->getBillingAddress())
			->setPayment($this->payment)
			->setOrder($this->order);
	}

	public function transformPaymentData()
	{
		$this->paymentData = $this->adapter->transform($this->data);
	}

	public function processPayment()
	{
		$res = $this->gateway->create($this->paymentData);

		$this->helper->log($res, $this->getCode() . '.log');

		if ($res['status'] !== 'SUCCESS') {
			// TODO: Make an error handler
			Mage::throwException($res['status_code'] . ' - ' . $res['status_message']);
		}

		// Set the URL for redirect<commit_msg>Caughting errors and sending to the user a more readable format<commit_after><?php

abstract class Ebanx_Gateway_Model_Payment extends Mage_Payment_Model_Method_Abstract
{
	static protected $redirect_url;
	protected $gateway;
	protected $payment;
	protected $ebanx;
	protected $adapter;
	protected $data;
	protected $result;
	protected $customer;
	protected $paymentData;
	protected $_isGateway = true;
	protected $_canUseFormMultishipping = false;
	protected $_isInitializeNeeded = true;
	protected $_canRefund = true;

	public function __construct()
	{
		parent::__construct();

		$this->ebanx = Mage::getSingleton('ebanx/api')->ebanx();
		$this->adapter = Mage::getModel('ebanx/adapters_paymentAdapter');
		$this->helper = Mage::helper('ebanx');
	}

	public function initialize($paymentAction, $stateObject)
	{
		try {
			parent::initialize($paymentAction, $stateObject);

			$this->payment = $this->getInfoInstance();
			$this->order = $this->payment->getOrder();
			$this->customer = Mage::getModel('sales/order')->load($this->order->getId());

			$this->setupData();

			$this->transformPaymentData();

			$this->processPayment();

			$this->persistPayment();
		} catch (Exception $e) {
			Mage::throwException($e->getMessage());
		}
	}

	public function setupData()
	{
		// Create payment data
		$id = $this->payment->getOrder()->getIncrementId();
		$time = time();
		$merchantPaymentCode = "$id-$time";

		$this->data = new Varien_Object();
		$this->data->setMerchantPaymentCode($merchantPaymentCode)
			->setDueDate($this->helper->getDueDate())
			->setEbanxMethod($this->getCode())
			->setStoreCurrency(Mage::app()->getStore()->getCurrentCurrencyCode())
			->setAmountTotal($this->order->getGrandTotal())
			->setPerson($this->customer)
			->setItems($this->order->getAllVisibleItems())
			->setRemoteIp($this->order->getRemoteIp())
			->setBillingAddress($this->order->getBillingAddress())
			->setPayment($this->payment)
			->setOrder($this->order);
	}

	public function transformPaymentData()
	{
		$this->paymentData = $this->adapter->transform($this->data);
	}

	public function processPayment()
	{
		$res = $this->gateway->create($this->paymentData);

		$this->helper->log($res, $this->getCode());

		if ($res['status'] !== 'SUCCESS') {
			$error = Mage::helper('ebanx/error');
			$country = $this->order->getBillingAddress()->getCountry();
			$code = $res['status_code'];

			$this->helper->errorLog($res);
			Mage::throwException($error->getError($code, $country));
		}

		// Set the URL for redirect
{'fraction_non_alphanumeric': 0.18105392659421776, 'fraction_numerical': 0.0, 'mean_word_length': 5.2378516624040925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2372045', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1540, 'n_words': 254}