Skip to content

Commit

Permalink
Implement Customer service
Browse files Browse the repository at this point in the history
  • Loading branch information
shimonhaga committed Mar 26, 2023
1 parent e8c7667 commit e4da745
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 23 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ APP_ID=
CLIENT_ID=
CLIENT_SECRET=
REDIRECT_URI=
TOKEN=
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ColorMeShopApi API client
GMOベポパが提供している ColorMeショップの API を PHP から利用するためのライブラリです。
GMOベポパ が提供している ColorMeショップ の API を PHP から利用するためのライブラリです。

現在一部のみ実装済み。

## 環境
PHP 8.1 以上
Expand Down Expand Up @@ -157,6 +159,16 @@ TODO: write
#### メールの送信
TODO: write

### 顧客
#### 顧客データの一覧を取得
TODO: write

#### 顧客データの取得
TODO: write

### 顧客データを追加
not implement

### 決済
#### 決済設定の一覧を取得
TODO: write
Expand All @@ -166,12 +178,10 @@ TODO: write
TODO: write

### 配送日時設定を取得
TODO: write

not implement

### 未実装
* ショップ
* 顧客
* 商品
* 在庫
* 商品グループ
Expand Down
18 changes: 12 additions & 6 deletions client
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ echo __NAMESPACE__ . " shell\n";
echo "-----\nexample:\n";
echo "\$client = new Client;\n\n";
echo "// get url for OAuth\n";
echo "\$oAuthUrl = \$client->getOAuthUrl(\$scopes);\n\n";
echo "\$oAuthUrl = \$client->getOAuthUrl(\$options, \$scopes);\n\n";
echo "// get sales\n";
echo "\$sales = \$client->getSales();\n";

Expand All @@ -24,16 +24,22 @@ $sh = new \Psy\Shell();
$sh->addCode(sprintf("namespace %s;", __NAMESPACE__));

// set default
$options = new Entities\OAuth\Options(
$_ENV['CLIENT_ID'],
$_ENV['CLIENT_SECRET'],
$_ENV['REDIRECT_URI'] ?? Constants\AuthRedirectUri::NO_REDIRECT,
);
$options = null;
if (!empty($_ENV['CLIENT_ID']) && !empty($_ENV['CLIENT_SECRET'])) {
$options = new Entities\OAuth\Options(
$_ENV['CLIENT_ID'],
$_ENV['CLIENT_SECRET'],
$_ENV['REDIRECT_URI'] ?? Constants\AuthRedirectUri::NO_REDIRECT,
);

}
$scopes = new Values\Scopes(Constants\AuthScope::cases());
$token = $_ENV['TOKEN'] ?? null;

$sh->setScopeVariables([
'options' => $options,
'scopes' => $scopes,
'token' => $token,
]);

$sh->run();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"minimum-stability": "dev",
"prefer-stable": true,
"version": "0.3.4",
"version": "0.4.0",
"license": "MIT",
"authors": [
{
Expand Down
77 changes: 68 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Shimoning\ColorMeShopApi\Services\Sales;
use Shimoning\ColorMeShopApi\Entities\Sales\Sale;
use Shimoning\ColorMeShopApi\Entities\Sales\SearchParameters;
use Shimoning\ColorMeShopApi\Entities\Sales\SearchParameters as SalesSearchParameters;
use Shimoning\ColorMeShopApi\Entities\Sales\SaleUpdater;
use Shimoning\ColorMeShopApi\Entities\Sales\Stat as SaleStat;

Expand All @@ -25,6 +25,10 @@
use Shimoning\ColorMeShopApi\Services\Delivery;
use Shimoning\ColorMeShopApi\Entities\Delivery\Delivery as DeliveryEntity;

use Shimoning\ColorMeShopApi\Services\Customer;
use Shimoning\ColorMeShopApi\Entities\Customer\SearchParameters as CustomerSearchParameters;
use Shimoning\ColorMeShopApi\Entities\Customer\Customer as CustomerEntity;

class Client
{
protected string $accessToken;
Expand Down Expand Up @@ -66,13 +70,17 @@ public function exchangeCode2Token(OAuthOptions $options, string $code): AccessT
* 受注データのリストを取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSales
* @param SearchParameters|null $searchParameters
* @param SalesSearchParameters|null $searchParameters
* @param string|null $accessToken
* @return Page<Sale>|Errors
*/
public function getSales(?SearchParameters $searchParameters = null, ?string $accessToken = null): Page|Errors
{
return $this->salesService($accessToken)->page($searchParameters ?? new SearchParameters([]), $accessToken);
public function getSales(
?SalesSearchParameters $searchParameters = null,
?string $accessToken = null,
): Page|Errors {
return $this
->salesService($accessToken)
->page($searchParameters ?? new SalesSearchParameters([]), $accessToken);
}

/**
Expand Down Expand Up @@ -123,8 +131,11 @@ public function updateSale(SaleUpdater $updater, ?string $accessToken = null): S
* @param string|null $accessToken
* @return Sale|Errors
*/
public function cancelSale(int|string $id, ?bool $restock = false, ?string $accessToken = null): Sale|Errors
{
public function cancelSale(
int|string $id,
?bool $restock = false,
?string $accessToken = null,
): Sale|Errors {
return $this->salesService($accessToken)->cancel($id, $restock, $accessToken);
}

Expand All @@ -137,8 +148,11 @@ public function cancelSale(int|string $id, ?bool $restock = false, ?string $acce
* @param string|null $accessToken
* @return true|Errors
*/
public function sendSalesMail(int|string $id, MailType $mailType, ?string $accessToken = null): bool|Errors
{
public function sendSalesMail(
int|string $id,
MailType $mailType,
?string $accessToken = null,
): bool|Errors {
return $this->salesService($accessToken)->sendMail($id, $mailType, $accessToken);
}

Expand All @@ -162,6 +176,7 @@ private function salesService(?string $accessToken = null): Sales
* 決済設定の一覧を取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/payment/operation/getPayments
* @param string|null $accessToken
* @return Collection<PaymentEntity>|Errors
*/
public function getPayments(?string $accessToken = null): Collection|Errors
Expand All @@ -180,6 +195,7 @@ public function getPayments(?string $accessToken = null): Collection|Errors
* 配送方法一覧を取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/delivery/operation/getDeliveries
* @param string|null $accessToken
* @return Collection<DeliveryEntity>|Errors
*/
public function getDeliveries(?string $accessToken = null): Collection|Errors
Expand All @@ -193,4 +209,47 @@ public function getDeliveries(?string $accessToken = null): Collection|Errors

return (new Delivery($this->accessToken))->all();
}

/**
* 顧客データのリストを取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/customer/operation/getCustomers
* @param CustomerSearchParameters|null $searchParameters
* @param string|null $accessToken
* @return Page<CustomerEntity>|Errors
*/
public function getCustomers(
?CustomerSearchParameters $searchParameters = null,
?string $accessToken = null,
): Page|Errors {
if ($accessToken) {
$this->accessToken = $accessToken;
}
if (empty($this->accessToken)) {
throw new ParameterException('アクセストークンは必ず指定してください');
}

return (new Customer($this->accessToken))
->page($searchParameters ?? new CustomerSearchParameters([]));
}

/**
* 顧客データの取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/customer/operation/getCustomer
* @param integer|string $id
* @param string|null $accessToken
* @return CustomerEntity|Errors
*/
public function getCustomer(int|string $id, ?string $accessToken = null): CustomerEntity|Errors
{
if ($accessToken) {
$this->accessToken = $accessToken;
}
if (empty($this->accessToken)) {
throw new ParameterException('アクセストークンは必ず指定してください');
}

return (new Customer($this->accessToken))->one($id, $accessToken);
}
}
4 changes: 2 additions & 2 deletions src/Entities/Customer/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Shimoning\ColorMeShopApi\Entities\Customer;

use Shimoning\ColorMeShopApi\Entities\Entity;
use Shimoning\ColorMeShopApi\Constants\Prefecture;
use Shimoning\ColorMeShopApi\Constants\Sex;
use Shimoning\ColorMeShopApi\Values\Furigana;
use Shimoning\ColorMeShopApi\Constants\Sex;
use Shimoning\ColorMeShopApi\Constants\Prefecture;

/**
* 顧客
Expand Down
63 changes: 63 additions & 0 deletions src/Entities/Customer/SearchParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Shimoning\ColorMeShopApi\Entities\Customer;

use Shimoning\ColorMeShopApi\Entities\Entity;
use Shimoning\ColorMeShopApi\Values\Furigana;
use Shimoning\ColorMeShopApi\Values\DateTime;
use Shimoning\ColorMeShopApi\Values\Limit;
use Shimoning\ColorMeShopApi\Constants\Sex;

/**
* before/after は直感的でないためサポートしない。
* make_date_max/make_date_min を使用すること。
*
* TODO: fields のサポート
*/
class SearchParameters extends Entity
{
const OBJECT_FIELDS = [
'furigana'=> [
'value' => Furigana::class,
],
'sex' => [
'enum' => Sex::class,
],

'makeDateMin'=> [
'value' => DateTime::class,
],
'makeDateMax'=> [
'value' => DateTime::class,
],
'updateDateMin'=> [
'value' => DateTime::class,
],
'updateDateMax'=> [
'value' => DateTime::class,
],

'limit'=> [
'value' => Limit::class,
],
];

protected ?array $ids;

protected ?string $name;
protected ?Furigana $furigana;

protected ?string $mail;
protected ?string $postal;
protected ?string $tel;
protected ?Sex $sex;
protected ?bool $member;

protected ?DateTime $makeDateMin; // after
protected ?DateTime $makeDateMax; // before
protected ?DateTime $updateDateMin;
protected ?DateTime $updateDateMax;

protected ?Limit $limit;
protected ?int $offset;
}
76 changes: 76 additions & 0 deletions src/Services/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Shimoning\ColorMeShopApi\Services;

use Shimoning\ColorMeShopApi\Communicator\Request;
use Shimoning\ColorMeShopApi\Communicator\RequestOptions;
use Shimoning\ColorMeShopApi\Communicator\Errors;
use Shimoning\ColorMeShopApi\Entities\Customer\SearchParameters;
use Shimoning\ColorMeShopApi\Entities\Collection;
use Shimoning\ColorMeShopApi\Entities\Page;
use Shimoning\ColorMeShopApi\Entities\Pagination;
use Shimoning\ColorMeShopApi\Entities\Customer\Customer as CustomerEntity;

class Customer
{
protected string $_accessToken;

public function __construct(string $accessToken)
{
$this->_accessToken = $accessToken;
}

/**
* 顧客データのリストを取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/Customer/operation/getCustomers
* @param SearchParameters $searchParameters
* @param string|null $accessToken
* @return Page<CustomerEntity>|Errors
*/
public function page(
SearchParameters $searchParameters,
?string $accessToken = null,
): Page|Errors {
$response = (new Request(new RequestOptions([
'authorization' => $accessToken ?? $this->_accessToken,
])))->get(
'https://api.shop-pro.jp/v1/customers',
$searchParameters->toArrayRecursive(),
);
if (! $response->isSuccess()) {
return Errors::build($response);
}
$data = $response->getParsedBody();

return new Page(
Collection::cast(CustomerEntity::class, $data['customers'] ?? []),
new Pagination($data['meta'] ?? []),
);
}

/**
* 顧客データの取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/customer/operation/getCustomer
* @param int|string $id
* @param string|null $accessToken
* @return CustomerEntity|Errors
*/
public function one(int|string $id, ?string $accessToken = null): CustomerEntity|Errors
{
$response = (new Request(new RequestOptions([
'authorization' => $accessToken ?? $this->_accessToken,
])))->get(
'https://api.shop-pro.jp/v1/customers/' . $id,
);
if (! $response->isSuccess()) {
return Errors::build($response);
}
$data = $response->getParsedBody();
return new CustomerEntity($data['customer'] ?? []);
}

// TODO: create
// https://developer.shop-pro.jp/docs/colorme-api#tag/customer/operation/postCustomers
}
6 changes: 6 additions & 0 deletions src/Services/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function __construct(string $accessToken)
}

/**
* 配送方法一覧を取得
*
* @link https://developer.shop-pro.jp/docs/colorme-api#tag/delivery/operation/getDeliveries
* @param string|null $accessToken
* @return Collection<DeliveryEntity>|Errors
*/
public function all(?string $accessToken = null): Collection|Errors
Expand All @@ -35,4 +38,7 @@ public function all(?string $accessToken = null): Collection|Errors

return Collection::cast(DeliveryEntity::class, $data['deliveries'] ?? []);
}

// TODO: 配送日時設定を取得
// https://developer.shop-pro.jp/docs/colorme-api#tag/delivery/operation/getDeliveryDateSetting
}
Loading

0 comments on commit e4da745

Please sign in to comment.