在 OpenCart 中設計個人化支付選項:第 3 部分

王林
發布: 2023-09-03 10:42:01
原創
865 人瀏覽過

如果您一直在關注本系列,您應該熟悉我們在後端為自訂付款方式設定的文件結構類型。如果您還沒有閱讀過本系列的前面部分,我強烈建議您在繼續閱讀本系列之前先閱讀它們。

我們也將為前端部分使用類似的檔案設定。

控制器設定

繼續並在 catalog/controller/ payment/custom.php 建立控制器檔案。將以下內容貼到新建立的控制器檔案 custom.php 中。

<?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'] = 'https://yourpaymentgatewayurl';
 
    $this->load->model('checkout/order');
    $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
 
    if ($order_info) {
      $this->data['text_config_one'] = trim($this->config->get('text_config_one')); 
      $this->data['text_config_two'] = trim($this->config->get('text_config_two')); 
      $this->data['orderid'] = date('His') . $this->session->data['order_id'];
      $this->data['callbackurl'] = $this->url->link('payment/custom/callback');
      $this->data['orderdate'] = date('YmdHis');
      $this->data['currency'] = $order_info['currency_code'];
      $this->data['orderamount'] = $this->currency->format($order_info['total'], $this->data['currency'] , false, false);
      $this->data['billemail'] = $order_info['email'];
      $this->data['billphone'] = html_entity_decode($order_info['telephone'], ENT_QUOTES, 'UTF-8');
      $this->data['billaddress'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
      $this->data['billcountry'] = html_entity_decode($order_info['payment_iso_code_2'], ENT_QUOTES, 'UTF-8');
      $this->data['billprovince'] = html_entity_decode($order_info['payment_zone'], ENT_QUOTES, 'UTF-8');;
      $this->data['billcity'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
      $this->data['billpost'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
      $this->data['deliveryname'] = html_entity_decode($order_info['shipping_firstname'] . $order_info['shipping_lastname'], ENT_QUOTES, 'UTF-8');
      $this->data['deliveryaddress'] = html_entity_decode($order_info['shipping_address_1'], ENT_QUOTES, 'UTF-8');
      $this->data['deliverycity'] = html_entity_decode($order_info['shipping_city'], ENT_QUOTES, 'UTF-8');
      $this->data['deliverycountry'] = html_entity_decode($order_info['shipping_iso_code_2'], ENT_QUOTES, 'UTF-8');
      $this->data['deliveryprovince'] = html_entity_decode($order_info['shipping_zone'], ENT_QUOTES, 'UTF-8');
      $this->data['deliveryemail'] = $order_info['email'];
      $this->data['deliveryphone'] = html_entity_decode($order_info['telephone'], ENT_QUOTES, 'UTF-8');
      $this->data['deliverypost'] = html_entity_decode($order_info['shipping_postcode'], ENT_QUOTES, 'UTF-8');
 
      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);
 
    if ($order_info) {
      $data = array_merge($this->request->post,$this->request->get);
 
      //payment was made successfully
      if ($data['status'] == 'Y' || $data['status'] == 'y') {
        // update the order status accordingly
      }
    }
  }
}
?>
登入後複製

如您所見,有兩種不同的方法。 index 方法將負責在表單提交到第三方支付網關時設定數據,而 callback 方法用於處理來自第三方支付網關的回應資料支付網關。話雖如此,如果您的支付網關需要,您可以定義更多方法。在此範例中,我們使流程盡可能簡單。

讓我們詳細了解每個部分。我們將從 index 方法開始。

首先,我們載入了語言檔案並設定了確認按鈕的值。我們也設定了 action 屬性,該屬性將由付款提交表單使用。您應該根據您的支付網關更改此設定。

$this->language->load('payment/custom');
$this->data['button_confirm'] = $this->language->get('button_confirm');
$this->data['action'] = 'https://yourpaymentgatewayurl';
登入後複製

接下來,我們從使用者的活動會話中載入訂單資訊。

$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
登入後複製

如果訂單資訊可用,我們將繼續設定隱藏變數的數據,這些數據將用於將表單提交到支付網關 URL。如果您密切注意程式碼,您會發現我們也使用了自訂參數 text_config_onetext_config_two,我們在管理設定表單中設定了這些參數本系列的前一部分。

這裡需要注意的另一個重要變數是 callbackurl,它保存支付網關在付款過程後將使用者重新導向回我們商店所使用的 URL。是的,查看 URL payment/custom/callback 應表明它將呼叫 callback 方法,正如我們將在此刻看到的那樣。

$this->data['text_config_one'] = trim($this->config->get('text_config_one')); 
$this->data['text_config_two'] = trim($this->config->get('text_config_two')); 
$this->data['orderid'] = date('His') . $this->session->data['order_id'];
$this->data['callbackurl'] = $this->url->link('payment/custom/callback');
$this->data['orderdate'] = date('YmdHis');
$this->data['currency'] = $order_info['currency_code'];
$this->data['orderamount'] = $this->currency->format($order_info['total'], $this->data['currency'] , false, false);
$this->data['billemail'] = $order_info['email'];
$this->data['billphone'] = html_entity_decode($order_info['telephone'], ENT_QUOTES, 'UTF-8');
$this->data['billaddress'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
$this->data['billcountry'] = html_entity_decode($order_info['payment_iso_code_2'], ENT_QUOTES, 'UTF-8');
$this->data['billprovince'] = html_entity_decode($order_info['payment_zone'], ENT_QUOTES, 'UTF-8');;
$this->data['billcity'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
$this->data['billpost'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
$this->data['deliveryname'] = html_entity_decode($order_info['shipping_firstname'] . $order_info['shipping_lastname'], ENT_QUOTES, 'UTF-8');
$this->data['deliveryaddress'] = html_entity_decode($order_info['shipping_address_1'], ENT_QUOTES, 'UTF-8');
$this->data['deliverycity'] = html_entity_decode($order_info['shipping_city'], ENT_QUOTES, 'UTF-8');
$this->data['deliverycountry'] = html_entity_decode($order_info['shipping_iso_code_2'], ENT_QUOTES, 'UTF-8');
$this->data['deliveryprovince'] = html_entity_decode($order_info['shipping_zone'], ENT_QUOTES, 'UTF-8');
$this->data['deliveryemail'] = $order_info['email'];
$this->data['deliveryphone'] = html_entity_decode($order_info['telephone'], ENT_QUOTES, 'UTF-8');
$this->data['deliverypost'] = html_entity_decode($order_info['shipping_postcode'], ENT_QUOTES, 'UTF-8');
登入後複製

最後,我們指派自訂範本檔案 custom.tpl 並渲染視圖。

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();
登入後複製

讓我們回顧一下 callback 方法的程式碼。當使用者從支付網關網站返回商店時,將呼叫此方法。

首先,我們在繼續操作之前檢查 orderid 變數是否可用。如果不可用,我們將停止進一步處理。

if (isset($this->request->post['orderid'])) {
  $order_id = trim(substr(($this->request->post['orderid']), 6));
} else {
  die('Illegal Access');
}
登入後複製

接下來,我們從資料庫載入訂單資訊。最後,我們將檢查支付網關回應中是否有 success 指示器。如果是這樣,我們將繼續相應地更新訂單狀態資訊。

$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($order_id);
 
if ($order_info) {
  $data = array_merge($this->request->post,$this->request->get);
 
  //payment was made succ
  if ($data['status'] == 'Y' || $data['status'] == 'y') {
    // update the order status accordingly
  }
}
登入後複製

這就是控制器設定。很簡單,不是嗎?

傳統模型

您可能知道,OpenCart 有自己的一套處理商店內部運作的慣例和標準。支付方式偵測的模型設定就是這種情況。您只需按照約定進行設置,它就會自動拾取。

繼續並在 catalog/model/ payment/custom.php 建立模型檔案。將以下內容貼到新建立的模型檔案 custom.php 中。

<?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;
  }
}
登入後複製

OpenCart 在結帳過程中列出有效付款方式時將使用此類。在此過程中,OpenCart 從後端收集有效支付方法的列表,並且對於每種方法,它將檢查適當的模型類別是否可用。僅當關聯模型類別可用時才會列出付款方式。

此設定中最重要的是 code 變數的值。在我們的例子中,我們將其定義為custom,這表示當您選擇付款方式並按繼續時,它將呼叫 payment/custom內部URL,最終為我們的支付網關設定表單。

簡而言之,我們可以說它是前端支付方式檢測和正常工作的強製文件。

語言和模板檔案

現在,我們只需要建立語言並查看檔案。繼續並在 catalog/language/english/ payment/custom.php 建立語言檔案。將以下內容貼到新建立的語言檔案 custom.php 中。

<?php
$_['text_title'] = 'Custom Payment Method';
$_['button_confirm'] = 'Confirm Order';
?>
登入後複製

相當容易理解:我們剛剛設定了結帳時將在前端使用的標籤。

繼續並在 catalog/view/theme/default/template/ payment/custom.tpl 建立範本檔案。將以下內容貼到新建立的範本檔案 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; ?>" />
 
  <div class="buttons">
    <div class="right">
      <input type="submit" value="<?php echo $button_confirm; ?>" class="button" />
    </div>
  </div>
</form>
登入後複製

正如您可能已經猜到的,這是當使用者點擊確認訂單按鈕時將提交的表單。我們剛剛設定了隱藏變數及其值,這些變數先前在控制器的 index 方法中定義。

讓我們看看前端的狀況:

在 OpenCart 中设计个性化支付选项:第 3 部分

讓我們快速瀏覽整個流程:

  • 首先,您必須為付款方式設定模型文件,以便可以在第 5 步:付款方式標籤中列出該模型文件。
  • 接下來,當使用者在第五個選項卡中選擇自訂付款方式並點擊繼續按鈕時,OpenCart 會在內部呼叫 payment /custom URL,最終呼叫index 方法並在第六個選項卡中呈現custom.tpl 檔案。
  • 最後,當使用者點擊確認訂單按鈕時,表單將被提交,使用者將被帶到付款網關網站,付款流程將由此開始。付款過程完成後,由於 callbackurl 隱藏變量,用戶將被重定向回我們的網站。當然,如果一切按預期運行,訂單狀態將作為 callback 方法的一部分進行更新。

結論

在本系列中,我解釋瞭如何透過建立自己的付款方式模組來設定幾乎所有付款方式。我希望您喜歡這個系列並學到一些有用的東西。

為任何框架創建自訂內容總是很有趣,不是嗎?請記住,您隨時可以使用下面的評論表提出評論和問題。

以上是在 OpenCart 中設計個人化支付選項:第 3 部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!