官 网: 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和Secretgit
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; }