一、準(zhǔn)備工作
1.下載:paypal-sdk 1.12 php(放在自己的項(xiàng)目中,如放在vendor文件夾下)
2.Thinkphp3.2.3
二、創(chuàng)建項(xiàng)目
1.創(chuàng)建自己的項(xiàng)目,如:文件夾app
2.在自己項(xiàng)目下創(chuàng)建四個(gè)php文件:UserupController.class.php,PaysuccessController.class.php,function.php
function.php
//實(shí)例化payapl
functionpayer(){
vendor('paypal.autoload');
$paypal=new\PayPal\Rest\ApiContext(new\PayPal\Auth\OAuthTokenCredential('你的clientId',’你的Secret‘));
return$paypal;
}
UserupController.class.php
public function paypal()
{
$paypal = payer();
$uid = $_POST['uid'];
$product = $_POST['product'];
$price = $_POST['price'];
$url = $_POST['url'];
if (!isset($product, $price)) {
die("lose some params,失去一些參數(shù)");
}
$shipping = 0.00;//運(yùn)費(fèi)
$total = $price + $shipping;
//設(shè)置付款人,將支付方式設(shè)置為paypal
$payer = newPayer();
$payer->setPaymentMethod('paypal');
$item = newItem();//項(xiàng)目
$item->setName($product)//名稱
->setCurrency('USD')//貨幣
->setQuantity(1)//數(shù)量
->setPrice($price);//價(jià)格
//項(xiàng)目組
$itemList = newItemList();
$itemList->setItems([$item]);
//額外的付款細(xì)節(jié)
$details = newDetails();
$details->setShipping($shipping)//運(yùn)輸,運(yùn)費(fèi)
->setSubtotal($price);//小計(jì)
//付款量
$amount = newAmount();
$amount->setCurrency('USD')//貨幣
->setTotal($total)//交易金額
->setDetails($details);
//交易
$transaction = newTransaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("支付描述內(nèi)容")
->setInvoiceNumber(uniqid());
//重定向網(wǎng)址(設(shè)置買(mǎi)方在付款批準(zhǔn)/取消后必須重定向到的網(wǎng)址。)
$redirectUrls = newRedirectUrls();
$redirectUrls->setReturnUrl("http://111.11.1.11/DatingNetwork/index.php/Admin/Paysuccess/paysuccess?success=true&uid=$uid&price=$price&product=$product&url=$url")
->setCancelUrl("http://111.11.1.11/DatingNetwork/index.php/Admin/Paysuccess/paysuccess?success=false&uid=$uid&price=$price&product=$product&url=$url");
//付款
$payment = newPayment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try {
$payment->create($paypal);//錯(cuò)誤點(diǎn)
} catch (PayPalConnectionException$e) {
echo $e->getData();
die();
}
$approvalUrl = $payment->getApprovalLink();
$this->ajaxReturn($approvalUrl);//這個(gè)是paypal用戶授權(quán)地址,你可以用header跳轉(zhuǎn)
}
PaysuccessController.class.php
public function paysuccess()
{
$paypal = payer();
if (!isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'])) {
die();
}
if ((bool)$_GET['success'] === 'false') {
echo 'Transaction cancelled!';
die();
}
$funds = M('funds');
$user = new\Admin\Model\UserModel();
$url = $_GET['url'];
$paymentID = $_GET['paymentId'];
$payerId = $_GET['PayerID'];
$token = $_GET['token'];
$price = $_GET['price'];
$product = $_GET['product'];
$uid = $_GET['uid'];
$time = date('Y-m-d H:i:s', time());
$arr = array(
'userid_funds' => $uid,
'money' => $price,
'time' => $time,
'product' => $product,
'token' => $token,
'paymentid' => $paymentID,
'payerid' => $payerId,
'type' => 0,
);
$user->where('id=' . "'$uid'")->setInc('user_money', $price);
$user->total($uid, $price);
$funds->add($arr);
$payment = Payment::get($paymentID, $paypal);
$execute = newPaymentExecution();
$execute->setPayerId($payerId);
try {
$result = $payment->execute($execute, $paypal);
} catch (Exception$e) {
die($e);
}
header("Location:$url");
}