Your SlideShare is downloading. ×
0
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Paypal REST api ( Japanese version )
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Paypal REST api ( Japanese version )

2,319

Published on

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

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

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,319
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PayPal REST APIYoshi.sakai@gmail.com@bluemooninc
  • 2. Live Demo Sitehttps://www.xoopsec.com
  • 3. GitHub / bluemooninc• XoopsEC Distoribution(GPL V3)– https://github.com/bluemooninc/xoopsec• BmPayPal – REST ApiModulehttps://github.com/bluemooninc/bmpaypal
  • 4. REST API Document• https://developer.paypal.com/webapps/developer/docs/api/#create-a-payment
  • 5. API利用に必要なパラメータ• EndPoint• Client ID• Secret
  • 6. Developer登録• https://developer.paypal.com/
  • 7. Test Account 作成• Country=USでPersonalとBusinessを作る
  • 8. REST API apps作成
  • 9. REST API Credentials 確認
  • 10. 注意点• Cookie,Session変数で制御しているので、実PayPalアカウントでログインしたブラウザでは、Sandboxアカウントは利用出来ない。PayPal 実アカウントログイン BrowserSandboxアカウントの作成と実行結果の確認別のBrowserでショッピングとPayPalアカウント支払いのテストを行うWeb Service App
  • 11. テストアカウントサイトへログイン• https://www.sandbox.paypal.com/>ブラウザを変更するもしくはCookie clear
  • 12. テスト口座の確認
  • 13. Make your first call• https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/
  • 14. PayPalアカウント決済($)
  • 15. 円ドル換算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;}
  • 16. API渡すパラメータの準備• https://www.xoopsec.com/modules/bmpaypal/bmpaypal/index?order_id=38&amount=82.450000&currency=USD
  • 17. PayPal API準備完了• REST APIでパラメータをセットしてPayPalアカウント決済の準備をする• https://www.xoopsec.com/modules/bmpaypal/AcceptPayment/index/25
  • 18. コントローラ部(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;}}
  • 19. モデルその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;}
  • 20. モデル部(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;}
  • 21. PayPal決済リンク取得public function getLinks(){if($this->json_resp) {return $this->json_resp[links];}else{return NULL;}}
  • 22. PayPalサイトへ
  • 23. ログインして支払う
  • 24. Return URL に戻る
  • 25. 管理画面の記録• 鍵をクリックすると、受け取りが実行される• https://www.xoopsec.com/modules/bmpaypal/admin/index.php?action=paymentExecute&id=25
  • 26. 受け取り実行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;}
  • 27. モデルその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();}}
  • 28. 受け取り完了

×