tauceti/exponea-php-sdk

Exponea API 的 PHP SDK

v1.0.1-beta 2024-04-19 06:37 UTC

README

库只包含集成 Exponea 所必需的基本功能。如果您缺少某些方法,请提交合并请求,因为我们的集成根本不需要它们。

整个库使用异步 Guzzle 请求。请注意,每个方法返回的 Promise 都必须使用 wait() 调用来执行。

Exponea API 使用公钥和私钥授权,因此您需要获取 3 个值来发送有效请求

  • 公钥
  • 私钥
  • 项目令牌

Exponea API 参考:https://docs.exponea.com/reference

使用示例

请检查以下源代码,该代码实现了 API 初始化和 getSystemTime() 方法

使用方法

use Tauceti\ExponeaApi\Client;

$client = new Client([
    'public_key' => getenv('EXPONEA_PUBLIC_KEY'),
    'private_key' => getenv('EXPONEA_PRIVATE_KEY'),
    'project_token' => getenv('EXPONEA_PROJECT_TOKEN'),
]);
try {
    $systemTime = $client->tracking()->getSystemTime()->wait(); // returns SystemTime object
} catch (...) { ... }

跟踪 API

所有方法都包含在 $client->tracking() 方法中。

设置联系协议(同意)

在 Exponea 中,电子邮件和 SMS 协议都称为 consents。它们可以被授予或撤销。

$event = new Consent(
    new RegisteredCustomer('example@example.com'),
    Consent::CATEGORY_NEWSLETTER,
    Consent::ACTION_GRANT
);
try {
    $client->tracking()->addEvent($event)->wait(); // does not return anything
} catch (...) { ... }

发送购买

Exponea 需要您发送至少两个事件:购买和 PurchaseItem(每个购买项目一个)。

$purchase = new Purchase(
    new RegisteredCustomer('example@example.com'),
    'PREFIX12345', // purchase id
    [
        new Item('012345', 2.99, 1),
    ], // purchase items
    'COD' // payment method
);
$purchaseItem = new PurchaseItem(
    new RegisteredCustomer('example@example.com'),
    'PREFIX12345', // purchase id
    '012345', // item id
    2.99, // price
    2, // quantity
    'SKU012345', // sku (stock keeping unit)
    'Product name',
    new Category('CAT1', 'Some > Category > Breadcrumb')
);

您可以选择发送在购买过程中使用的优惠券。请参阅 Purchase 构造函数的 $voucher 参数。

获取系统时间

try {
    $systemTime = $client->tracking()->getSystemTime()->wait(); // returns SystemTime object
} catch (...) { ... }

更新客户属性

try {
    $properties = [
        'fidelity_points' => 657,
        'first_name' => 'Marian',
    ];

    $client->tracking()->updateCustomerProperties(
                         new RegisteredCustomer('marian@exponea.com'), $properties
                     )->wait();
} catch (...) { ... }

使用此方法可以更新客户属性。属性中的必填字段是 'first_name'。