robinhq/api

ROBIN API 的 PHP 封装


README

这个库在用户友好的界面后面封装了 ROBIN Api。

基本概念

本质上,这个库可以将您的电子商务解决方案中的客户和订单直接发送到 ROBIN。ROBIN API 期望这些客户和订单以特定的格式进行格式化,这也可以帮助您将内部的订单和客户对象转换为 ROBIN 可以理解的对象。

对象

ROBIN API 期望两种不同的对象集合,即客户和订单。以下我将解释如何创建这两个对象。

客户

要创建 ROBIN 客户对象,您只需执行以下操作

<?php
use Robin\Api\Models\Customer;
use Robin\Api\Views\Panel;

$panel = Panel::make($totalOrders, $totalSpend);
$robinCustomer = Customer::make($email, $customerSince, $ordersCount, $totalSpent, $panel);

要发送客户(或客户集合),您应该始终创建一个 Customers 集合

<?php

$robinCustomer = //...

$customers = new Robin\Api\Collections\Customers([$robinCustomer]);

现在,您可以使用以下代码行将客户集合发送到 ROBIN

<?php
//...

$robin = new Robin\Api\Robin($key, $secret, $url);

$response = $robin->customers($customers); //returns Psr\Http\Message\ResponseInterface

所以,要组合所有这些

<?php
use Robin\Api\Robin;
use Robin\Api\Models\Customer;
use Robin\Api\Views\Panel;
use Robin\Api\Collections\Customers;

$robin = new Robin($key, $secret, $url);
$panel = Panel::make($totalOrders, $totalSpend);
$robinCustomer = Customer::make($email, $customerSince, $ordersCount, $totalSpend, $panel);
$customers = new Customers([$robinCustomer]);

$response = $robin->customers($customers);

Customers 集合使得向 ROBIN 发送更多客户变得非常容易。只需遍历您现有的客户,并将 ROBIN 客户推入 Customers 集合,就像这样

<?php
use Robin\Api\Robin;
use Robin\Api\Models\Customer;
use Robin\Api\Views\Panel;
use Robin\Api\Collections\Customers;

$robin = new Robin($key, $secret, $url);
$shopCustomers = $shop->allCustomers();
$robinCustomers = new Customers();

foreach($shopCustomers as $shopCustomer){
    $panel = Panel::make($shopCustomer->totalOrders, $shopCustomer->totalSpend);
    $robinCustomer = Customer::make(
        $shopCustomer->email, 
        $shopCustomer->createdAt,
        $shopCustomer->totalOrders, 
        $shopCustomer->totalSpend, 
        $panel
    );
    $robinCustomers->push($robinCustomer);
    
    $response = $robin->customers($robinCustomers);
}

订单

由于 API 期望以特定的方式提供数据,因此 ROBIN API 的订单对象理解起来有点困难。订单的创建方式如下

<?php
 Order::make(
        $number,
        $email,
        $createdAt,
        $price,
        $editUrl,
        $listView,
        $detailsView
    );

其中 $listViewListView 类的实例,而 $detailsViewDetailsView 类的实例。要了解更多关于这两个对象的信息,请参阅 ROBIN API 文档。

ListView 是表示订单在列表中查看时的数据的对象。该对象包含订单号、日期和状态。创建 ListView 的方式如下

<?php
use Robin\Api\Models\Views\ListView;

$listView = ListView::make($orderNumber, $date, $status);

到目前为止,这并不难。更困难的部分是我们将如何将详细信息视图添加到 ROBIN 订单中。在详细信息视图中,可以包含任何类型的对象。这个库提供了 OrderDetailsProductsInvoicesShipment 详细信息。

要创建这些详细信息,我们首先必须创建包含所有详细信息的 DetailsView 集合。

<?php
use Robin\Api\Collections\DetailsView;

$detailsView = new DetailsView();

接下来创建订单详细信息,并将它们添加到 DetailsView

<?php
use Robin\Api\Models\Views\Details\OrderDetails;

$orderDetails = OrderDetails::make($date, $status, $paymentStatus, $shipmentStatus);
$detailsView->addDetails($orderDetails);

这将添加一个具有 OrderDetails 作为其数据的 DetailsViewItemDetailsViewDetailsViewItem 将显示为 details

当将 detailViewItem 转换为 Json 或数组时,它们的格式如下

{
  "display_as":"details",
  "caption":"",
  "data":{
    "date":"16-05-2015",
    "status":"processing",
    "payment_status":"paid",
    "shipment_status":"shipped"
  }
}

所有公共属性都被转换为蛇形命名,因为这是 ROBIN API 所期望的。

要添加产品详细信息,只需执行以下操作

<?php

$products = new Products();

$product = Product::make($product->title, $product->quantity, $product->price);

$products->push($product);

$detailsView->addColumns($products, "Products");

这同样适用于 InvoicesShipments 详细信息。

创建自己的详细信息

ROBIN API 允许您向其发送自己的数据。下面的代码展示了如何做到这一点。

<?php
use Robin\Api\Models\Views\Details\Detail;
        
class CustomDetailsView extends Detail
{

    public $fooBar;

    public $barFoo;

    public static function make($foo, $bar)
    {
        $view = new static;

        $view->fooBar = $foo;
        $view->barFoo = $bar;

        return $view;
    }
   
}

$details = new DetailsView();
$custom = CustomDetailsView::make("foo", "bar");
$details->addColumns($custom, "Dummy");

$json = $details->toJson(); // {"display_as":"columns","caption":"Dummy","data":{"foo_bar":"foo","bar_foo":"bar"}}