close/partner-sdk

Close合作伙伴的官方SDK。


README

Lastest Version Run PHP Unit tests Apache 2 License

PHP Close SDK 让开发者能够轻松地在PHP代码中与 The Close App 进行通信。通过通过 通过Composer安装SDK 快速开始。

跳转到

入门

  1. 获取您的凭据 – 在开始之前,您需要已经拥有Close的账户。如果不是这样,请随时 联系我们

  2. 最低要求 – 为了使用Close SDK,您的系统需要满足[最低要求][docs-requirements],包括拥有 PHP >= 7.4

  3. 安装SDK – 使用Composer 安装Close SDK是推荐的方式

    composer require close/partner-sdk
    
  4. 使用SDK – 在本页中,您将了解如何使用SDK,但如果您想了解更多关于调用的信息,您始终可以查看我们的Close Partner API文档,该SDK是其封装器。

快速示例

创建Close SDK客户端

<?php
// Require the Composer autoloader.
require 'vendor/autoload.php';

use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Options;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Instantiate the Close client using the client credentials given by Close
  $sdk = new CloseSdk(
       new Options([
            'client_id' => 'client_test',
            'client_secret' => 'client_test_secret',
            'base_uri' => 'the endpoint for your environment, default is the close app production endpoint url',
       ])
  );
} catch (CloseSdkException $closeSdkException) {
    // You can receive an error if the token was not generated because of invalid credentials
}

使用Close App导入票务

导入包含必要信息的票务

<?php
use ClosePartnerSdk\Dto\Ticket;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\TicketGroup;
use ClosePartnerSdk\Dto\EventTime;
use ClosePartnerSdk\Exception\CloseSdkException;

try {  
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $ticketGroup = new TicketGroup('+31666111000');
  $productTitle = 'Singular entrance ticket';
  $scanCode = '1234567890123';
  $ticket = new Ticket(
      $scanCode,
      new EventTime(new DateTime('2022-10-10 20:00:00')),
      $productTitle
  );
  $ticketGroup->addTicket($ticket);
  // Call endpoint
  $sdk
    ->ticket()
    ->import($eventId, $ticketGroup);
} catch (CloseSdkException $e) {
    echo "The ticket has not been imported.\n";
    // We recommend to retry after a couple of seconds.
}

导入包含座位信息的票务

<?php
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\TicketGroup;
use ClosePartnerSdk\Dto\EventTime;
use ClosePartnerSdk\Exception\CloseSdkException;
use ClosePartnerSdk\Dto\Ticket;
use ClosePartnerSdk\Dto\SeatInfo;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $ticketGroup = new TicketGroup('+31666111000');
  $productTitle = 'Singular entrance ticket';
  $scanCode = '1234567890123';
  
  $ticket = new Ticket(
      $scanCode,
      new EventTime(new DateTime('2022-10-10 20:00:00')),
      $productTitle
  );

  $seatInfo = new SeatInfo()
    ->withChair('12')
    ->withEntrance('E')
    ->withRow('3')
    ->withSection('A');

  $ticket = $ticket->withSeatInfo($seatInfo);

  $ticketGroup->addTicket($ticket);
  // Call endpoint
  $sdk
    ->ticket()
    ->import($eventId, $ticketGroup);
} catch (CloseSdkException $e) {
    echo "The ticket has not been imported.\n";
    // We recommend to retry after a couple of seconds.
}

取消票务

在Close App中取消票务

<?php
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\Product;
use ClosePartnerSdk\Dto\EventTime;
use ClosePartnerSdk\Exception\CloseSdkException;
use ClosePartnerSdk\Dto\TicketCancelDto;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $scanCode = 'ABCD';
  $phoneNumber = '+31631111111';
  $eventTime = new EventTime(new DateTime('2022-10-10 20:00:00'));
  $ticketCancelDto = new TicketCancelDto($scanCode, $phoneNumber, $eventTime);
  // Call cancel endpoint
  $sdk
    ->ticket()
    ->cancel($eventId, $ticketCancelDto);
} catch (CloseSdkException $e) {
    echo "The ticket has not been cancelled.\n";
    // We recommend to retry after a couple of seconds.
}

发送消息

向聊天中的所有用户发送消息

<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\ChatId;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $chatId = new ChatId('CLECxxxxx');
  $message = 'This is the message to send';
  
  $sdk
    ->textMessage()
    ->sendToAllUsersForChat($eventId, $chatId, $message);
} catch (CloseSdkException $e) {
    echo "The text has not been sent.\n";
    // We recommend to retry after a couple of seconds.
}

流程属性

在流程属性中设置值

<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\ItemFlowProperty;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $properties = [
    new ItemFlowProperty('vip', 'This is a great vip event!'),
    new ItemFlowProperty('promotion', 'This event has a special promotion'),
  ];
  
  $sdk->flowProperty()->setForAllUsersInAllChats(
    $eventId,
    $properties
  );
} catch (CloseSdkException $e) {
    echo "The property has not been sent.\n";
    // We recommend to retry after a couple of seconds.
}

在聊天中设置属性

您还可以检索和设置将在表演聊天中可用的属性。

用于检索所有流程属性

<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  
  $properties = $sdk->flowConfig()->getChatConfig($eventId);
  
} catch (CloseSdkException $e) {
    echo "The event is not found sent.\n";
    // We recommend to retry after a couple of seconds.
}

用于设置整个聊天的属性

<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\ChatId;
use ClosePartnerSdk\Dto\ItemFlowProperty;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  $chatId = new ChatId('CLECxxxxx');
  
  $properties = $sdk->flowConfig()->getChatConfig($eventId);
  
   $itemFlowProperties = [
      new ItemFlowProperty('vip', 'true'),
      new ItemFlowProperty('promotion', 'This chat is the selected winner!'),
   ];

    $sdk->flowConfig()->setChatConfig($eventId, $chatId, $itemFlowProperties);
  
} catch (CloseSdkException $e) {
    echo "The event is not found sent.\n";
    // We recommend to retry after a couple of seconds.
}
<?php
use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\Dto\EventId;
use ClosePartnerSdk\Dto\ItemFlowProperty;
use ClosePartnerSdk\Exception\CloseSdkException;

try {
  // Define DTO structure
  $eventId = new EventId('CLEV3BX47D58YCMERC6CGJ2L7xxx');
  
  $properties = $sdk->flowConfig()->getChatConfig($eventId);
  
} catch (CloseSdkException $e) {
    echo "The event is not found sent.\n";
    // We recommend to retry after a couple of seconds.
}

获取帮助

如果您在使用我们的SDK时遇到任何问题或问题,请随时告诉我们。我们将尽力尽快给您回复。

  • 如果您在本文档中未找到答案的问题,请联系我们
  • 如果您认为您可能发现了一个错误,请随时提交问题

功能

  • 为所有支持的端点提供了一个与我们的Partner API通信的非常简单的方法。这意味着我们始终根据您的API凭据获取正确的数据。
  • 它基于最新的软件,具有最高的安全标准,并遵循PSR约定
  • 您可以在客户端构建器的依赖项中提供自己的Http客户端,并在选项对象中提供实例。上面有一个示例。
  • 我们使用 Guzzle 生成这些请求,并利用其技术(异步请求、中间件等)。
  • 我们提供了一种易于外部PHP应用程序使用的数据结构,适用于我们的领域。
  • 如果预期之外的情况发生,我们会返回清晰的响应和异常。

高级功能

  1. 如果您想使用自己的HttpClient,可以在实例化我们的SDK时将其实现提供给客户端构建器。
<?php
// Require the Composer autoloader.
require 'vendor/autoload.php';

use ClosePartnerSdk\CloseSdk;
use ClosePartnerSdk\HttpClient\HttpClientBuilder;

// Instantiate the Close client using the client credentials given by Close
  return new CloseSdk(
       new Options([
            'client_builder' => new HttpClientBuilder($myownHttpClient),
            'client_id' => 'client_test',
            'client_secret' => 'client_test_secret',
       ])
  );

重要:客户端需要实现 PSR-7 规范,以便被我们的SDK接受。 如果您未提供任何实例,我们将使用来自 HttpPlug 的发现功能,该功能会查找可用的 \Http\Client\HttpClient 实现。

贡献

如果您对我们的SDK有任何改进建议,请不要犹豫,通过 创建一个issue 并告诉我们!如果您已经有了可以帮助我们改进系统的代码,您可以自由地 提交一个PR。所有额外的帮助都受到高度赞赏!

资源

  • API 文档 – 了解每个端点的参数、验证和响应的详细信息。
  • 网站 – 了解Close及其工作内容。
  • 问题 – 报告问题和提交pull请求。
  • 许可证 – 了解我们的许可证的更多信息。