paypal支付接口

一、準(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");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容