Mengintegrasikan Bitcoin Payment Gateway ke OpenCart Part 1

Update Terakhir pada

Dalam seri ini, kita akan melihat membangun sistem pembayaran Bitcoin ke OpenCart yang sudah terinstal. Sebelum kita mulai, saya sarankan Anda pelajari dan berlatih mengenai dasar pengembangan modul OpenCart j, agar terbiasa membangun modul Anda sendiri.

Karena pada pembahasan ini akan berkaitan , Maka dari itu Anda harus memiliki pengetahuan yang cukup untuk dalam pembuatan modul.

Start

Sebelum kita mulai membangun ekstensi, buka folder Opencart dikomputer Anda. Selanjutnya, download API Perpustakaan BitPay dari lampiran artikel dan copas ke folder opencart Anda seperti yang ditunjukkan pada gambar berikut:


Untuk mengkonfigurasi dan mengatur Modul Bitcoin Payment kita, kita perlu sebuah antarmuka admin. Untuk melakukan hal ini, kita akan mengikuti pola MVC untuk menerapkan panel admin kami.

1. Buat Controller Admin
1. Arahkan ke admin/controller/payment .
2. Buat file PHP dan beri nama bitpay.php .
3. Buka file di IDE favorit Anda dan membuat kelas setelah kelas OpenCart konvensi penamaan, misalnya: class Controller Payment Bitpay extends Controller {} .
4. Di dalam kelas controller, menentukan standar index fungsi.

5. Kemudian, di dalam fungsi ini, mulai menulis kode yang ditampilkan di bawah ini.

1.1. Loading Bahasa dan Model

$this->language->load('payment/bitpay');
$this->load->model('localisation/order_status'); // loading the model to get system Order Statuses

1.2. Mengatur Page Title

Untuk mengatur Judul Halaman dari dokumen yang kita gunakan kode berikut:

$this->document->setTitle($this->language->get('text_title')); // Setting the document heading title

1.3. Parsing Controllers Elements to View

/* Assigning and parsing the from values  */
 
$this->data['heading_title']           = $this->language->get('text_title');
$this->data['text_enabled']            = $this->language->get('text_enabled'); // gets default language text
$this->data['text_disabled']           = $this->language->get('text_disabled'); // gets default language text
$this->data['text_high']               = $this->language->get('text_high');
$this->data['text_medium']             = $this->language->get('text_medium');
$this->data['text_low']                = $this->language->get('text_low');
$this->data['api_key']                 = $this->language->get('text_api_key');
$this->data['confirmed_status']        = $this->language->get('text_confirmed_status');
$this->data['invalid_status']          = $this->language->get('text_invalid_status');
$this->data['transaction_speed']       = $this->language->get('text_transaction_speed');
$this->data['test_mode']               = $this->language->get('text_test_mode');
$this->data['status']                  = $this->language->get('text_status');
$this->data['sort_order']              = $this->language->get('text_sort_order');
$this->data['button_save']             = $this->language->get('button_save'); // gets default language text
$this->data['button_cancel']           = $this->language->get('button_cancel'); // gets default language text
$this->data['tab_general']             = $this->language->get('tab_general'); // gets default language text
 
/* End Text Assignation */

1.4. Routing Save & Cancel Buttons

$this->data['action'] = HTTPS_SERVER . 'index.php?route=payment/'.$this->payment_module_name.'&token=' . $this->session->data['token'];
$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token'];

1.5. Getting Available Order Statuses
Kode berikut mendapat semua status pesanan dari Lokalisasi-Model (yang pra-kode ke OpenCart) dan mem-parsing untuk melihat:

$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

1.6. Fetching Data From the Database

/* Fetching the Field Values saved in Database*/
$this->data['bitpay_api_key'] = $this->config->get('bitpay_api_key');
 
$this->data['bitpay_confirmed_status_id'] = $this->config->get('bitpay_confirmed_status_id'); 
 
$this->data['bitpay_invalid_status_id'] = $this->config->get('bitpay_invalid_status_id'); 
 
$this->data['bitpay_transaction_speed'] = $this->config->get('bitpay_transaction_speed'); 
 
$this->data['bitpay_test_mode'] = $this->config->get('bitpay_test_mode'); 
 
$this->data['bitpay_status'] = $this->config->get('bitpay_status');
 
$this->data['bitpay_sort_order'] = $this->config->get('bitpay_sort_order');
/* Database fields fetching ends */

1.7. Rendering the View Template
Kode berikut menambahkan anak-anak untuk tampilan template kita dan menjadikan itu untuk tampilan pengguna.

$this->template = 'payment/bitpay.tpl'; // Template Assigning
$this->children = array(
'common/header',    
'common/footer'   
); // Provide the list of template childeren
 
$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); // Rendering the Template Output

2. Membuat Admin View
1. Arahkan ke admin/view/template/payment .
2. Buat file dan nama itu bitpay.tpl .
3. Anda dapat menempatkan HTML Anda sendiri. Dalam kasus saya, saya sudah melakukan itu menggunakan langkah-langkah berikut.

2.1. Adding Header Child

Untuk memuat header yang kami melewati sebagai seorang child di 1.6:

<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"><?php echo $header;</span> <? Php echo $ kepala;</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">?></span> ?></span>

2.2 Warning Block

Peringatan Blok adalah bagian div yang akan menampilkan peringatan pengguna jika ada:

<?php if ($error_warning) {  ?> <div class="warning"><?php echo $error_warning; ?></div><?php } ?>

The Complete Template

<div id="content">
   
<?php
/* Warning Block*/
if ($error_warning) {  ?>
<div class="warning"><?php echo $error_warning; ?></div>
<?php }
/* Warning Block Ends */
 ?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1><?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><span><?php echo $button_save; ?></span></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><span><?php echo $button_cancel; ?></span></a></div>
    </div>
  <div class="content">
    <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
      <table class="form">
        <tr>
          <td><span class="required">*</span> <?php echo $api_key; ?></td>
          <td><input type="text" name="bitpay_api_key" value="<?php echo $bitpay_api_key; ?>" style="width:300px;" />
            <?php
            // API Key Error case block
            if ($error_api_key) { ?>
            <span class="error"><?php echo $error_api_key; ?></span>
            <?php }
            // Error Case Block Ends
             ?></td>
        </tr>
         <tr>
            <td><?php echo $confirmed_status; ?></td>
            <td><select name="bitpay_confirmed_status_id">
                <?php foreach ($order_statuses as $order_status) { ?>
                <?php if ($order_status['order_status_id'] == $bitpay_confirmed_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 $invalid_status; ?></td>
            <td><select name="bitpay_invalid_status_id">
                <?php foreach ($order_statuses as $order_status) { ?>
                <?php if ($order_status['order_status_id'] == $bitpay_invalid_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 $transaction_speed; ?></td>
            <td><select name="bitpay_transaction_speed">
                <?php if ($bitpay_transaction_speed == 'high') { ?>
                <option value="high" selected="selected"><?php echo $text_high; ?></option>
                <?php } else { ?>
                <option value="high"><?php echo $text_high; ?></option>
                <?php } ?>
                <?php if ($bitpay_transaction_speed == 'medium') { ?>
                <option value="medium" selected="selected"><?php echo $text_medium; ?></option>
                <?php } else { ?>
                <option value="medium"><?php echo $text_medium; ?></option>
                <?php } ?>
                <?php if ($bitpay_transaction_speed == 'low') { ?>
                <option value="low" selected="selected"><?php echo $text_low; ?></option>
                <?php } else { ?>
                <option value="low"><?php echo $text_low; ?></option>
                <?php } ?>
              </select></td>
          </tr>
          <tr>
            <td><?php echo $test_mode; ?></td>
            <td><select name="bitpay_test_mode"> 
                <?php if ($bitpay_test_mode) { ?>
                <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>
        <tr>
          <td><?php echo $status; ?></td>
          <td><select name="bitpay_status"> 
              <?php if ($bitpay_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>
        <tr>
          <td><?php echo $sort_order; ?></td>
          <td><input type="text" name="bitpay_sort_order" value="<?php echo $bitpay_sort_order; ?>" size="1" /></td>
        </tr>
      </table>
    </form>
  </div>
</div>
</div>
<?php echo $footer; //loading the footer ?>

Pada titik ini, kita sudah selesai dengan tata letak modul. Sekarang waktunya untuk menyelamatkan data Modul ke dalam database. Jadi kita akan menempatkan kode kecil di dalam controller untuk tujuan ini.

Menyimpan Data

Di dalam fungsi default kontroler Anda, tempatkan beberapa kode serialisasi seperti yang ditunjukkan di bawah ini:

if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) // Checking if the data is coming through post and validate it
        {
            $this->model_setting_setting->editSetting($this->payment_module_name, $this->request->post); // saving all the data into  the database
            $this->session->data['success'] = $this->language->get('text_success'); // Success Identifier
            $this->redirect(HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token']); // Redirecting to the payment modules list
        }

Pengenalan Validasi

Berikut ini adalah fungsi validasi yang memvalidasi data yang diperlukan:

<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private $error = array();</span> swasta $ error = array ();</span> <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// All errors will be saved into this array</span> // Semua kesalahan akan disimpan ke dalam array ini</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">private function validate()</span> fungsi swasta validate ()</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// User Module Permission Check</span> // Pengguna Modul Perijinan</span> 
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (!$this->user->hasPermission('modify', 'payment/'.$this->payment_module_name))</span> if (! $ this-> user-> hasPermission ('memodifikasi', 'pembayaran /'.$ this-> payment_module_name))</span> 
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">$this->error['warning'] = $this->language->get('error_permission');</span> $ This-> kesalahan ['peringatan'] = $ this-> language-> get ('error_permission');</span> 
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// User Module Permission Check Ends</span> // Pengguna Modul Izin Periksa Berakhir</span>
    
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// Check if the API Key is not empty</span> // Periksa apakah kunci API tidak kosong</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (!$this->request->post['bitpay_api_key'])</span> if (! $ this-> request-> pos ['bitpay_api_key'])</span> 
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">$this->error['api_key'] = $this->language->get('error_api_key');</span> $ This-> kesalahan ['api_key'] = $ this-> language-> get ('error_api_key');</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// Api Check ends here</span> // Api Periksa berakhir di sini</span>
 
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// If there is no error, the function returns True else False</span> // Jika tidak ada kesalahan, fungsi mengembalikan Benar lain Salah</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">if (!$this->error)</span> if (! $ this-> error)</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return TRUE;</span> mengembalikan TRUE;</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">else</span> lain</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">{</span> {</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">return FALSE;</span> kembali PALSU;</span>
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>    
       <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">// Boolean Check Ends here</span> // Boolean Periksa Berakhir di sini</span>
   <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">}</span> }</span>

Kita telah selesai membuat tata letak modul pembayaran untuk dashboard, dan Anda harus dapat menguji-drive panel admin. Ingatlah untuk memberikan izin kepada pengguna administrasi sebelum pengujian.

Kesimpulan

Dalam artikel ini, kami memperkenalkan bagian administrasi modul pembayaran. Perhatikan bahwa itu adalah ide yang baik untuk memberikan administrator situs kontrol lebih melalui antarmuka dari, katakanlah, untuk meminta mereka menulis dalam kode.

Dalam artikel selanjutnya, kita akan menerapkan modul pembayaran sehubungan dengan front-end.