Authorize.php 1.3 KB
<?php

namespace Lackoxygen\TiktokShop\Passage;

use Illuminate\Support\Arr;

class Authorize extends Passage
{
    /**
     * @param string $code
     * @param string $grantType {authorization_self, authorization_code}
     * @param bool $sandbox
     * @return ResultSet
     */
    public function token(string $code, string $grantType = 'authorization_code', bool $sandbox = false)
    {
        $params = ['code' => $code, 'grant_type' => $grantType];
        if ($sandbox) {
            if ($grantType === 'authorization_self') {
                if ($code == '4463798') {
                    Arr::set($params, 'test_shop', 1);
                } else {
                    Arr::set($params, 'shop_id', $code);
                }
            }
        }
        $this->builder->service('token.create');
        $this->builder
            ->method('GET')
            ->path('/token/create')
            ->service('token.create')
            ->params($params);
    }

    /**
     * @param string $refreshToken
     * @param string $grantType
     * @return ResultSet
     */
    public function refresh(string $refreshToken, string $grantType = 'refresh_token')
    {
        $this->builder
            ->method('POST')
            ->service('token.refresh')
            ->params(['refresh_token' => $refreshToken, 'grant_type' => $grantType]);
    }
}