【问题标题】:Implementing Manual Checkout API in OpenCart在 OpenCart 中实现手动结账 API
【发布时间】:2014-11-11 02:44:53
【问题描述】:

我目前正在为 OpenCart 开发提交订单 API。这个 API 的目的不是为了 OpenCart 的网页部分,而是为了我们的移动应用与 OpenCart 交互。

根据我的研究,提交订单似乎有两种方式:

方法一:

在 OpenCart 网页上,当您想实际提交订单时,您需要完成以下工作流程:

  1. 通过checkout/cart/add将产品添加到购物车

  2. 通过checkout/payment_address/validate输入支付地址

  3. 通过checkout/shipping_address/validate输入shipping_address

  4. 通过checkout/shipping_method/validate输入送货方式

  5. 通过checkout/payment_method/validate输入支付方式

  6. 通过确认/提交订单(我不知道提交订单实际请求的 URL 是什么)。

这种方法的好处似乎是,在客户已登录的条件下,并且您拥有将商品添加到购物车的 API,您不需要不需要通过 URL 中手动将产品添加到购物车的步骤。您还可以在 payment_address 和 shipping_address 中使用 existing 等字符串来表示现有地址(因此您根本不需要在 URL 中手动输入任何内容)。

方法二:

使用checkout/manual。但是,此方法需要完全手动设置所有内容的输入:

  1. store_id

  2. customer_id

  3. customer_group_id

  4. payment_country_id

  5. payment_zone_id

  6. payment_country_id

  7. order_product[]

  8. order_voucher

  9. shipping_method[]

  10. order_total[contains keys such as code, title, text, value, and sort_order]

  11. 支付[]

  12. 可能遗漏了什么..

checkout/manual 的使用似乎是我们需要的 API,我特别喜欢如果请求不成功它会返回警告/错误消息 - 但是,第一种方法允许客户拥有一个接口,用于将产品添加到购物车并访问现有变量,例如 shipping_address、payment_address 等,前提是他们已登录(我知道有代码可以执行 cart->clear()customer->logout() 如果用户已登录)。

我的问题是 - 以前有没有人实施过手动签出?你是如何实施的?

更新

目前正在编写一个脚本,将一些虚拟输入发布到checkout/manual

public function submit()
{
    # Our new data
    $data = array(
        "store_id" => 0,
        "customer" => "My Name",
        "customer_id" => 1,
        "customer_group_id" => 1,
        "payment_country_id" => 223,
        "payment_zone_id" => 3663,
        "order_product[0][product_id]" => 1791,
        "order_product[0][quantity]" => 1,
        "order_product[0][price]" => 5.01,
        "order_total[0][code]" => "sub_total",
        "order_total[0][title]" => "Sub-Total",
        "order_total[0][text]" => "$5.01",
        "order_total[0][value]" => 5.01,
        "order_total[0][sort_order]" => 1,
        "order_total[1][code]" => "total",
        "order_total[1][title]" => "Total",
        "order_total[1][text]" => "$5.01",
        "order_total[1][value]" => 5.01,
        "order_total[1][sort_order]" => 9,
        "payment_firstname" => "My",
        "payment_lastname" => "Name",
        "payment_company" => "SomeCompany",
        "payment_address_1" => "#123 1234 NameOf street",
        "payment_address_2" => "",
        "payment_postcode" => "12345",
        "payment_city" => "New York",
        "shipping_firstname" => "My",
        "shipping_lastname" => "Name",
        "shipping_company" => "SomeCompany",
        "shipping_address_1" => "#123 1234 NameOf street",
        "shipping_address_2" => "",
        "shipping_postcode" => "12345",
        "shipping_city" => "New York",
        "shipping" => "free.free",
        "shipping_method" => "Free Shipping",
        "shipping_code" => "free.free",
        "shipping_country_id" => 223,
        "shipping_zone_id" => 3663,
        "payment" => "cod",
        "payment_method" => "Cash On Delivery",
        "payment_code" => "cod",
        "order_status_id" => 1
    );

    # Create a connection
    $url = HTTP_SERVER . 'index.php?route=checkout/manual';
    $ch = curl_init($url);

    # Form data string
    $postString = http_build_query($data, '', '&');

    # Setting our options
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    # Get the response
    $response = curl_exec($ch);
    echo $response;
    curl_close($ch);
}

但是,返回的数据是这样的:

{"error":{"warning":"You do not have permission to access this page, please refer to your system administrator."}}

更新 2

由于我在调用checkout/manual 时无法满足这两个条件中的任何一个而引发错误:$this->user->isLogged() && $this->user->hasPermission('modify', 'sale/order')

我通过将其替换为true 暂时将其删除,因此我始终可以手动添加订单。

使用我的输入参数,我确实得到了这个 JSON:

"success":"Order totals has been successfully re-calculated!",然而,检​​查行政命令列表,并没有出现新的命令被添加。

更新 3

尽管可以创建订单,但我不知道如何提交订单。

【问题讨论】:

  • 这种非常骇人听闻的方法与其他前端文件一起使用的唯一原因是它可以更轻松地访问后端不存在的总计模块。它是管理编辑顺序页面的一部分(特别是用于更新总数)。我强烈建议忽略它并创建自己的方法。

标签: php api opencart


【解决方案1】:

首先,注释掉代码$this->user->isLogged() && $this->user->hasPermission('modify', 'sale/order')是很危险的。而是向引用 $this->request->post['customer_id'] 的部分添加某种覆盖,以便您根据实际客户 ID 设置发布数据。如果不是这样,普通客户可以通过发送他们想要的任何POST 数据来欺骗其他客户的登录和结帐数据。

关于实际订单创建,您会注意到checkout/manual 返回一个包含所有必要订单数据的 json。您需要做的就是在if (!isset($json['error'])) {之后添加类似这样的内容

$this->load->model('checkout/order');
$json['order_id'] = $this->model_checkout_order->addOrder($json);

然后将 json 返回到您的移动应用程序并显示确认信息或对订单数据执行任何您想要的操作。在验证是否允许请求和/或链接到适当的客户帐户时发送唯一的移动应用程序密钥也可能会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 2013-05-09
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多