linccce

Paysera Akeeba Subscribers

Jul 18th, 2014
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.17 KB | None | 0 0
  1. <?php
  2.  
  3. defined('_JEXEC') or die();
  4.  
  5. $akpaymentinclude = include_once JPATH_ADMINISTRATOR.'/components/com_akeebasubs/assets/akpayment.php';
  6. if(!$akpaymentinclude) { unset($akpaymentinclude); return; } else { unset($akpaymentinclude); }
  7.  
  8. class plgAkpaymentPaysera extends plgAkpaymentAbstract {
  9.     public function __construct(&$subject, $config = array())
  10.     {
  11.         $config = array_merge($config, array(
  12.             'ppName'        => 'paysera',
  13.             'ppKey'         => 'PLG_AKPAYMENT_PAYSERA_TITLE',
  14.             'ppImage'       => 'https://www.mokejimai.lt/payment/m/m_images/wfiles/i56je3562.png'
  15.         ));
  16.  
  17.         parent::__construct($subject, $config);
  18.     }
  19.  
  20.     public function onAKPaymentNew($paymentmethod, $user, $level, $subscription)
  21.     {
  22.         if($paymentmethod != $this->ppName) return false;
  23.  
  24.         $nameParts = explode(' ', $user->name, 2);
  25.         $firstName = $nameParts[0];
  26.         if(count($nameParts) > 1) {
  27.             $lastName = $nameParts[1];
  28.         } else {
  29.             $lastName = '';
  30.         }
  31.  
  32.         $slug = F0FModel::getTmpInstance('Levels','AkeebasubsModel')
  33.             ->setId($subscription->akeebasubs_level_id)
  34.             ->getItem()
  35.             ->slug; //reikalingas formuojant nuorodą, kitaip negaus teisingos nuorodos į užsakomą daiktą
  36.  
  37.         $kuser = F0FModel::getTmpInstance('Users','AkeebasubsModel')
  38.             ->user_id($user->id)
  39.             ->getFirstItem();
  40.  
  41.         $rootURL = rtrim(JURI::base(),'/');
  42.         $subpathURL = JURI::base(true);
  43.  
  44.         if(!empty($subpathURL) && ($subpathURL != '/')) {
  45.             $rootURL = substr($rootURL, 0, -1 * strlen($subpathURL));
  46.         }
  47.  
  48.         $Language = & JFactory::getLanguage();
  49.  
  50.         $data = (object)array(
  51.             'projectid'     => $this->params->get('project_id'),
  52.             'sign_password' => $this->params->get('project_password'),
  53.  
  54.             'orderid'       => $subscription->akeebasubs_subscription_id,
  55.             'amount'        => $subscription->net_amount + $subscription->tax_amount,
  56.             'currency'      => strtoupper(AkeebasubsHelperCparams::getParam('currency','LTL')),
  57.             'lang'          => substr($Language->get('name'), 0, 3),
  58.  
  59.             'accepturl'     => $rootURL.str_replace('&amp;','&',JRoute::_('index.php?option=com_akeebasubs&view=message&slug='.$slug.'&layout=order&subid='.$subscription->akeebasubs_subscription_id)),
  60.             'cancelurl'     => $rootURL.str_replace('&amp;','&',JRoute::_('index.php?option=com_akeebasubs&view=message&slug='.$slug.'&layout=cancel&subid='.$subscription->akeebasubs_subscription_id)),
  61.             'callbackurl'   => JURI::base().'index.php?option=com_akeebasubs&view=callback&paymentmethod=paysera',
  62.             'payment'       => '',
  63.             'p_countrycode' => 'LT',
  64.  
  65.             'p_firstname'   => $firstName,
  66.             'p_lastname'    => $lastName,
  67.             'p_email'       => $user->email,
  68.             'p_street'      => $kuser->address1.''.$kuser->address2,
  69.             'p_city'        => $kuser->city,
  70.             'p_state'       => $kuser->state,
  71.             'p_zip'         => $kuser->zip,
  72.             'test'          => $this->params->get('test')
  73.         );
  74.  
  75.         require_once('vendor/webtopay/libwebtopay/WebToPay.php');
  76.  
  77.         try {
  78.             $request = WebToPay::buildRequest(array(
  79.                 'projectid'     => $data->projectid,
  80.                 'sign_password' => $data->sign_password,
  81.  
  82.                 'orderid'       => $data->orderid,
  83.                 'amount'        => $data->amount*100,
  84.                 'currency'      => $data->currency,
  85.                 'lang'          => $data->lang,
  86.  
  87.                 'accepturl'     => $data->accepturl,
  88.                 'cancelurl'     => $data->cancelurl,
  89.                 'callbackurl'   => $data->callbackurl,
  90.                 'payment'       => $data->payment,
  91.                 'p_countrycode' => $data->p_countrycode,
  92.  
  93.                 'p_firstname'   => $data->p_firstname,
  94.                 'p_lastname'    => $data->p_lastname,
  95.                 'p_email'       => $data->p_email,
  96.                 'p_street'      => $data->p_street,
  97.                 'p_city'        => $data->p_city,
  98.                 'p_state'       => $data->p_state,
  99.                 'p_zip'         => $data->p_zip,
  100.                 'test'          => $data->test
  101.             ));
  102.  
  103.         } catch (WebToPayException $e) {
  104.             echo get_class($e) . ': ' . $e->getMessage();
  105.         }
  106.  
  107.         @ob_start();
  108.         include dirname(__FILE__).'/paysera/form.php';
  109.         $html = @ob_get_clean();
  110.  
  111.         return $html;
  112.     }
  113.  
  114.     public function onAKPaymentCallback($paymentmethod, $data)
  115.     {  
  116.  
  117.         $isValid = ($paymentmethod != $this->ppName) ? true : false;
  118.  
  119.         try {
  120.             WebToPay::toggleSS2(true);
  121.             $response = WebToPay::checkResponse($data, array(
  122.                 'projectid'     => $this->params->get('project_id'),
  123.                 'sign_password' => $this->params->get('project_password'),
  124.                
  125.             ));
  126.            
  127.             echo 'OK';
  128.            
  129.            
  130.             $orderId = $response['orderid'];
  131.             $amount = $response['amount'];
  132.             $currency = $response['currency'];
  133.            
  134.  
  135.             JLoader::import('joomla.utilities.date');
  136.  
  137.             // Check if we're supposed to handle this
  138.         //
  139.        
  140.         // Load the relevant subscription row
  141.         if($response) {
  142.             $id = isset($orderId) ? (int)$orderId : -1;
  143.             $subscription = null;
  144.             if($id > 0) {
  145.                 $subscription = F0FModel::getTmpInstance('Subscriptions','AkeebasubsModel')
  146.                     ->setId($id)
  147.                     ->getItem();
  148.                    
  149.                 $isValid = true;   
  150.                 if( ($subscription->akeebasubs_subscription_id <= 0) || ($subscription->akeebasubs_subscription_id != $id) ) {
  151.                     $subscription = null;
  152.                     $isValid = false;
  153.                 }
  154.             } else {
  155.                 $isValid = false;
  156.             }
  157.             if(!$isValid) $response['akeebasubs_failure_reason'] = 'The referenced subscription ID ("transaction_id" field) is invalid';
  158.         }
  159.        
  160.         switch($response['status'])
  161.         {
  162.             case '1':
  163.                 $newStatus = 'C';
  164.                 break;
  165.  
  166.             case '2':
  167.                 $newStatus = 'P';
  168.                 break;
  169.                
  170.             case '3':
  171.                 $newStatus = 'P';
  172.                 break;
  173.  
  174.             case '0':
  175.                 $newStatus = 'X';
  176.                 break;
  177.         }
  178.  
  179.         // Update subscription status (this also automatically calls the plugins)
  180.         $updates = array(
  181.             'akeebasubs_subscription_id' => $orderId, //$id
  182.             'processor_key'     => $response['requestid'],
  183.             'state'             => $newStatus,
  184.             'enabled'           => 1
  185.         );
  186.        
  187.         JLoader::import('joomla.utilities.date');
  188.         if($newStatus == 'C') {
  189.             $this->fixDates($subscription, $updates);
  190.         }
  191.        
  192.         $subscription->save($updates);
  193.  
  194.         // Run the onAKAfterPaymentCallback events
  195.         JLoader::import('joomla.plugin.helper');
  196.         JPluginHelper::importPlugin('akeebasubs');
  197.         $app = JFactory::getApplication();
  198.         $jResponse = $app->triggerEvent('onAKAfterPaymentCallback',array(
  199.             $subscription
  200.         ));
  201.        
  202.        
  203.  
  204.         //@todo: patikrinti, ar užsakymas su $orderId dar nepatvirtintas (callback gali būti pakartotas kelis kartus)
  205.         //@todo: patikrinti, ar užsakymo suma ir valiuta atitinka $amount ir $currency
  206.         //@todo: patvirtinti užsakymą
  207.            
  208.         return true;
  209.         } catch (Exception $e) {
  210.             echo get_class($e) . ': ' . $e->getMessage();
  211.         }
  212.  
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment