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);

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

<?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);

客户集合使得向ROBIN发送更多客户变得非常简单。只需遍历您的现有客户,并将ROBIN客户推送到客户集合中,如下所示

<?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);

这将向DetailsView添加一个包含OrderDetails作为其数据的DetailsViewItemDetailsViewItem将显示为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"}}