curl SDK Example

<?php

class Pchomepay {

    private $app_id = 'xxxx';
    private $secret = 'xxxxxxxxxxx';
    public $token = '';
    private $urls = [];

    function __construct() {
        $this->init_urls();
        $this->token();
    }

    private function init_urls(){
        if(ENVIRONMENT == 'development'){
            $this->urls = [
                'token' => 'https://sandbox-api.pchomepay.com.tw/v1/token', 
                'payment' => 'https://sandbox-api.pchomepay.com.tw/v1/payment'
            ];
        }else if(ENVIRONMENT == 'testing'){
            $this->urls = [
                'token' => 'https://sandbox-api.pchomepay.com.tw/v1/token', 
                'payment' => 'https://sandbox-api.pchomepay.com.tw/v1/payment'
            ];
        }else{
            $this->urls = [
                'token' => 'https://api.pchomepay.com.tw/v1/token', 
                'payment' => 'https://api.pchomepay.com.tw/v1/payment'
            ];
        }
    }

    private function post($url, $parameters=null, $header=null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, FALSE);//回傳資料不包含header
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

        if($parameters != false){
            if(is_array($parameters) == true){
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
            }
        }

        if($header != false){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        $rs = curl_exec($ch);
        //$info = curl_getinfo($ch);
        //$errors = curl_error($ch);
        //logg($errors);

        curl_close($ch);
        return $rs;
    }

    private function token() {
        $header = [
            'Content-Type:application/json',
             //將帳號密碼以 base64 encode 後帶在 header 中取得 token
             'Authorization: Basic ' . base64_encode($this->app_id . ':' . $this->secret)
        ];

        $result = $this->post($this->urls['token'], null, $header);
        $result = json_decode($result);
        //logg($result);

        $this->token = $result->token;
    }

    public function credit_card(){
        $headers = array(
            'Content-Type:application/json',
            'pcpay-token:'.$this->token
        );
        
        $params = [
            "order_id" => "IST". time(),
            "pay_type" => ["ACCT"],
            "amount" => 100,
            "return_url" => "http://www.pchomepay.com.tw",
            "seller_id" => "james002",
            "buyer_id" => "james001",
            "item_name" =>  "武功祕笈",
            "item_url" =>  "http://www.google.com.tw", 
            'card_info' => [
                ['installment' => 3], 
                ['installment' => 6]
            ]
        ];
        $params = json_encode($params);

        $result = $this->post($this->urls['payment'], $params, $headers);
        logg($result);
    }
}