paypal rest api ( japanese version )

29
PayPal REST API Yoshi.sakai@g mail.com @bluemooninc

Upload: yoshi-sakai

Post on 14-May-2015

2.293 views

Category:

Business


4 download

DESCRIPTION

Paypal payment using REST API for BluemoonXoops / XoopsEC and XOOPS Cube.

TRANSCRIPT

Page 2: Paypal REST api ( Japanese version )

Live Demo Site

https://www.xoopsec.com

Page 3: Paypal REST api ( Japanese version )

GitHub / bluemooninc

• XoopsEC Distoribution(GPL V3)– https://github.com/bluemooninc/xoopsec

• BmPayPal – REST Api Modulehttps://github.com/bluemooninc/bmpaypal

Page 4: Paypal REST api ( Japanese version )

REST API Document

• https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment

Page 5: Paypal REST api ( Japanese version )

API 利用に必要なパラメータ• EndPoint• Client ID• Secret

Page 6: Paypal REST api ( Japanese version )

Developer 登録

• https://developer.paypal.com/

Page 7: Paypal REST api ( Japanese version )

Test Account 作成

• Country=US で Personal と Business を作る

Page 8: Paypal REST api ( Japanese version )

REST API apps 作成

Page 9: Paypal REST api ( Japanese version )

REST API Credentials 確認

Page 10: Paypal REST api ( Japanese version )

注意点

• Cookie,Session 変数で制御しているので、実 PayPal アカウントでログインしたブラウザでは、 Sandbox アカウントは利用出来ない。

PayPal 実アカウントログイン Browser

Sandbox アカウントの作成と

実行結果の確認

別の Browser でショッピングと

PayPal アカウント支払いのテストを行う

Web Service App

Page 11: Paypal REST api ( Japanese version )

テストアカウントサイトへログイン

• https://www.sandbox.paypal.com/>ブラウザを変更するもしくは Cookie clear

Page 12: Paypal REST api ( Japanese version )

テスト口座の確認

Page 13: Paypal REST api ( Japanese version )

Make your first call

• https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/

Page 14: Paypal REST api ( Japanese version )

PayPal アカウント決済 ($)

Page 15: Paypal REST api ( Japanese version )

円ドル換算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;

}

Page 16: Paypal REST api ( Japanese version )

API 渡すパラメータの準備

• https://www.xoopsec.com/modules/bmpaypal/bmpaypal/index?order_id=38&amount=82.450000&currency=USD

Page 17: Paypal REST api ( Japanese version )

PayPal API 準備完了

• REST API でパラメータをセットして PayPal アカウント決済の準備をする• https://www.xoopsec.com/modules/bmpaypal/AcceptPayment/index/25

Page 18: Paypal REST api ( Japanese version )

コントローラ部 (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;}

}

Page 19: Paypal REST api ( Japanese version )

モデルその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 happeneddie(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 handlerif($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;

}

Page 20: Paypal REST api ( Japanese version )

モデル部 (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;

}

Page 21: Paypal REST api ( Japanese version )

PayPal 決済リンク取得public function getLinks(){

if($this->json_resp) {return $this->json_resp['links'];

}else{return NULL;

}}

Page 22: Paypal REST api ( Japanese version )

PayPal サイトへ

Page 23: Paypal REST api ( Japanese version )

ログインして支払う

Page 24: Paypal REST api ( Japanese version )

Return URL に戻る

Page 25: Paypal REST api ( Japanese version )

管理画面の記録

• 鍵をクリックすると、受け取りが実行される• https://www.xoopsec.com/modules/bmpaypal/admin/index.php?

action=paymentExecute&id=25

Page 26: Paypal REST api ( Japanese version )
Page 27: 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;

}

Page 28: Paypal REST api ( Japanese version )

モデルその2function 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 happeneddie(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 handlerif($info['http_code'] != 200 && $info['http_code'] != 201 ) {

echo "Received error: " . $info['http_code']. "<br />";echo "Raw response:".$response."<br />";die();

}}

Page 29: Paypal REST api ( Japanese version )

受け取り完了