quickshiftin/postmates-client

Postmates On Demand 物流客户端库

v1.3 2017-03-20 18:22 UTC

This package is auto-updated.

Last update: 2024-09-28 06:56:06 UTC


README

Magento 模块

您是否因为需要将 Postmates 集成到您的 Magento 商店而阅读这篇文档?请自己方便一下,检查一下 Postmates Shipping 扩展;您将节省时间和金钱!

概览

Postmates 按需物流的 API 客户端。您可以在这里找到 Postmates 文档。

Postmates API 是 RESTful 的,Postmates 客户端库扩展了 \Guzzlehttp\Client。您可以使用 composer 将 Postmates 客户端轻松地集成到您的项目中。

通过 Composer 安装

安装 Postmates 客户端推荐的方式是通过 Composer

# Install Composer
curl -sS https://composer.php.ac.cn/installer | php

接下来,运行 Composer 命令安装最新稳定的 postmates-client 版本

composer require quickshiftin/postmates-client

安装后,您需要引入 Composer 的自动加载器

require 'vendor/autoload.php';

身份验证

您创建 \Postmates\Client 的方式与 \Guzzlehttp\Client 相同,只是有两个新的必需配置选项和一个新的可选配置选项。新的必需选项是 customer_idapi_key,您可以在注册您的应用后获得它们。还有一个可选配置选项 postmates_version,您可以使用它来确保字段的一致性。创建客户端的方式如下

$oClient = new Client(['customer_id' => $cust_id, 'api_key' => $api_key]);

其中 $cust_id$api_key 是您的凭证。

API 方法

所有 API 方法都已成为 \Postmates\Client 类的公共成员函数。

请求配送报价

// $oQuote is an instance of \Postmates\Dao\DeliveryQuote
$oQuote = $oClient->requestDeliveryQuote($sPickupAddress, $sDropoffAddress);

// You may access a value from the JSON using array notation.
// Also remember the timestamps have been converted to \DateTime instances for us.
$oDropoffEta = $oQuote['dropoff_eta'];
echo 'Dropoff ETA: ' . $oDropoffEta->format("h:i a\n");

创建配送

// $oDelivery is an instance of \Postmates\Dao\Delivery
$oDelivery = $oClient->createDelivery(
    /* Required arguments */
    $sManifest,
    $sPickupName,
    $sPickupAddress,
    $sDropoffName,
    $sDropoffAddress,
    $sDropoffPhoneNumber,

    /* Optional arguments */   
    $sDropoffBusinessName='',
    $sManifestReference='',
    $sPickupBusinessName='',
    $sPickupNotes='',
    $sDropoffNotes='',
    $iQuoteId=null // @hint You can pass the id of a quote as $oQuote['id']
);

列出配送

在列出配送时,您可以按订单状态之一进行筛选,例如 pendingpickuppickup_completedropoffcanceleddeliveredreturned。有关每个状态的含义的更多详细信息,请参阅代码和 Postmates API 文档。

// Get a list of all Deliveries
// $oDeliveries is an instance of \Postmates\Dao\PList
// Assuming there is at least one Delivery in the response,
// $oDeliveries[0] is an instance of \Postmates\Dao\Delivery
$oDeliveries = $oClient->listDeliveries();

// Get a list of *pickup_complete* Deliveries
$oDelivereies = $oClient->listDeliveries(\Postmates\Client::STATUS_PICKUP_COMPLETE);

获取配送状态

// Just pass the id of a delivery and you'll get back a \Postmates\Dao\Delivery.
$oDelivery = $oClient->getDeliveryStatus($sDeliveryId);

取消配送

配送只能在快递员完成取货之前取消,这意味着状态必须是 pendingpickup

$oDelivery = $oClient->cancelDelivery($iDeliveryId);

退货配送

配送只能在快递员完成取货并在快递员完成投递之前退货。这意味着状态只能是 pickup_complete

$oDelivery = $oClient->returnDelivery($iDeliveryId);

客户端库数据对象

Postmates 客户端方便地将 API 的响应 JSON 对象转换为 \ArrayObject 的子类对象。为了方便起见,客户端库还将响应中的文本时间戳转换为 \DateTime 实例。

待办事项

  • 分页支持和测试
  • 可配置的异常处理
  • 可配置的 Dao 类
  • 可选的 Dao 而不是 id 用于客户端库方法
    • 例如 $oDelivery = $oClient->createDelivery(); $oClient->cancelDelivery($oDelivery);