官 網: www.paypal.com
開發者網站: developer.paypal.com
paypal的sdk是有TLS1.2的硬需求的,請在你的服務器上面啓用TLS1.2支持,
能夠參考個人這篇文章啓用TLS1.2實踐
雖然是windows的,可是apache那段應該一樣適用於linux都是同樣的(apache + php 5.5 + php_openssl)php
還有一點就是, 這篇文章只是一個流程的實踐, 一切以官方的SDK爲準, 請考照sdk進行擴展html
自行註冊linux
登錄developer.paypal.com
> Dashboard
> My Apps & Credentials
> REST API apps
> Create App
> 輸入App Name
> Create App
> 記錄下Clien ID和Secret
git
Dashboard
> Sandbox
> Accounts
> Profile
> Change password
github
facilitator和buyer的密碼都改了
git clone https://github.com/paypal/PayPal-PHP-SDK.git paypal cd paypal composer update
composer怎麼安裝不是本文討論的內容, 請google搜索apache
建立支付獲取支付的地址json
跳轉到支付地址segmentfault
支付->成功會跳轉到回調地址$site['success']windows
bb, show codeapi
再說一句, 我是保存到本地的本地地址爲 127.0.0.1/paypal/pay.php
<?php require "vendor/autoload.php"; // load paypal sdk use \PayPal\Api\Payer; use \PayPal\Api\Item; use \PayPal\Api\ItemList; use \PayPal\Api\Details; use \PayPal\Api\Amount; use \PayPal\Api\Transaction; use \PayPal\Api\RedirectUrls; use \PayPal\Api\Payment; use \PayPal\Exception\PayPalConnectionException; // 須要填寫的設置 $order['intent'] = 'sale'; $order['title'] = 'paypal php sdk'; $order['body'] = 'body'; $order['currency'] = 'USD'; $order['price'] = 100; $order['shipping'] = 8; $site['success'] = 'http://127.0.0.1/paypal/return.php?result=success'; $site['cancel'] = 'http://127.0.0.1/paypal/return.php?result=failed'; $key['cliend_id'] = '填寫你paypal的client_id'; $key['secret'] = '填寫你paypal的secret'; // 支付實例 $apiContent = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( $key['cliend_id'], $key['secret'] ) ); $payer = new Payer(); $payer->setPaymentMethod('paypal'); $item = new Item(); $item->setName($order['title']) ->setCurrency($order['currency']) ->setQuantity(1) ->setPrice($order['price']); $itemList = new ItemList(); $itemList->setItems([$item]); $details = new Details(); $details->setShipping($order['shipping']) ->setSubtotal($order['price']); $amount = new Amount(); $amount->setCurrency($order['currency']) ->setTotal($order['price'] + $order['shipping']) ->setDetails($details); $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription($order['body']) ->setInvoiceNumber(uniqid()); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl($site['success']) ->setCancelUrl($site['cancel']); $payment = new Payment(); $payment->setIntent('sale'); $payment->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions([$transaction]); try { $payment->create($apiContent); } catch (PayPalConnectionException $e) { // get error by $e->getData(); return false; } $pay_url = $payment->getApprovalLink(); // die($pay_url); header("Location: " . $pay_url);
回調return.php
<?php var_dump($_GET); if ('failed' === $_GET['result']) echo '支付失敗'; // 支付成功, 入庫 echo "支付成功";
捕獲到的回調數據
paypal支持但不限於美圓(USD) 歐元(EUR) 日元(JPY) 港元(HKD) 臺幣(TWD), 完整支持請看 Currencies and Currency Codes 至今不支持人民幣,爲何是至今呢?之後確定會支持的
因爲paypal不支持人民幣(CNY)結算, 因此可能在實際的操做中須要把人民幣轉換爲美圓或者港元結算, 這裏就須要用到匯率轉換接口
我收集了3種匯率轉換方式Baidu api
NOWAPI
Yahoo api
我都沒有深度用過,這裏也說不上哪一個好哪一個差,不少人都推薦雅虎接口,相比必有他的可取之處,我百度用起來方便,先百度用着,若是有其餘的需求再換口接
http://apistore.baidu.com/api...
https://www.nowapi.com/api/fi...
這個接口自行搜索,使用起來不是很方便
我這裏提供百度的api的例程, 看代碼, 在實際操做中確定須要再加一些東西, 如超時等異常處理, 這裏爲了簡單好懂這裏就沒加上
function _currency_service($from, $to, $money){ $apikey = "你的百度的apikey"; $url_param = [ 'fromCurrency' => $from, 'toCurrency' => $to, 'amount' => $money ]; // paypal 支持的貨幣 $currency_support = ['AUD', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'JPY', 'NOK', 'NZD', 'PLN', 'SEK', 'SGD', 'USD']; if (in_array($to, $currency_support)) return false; $ch = curl_init(); $url = 'http://apis.baidu.com/apistore/currencyservice/currency?' . http_build_query($url_param); $header = array( 'apikey: '. $apikey, ); curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 執行HTTP請求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); if($ret = json_decode($res)){ $cny = $ret->retData->convertedamount; return $cny; } return false; }