SlideShare a Scribd company logo
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
Paypal REST api ( Japanese version )
受け取り実行
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();
}
}
受け取り完了

More Related Content

PDF
RESTful web services
Tudor Constantin
 
KEY
Mojolicious - A new hope
Marcus Ramberg
 
PDF
Mojolicious
Marcos Rebelo
 
PPTX
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Dotan Dimet
 
PDF
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
ODP
Mojolicious on Steroids
Tudor Constantin
 
ODP
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
 
PDF
Saferpay Checkout Page - PHP Sample (Hosting)
webhostingguy
 
RESTful web services
Tudor Constantin
 
Mojolicious - A new hope
Marcus Ramberg
 
Mojolicious
Marcos Rebelo
 
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Dotan Dimet
 
Mojolicious. Веб в коробке!
Anatoly Sharifulin
 
Mojolicious on Steroids
Tudor Constantin
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani
 
Saferpay Checkout Page - PHP Sample (Hosting)
webhostingguy
 

What's hot (20)

PDF
Mojolicious: what works and what doesn't
Cosimo Streppone
 
PPT
Slim RedBeanPHP and Knockout
Vic Metcalfe
 
PDF
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Caldera Labs
 
PDF
Zend Server: Not just a PHP stack
Jeroen van Dijk
 
PDF
Bullet: The Functional PHP Micro-Framework
Vance Lucas
 
PPT
Perl调用微博API实现自动查询应答
琛琳 饶
 
KEY
Zend Framework Study@Tokyo #2
Shinya Ohyanagi
 
PDF
Curso Symfony - Clase 4
Javier Eguiluz
 
PDF
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
PDF
PerlでWeb API入門
Yusuke Wada
 
ODP
Symfony CMF - PHP Conference Brazil 2011
Jacopo Romei
 
PDF
Introduction to AngularJS For WordPress Developers
Caldera Labs
 
PDF
AngularJS with Slim PHP Micro Framework
Backand Cohen
 
ODP
Perl5i
Marcos Rebelo
 
PPTX
Webinar: AngularJS and the WordPress REST API
WP Engine UK
 
PPTX
Dart and AngularDart
Loc Nguyen
 
PDF
Payments On Rails
E-xact Transactions
 
KEY
Apostrophe (improved Paris edition)
tompunk
 
PDF
Curso Symfony - Clase 2
Javier Eguiluz
 
PDF
Cloud Entwicklung mit Apex
Aptly GmbH
 
Mojolicious: what works and what doesn't
Cosimo Streppone
 
Slim RedBeanPHP and Knockout
Vic Metcalfe
 
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Caldera Labs
 
Zend Server: Not just a PHP stack
Jeroen van Dijk
 
Bullet: The Functional PHP Micro-Framework
Vance Lucas
 
Perl调用微博API实现自动查询应答
琛琳 饶
 
Zend Framework Study@Tokyo #2
Shinya Ohyanagi
 
Curso Symfony - Clase 4
Javier Eguiluz
 
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall
 
PerlでWeb API入門
Yusuke Wada
 
Symfony CMF - PHP Conference Brazil 2011
Jacopo Romei
 
Introduction to AngularJS For WordPress Developers
Caldera Labs
 
AngularJS with Slim PHP Micro Framework
Backand Cohen
 
Webinar: AngularJS and the WordPress REST API
WP Engine UK
 
Dart and AngularDart
Loc Nguyen
 
Payments On Rails
E-xact Transactions
 
Apostrophe (improved Paris edition)
tompunk
 
Curso Symfony - Clase 2
Javier Eguiluz
 
Cloud Entwicklung mit Apex
Aptly GmbH
 
Ad

Similar to Paypal REST api ( Japanese version ) (20)

PPTX
Securing RESTful Payment APIs Using OAuth 2
Jonathan LeBlanc
 
PDF
Do you want a SDK with that API? (Nordic APIS April 2014)
Nordic APIs
 
PPT
PayPal Dev Con 2009 Driving Business
Aduci
 
PDF
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Francois Marier
 
ODP
Modern Web Development with Perl
Dave Cross
 
PPTX
HTML5 Gaming Payment Platforms
Jonathan LeBlanc
 
PPTX
2012 SVCodeCamp: In App Payments with HTML5
Jonathan LeBlanc
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PPTX
An introduction to Laravel Passport
Michael Peacock
 
PPTX
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
PDF
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Francois Marier
 
PDF
How to build a High Performance PSGI/Plack Server
Masahiro Nagano
 
PDF
WordPress REST API hacking
Jeroen van Dijk
 
PDF
How to Create Login and Registration API in PHP.pdf
Appweb Coders
 
PDF
Mojolicious
Lenz Gschwendtner
 
PDF
PHPSpec - the only Design Tool you need - 4Developers
Kacper Gunia
 
PDF
Blog Hacks 2011
Yusuke Wada
 
PDF
Dealing with Legacy PHP Applications
Clinton Dreisbach
 
PDF
Passwords suck, but centralized proprietary services are not the answer
Francois Marier
 
Securing RESTful Payment APIs Using OAuth 2
Jonathan LeBlanc
 
Do you want a SDK with that API? (Nordic APIS April 2014)
Nordic APIs
 
PayPal Dev Con 2009 Driving Business
Aduci
 
Building Persona: federated and privacy-sensitive identity for the Web (LCA 2...
Francois Marier
 
Modern Web Development with Perl
Dave Cross
 
HTML5 Gaming Payment Platforms
Jonathan LeBlanc
 
2012 SVCodeCamp: In App Payments with HTML5
Jonathan LeBlanc
 
Silex Cheat Sheet
Andréia Bohner
 
Silex Cheat Sheet
Andréia Bohner
 
An introduction to Laravel Passport
Michael Peacock
 
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
Building Persona: federated and privacy-sensitive identity for the Web (Open ...
Francois Marier
 
How to build a High Performance PSGI/Plack Server
Masahiro Nagano
 
WordPress REST API hacking
Jeroen van Dijk
 
How to Create Login and Registration API in PHP.pdf
Appweb Coders
 
Mojolicious
Lenz Gschwendtner
 
PHPSpec - the only Design Tool you need - 4Developers
Kacper Gunia
 
Blog Hacks 2011
Yusuke Wada
 
Dealing with Legacy PHP Applications
Clinton Dreisbach
 
Passwords suck, but centralized proprietary services are not the answer
Francois Marier
 
Ad

More from Yoshi Sakai (18)

PPTX
いきなりAi tensor flow gpuによる画像分類と生成
Yoshi Sakai
 
PPTX
Access で Excel ファイルの操作を行う為のライブラリ設定
Yoshi Sakai
 
PPTX
Rhodes mobile Framework
Yoshi Sakai
 
PPTX
Rhodes mobile Framework (Japanese)
Yoshi Sakai
 
PPTX
Xoopsec
Yoshi Sakai
 
PDF
Osc2009tokyofall xoops groupware
Yoshi Sakai
 
PPTX
XOOPS EC Distribution
Yoshi Sakai
 
PPTX
XOOPS and Twitter Bootstrap
Yoshi Sakai
 
PPTX
XOOPS EC on C4SA Paas deployment
Yoshi Sakai
 
PPTX
Xcc2012
Yoshi Sakai
 
PPTX
Xoops x
Yoshi Sakai
 
PPTX
Oss活動指針
Yoshi Sakai
 
PDF
Weeklycms20120218
Yoshi Sakai
 
PPTX
XOOPS Cube 2.2 Pack 2011
Yoshi Sakai
 
PPTX
XOOPS Securilty flow
Yoshi Sakai
 
PDF
Seminer20110119
Yoshi Sakai
 
ODP
Satlab20101127
Yoshi Sakai
 
ODP
Xoops Cube Saturday Lab. 2010/09/25
Yoshi Sakai
 
いきなりAi tensor flow gpuによる画像分類と生成
Yoshi Sakai
 
Access で Excel ファイルの操作を行う為のライブラリ設定
Yoshi Sakai
 
Rhodes mobile Framework
Yoshi Sakai
 
Rhodes mobile Framework (Japanese)
Yoshi Sakai
 
Xoopsec
Yoshi Sakai
 
Osc2009tokyofall xoops groupware
Yoshi Sakai
 
XOOPS EC Distribution
Yoshi Sakai
 
XOOPS and Twitter Bootstrap
Yoshi Sakai
 
XOOPS EC on C4SA Paas deployment
Yoshi Sakai
 
Xcc2012
Yoshi Sakai
 
Xoops x
Yoshi Sakai
 
Oss活動指針
Yoshi Sakai
 
Weeklycms20120218
Yoshi Sakai
 
XOOPS Cube 2.2 Pack 2011
Yoshi Sakai
 
XOOPS Securilty flow
Yoshi Sakai
 
Seminer20110119
Yoshi Sakai
 
Satlab20101127
Yoshi Sakai
 
Xoops Cube Saturday Lab. 2010/09/25
Yoshi Sakai
 

Recently uploaded (20)

PDF
India Cold Chain Storage And Logistics Market: From Farm Gate to Consumer – T...
Kumar Satyam
 
PDF
2025 07 29 The Future, Backwards Agile 2025.pdf
Daniel Walsh
 
PPTX
PUBLIC RELATIONS N6 slides (4).pptx poin
chernae08
 
PDF
Bihar Idea festival - Pitch deck-your story.pdf
roharamuk
 
PDF
William Trowell - A Construction Project Manager
William Trowell
 
PDF
MBA-I-Year-Session-2024-20hzuxutiytidydy
cminati49
 
PPTX
Appreciations - July 25.pptxdddddddddddss
anushavnayak
 
PDF
GenAI for Risk Management: Refresher for the Boards and Executives
Alexei Sidorenko, CRMP
 
PDF
Followers to Fees - Social media for Speakers
Corey Perlman, Social Media Speaker and Consultant
 
PDF
Tariff Surcharge and Price Increase Decision
Joshua Gao
 
PPTX
Appreciations - July 25.pptxffsdjjjjjjjjjjjj
anushavnayak
 
PPTX
E-Way Bill under GST – Transport & Logistics.pptx
Keerthana Chinnathambi
 
DOCX
UNIT 2 BC.docx- cv - RESOLUTION -MINUTES-NOTICE - BUSINESS LETTER DRAFTING
MANJU N
 
PDF
Unveiling the Latest Threat Intelligence Practical Strategies for Strengtheni...
Auxis Consulting & Outsourcing
 
PPTX
Business Plan Presentation: Vision, Strategy, Services, Growth Goals & Future...
neelsoni2108
 
PPTX
Integrative Negotiation: Expanding the Pie
badranomar1990
 
PDF
Danielle Oliveira New Jersey - A Seasoned Lieutenant
Danielle Oliveira New Jersey
 
PDF
NewBase 24 July 2025 Energy News issue - 1805 by Khaled Al Awadi._compressed...
Khaled Al Awadi
 
PDF
High Capacity Core IC Pneumatic Spec-Sheet
Forklift Trucks in Minnesota
 
PPTX
Struggling to Land a Social Media Marketing Job Here’s How to Navigate the In...
RahulSharma280537
 
India Cold Chain Storage And Logistics Market: From Farm Gate to Consumer – T...
Kumar Satyam
 
2025 07 29 The Future, Backwards Agile 2025.pdf
Daniel Walsh
 
PUBLIC RELATIONS N6 slides (4).pptx poin
chernae08
 
Bihar Idea festival - Pitch deck-your story.pdf
roharamuk
 
William Trowell - A Construction Project Manager
William Trowell
 
MBA-I-Year-Session-2024-20hzuxutiytidydy
cminati49
 
Appreciations - July 25.pptxdddddddddddss
anushavnayak
 
GenAI for Risk Management: Refresher for the Boards and Executives
Alexei Sidorenko, CRMP
 
Followers to Fees - Social media for Speakers
Corey Perlman, Social Media Speaker and Consultant
 
Tariff Surcharge and Price Increase Decision
Joshua Gao
 
Appreciations - July 25.pptxffsdjjjjjjjjjjjj
anushavnayak
 
E-Way Bill under GST – Transport & Logistics.pptx
Keerthana Chinnathambi
 
UNIT 2 BC.docx- cv - RESOLUTION -MINUTES-NOTICE - BUSINESS LETTER DRAFTING
MANJU N
 
Unveiling the Latest Threat Intelligence Practical Strategies for Strengtheni...
Auxis Consulting & Outsourcing
 
Business Plan Presentation: Vision, Strategy, Services, Growth Goals & Future...
neelsoni2108
 
Integrative Negotiation: Expanding the Pie
badranomar1990
 
Danielle Oliveira New Jersey - A Seasoned Lieutenant
Danielle Oliveira New Jersey
 
NewBase 24 July 2025 Energy News issue - 1805 by Khaled Al Awadi._compressed...
Khaled Al Awadi
 
High Capacity Core IC Pneumatic Spec-Sheet
Forklift Trucks in Minnesota
 
Struggling to Land a Social Media Marketing Job Here’s How to Navigate the In...
RahulSharma280537
 

Paypal REST api ( Japanese version )