Paypal REST api ( Japanese version )
Upcoming SlideShare
Loading in...5
×
 

Paypal REST api ( Japanese version )

on

  • 2,454 views

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

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

Statistics

Views

Total Views
2,454
Views on SlideShare
2,454
Embed Views
0

Actions

Likes
2
Downloads
12
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Paypal REST api ( Japanese version ) Paypal REST api ( Japanese version ) Presentation Transcript

  • PayPal REST APIYoshi.sakai@gmail.com@bluemooninc
  • Live Demo Sitehttps://www.xoopsec.com
  • GitHub / bluemooninc• XoopsEC Distoribution(GPL V3)– https://github.com/bluemooninc/xoopsec• BmPayPal – REST ApiModulehttps://github.com/bluemooninc/bmpaypal
  • REST API Document• https://developer.paypal.com/webapps/developer/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 実アカウントログイン BrowserSandboxアカウントの作成と実行結果の確認別のBrowserでショッピングとPayPalアカウント支払いのテストを行うWeb Service App
  • テストアカウントサイトへログイン• https://www.sandbox.paypal.com/>ブラウザを変更するもしくはCookie clear
  • テスト口座の確認
  • Make your first call• https://developer.paypal.com/webapps/developer/docs/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/bmpaypal/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 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;}
  • モデル部(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=paymentExecute&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;}
  • モデルその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();}}
  • 受け取り完了