<?php

namespace App\Libraries\Adapter\Payment;

use App\Libraries\Adapter\API\CurlAPI as APIAdapter;

// 참고:
// https://techhub.hpe.com/eginfolib/servers/docs/HPRestfultool/iLo4/data_model_reference.html
// https://github.com/SyntaxPhoenix/iloclient
class CookiePayment extends Payment
{
    public function __construct($debug = false)
    {
        parent::__construct($debug);
    }

    private function getToken()
    {
        $adapter = new APIAdapter();
        $adapter->setHeader("content-type", "application/json; charset=utf-8");
        /* 토큰 발행 API */
        $adapter->setURL(MALLS['payments']['CookiePayment']['token_url']);
        $adapter->setDatas(array(
            MALLS['payments']['CookiePayment']['token_id'],
            MALLS['payments']['CookiePayment']['token_key'],
        ));
        $token = $adapter->execute();
        /* 여기 까지 */
        if ($token['RTN_CD'] != '0000') {
            throw new \Exception("Cookipay에서 Token을 받는데 실패했습니다.");
        }
        return $token;
    }
    protected function execute_process(): object
    {
        $adapter = new APIAdapter();
        $adapter->setHeader("content-type", "application/json; charset=utf-8");
        $adapter->setHeader("ApiKey", MALLS['payments']['CookiePayment']['api_key']);
        $adapter->setHeader("TOKEN", $this->getToken());
        $adapter->setURL(MALLS['payments']['CookiePayment']['api_url']);

        $datas = $this->getDatas();
        // array(
        //     'API_ID' => 'COOKIEPAY에서 발급받은 가맹점연동 ID',
        //     'ORDERNO' => '주문번호',
        //     'PRODUCTNAME' => '상품명',
        //     'AMOUNT' => '결제금액',
        //     'BUYERNAME' => '고객명',
        //     'BUYEREMAIL' => '고객 E-MAIL',
        //     'CARDNO' => '카드번호',
        //     'EXPIREDT' => '카드유효기간',
        //     'PRODUCTCODE' => '상품코드',
        //     'BUYERID' => '고객 ID',
        //     'BUYERADDRESS' => '고객 주소',
        //     'BUYERPHONE' => '고객 휴대폰번호',
        //     'QUOTA' => '할부개월',
        //     'ETC1' => '추가필드 1',
        //     'ETC2' => '추가필드 2',
        //     'ETC3' => '추가필드 3',
        //     'ETC4' => '추가필드 4',
        //     'ETC5' => '추가필드 5',
        // )
        $adapter->setDatas($datas);
        return $adapter->execute();
    }
}
