英文:
How I can download invoice file by API in PRESTASHOP?
问题
我目前正在开发一个连接到Prestashop API的外部工具(使用JavaScript)。
Prestashop API提供了各种信息,但没有用于下载或生成附件的API。
我需要为每个已验证的订单生成一个PDF文件(而不是默认提供的全局PDF)。
你知道是否有可用的模块可以执行此操作吗?如果没有,你有关于如何生成PDF并通过新模块使其可通过API下载发票的任何想法吗?
英文:
I am currently working on developing an external tool (in JavaScript) connected to the Prestashop APIs.
The Prestashop APIs provide various pieces of information, but there is no API to download or generate attachments.
I need to generate a PDF file per order validated (rather than a global PDF as offered by default).
Do you know if there is a module available to perform this operation? If not, do you have any ideas on how I should proceed to generate PDFs and make them accessible via a new module for dowload invoice by API?
答案1
得分: 1
Prestashop Web Services不允许您下载发票的“副本”。您可以查看发票的详细信息,如已付总额、交付日期等。更多信息可以在Prestashop DevDocs(Webservice Api - 订单发票)中找到。
我们可以构建自己的PHP页面,使用Prestashop的内置函数来制作PDF发票的精确副本。
为此,我们需要使用Prestashop类OrderInvoice.php
和PDF.php
。
我们还希望在其中使用Prestashop的一些标准函数,例如“Context”,因此我们需要将config/config.inc.php
和init.php
都添加到我们的代码中。
此外,我们不希望任何人都能访问此URL。否则,我们将侵犯GDPR。您的发票包含所有客户的敏感地址和付款信息。
在我们的示例中,我们将使用一个名为“secure_key”的参数,只允许授权用户访问您的页面。
为了完成整个过程,还有一点很有趣,我们可以立即指定要下载发票的订单,为此我们将使用一个名为“id_order”的参数。
将所有这些组合在一起,我们可以轻松地将我们的文件getPDFinvoice.php
添加到我们商店的根目录中,然后您可以通过浏览到https://yourshop.com/getPDFinvoice.php?secure_key=YOUR_UNIQE_CODE&id_order=WEBSHOP_ORDER_ID
来访问它。
getPDFinvoice.php
<?php
// 设置与config.inc.php的连接(用于数据库连接,...)
include 'config/config.inc.php';
include 'init.php';
include 'classes/order/OrderInvoice.php';
include 'classes/pdf/PDF.php';
// 声明我们的secure_key。请记住所提供的secure_key仅为示例,请确保自行创建一个。
$secure_key = 'ed3fa1ce558e1cfbaa3f99403';
// 检查secure_key是否正确,如果secure_key未设置或不等于php页面将停止运行。
if (!Tools::getValue('secure_key') || Tools::getValue('secure_key') != $secure_key) die('UNAUTHORIZED: We dont want you on this page!');
$id_order = (int)Tools::getValue('id_order');
if (!$id_order) die("Please provide an order ID!");
$order = new Order($id_order);
if (!Validate::isLoadedObject($order)) die('The order invoice cannot be found within your database.');
$pdf = new PDF($order->getInvoicesCollection(), 'Invoice', Context::getContext()->smarty);
$pdf->render();
请注意:此代码已在使用PHP 8.1构建的Prestashop 8.0.2版本上进行了测试。在使用不同版本时,某些功能可能无法工作或显示某些错误消息。
英文:
Prestashop Web Services does not allow you to download a 'copy' of invoices. You can consult invoice details such as total paid, delivery date, ... more information can be found in the Prestashop DevDocs (Webservice Api - Order Invoices)
We can build our own php page where we use the built-in functions of Prestashop to make an exact copy of the PDF invoices.
For this we have to use the Prestashop classes OrderInvoice.php
and PDF.php
.
We also want to use some of the standard functions of Prestashop such as 'Context' for this we need to add both config/config.inc.php
and init.php
to our code.
Next, we don't want anyone to have access to this url. Because otherwise we commit infringements against the GDPR. Your invoices contain sensitive address and payment details of all your customers.
In our example we are going to use a secure_key
which will allow only authorized users to access your page.
To round off the whole thing, it is also interesting that we can immediately indicate from which order we want to download the invoice, for this we will use a parameter called id_order
.
Putting all this together we can easily add our file getPDFinvoice.php
in the root of our shop, and you can then access it by surfing to https://yourshop.com/getPDFinvoice.php?secure_key=YOUR_UNIQE_CODE&id_order=WEBSHOP_ORDER_ID
.
getPDFinvoice.php
<?php
// Setup connection with config.inc.php (required for database connection, ...)
include 'config/config.inc.php';
include 'init.php';
include 'classes/order/OrderInvoice.php';
include 'classes/pdf/PDF.php';
// Declare our secure_key. Remeber the secure_key provided is an example, make sure to create one by yourself.
$secure_key = 'ed3fa1ce558e1cfbaa3f99403';
// controle if secure_key is correct, if the secure_key is not set our not equal the php page will stop running.
if (!Tools::getValue('secure_key') || Tools::getValue('secure_key') != $secure_key) die('UNAUTHORIZED: We dont want you on this page!');
$id_order = (int)Tools::getValue('id_order');
if (!$id_order) die("Please provide and order ID!");
$order = new Order($id_order);
if (!Validate::isLoadedObject($order)) die('The order invoice cannot be found within your database.');
$pdf = new PDF($order->getInvoicesCollection(), 'Invoice', Context::getContext()->smarty);
$pdf->render();
Please note: This is code has been tested on a Prestashop 8.0.2 build using PHP 8.1. When using a different version, certain functions may not work or certain error messages may be displayed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论