<commit_before><?php
namespace Payment\Controller;

use Eva\Mvc\Controller\ActionController,
    Payment\Service\Exception,
    Eva\View\Model\ViewModel;

class RequestController extends ActionController
{
    protected $addResources = array(
    );

    public function indexAction()
    {
        $adapter = $this->params()->fromQuery('adapter');
        $callback = $this->params()->fromQuery('callback');
        $amount = $this->params()->fromQuery('amount');
        $data = $this->params()->fromQuery('data');
        $signed = $this->params()->fromQuery('signed');
        
        if(!$amount){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment amount found'
            ));
        }
        
        if(!$adapter){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment adapter key found'
            ));
        }

        if(!$callback){
            throw new Exception\InvalidArgumentException(sprintf(
                'No oauth callback found'
            ));
        }
        
        if(!$signed){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment signed time found'
            ));
        }

        if (!$this->authenticate($this->params()->fromQuery())) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Signed not match'
            )); 
            return; 
        }

        $config = $this->getServiceLocator()->get('config');
        $helper = $this->getEvent()->getApplication()->getServiceManager()->get('viewhelpermanager')->get('serverurl');

        $url = $helper() . $config['payment']['return_url_path'] . '?' . http_build_query(array(
            'callback' => $callback,
        ));

        $amount = (float) $amount;

        if (strtolower($adapter) == "paypalec") {
            $cancel = $helper() . $config['payment']['cancel_url_path'] . '?' . http_build_query(array(
                'callback' =>$callback,
            ));
            $this->paypal($amount, $url, $data, $cancel);
        } else {
            $this->alipay($amount, $url, $data);
        }
    }

    public function paypal($amount, $callback, $data = array(), $cancel = null)
    {
        $config = \Eva\Api::_()->getModuleConfig('Payment');
        $options = $config['payment']['paypal']; 

        $pay = new \Payment\Service\Payment('PaypalEc', false, $options);
        $pay->setServiceLocator($this->getServiceLocator());

        if (isset($data['title'])) {
            $pay->setOrderTitle($data['title']);
        }

        return $pay->setAmount($amount)
            ->setCallback($callback)
            ->setCancel($cancel)
            ->setLogData($data)
            ->sendRequest();
    }

    public function alipay($amount, $callback, $data = array())
    {
        $config = \Eva\Api::_()->getModuleConfig('Payment');
        $options = $config['payment']['alipay']; 

        $orderId = time();
        
        $config = $this->getServiceLocator()->get('config');
        $helper = $this->getEvent()->getApplication()->getServiceManager()->get('viewhelpermanager')->get('serverurl');
        $notify = $helper() . $config['payment']['return_url_path'] . '?' . http_build_query(array(
            'callback' => 'notify',
        ));
        
        $pay = new \Payment\Service\Payment('AlipayEc', false, $options);
        $pay->setServiceLocator($this->getServiceLocator());
        if (isset($data['title'])) {
            $pay->setOrderTitle($data['title']);
        }
        $link = $pay->setAmount($amount)
            ->setOrderId($orderId)
            ->setNotify($notify)
            ->setCallback($callback)
            ->setLogData($data)
            ->sendRequest();

        return $this->redirect()->toUrl($link); 
    }

    public function authenticate($params)
    {
        $adapter     = $params['adapter'];
        $callback    = $params['callback'];
        $amount      = $params['amount'];
        $data        = $params['data'];
        $signed      = $params['signed'];

        $config = $this->getServiceLocator()->get('config');
        
        $queryString = http_build_query(array(
            'adapter'  => $adapter,
            'amount'   => $amount,
            'callback' => $callback,
            'data'     => $data,<commit_msg>alipay notify url<commit_after><?php
namespace Payment\Controller;

use Eva\Mvc\Controller\ActionController,
    Payment\Service\Exception,
    Eva\View\Model\ViewModel;

class RequestController extends ActionController
{
    protected $addResources = array(
    );

    public function indexAction()
    {
        $adapter = $this->params()->fromQuery('adapter');
        $callback = $this->params()->fromQuery('callback');
        $amount = $this->params()->fromQuery('amount');
        $data = $this->params()->fromQuery('data');
        $signed = $this->params()->fromQuery('signed');
        
        if(!$amount){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment amount found'
            ));
        }
        
        if(!$adapter){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment adapter key found'
            ));
        }

        if(!$callback){
            throw new Exception\InvalidArgumentException(sprintf(
                'No oauth callback found'
            ));
        }
        
        if(!$signed){
            throw new Exception\InvalidArgumentException(sprintf(
                'No payment signed time found'
            ));
        }

        if (!$this->authenticate($this->params()->fromQuery())) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Signed not match'
            )); 
            return; 
        }

        $config = $this->getServiceLocator()->get('config');
        $helper = $this->getEvent()->getApplication()->getServiceManager()->get('viewhelpermanager')->get('serverurl');

        $url = $helper() . $config['payment']['return_url_path'] . '?' . http_build_query(array(
            'callback' => $callback,
        ));

        $amount = (float) $amount;

        if (strtolower($adapter) == "paypalec") {
            $cancel = $helper() . $config['payment']['cancel_url_path'] . '?' . http_build_query(array(
                'callback' =>$callback,
            ));
            $this->paypal($amount, $url, $data, $cancel);
        } else {
            $this->alipay($amount, $url, $data);
        }
    }

    public function paypal($amount, $callback, $data = array(), $cancel = null)
    {
        $config = \Eva\Api::_()->getModuleConfig('Payment');
        $options = $config['payment']['paypal']; 

        $pay = new \Payment\Service\Payment('PaypalEc', false, $options);
        $pay->setServiceLocator($this->getServiceLocator());

        if (isset($data['title'])) {
            $pay->setOrderTitle($data['title']);
        }

        return $pay->setAmount($amount)
            ->setCallback($callback)
            ->setCancel($cancel)
            ->setLogData($data)
            ->sendRequest();
    }

    public function alipay($amount, $callback, $data = array())
    {
        $config = \Eva\Api::_()->getModuleConfig('Payment');
        $options = $config['payment']['alipay']; 

        $orderId = time();
        
        $config = $this->getServiceLocator()->get('config');
        $helper = $this->getEvent()->getApplication()->getServiceManager()->get('viewhelpermanager')->get('serverurl');
        $notify = $helper() . $config['payment']['return_url_path'] . '?' . http_build_query(array(
            'callback' => 'notify',
        ));
        
        $pay = new \Payment\Service\Payment('AlipayEc', false, $options);
        $pay->setServiceLocator($this->getServiceLocator());
        if (isset($data['title'])) {
            $pay->setOrderTitle($data['title']);
        }
        $link = $pay->setAmount($amount)
            ->setOrderId($orderId)
    //        ->setNotify($notify)
            ->setCallback($callback)
            ->setLogData($data)
            ->sendRequest();

        return $this->redirect()->toUrl($link); 
    }

    public function authenticate($params)
    {
        $adapter     = $params['adapter'];
        $callback    = $params['callback'];
        $amount      = $params['amount'];
        $data        = $params['data'];
        $signed      = $params['signed'];

        $config = $this->getServiceLocator()->get('config');
        
        $queryString = http_build_query(array(
            'adapter'  => $adapter,
            'amount'   => $amount,
            'callback' => $callback,
            'data'     => $data,
{'fraction_non_alphanumeric': 0.16914966765986705, 'fraction_numerical': 0.0, 'mean_word_length': 2.097976570820021, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, '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': '5829451', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2312, 'n_words': 385}