paypal rest api ( japanese version )
DESCRIPTION
Paypal payment using REST API for BluemoonXoops / XoopsEC and XOOPS Cube.TRANSCRIPT
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/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 実アカウントログイン Browser
Sandbox アカウントの作成と
実行結果の確認
別の 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¤cy=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();
}}
受け取り完了