OPENCART MĀJAS LAPAS IZSTRĀDE. JAUNAS MAKSĀŠANAS METODES PIEVIENOŠANA

Dažkārt sanāk tā, ka ir vajadzība pēc maksāšanas metodes kura nav standarta OpenCart piedāvājumā. Piemēram, līzings.

Izstrāde – 2 daļas.

  • Administrēšanas daļas pilnveidošana;
  • Maksāšanas groza papildināšana ar jauno metodi un vajadzīgajiem laukiem;

Administrēšanas daļas pilnveidošana

Jaunā metode paradīsies zem sadaļas Extensions->Payments
modules-payment

Jāizveido jauni faili sekojošās mapēs:
../admin/controller/payment/custom.php

class ControllerPaymentCustom extends Controller {
  private $error = array();
 
  public function index() {
    $this->language->load('payment/custom');
    $this->document->setTitle('Leasing');
    $this->load->model('setting/setting');
 
    if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
      $this->model_setting_setting->editSetting('custom', $this->request->post);
      $this->session->data['success'] = 'Saved.';
      $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
    }
 
    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['entry_text_config_one'] = $this->language->get('text_config_one');
    $this->data['entry_text_config_two'] = $this->language->get('text_config_two');
    $this->data['button_save'] = $this->language->get('text_button_save');
    $this->data['button_cancel'] = $this->language->get('text_button_cancel');
    $this->data['entry_order_status'] = $this->language->get('entry_order_status');
    $this->data['text_enabled'] = $this->language->get('text_enabled');
    $this->data['text_disabled'] = $this->language->get('text_disabled');
    $this->data['entry_status'] = $this->language->get('entry_status');
 
    $this->data['action'] = $this->url->link('payment/custom', 'token=' . $this->session->data['token'], 'SSL');
    $this->data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');
 
    if (isset($this->request->post['text_config_one'])) {
      $this->data['text_config_one'] = $this->request->post['text_config_one'];
    } else {
      $this->data['text_config_one'] = $this->config->get('text_config_one');
    }
        
    if (isset($this->request->post['text_config_two'])) {
      $this->data['text_config_two'] = $this->request->post['text_config_two'];
    } else {
      $this->data['text_config_two'] = $this->config->get('text_config_two');
    }
            
    if (isset($this->request->post['custom_status'])) {
      $this->data['custom_status'] = $this->request->post['custom_status'];
    } else {
      $this->data['custom_status'] = $this->config->get('custom_status');
    }
        
    if (isset($this->request->post['custom_order_status_id'])) {
      $this->data['custom_order_status_id'] = $this->request->post['custom_order_status_id'];
    } else {
      $this->data['custom_order_status_id'] = $this->config->get('custom_order_status_id');
    }
 
    $this->load->model('localisation/order_status');
    $this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
    $this->template = 'payment/custom.tpl';
            
    $this->children = array(
      'common/header',
      'common/footer'
    );
 
    $this->response->setOutput($this->render());
  }
}

../admin/language/english/payment/custom.php

$_['heading_title'] = 'Leasing';
 
$_['text_enabled'] = 'Enabled';
$_['text_disabled'] = 'Disabled';
 
$_['text_config_one'] = 'Parameter one';
$_['text_config_two'] = 'Parameter Two';
 
$_['entry_status'] = 'Status:';
$_['entry_order_status'] = 'Order Status:';
 
$_['text_button_save'] = 'Save';
$_['text_button_cancel'] = 'Cancel';

../admin/model/payment/custom.php

class ModelCustom extends Model {
    
}

../admin/view/template/payment/custom.tpl

<?php echo $header; ?>
<div id="content">
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/payment.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="form">
          <tr>
            <td><?php echo $entry_order_status; ?></td>
            <td><select name="custom_order_status_id">
                <?php foreach ($order_statuses as $order_status) { ?>
                <?php if ($order_status['order_status_id'] == $custom_order_status_id) { ?>
                <option value="<?php echo $order_status['order_status_id']; ?>" selected="selected"><?php echo $order_status['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $order_status['order_status_id']; ?>"><?php echo $order_status['name']; ?></option>
                <?php } ?>
                <?php } ?>
              </select></td>
          </tr>
           
          <tr>
            <td><?php echo $entry_text_config_one; ?></td>
            <td><input type="text" name="text_config_one" value="<?php echo $text_config_one; ?>" size="10" /></td>
          </tr>
           
          <tr>
            <td><?php echo $entry_text_config_two; ?></td>
            <td><input type="text" name="text_config_two" value="<?php echo $text_config_two; ?>" size="10" /></td>
          </tr>
           
          <tr>
            <td><?php echo $entry_status; ?></td>
            <td><select name="custom_status">
                <?php if ($custom_status) { ?>
                <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                <option value="0"><?php echo $text_disabled; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_enabled; ?></option>
                <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                <?php } ?>
              </select></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<?php echo $footer; ?>

Pēс šīs manipulācijas paradīsies jaunā maksāšanas metode administrēšanas daļā, kuru vienkarši jāuzinstalē un pēс tam jāieslēdz.

Maksāšanas groza papildināšana ar jauno metodi un vajadzīgajiem laukiem

Jāizveido jauni faili sekojošās mapēs:
../catalog/controler/payment/custom.php

class ControllerPaymentCustom extends Controller {
  protected function index() {
    $this->language->load('payment/custom');
    $this->data['button_confirm'] = $this->language->get('button_confirm');
    $this->data['action'] = '/index.php?route=payment/custom/callback';
  
    $this->load->model('checkout/order');
    $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  
    $this->data['text_payment_method'] = $this->language->get('text_payment_method');
    Parēje lauki, kuri izmantoti priekš metodes
    
    if ($order_info) {
      if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/custom.tpl')){
        $this->template = $this->config->get('config_template') . '/template/payment/custom.tpl';
      } else {
        $this->template = 'default/template/payment/custom.tpl';
      }
      $this->render();
    }
  }
  
  public function callback() {
    if (isset($this->request->post['orderid'])) {
      $order_id = trim(substr(($this->request->post['orderid']), 6));
    } else {
      die('Illegal Access');
    }
  
    $this->load->model('checkout/order');
    $order_info = $this->model_checkout_order->getOrder($order_id);
    
    $this->model_checkout_order->confirm($order_id, $this->config->get('custom_order_status_id'));
    $subject = "Līzinga pieteikums. Pasūtijuma numurs: ".$order_info['order_id'];
    $text="";
    // Text
    $mail = new Mail();
    $mail->protocol = $this->config->get('config_mail_protocol');
    $mail->parameter = $this->config->get('config_mail_parameter');
    $mail->hostname = $this->config->get('config_smtp_host');
    $mail->username = $this->config->get('config_smtp_username');
    $mail->password = $this->config->get('config_smtp_password');
    $mail->port = $this->config->get('config_smtp_port');
    $mail->timeout = $this->config->get('config_smtp_timeout');
    $mail->setTo($this->config->get('config_email'));
    $mail->setFrom($this->config->get('config_email'));
    $mail->setSender($order_info['store_name']);
    $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
    $mail->setText(html_entity_decode($text, ENT_QUOTES, 'UTF-8'));
    $mail->send();
    
    // Send to additional alert emails
    $emails = explode(',', $this->config->get('config_alert_emails'));
    
    foreach ($emails as $email) {
        if ($email && preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $email)) {
            $mail->setTo($email);
            $mail->send();
        }
    }
    $this->redirect($this->url->link('checkout/success'));
  }
}

../catalog/language/…/payment/custom.php

$_['heading_title'] = 'Custom Payment Method';
$_['text_title'] = 'Lizings';
 
$_['text_enabled'] = 'Enabled';
$_['text_disabled'] = 'Disabled';
 
$_['text_config_one'] = 'Parameter one';
$_['text_config_two'] = 'Parameter Two';
 
$_['entry_status'] = 'Status:';
$_['entry_order_status'] = 'Order Status:';
 
$_['text_button_save'] = 'Save';
$_['text_button_cancel'] = 'Cancel';

../catalog/model/payment/custom.php

class ModelPaymentCustom extends Model {
  public function getMethod($address, $total) {
    $this->load->language('payment/custom');
  
    $method_data = array(
      'code'     => 'custom',
      'title'    => $this->language->get('text_title'),
      'sort_order' => $this->config->get('custom_sort_order')
    );
  
    return $method_data;
  }
}

../catalog/view/theme/XXX/template/payment/custom.tpl

<form action="<?php echo $action; ?>" method="post">
<input type="hidden" name="text_config_one" value="<?php echo $text_config_one; ?>" />
  <input type="hidden" name="text_config_two" value="<?php echo $text_config_two; ?>" />
  <input type="hidden" name="orderid" value="<?php echo $orderid; ?>" />
  <input type="hidden" name="callbackurl" value="<?php echo $callbackurl; ?>" />
  <input type="hidden" name="orderdate" value="<?php echo $orderdate; ?>" />
  <input type="hidden" name="currency" value="<?php echo $currency; ?>" />
  <input type="hidden" name="orderamount" value="<?php echo $orderamount; ?>" />
  <input type="hidden" name="billemail" value="<?php echo $billemail; ?>" />
  <input type="hidden" name="billphone" value="<?php echo $billphone; ?>" />
  <input type="hidden" name="billaddress" value="<?php echo $billaddress; ?>" />
  <input type="hidden" name="billcountry" value="<?php echo $billcountry; ?>" />
  <input type="hidden" name="billprovince" value="<?php echo $billprovince; ?>" />
  <input type="hidden" name="billcity" value="<?php echo $billcity; ?>" />
  <input type="hidden" name="billpost" value="<?php echo $billpost; ?>" />
  <input type="hidden" name="deliveryname" value="<?php echo $deliveryname; ?>" />
  <input type="hidden" name="deliveryaddress" value="<?php echo $deliveryaddress; ?>" />
  <input type="hidden" name="deliverycity" value="<?php echo $deliverycity; ?>" />
  <input type="hidden" name="deliverycountry" value="<?php echo $deliverycountry; ?>" />
  <input type="hidden" name="deliveryprovince" value="<?php echo $deliveryprovince; ?>" />
  <input type="hidden" name="deliveryemail" value="<?php echo $deliveryemail; ?>" />
  <input type="hidden" name="deliveryphone" value="<?php echo $deliveryphone; ?>" />
  <input type="hidden" name="deliverypost" value="<?php echo $deliverypost; ?>" />
  
Formas lauki, izmantoti pie maksāšanas veida
        
  
  <div class="buttons">
    <div class="right">
      <input type="submit" value="<?php echo $button_confirm; ?>" class="button" id="button-confirm" />
    </div>
  </div>
</form>

Tekošā loģika darbojas tā – pircējs maksāšanas grozā izvēlas jauno metodi (Līzings) un nakošajā solī tiek piedāvats aizpildīt vajadzīgos laukus. Aizpildot laukus un apstiprinot, pasūtijums tiek izveidots ar attiecīgo statusu (Procesā) un visu lauku informācija tiek nosūtīta uz veikala administratora epastu.

Pilnveidot loģiku var pēс savas velēšānās, šis ir tikai viens no izmantošanas scenārijiem.