0

0

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

王林

王林

发布时间:2023-09-03 10:41:07

|

1025人浏览过

|

来源于php中文网

原创

如果您一直在关注本系列,您应该熟悉我们在后端为自定义付款方式设置的文件结构类型。如果您还没有阅读过本系列的前面部分,我强烈建议您在继续阅读本系列之前先阅读它们。

我们也将为前端部分使用类似的文件设置。

控制器设置

继续并在 catalog/controller/ payment/custom.php 创建控制器文件。将以下内容粘贴到新创建的控制器文件 custom.php 中。

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 有自己的一套处理商店内部运作的惯例和标准。支付方式检测的模型设置就是这种情况。您只需按照约定进行设置,它就会被自动拾取。

Red Panda AI
Red Panda AI

AI文本生成图像

下载

继续并在 catalog/model/ payment/custom.php 创建模型文件。将以下内容粘贴到新创建的模型文件 custom.php 中。

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 中。


相当容易理解:我们刚刚设置了结账时将在前端使用的标签。

继续并在 catalog/view/theme/default/template/ payment/custom.tpl 创建模板文件。将以下内容粘贴到新创建的模板文件 custom.tpl 中。

正如您可能已经猜到的,这是当用户单击确认订单按钮时将提交的表单。我们刚刚设置了隐藏变量及其值,这些变量先前在控制器的 index 方法中定义。

让我们看看前端的情况:

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

让我们快速浏览一下整个流程:

  • 首先,您必须为付款方式设置模型文件,以便可以在第 5 步:付款方式标签中列出该模型文件。
  • 接下来,当用户在第五个选项卡中选择自定义付款方式并点击继续按钮时,OpenCart 会在内部调用 payment/custom URL,最终调用 index 方法并在第六个选项卡中呈现 custom.tpl 文件。
  • 最后,当用户点击确认订单按钮时,表单将被提交,用户将被带到付款网关网站,付款流程将由此开始。付款过程完成后,由于 callbackurl 隐藏变量,用户将被重定向回我们的网站。当然,如果一切按预期运行,订单状态将作为 callback 方法的一部分进行更新。

结论

在本系列中,我解释了如何通过创建您自己的付款方式模块来设置几乎所有付款方式。我希望您喜欢这个系列并学到一些有用的东西。

为任何框架创建自定义内容总是很有趣,不是吗?请记住,您随时可以使用下面的评论表提出评论和问题。

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

1958

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1282

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1188

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

948

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1400

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1229

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1439

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1303

2023.11.13

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号