diff --git a/.env.example b/.env.example index df7cf9c..c3b4590 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ APP_ID= CLIENT_ID= CLIENT_SECRET= REDIRECT_URI= +TOKEN= diff --git a/README.md b/README.md index 9a341c1..a04bf0c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # ColorMeShopApi API client -GMOベポパが提供している ColorMeショップの API を PHP から利用するためのライブラリです。 +GMOベポパ が提供している ColorMeショップ の API を PHP から利用するためのライブラリです。 + +現在一部のみ実装済み。 ## 環境 PHP 8.1 以上 @@ -157,6 +159,16 @@ TODO: write #### メールの送信 TODO: write +### 顧客 +#### 顧客データの一覧を取得 +TODO: write + +#### 顧客データの取得 +TODO: write + +### 顧客データを追加 +not implement + ### 決済 #### 決済設定の一覧を取得 TODO: write @@ -166,12 +178,10 @@ TODO: write TODO: write ### 配送日時設定を取得 -TODO: write - +not implement ### 未実装 * ショップ -* 顧客 * 商品 * 在庫 * 商品グループ diff --git a/client b/client index 9f0d21e..0ab6192 100755 --- a/client +++ b/client @@ -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"; @@ -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(); diff --git a/composer.json b/composer.json index d351f2d..259dc0d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "minimum-stability": "dev", "prefer-stable": true, - "version": "0.3.4", + "version": "0.4.0", "license": "MIT", "authors": [ { diff --git a/src/Client.php b/src/Client.php index 54ac40c..3115ff6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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; @@ -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|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); } /** @@ -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); } @@ -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); } @@ -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|Errors */ public function getPayments(?string $accessToken = null): Collection|Errors @@ -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|Errors */ public function getDeliveries(?string $accessToken = null): Collection|Errors @@ -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|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); + } } diff --git a/src/Entities/Customer/Customer.php b/src/Entities/Customer/Customer.php index 36507d9..47a2e49 100644 --- a/src/Entities/Customer/Customer.php +++ b/src/Entities/Customer/Customer.php @@ -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; /** * 顧客 diff --git a/src/Entities/Customer/SearchParameters.php b/src/Entities/Customer/SearchParameters.php new file mode 100644 index 0000000..801579a --- /dev/null +++ b/src/Entities/Customer/SearchParameters.php @@ -0,0 +1,63 @@ + [ + '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; +} diff --git a/src/Services/Customer.php b/src/Services/Customer.php new file mode 100644 index 0000000..109ee0b --- /dev/null +++ b/src/Services/Customer.php @@ -0,0 +1,76 @@ +_accessToken = $accessToken; + } + + /** + * 顧客データのリストを取得 + * + * @link https://developer.shop-pro.jp/docs/colorme-api#tag/Customer/operation/getCustomers + * @param SearchParameters $searchParameters + * @param string|null $accessToken + * @return Page|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 +} diff --git a/src/Services/Delivery.php b/src/Services/Delivery.php index c26ba14..72d0f0d 100644 --- a/src/Services/Delivery.php +++ b/src/Services/Delivery.php @@ -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|Errors */ public function all(?string $accessToken = null): Collection|Errors @@ -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 } diff --git a/src/Services/Payment.php b/src/Services/Payment.php index 5d279e0..7f5a53c 100644 --- a/src/Services/Payment.php +++ b/src/Services/Payment.php @@ -18,7 +18,10 @@ public function __construct(string $accessToken) } /** + * 決済設定の一覧を取得 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/payment/operation/getPayments + * @param string|null $accessToken * @return Collection|Errors */ public function all(?string $accessToken = null): Collection|Errors diff --git a/src/Services/Sales.php b/src/Services/Sales.php index bad8d9d..79b76f2 100644 --- a/src/Services/Sales.php +++ b/src/Services/Sales.php @@ -10,8 +10,8 @@ use Shimoning\ColorMeShopApi\Entities\Sales\Stat; use Shimoning\ColorMeShopApi\Entities\Sales\SaleUpdater; use Shimoning\ColorMeShopApi\Entities\Collection; -use Shimoning\ColorMeShopApi\Entities\Pagination; use Shimoning\ColorMeShopApi\Entities\Page; +use Shimoning\ColorMeShopApi\Entities\Pagination; use Shimoning\ColorMeShopApi\Constants\MailType; class Sales @@ -24,6 +24,8 @@ public function __construct(string $accessToken) } /** + * 受注データのリストを取得 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSales * @param SearchParameters $searchParameters * @param string|null $accessToken @@ -51,6 +53,8 @@ public function page( } /** + * 受注データの取得 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/getSale * @param int|string $id * @param string|null $accessToken @@ -71,6 +75,8 @@ public function one(int|string $id, ?string $accessToken = null): Sale|Errors } /** + * 売上集計の取得 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/statSale * @param DateTimeInterface $dateTime * @param string|null $accessToken @@ -97,6 +103,8 @@ public function stat( } /** + * 受注データの更新 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/updateSale * @param SaleUpdater $updater * @param string|null $accessToken @@ -123,6 +131,8 @@ public function update( } /** + * 受注のキャンセル + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/cancelSale * @param int|string $id * @param bool|null $restock @@ -151,6 +161,8 @@ public function cancel( } /** + * メールの送信 + * * @link https://developer.shop-pro.jp/docs/colorme-api#tag/sale/operation/sendSalesMail * @param int|string $id * @param MailType $mailType