PayPal REST API
Yoshi.sakai@gmail.com
@bluemooninc
Live Demo Site
https://www.xoopsec.com
GitHub / bluemooninc
• XoopsEC Distoribution(GPL V3)
– https://github.com/bluemooninc/xoopsec
• BmPayPal – REST Api
Modulehttps://github.com/bluemooninc/bmp
aypal
REST API Document
• https://developer.paypal.com/webapps/develope
r/docs/api/#create-a-payment
API利用に必要なパラメータ
• EndPoint
• Client ID
• Secret
Developer登録
• https://developer.paypal.com/
Test Account 作成
• Country=USでPersonalとBusinessを作る
REST API apps作成
REST API Credentials 確認
注意点
• Cookie,Session変数で制御しているので、実PayPalアカウントでログ
インしたブラウザでは、Sandboxアカウントは利用出来ない。
PayPal 実アカウント
ログイン Browser
Sandboxアカウントの作成と
実行結果の確認
別のBrowserで
ショッピングと
PayPalアカウント支払いのテス
トを行う
Web Service App
テストアカウントサイトへログ
イン
• https://www.sandbox.paypal.com/
>ブラウザを変更する
もしくはCookie clear
テスト口座の確認
Make your first call
• https://developer.paypal.com/webapps/developer/doc
s/integration/direct/make-your-first-call/
PayPalアカウント決済($)
円ドル換算
private function getRatefromGoogle($to,$from){
$exchangeEndpoint = sprintf("http://rate-
exchange.appspot.com/currency?from=%s&to=%s",$from,$to);
$json = file_get_contents($exchangeEndpoint);
$data = json_decode($json, TRUE);
if($data){
return $data['rate'];
}
}
private function exchangeToUSD($amount,$currency="USD"){
if ($currency!="USD"){
$this->rate = $this->getRatefromGoogle($currency,"USD");
$amount_usd = round($amount / $this->rate, 2);
}else{
$amount_usd = $amount;
}
return $amount_usd;
}
API渡すパラメータの準備
• https://www.xoopsec.com/modules/bmpaypal/b
mpaypal/index?order_id=38&amount=82.450000
&currency=USD
PayPal API準備完了
• REST APIでパラメータをセットしてPayPalアカウント決済の準備をす
る
• https://www.xoopsec.com/modules/bmpaypal/AcceptPayment/index/25
コントローラ部(AcceptPayment)
public function __construct(){
parent::__construct();
$this->mModel = Model_Payment::forge();
$this->Model_PayPal = Model_PayPal::forge();
$this->return_url = XOOPS_URL . "/modules/bmpaypal/ExecutePayment/return/";
$this->cencel_url = XOOPS_URL . "/modules/bmpaypal/ExecutePayment/cancel/";
}
public function action_index(){
$payment_id = $this->mParams[0];
$this->template = 'AcceptPayment.html';
$object = $this->mModel->get($payment_id);
$uid = $this->root->mContext->mXoopsUser->get('uid');
$this->Model_PayPal->set($object);
$json_resp = $this->Model_PayPal->AcceptPayment( $this->return_url, $this->cencel_url );
// call REST api
$this->mModel->SavePaymentInfo( $payment_id, $json_resp['id'], $json_resp['state'] );
$this->links = $this->Model_PayPal->getLinks();
if ($json_resp){
$_SESSION['bmpaypal'] = $json_resp;
}
}
モデルその2(get_access_token)
function get_access_token($url, $postdata) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_USERPWD, $this->clientId . ":" . $this->clientSecret);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$response = curl_exec( $curl );
if (empty($response)) {
// some kind of an error happened
die(curl_error($curl));
curl_close($curl); // close cURL handler
} else {
$info = curl_getinfo($curl);
$this->message[] = "Time took: " . $info['total_time']*1000 . "ms<br />";
curl_close($curl); // close cURL handler
if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
$this->message[] = "Received error: " . $info['http_code']. "<br />";
$this->message[] = "Raw response:".$response."<br />";
return NULL;
}
}
// Convert the result from JSON format to a PHP array
$jsonResponse = json_decode( $response );
return $jsonResponse->access_token;
}
モデル部(AcceptPayment)public function &AcceptPayment($returnUrl,$cancelUrl)
{
// Get token for Authorization: Bearer
$this->token = $this->get_access_token($this->host.$this->token_endpoint,$this->token_postArgs);
if(is_null($this->token)) echoMessage($this->message);
$url = $this->host.'/v1/payments/payment';
$payment = array(
'intent' => 'sale',
'redirect_urls' => array(
'return_url' => $returnUrl,
'cancel_url' => $cancelUrl
),
'payer' => array(
'payment_method' => 'paypal'
),
'transactions' => array (array(
'amount' => array(
'total' => $this->object->getVar('amount'),
'currency' => $this->object->getVar('currency')
),
'description' => 'Pass payment information to create a payment'
))
);
$json = json_encode($payment);
$this->json_resp = $this->make_post_call($url, $json);
return $this->json_resp;
}
PayPal決済リンク取得
public function getLinks(){
if($this->json_resp) {
return $this->json_resp['links'];
}else{
return NULL;
}
}
PayPalサイトへ
ログインして支払う
Return URL に戻る
管理画面の記録
• 鍵をクリックすると、受け取りが実行される
• https://www.xoopsec.com/modules/bmpaypal/admin/index.php?action=payment
Execute&id=25
受け取り実行
public function executePayment($paypal_id,$payer_id){
// Get token for Authorization: Bearer
$this->token = $this->get_access_token($this->host.$this-
>token_endpoint,$this->token_postArgs);
if ( is_null($this->token) ) echoMessage($this->message);
$url = $this->host.'/v1/payments/payment/'.$paypal_id."/execute/";
$payment = array(
'payer_id' => $payer_id
);
$json = json_encode($payment);
$this->json_resp = $this->make_post_call($url, $json);
return $this->json_resp;
}
モデルその2
function make_post_call($url, $postdata) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->token,
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
#curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$response = curl_exec( $curl );
if (empty($response)) {
// some kind of an error happened
die(curl_error($curl));
curl_close($curl); // close cURL handler
} else {
$info = curl_getinfo($curl);
echo "Time took: " . $info['total_time']*1000 . "ms<br />";
curl_close($curl); // close cURL handler
if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
echo "Received error: " . $info['http_code']. "<br />";
echo "Raw response:".$response."<br />";
die();
}
}
受け取り完了

Paypal REST api ( Japanese version )