PHP Quickbooks SDK - 批量请求和处理失败

PHP Quickbooks SDK - Batch requests and handling failures

我正在构建一个通过 SDK 连接到 Quickbooks API 的小应用程序。 SDK 提供批处理操作以帮助减少所需的 API 请求数。

但是,我希望发出大量请求(即:批量删除、上传 100 秒/1000 秒)。我已经让删除工作,但是,现在我希望整合 Laravel 的 Queue system so that any items in the $batch that fail (due to these business-rules 或其他原因)被发送给工作人员,他将在等待一分钟后重新尝试删除。

下面是一个删除请求的例子。

class QuickBooksAPIController extends Controller
{
    public function batchDelete(Request $request, $category)
    {

        $chunks = array_chunk($request->data, 30);

        foreach ($chunks as $key => $value) {
            $batch[$key] = $this->dataService()->CreateNewBatch();

            foreach ($value as $id) {
                $item = $this->dataService()->FindById($category, $id);
                $batch[$key]->AddEntity($item, $id, "delete");
            }

            $batch[$key]->Execute();
        }

        return response()->json(['message' => 'Items Deleted'], 200);
    }
}

虽然我的场景的文档有点稀疏。如何获取失败的批次订单重试?

在这里使用批处理是正确的选择吗?因为无论如何我都必须点击 API 才能获得 $item... 这对我来说没有意义(我认为我在那里做错了)。

编辑: 我故意发送了一个包含超过 30 个项目的请求,这是失败消息。哪个没有没有成功的价值观。

编辑#2:

最终使用 array_chunk 将负载分成 30 项(这是 API 的限制)。这样做有助于处理许多请求。我已经调整了上面的代码以代表我当前的代码。

How can I get the failed batch items on order to try again?

如果您查看 Intuit 的文档,您会发现 API returns 的 HTTP 响应包含此信息。这是他们显示的 request 示例:

{
  "BatchItemRequest": [
    {
      "bId": "bid1", 
      "Vendor": {
        "DisplayName": "Smith Family Store"
      }, 
      "operation": "create"
    }, 
    {
      "bId": "bid2", 
      "operation": "delete", 
      "Invoice": {
        "SyncToken": "0", 
        "Id": "129"
      }
    }, 
    {
      "SalesReceipt": {
        "PrivateNote": "A private note.", 
        "SyncToken": "0", 
        "domain": "QBO", 
        "Id": "11", 
        "sparse": true
      }, 
      "bId": "bid3", 
      "operation": "update"
    }, 
    {
      "Query": "select * from SalesReceipt where TotalAmt > '300.00'", 
      "bId": "bid4"
    }
  ]
}

以及对应的回复:

{
  "BatchItemResponse": [
    {
      "Fault": {
        "type": "ValidationFault", 
        "Error": [
          {
            "Message": "Duplicate Name Exists Error", 
            "code": "6240", 
            "Detail": "The name supplied already exists. : Another customer, vendor or employee is already using this \nname. Please use a different name.", 
            "element": ""
          }
        ]
      }, 
      "bId": "bid1"
    }, 
    {
      "Fault": {
        "type": "ValidationFault", 
        "Error": [
          {
            "Message": "Object Not Found", 
            "code": "610", 
            "Detail": "Object Not Found : Something you're trying to use has been made inactive. Check the fields with accounts, customers, items, vendors or employees.", 
            "element": ""
          }
        ]
      }, 
      "bId": "bid2"
    }, 
    {
      "Fault": {
        "type": "ValidationFault", 
        "Error": [
          {
            "Message": "Stale Object Error", 
            "code": "5010", 
            "Detail": "Stale Object Error : You and root were working on this at the same time. root finished before you did, so your work was not saved.", 
            "element": ""
          }
        ]
      }, 
      "bId": "bid3"
    }, 
    {
      "bId": "bid4", 
      "QueryResponse": {
        "SalesReceipt": [
          {
            "TxnDate": "2015-08-25", 
            "domain": "QBO", 
            "CurrencyRef": {
              "name": "United States Dollar", 
              "value": "USD"
            }, 
            "PrintStatus": "NotSet", 
            "PaymentRefNum": "10264", 
            "TotalAmt": 337.5, 
            "Line": [
              {
                "Description": "Custom Design", 
                "DetailType": "SalesItemLineDetail", 
                "SalesItemLineDetail": {
                  "TaxCodeRef": {
                    "value": "NON"
                  }, 
                  "Qty": 4.5, 
                  "UnitPrice": 75, 
                  "ItemRef": {
                    "name": "Design", 
                    "value": "4"
                  }
                }, 
                "LineNum": 1, 
                "Amount": 337.5, 
                "Id": "1"
              }, 
              {
                "DetailType": "SubTotalLineDetail", 
                "Amount": 337.5, 
                "SubTotalLineDetail": {}
              }
            ], 
            "ApplyTaxAfterDiscount": false, 
            "DocNumber": "1003", 
            "PrivateNote": "A private note.", 
            "sparse": false, 
            "DepositToAccountRef": {
              "name": "Checking", 
              "value": "35"
            }, 
            "CustomerMemo": {
              "value": "Thank you for your business and have a great day!"
            }, 
            "Balance": 0, 
            "CustomerRef": {
              "name": "Dylan Sollfrank", 
              "value": "6"
            }, 
            "TxnTaxDetail": {
              "TotalTax": 0
            }, 
            "SyncToken": "1", 
            "PaymentMethodRef": {
              "name": "Check", 
              "value": "2"
            }, 
            "EmailStatus": "NotSet", 
            "BillAddr": {
              "Lat": "INVALID", 
              "Long": "INVALID", 
              "Id": "49", 
              "Line1": "Dylan Sollfrank"
            }, 
            "MetaData": {
              "CreateTime": "2015-08-27T14:59:48-07:00", 
              "LastUpdatedTime": "2016-04-15T09:01:10-07:00"
            }, 
            "CustomField": [
              {
                "DefinitionId": "1", 
                "Type": "StringType", 
                "Name": "Crew #"
              }
            ], 
            "Id": "11"
          }
        ], 
        "startPosition": 1, 
        "maxResults": 1
      }
    }
  ], 
  "time": "2016-04-15T09:01:18.141-07:00"
}

注意每个请求的单独响应对象。

bId 值是一个唯一值你在请求中发送,然后在响应中回显给你,所以你可以匹配您发送的请求以及您收到的回复。

这是文档:

Is using batches even the right choice here?

当你同时做很多事情时,批处理很有意义。

您尝试使用它们的方式...很奇怪。你可能应该做的是:

Batch 1
 - go find all your items

Batch 2
- delete all the items

您现有的代码没有意义,因为您正试图 查找 项目并删除 项目同一批 HTTP 请求,这是不可能通过 API.

I intentionally sent out a request with more then 30 items and this is the failure message.

不,不是。这是一条 PHP 错误消息 - 您的代码有错误。

您需要修复 PHP 错误,然后查看您从 API 返回的实际响应。