Thinkphp对接Paypal支付

来源:csdn博客 分类: 文章浏览史 发布时间:2020-09-16 15:20:44 最后更新:2020-09-16 浏览:175
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-09-16 15:20:44

1.首先去paypal开发者平台developer.paypal.com,去创建应用;一般我们是先创建sandbox,等sandbox跑通了再改回 live                  

2.将paypal的sdk放入thinkPHP,我习惯TP3和TP5都放在Vendor文件夹下

3.在控制器里或者自己封装一个paypal配置信息

require "vendor/autoload.php"; //载入sdk的自动加载文件
define('SITE_URL', 'http://localhost/PayPal'); //网站url自行定义
//创建支付对象实例
$paypal = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        //模拟
        'AckAgdCn4XCrgnhpUr7JszYo-svo5MQIYP94Y96TJ19saTrTlFkst9V1P75YCNTM_Knxb3OmjIUlcuGM',//clientId
        'EKlhu2S9hWjJl9E9sg3YB4voaH7JR6JTXEr1R-lHbVzskMWl0cgFpIju1Fisd3z8HnL5RpMtl723fE7H'//clientSecret
    
    )
);

4.具体接口

public function checkout()
{
    $paypal = $this->start();
    $product = I('post.product');
    $price = I('post.price');
    if (empty($product) && empty($price)) {
        echo '<Script language="JavaScript">alert("lose some params");history.go(-1)</Script>';
        die("lose some params");
    }
    $shipping = 0.00; //运费

    $total = $price + $shipping;

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $item = new Item();
    $item->setName($product)
        ->setCurrency('USD')
        ->setQuantity(1)
        ->setPrice($price);

    $itemList = new ItemList();
    $itemList->setItems([$item]);

    $details = new Details();
    $details->setShipping($shipping)
        ->setSubtotal($price);

    $amount = new Amount();
    $amount->setCurrency('USD')
        ->setTotal($total)
        ->setDetails($details);

    $transaction = new Transaction();
    $transaction->setAmount($amount)
        ->setItemList($itemList)
        ->setDescription("支付描述内容1111")
        ->setInvoiceNumber(uniqid());

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl(SITE_URL . '/pay?success=true')
        ->setCancelUrl(SITE_URL . '/pay?success=false');


    $payment = new Payment();
    $payment->setIntent('sale')
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions([$transaction]);

    try {
        $payment->create($paypal);
    } catch (PayPalConnectionException $e) {
        echo $e->getData();
        die();
    }

    $approvalUrl = $payment->getApprovalLink();
    header("Location: {$approvalUrl}");
}

public function pay()
{
    $paypal = $this->start();
    //print_r($_GET);die;
    if (!isset($_GET['success'], $_GET['paymentId'], $_GET['PayerID'])) {
        die();
    }

    if ((bool)$_GET['success'] === 'false') {

        echo 'Transaction cancelled!';
        die();
    }

    $paymentID = $_GET['paymentId'];
    $payerId = $_GET['PayerID'];

    $payment = Payment::get($paymentID, $paypal);

    $execute = new PaymentExecution();
    $execute->setPayerId($payerId);
    print_r($_GET);die;
    try {
        $result = $payment->execute($execute, $paypal);
    } catch (Exception $e) {
        die($e);
    }
    //print_r($_GET);die;
    echo '<script>alert("支付成功!感谢支持!");window.location.href="index"</script>';
}

接口的具体写法就是这样,有些数据是可以改的.

 

有不同的意见可以在下方留言。

 

 

php技术微信