Skip to content

Commit

Permalink
added webhook receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin committed Sep 25, 2022
1 parent 15bd049 commit ca841ca
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Unofficial TiktokShop API Client in PHP
# Unofficial Tiktok Shop API Client in PHP

[![Total Downloads](https://poser.pugx.org/nvuln/tiktokshop-client/downloads)](https://packagist.org/packages/nvuln/tiktokshop-client)
[![Latest Stable Version](https://poser.pugx.org/nvuln/tiktokshop-client/v/stable)](https://packagist.org/packages/nvuln/tiktokshop-client)
[![Latest Unstable Version](https://poser.pugx.org/nvuln/tiktokshop-client/v/unstable)](https://packagist.org/packages/nvuln/tiktokshop-client)
[![License](https://poser.pugx.org/nvuln/tiktokshop-client/license)](https://packagist.org/packages/nvuln/tiktokshop-client)

TiktokShop API Client is a simple SDK implementation of Tiktok Shop API.
Tiktok Shop API Client is a simple SDK implementation of Tiktok Shop API.

## Installation

Install with Composer

```shell
composer require nvuln/tiktokshop-client
```
Expand Down Expand Up @@ -111,3 +113,35 @@ $orders = $client->Order->getOrderList([
'page_size' => 50,
]);
```

## Webhook

Use webhook to receive incoming notification from tiktok shop

```php
$webhook = $client->webhook();
```

or manually configure the webhook receiver

```php
use NVuln\TiktokShop\Webhook;
use NVuln\TiktokShop\Errors\TiktokShopException;

$webhook = new Webhook($client);
try {
$webhook->verify();
$webhook->capture($_POST);
} catch (TiktokShopException $e) {
echo "webhook error: " . $e->getMessage() . "\n";
}
```

```php
echo "Type: " . $webhook->getType() . "\n";
echo "Timestamp: " . $webhook->getTimestamp() . "\n";
echo "Shop ID: " . $webhook->getShopId() . "\n";
echo "Data: \n"; // data is array
print_r($webhook->getData());

```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nvuln/tiktokshop-client",
"description": "Unofficial TiktokShop API Client in PHP",
"description": "Unofficial Tiktok Shop API Client for PHP",
"type": "library",
"license": "Apache-2.0",
"authors": [
Expand Down
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public function auth()
return new Auth($this, $this->sandbox);
}

public function webhook()
{
$webhook = new Webhook($this);
$webhook->verify();
$webhook->capture();

return $webhook;
}

protected function httpClient()
{
$stack = HandlerStack::create();
Expand Down
102 changes: 102 additions & 0 deletions src/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/*
* This file is part of tiktokshop-client.
*
* (c) Jin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NVuln\TiktokShop;

use NVuln\TiktokShop\Errors\TiktokShopException;

class Webhook
{
public const ORDER_STATUS_UPDATE = 1;
public const REVERSE_ORDER_STATUS_UPDATE = 2;
public const RECIPIENT_ADDRESS_UPDATE = 3;
public const PACKAGE_UPDATE = 4;
public const PRODUCT_AUDIT_RESULT_UPDATE = 5;

/**
* @var Client
*/
protected $client;

protected $type;
protected $shop_id;
protected $data;
protected $timestamp;

public function __construct(Client $client)
{
$this->client = $client;
}

public function getType()
{
return $this->type;
}

public function getShopId()
{
return $this->shop_id;
}

public function getData()
{
return $this->data;
}

public function getTimestamp()
{
return $this->timestamp;
}

public function capture($params = null)
{
if ($params === null) {
$rawData = file_get_contents('php://input');
$params = json_decode($rawData, true);
}

$this->type = $params['type'];
$this->shop_id = $params['shop_id'];
$this->data = $params['data'];
$this->timestamp = $params['timestamp'];
}

public function verify($signature = null, $rawData = null)
{
$signature = $signature ?? self::getAuthorizationHeader();
$rawData = $rawData ?? file_get_contents('php://input');

$stringToBeSigned = $this->client->getAppKey() . $rawData;
$sign = hash_hmac('sha256', $stringToBeSigned, $this->client->getAppSecret());

if (!hash_equals($sign, $signature)) {
throw new TiktokShopException("Incoming webhook request invalid. Signature not match.");
}
}

private static function getAuthorizationHeader()
{
$headers = null;
if (isset($_SERVER['Authorization'])) {
$headers = trim($_SERVER["Authorization"]);
} elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) {
$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
} elseif (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));

if (isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
}

return $headers;
}
}

0 comments on commit ca841ca

Please sign in to comment.