1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?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); } } |