<commit_before><?php

/**
 * PagosonlineGateway for Symfony2
 *
 * This Bundle is part of Symfony2 Payment Suite
 *
 * @package PagosonlineGatewayBundle
 *
 */

namespace Scastells\PagosonlineGatewayBundle\Controller;

use Mmoreram\PaymentCoreBundle\Exception\PaymentOrderNotFoundException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Mmoreram\PaymentCoreBundle\Services\PaymentEventDispatcher;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Scastells\PagosonlineGatewayBundle\PagosonlineGatewayMethod;
use Symfony\Component\HttpFoundation\Response;


/**
 * PagosonlineGatewayController
 *
 */
class PagosonlineGatewayController extends Controller
{

    /**
     * Payment execution
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse
     *
     * @Method("POST")
     * @Template()
     */
    public function executeAction(Request $request)
    {
        $paymentMethod = new PagosonlineGatewayMethod();
        $paymentBridge = $this->get('payment.bridge');

        /**
         * New order from cart must be created right here
         */
        $this->get('payment.event.dispatcher')->notifyPaymentOrderLoad($paymentBridge, $paymentMethod);


        /**
         * Order Not found Exception must be thrown just here
         */
        if (!$paymentBridge->getOrder()) {

            throw new PaymentOrderNotFoundException;
        }


        /**
         * Loading success route for returning from pagosonline
         */
        $redirectSuccessUrl = $this->container->getParameter('pagosonline_gateway.success.route');
        $redirectSuccessAppend = $this->container->getParameter('pagosonline_gateway.success.order.append');
        $redirectSuccessAppendField = $this->container->getParameter('pagosonline_gateway.success.order.field');

        $redirectSuccessData    = $redirectSuccessAppend
                                ? array(
                                    $redirectSuccessAppendField => $this->get('payment.bridge')->getOrderId(),
                                )
                                : array();

        $successRoute = $this->generateUrl($redirectSuccessUrl, $redirectSuccessData, true);

        $paymentMethod->setReference($paymentBridge->getOrderId() . '#' . date('Ymdhis'));
        $this->get('payment.event.dispatcher')->notifyPaymentOrderDone($paymentBridge, $paymentMethod);

        /*
         * url send to pagosonline
         */
        $redirectResponseUrl = $this->container->getParameter('pagosonline_gateway.controller.route.response.name');

        $responseRoute = $this->generateUrl($redirectResponseUrl, array(), true);

        $redirectConfirmUrl = $this->container->getParameter('pagosonline_gateway.controller.route.confirmation.name');

        $confirmRoute = $this->generateUrl($redirectConfirmUrl, array(), true);
        /**
         * Build form
         */
        $formView = $this
            ->get('pagosonline_gateway.form.type.wrapper')
            ->buildForm($responseRoute, $confirmRoute)
            ->getForm($successRoute)
            ->createView();
        return array(

            'pagosonline_gateway_form' => $formView,
        );
    }


    /**
     * Payment reponse
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse
     *
     */
    public function confirmationAction(Request $request)
    {
        //$request->request
        $signature = $request->query->get('firma');
        $status_pol = $request->query->get('estado_pol');
        $currency = $request->query->get('moneda');
        $value = $request->query->get('valor');
        $orderRef = $request->query->get('ref_venta');
        $userId = $request->query->get('usuario_id');
        $key = $this->container->getParameter('pagosonline_gateway.key');
        $signatureHash = md5($key.'~'.$userId.'~'.$orderRef.'~'.$value.'~'.$currency.'~'.$status_pol);
        $referencePol = $request->query->get('ref_pol');

        $paymentBridge = $this->get('payment.bridge');

        $trans = $this->getDoctrine()->getRepository('PagosonlineGatewayBridgeBundle:PagosonlineGatewayOrderTransaction')->find($orderRef);
        $trans->getOrder()->getId();

        //save values
        $paymentMethod = new PagosonlineGatewayMethod();
        $paymentMethod->setPagosonlineGatewayTransactionId($referencePol);
        $paymentMethod->setPagosonlineGatewayReference($referencePol);
        $paymentMethod->setReference($orderRef);
        $paymentMethod->setStatus($status_pol);
        $order = $paymentBridge->findOrder($trans->getOrder()->getId());
        $paymentBridge->setOrder($order);
        if (strtoupper($signatureHash) == $signature) {
            if ($status_pol == 4) {
                $this->get('payment.event.dispatcher')->notifyPaymentOrderSuccess($paymentBridge, $paymentMethod);

            } elseif ($status_pol == 5 || $status_pol == 6) {
                $this->get('payment.event.dispatcher')->notifyPaymentOrderFail($paymentBridge, $paymentMethod);
            }
        }
        return new Response();
    }

    /**
     * Payment response
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse<commit_msg>get request of pagosonline with post method<commit_after><?php

/**
 * PagosonlineGateway for Symfony2
 *
 * This Bundle is part of Symfony2 Payment Suite
 *
 * @package PagosonlineGatewayBundle
 *
 */

namespace Scastells\PagosonlineGatewayBundle\Controller;

use Mmoreram\PaymentCoreBundle\Exception\PaymentOrderNotFoundException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Mmoreram\PaymentCoreBundle\Services\PaymentEventDispatcher;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Scastells\PagosonlineGatewayBundle\PagosonlineGatewayMethod;
use Symfony\Component\HttpFoundation\Response;


/**
 * PagosonlineGatewayController
 *
 */
class PagosonlineGatewayController extends Controller
{

    /**
     * Payment execution
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse
     *
     * @Method("POST")
     * @Template()
     */
    public function executeAction(Request $request)
    {
        $paymentMethod = new PagosonlineGatewayMethod();
        $paymentBridge = $this->get('payment.bridge');

        /**
         * New order from cart must be created right here
         */
        $this->get('payment.event.dispatcher')->notifyPaymentOrderLoad($paymentBridge, $paymentMethod);


        /**
         * Order Not found Exception must be thrown just here
         */
        if (!$paymentBridge->getOrder()) {

            throw new PaymentOrderNotFoundException;
        }


        /**
         * Loading success route for returning from pagosonline
         */
        $redirectSuccessUrl = $this->container->getParameter('pagosonline_gateway.success.route');
        $redirectSuccessAppend = $this->container->getParameter('pagosonline_gateway.success.order.append');
        $redirectSuccessAppendField = $this->container->getParameter('pagosonline_gateway.success.order.field');

        $redirectSuccessData    = $redirectSuccessAppend
                                ? array(
                                    $redirectSuccessAppendField => $this->get('payment.bridge')->getOrderId(),
                                )
                                : array();

        $successRoute = $this->generateUrl($redirectSuccessUrl, $redirectSuccessData, true);

        $paymentMethod->setReference($paymentBridge->getOrderId() . '#' . date('Ymdhis'));
        $this->get('payment.event.dispatcher')->notifyPaymentOrderDone($paymentBridge, $paymentMethod);

        /*
         * url send to pagosonline
         */
        $redirectResponseUrl = $this->container->getParameter('pagosonline_gateway.controller.route.response.name');

        $responseRoute = $this->generateUrl($redirectResponseUrl, array(), true);

        $redirectConfirmUrl = $this->container->getParameter('pagosonline_gateway.controller.route.confirmation.name');

        $confirmRoute = $this->generateUrl($redirectConfirmUrl, array(), true);
        /**
         * Build form
         */
        $formView = $this
            ->get('pagosonline_gateway.form.type.wrapper')
            ->buildForm($responseRoute, $confirmRoute)
            ->getForm($successRoute)
            ->createView();
        return array(

            'pagosonline_gateway_form' => $formView,
        );
    }


    /**
     * Payment reponse
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse
     *
     */
    public function confirmationAction(Request $request)
    {
        $signature = $request->request->get('firma');
        $status_pol = $request->request->get('estado_pol');
        $currency = $request->request->get('moneda');
        $value = $request->request->get('valor');
        $orderRef = $request->request->get('ref_venta');
        $userId = $request->request->get('usuario_id');
        $key = $this->container->getParameter('pagosonline_gateway.key');
        $signatureHash = md5($key.'~'.$userId.'~'.$orderRef.'~'.$value.'~'.$currency.'~'.$status_pol);
        $referencePol = $request->request->get('ref_pol');

        $paymentBridge = $this->get('payment.bridge');

        $trans = $this->getDoctrine()->getRepository('PagosonlineGatewayBridgeBundle:PagosonlineGatewayOrderTransaction')->find($orderRef);
        $trans->getOrder()->getId();

        //save values
        $paymentMethod = new PagosonlineGatewayMethod();
        $paymentMethod->setPagosonlineGatewayTransactionId($referencePol);
        $paymentMethod->setPagosonlineGatewayReference($referencePol);
        $paymentMethod->setReference($orderRef);
        $paymentMethod->setStatus($status_pol);
        $order = $paymentBridge->findOrder($trans->getOrder()->getId());
        $paymentBridge->setOrder($order);
        if (strtoupper($signatureHash) == $signature) {
            if ($status_pol == 4) {
                $this->get('payment.event.dispatcher')->notifyPaymentOrderSuccess($paymentBridge, $paymentMethod);

            } elseif ($status_pol == 5 || $status_pol == 6) {
                $this->get('payment.event.dispatcher')->notifyPaymentOrderFail($paymentBridge, $paymentMethod);
            }
        }
        return new Response();
    }

    /**
     * Payment response
     *
     * @param Request $request Request element
     *
     * @return RedirectResponse
{'fraction_non_alphanumeric': 0.13208071084783413, 'fraction_numerical': 0.0011106997408367272, 'mean_word_length': 2.8575508746876115, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 130, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3859217', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2829, 'n_words': 436}