一、准备工作
1.下载:paypal-sdk 1.12 php(放在自己的项目中,如放在vendor文件夹下)
2.Thinkphp3.2.3
二、创建项目
1.创建自己的项目,如:文件夹app
2.在自己项目下创建四个php文件:UserupController.class.php,PaysuccessController.class.php,function.php
function.php
//实例化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,失去一些参数");
}
$shipping = 0.00;//运费
$total = $price + $shipping;
//设置付款人,将支付方式设置为paypal
$payer = newPayer();
$payer->setPaymentMethod('paypal');
$item = newItem();//项目
$item->setName($product)//名称
->setCurrency('USD')//货币
->setQuantity(1)//数量
->setPrice($price);//价格
//项目组
$itemList = newItemList();
$itemList->setItems([$item]);
//额外的付款细节
$details = newDetails();
$details->setShipping($shipping)//运输,运费
->setSubtotal($price);//小计
//付款量
$amount = newAmount();
$amount->setCurrency('USD')//货币
->setTotal($total)//交易金额
->setDetails($details);
//交易
$transaction = newTransaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("支付描述内容")
->setInvoiceNumber(uniqid());
//重定向网址(设置买方在付款批准/取消后必须重定向到的网址。)
$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);//错误点
} catch (PayPalConnectionException$e) {
echo $e->getData();
die();
}
$approvalUrl = $payment->getApprovalLink();
$this->ajaxReturn($approvalUrl);//这个是paypal用户授权地址,你可以用header跳转
}
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");
}
网友评论