เปลี่ยนวิธีการเก็บ Access Token

โดยปกติแล้ว, sdk ตัวนี้จะคอยเก็บ access token และ ดึง access token ล่าสุดมาจาก temp system ให้เองจากการสร้าง access token oauth2

ถ้าท่านต้องการเปลี่ยนวิธีการเก็บให้, ท่านสามารถแก้ไขได้โดยการ implement Farzai\KApi\Contracts\AccessTokenRepositoryInterface class ได้เลย

ยกตัวอย่างเช่น

<?php

namespace App\Repositories;

use Farzai\KApi\Contracts\AccessTokenRepositoryInterface;

class DatabaseAccessTokenRepository implements AccessTokenRepositoryInterface
{

    /**
     * Create a new storage instance.
     */
    public function __construct()
    {
        // Connect database
    }

    /**
     * Get the access token.
     */
    public function retrieve(): ?AccessToken
    {
        // Load access token from your database
        $data = [
            'access_token' => '',
            
            // int The expiration time for the access token. Expressed in seconds.
            'expires_in' => '', 
            
            // The time the access token was issued. (Y-m-d H:i:s\.u)
            'issued_at' => '',
            
            // The scope (if any) associated with the access token.
            'scope' => '',
            
            // The status of the access token (e.g., approved or revoked).
            'status' => '',
            
            // It is a required parameter which is assigned by the KBank Open API and specifies the type of token.
            'token_type' => '',
        ];

        return new AccessToken($data);
    }

    /**
     * Store the access token.
     */
    public function store(AccessToken $token): void
    {
        // Encode the access token.
        $data = json_encode($token->toArray());

        // Store the access token.
        // ....
    }

    /**
     * Forget the access token.
     */
    public function forget(): void
    {
        // Remove the access token from your database
        // 
    }
}

เพิ่มเข้าไปยัง ClientBuilder

$accessTokenRepository = new DatabaseAccessTokenRepository();

$client = ClientBuilder::make()
    ->setConsumer("<YOUR_CONSUMER_ID>", "<YOUR_CONSUMER_SECRET>")
    ->asSandbox()
    ->setTokenRepository($accessTokenRepository) // <-- เพิ่มเข้าไปยัง Client Builder
    ->build();

Last updated