Last active
August 29, 2015 14:04
-
-
Save takuya/0401494d266b35450ce4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//paypal | |
class Invoice { | |
public $client_id; | |
public $secret; | |
public $access_token; | |
public $paths; | |
public function __construct(){ | |
$this->initalize(); | |
} | |
public function initalize(){ | |
$this->paths = array(); | |
//general | |
$this->paths["getAccessToken"] = "/v1/oauth2/token"; | |
//invoicing | |
$this->paths["create"] = "/v1/invoicing/invoices"; | |
$this->paths["invoicies"] = "/v1/invoicing/invoices"; | |
$this->paths["retrieve"] = "/v1/invoicing/invoices/{invoiceId}"; | |
$this->paths["cancel"] = "/v1/invoicing/invoices/{invoiceId}/cancel"; | |
$this->paths["delete"] = "/v1/invoicing/invoices/{invoiceId}"; | |
$this->paths["update"] = "/v1/invoicing/invoices/{invoiceId}"; | |
$this->paths["send"] = "/v1/invoicing/invoices/{invoiceId}/send"; | |
$this->endpoint_host = "api.paypal.com"; | |
$this->schema = "https"; | |
// $this->client_id = "XXXXXXXXXX"; | |
// $this->secret = "XXXXXXXXXXXXXX"; | |
// $this->endpoint_host = "api.sandbox.paypal.com"; | |
} | |
public function getAccessToken($id,$sec){ | |
$url = "{$this->schema}://{$this->endpoint_host}".$this->paths[__FUNCTION__]; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Accept: application/json', | |
"Accept-Language: en_US", | |
"Authorization: Basic " . base64_encode($id . ":" . $sec) | |
), | |
//USER | |
//CURLOPT_USERPWD => "{$id}:{$sec}", | |
//Method | |
CURLOPT_POST => true,//POST | |
//body | |
CURLOPT_POSTFIELDS => "grant_type=client_credentials", | |
); | |
$ret = $this->request($url,$options); | |
$obj = json_decode($ret,true); | |
$this->access_token["access_token"] = $obj["access_token"]; | |
var_dump($obj); | |
return $ret; | |
} | |
public function invoicies($page=0, $page_size=100, $total_count_required=false) | |
{ | |
$params = compact("page","page_size","total_count_required"); | |
$query = http_build_query($params); | |
$url = "{$this->schema}://{$this->endpoint_host}".$this->paths[__FUNCTION__]; | |
$url = "$url?$query"; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_HTTPGET => true,//GET | |
); | |
$ret = $this->request($url,$options); | |
//JSON整形 | |
$ret = json_decode($ret,false); | |
$ret = json_encode($ret,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); | |
return $ret; | |
} | |
public function retrieve( $invoiceId ) | |
{ | |
$path = $this->paths[__FUNCTION__]; | |
$path = str_replace("{invoiceId}", $invoiceId, $path); | |
$url = "{$this->schema}://{$this->endpoint_host}".$path; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_HTTPGET => true,//GET | |
); | |
$ret = $this->request($url,$options); | |
//JSON整形 | |
$ret = json_decode($ret,false); | |
$ret = json_encode($ret,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); | |
return $ret; | |
} | |
public function isDraft($invoiceId){ | |
$ret = $this->retrieve($invoiceId); | |
$ret = json_decode($ret,true); | |
return $ret["status"] == "DRAFT"; | |
} | |
public function send($invoiceId ) { | |
$path = $this->paths[__FUNCTION__]; | |
$path = str_replace("{invoiceId}", $invoiceId, $path); | |
$url = "{$this->schema}://{$this->endpoint_host}".$path; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_POST => true,//POST | |
//body | |
); | |
$ret = $this->request($url,$options); | |
return $ret; | |
} | |
public function cancel($invoiceId, $cancel_data=array()){ | |
$path = $this->paths[__FUNCTION__]; | |
$path = str_replace("{invoiceId}", $invoiceId, $path); | |
$url = "{$this->schema}://{$this->endpoint_host}".$path; | |
$cancel_data["subject"] = ($cancel_data["subject"]) ? $cancel_data["subject"] : "キャンセル扱い"; | |
$cancel_data["note"] = ($cancel_data["note"]) ? $cancel_data["note"] : "メモ省略"; | |
$cancel_data["send_to_merchant"] = ($cancel_data["send_to_merchant"]) ? $cancel_data["send_to_merchant"] : true; | |
$cancel_data["send_to_payer"] = ($cancel_data["send_to_payer"]) ? $cancel_data["send_to_payer"] : false; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_POST => true,//POST | |
//body | |
CURLOPT_POSTFIELDS => json_encode($cancel_data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES ), //キャンセルするもの | |
); | |
$ret = $this->request($url,$options); | |
return $ret; | |
} | |
public function delete($invoiceId) { | |
$path = $this->paths[__FUNCTION__]; | |
$path = str_replace("{invoiceId}", $invoiceId, $path); | |
$url = "{$this->schema}://{$this->endpoint_host}".$path; | |
/// | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_CUSTOMREQUEST => "DELETE",//DELETE | |
//body | |
); | |
$ret = $this->request($url,$options); | |
return $ret; | |
} | |
//デフォルトスタイルのInvoice作成テンプレ | |
public function invoice_obj(){ | |
$text = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.("invoice_info_sample.json")); | |
return json_decode($text,true); | |
} | |
public function create($invoice_info) { | |
//json 整形 | |
$obj = json_decode($invoice_info); | |
$invoice_info = json_encode($obj, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); | |
//echo $invoice_info; | |
//exit; | |
// | |
$url = "{$this->schema}://{$this->endpoint_host}".$this->paths[__FUNCTION__]; | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_POST => true,//POST | |
//body | |
CURLOPT_POSTFIELDS => $invoice_info, // 請求書本文 | |
); | |
$ret = $this->request($url,$options); | |
return $ret; | |
} | |
public function update($invoiceId, $invoice_info) | |
{ | |
//path解決 | |
$path = $this->paths[__FUNCTION__]; | |
$path = str_replace("{invoiceId}", $invoiceId, $path); | |
$url = "{$this->schema}://{$this->endpoint_host}".$path; | |
//json 整形 | |
$obj = json_decode($invoice_info); | |
$invoice_info = json_encode($obj, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); | |
$options = array( | |
//HEADER | |
CURLOPT_HTTPHEADER => array( | |
'Content-Type: application/json', | |
"Authorization: Bearer {$this->access_token}", | |
), | |
//Method | |
CURLOPT_CUSTOMREQUEST => "PUT",//PUT | |
//body | |
CURLOPT_POSTFIELDS => $invoice_info, // 請求書本文 | |
); | |
$ret = $this->request($url,$options); | |
return $ret; | |
} | |
public function request( $url, $options) { | |
//var_dump($url); | |
$curl = curl_init($url); | |
//general | |
$options[CURLOPT_RETURNTRANSFER]=true;//実行結果取得 | |
$options[CURLOPT_HEADER] = true;//実行結果取得 | |
//var_dump($options); | |
curl_setopt_array($curl, $options); | |
$result = curl_exec($curl); | |
$info = curl_getinfo($curl); | |
$status_code = $info["http_code"]; | |
if($status_code>=400){ | |
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); | |
$header = substr($result, 0, $header_size); | |
$body = substr($result, $header_size); | |
var_dump($options); | |
var_dump($header); | |
var_dump($body); | |
throw new \Exception("Paypalリクエスト失敗した....."); | |
exit; | |
} | |
return $result; | |
} | |
} | |
/* | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment