1. 程式人生 > >TP5整合PayPal支付

TP5整合PayPal支付

專案需要使用到PayPal支付,在網上找了一圈大多寫的太過簡陋不易看懂,在這裡詳細記錄整合過程方便後期使用.

第一步:下載PayPal-PHP-SDK整合到專案中

        最新SDK下載地址: https://github.com/paypal/PayPal-PHP-SDK/releases

        下載sdk解壓,我們需要使用到路徑:PayPal-PHP-SDK-1.13.0\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib下的資料夾

        如圖:

        將資料夾匯入到tp5專案的extend目錄下:

                

        同時也需要將壓縮包中log記錄的Psr資料夾匯入,否則執行會報錯

                

第二步:專案中的使用

        step1: 配置ApiContext,也就是設定PayPal官網申請的clientId和clientSecret,具體申請方法很多教程有,這裡不寫

        $apiContext = new ApiContext(
            new OAuthTokenCredential(
                config('paypal')['clientId'],       // paypal 官網獲取的clientId和clientSecret
                config('paypal')['clientSecret']
            )
        );
        $apiContext->setConfig(
            array(
                // 'mode' => 'sandbox',
                'mode' => 'live',
                'log.LogEnabled' => false,
                'log.FileName' => '../PayPal.log',
                'log.LogLevel' => 'Info', // PLEASE USE `INFO` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
                'cache.enabled' => true,
                'http.CURLOPT_CONNECTTIMEOUT' => 60
                // 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
                //'log.AdapterFactory' => '\PayPal\Log\DefaultLogFactory' // Factory class implementing \PayPal\Log\PayPalLogFactory
            )
        );

    step2: 建立設定訂單,item代表一個訂單,ItemList 用於存放多個訂單統一支付,支付成功和取消都會跳回你設定的介面,訂單建立後會生成一個跳轉PayPal支付的地址,和訂單號,使用者訪問該地址就可以在PayPal上操作支付了,具體程式碼如下:

        $apiContext = $this->initPayPal();  // 獲取配置好的ApiContext
        $itemList = new ItemList();
        $items = array();
        $total = 0;
         $product = 'test支付測試';
         $price = 0.01;  // 金額

        $total = $total + $price ;

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

        $item = new Item();
        $item->setName($product)
            ->setCurrency('HKD')
            ->setQuantity(1)
            ->setPrice($price);
        $items[] = $item;

        $itemList->setItems($items);


        $shipping = 0.00; //運費
        $total = $total  + $shipping;


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

        $amount = new Amount();
        $amount->setCurrency('HKD')
            ->setTotal($total);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription($product)
            ->setInvoiceNumber(uniqid());

        $baseUrl = "http://localhost/home/order/test";

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl($baseUrl.'?lz_type=1')
            ->setCancelUrl($baseUrl.'?lz_type=2');

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

        try {
            $payment->create($apiContext);  //錯誤點
            //

        } catch (PayPalConnectionException $e) {
            echo $e->getData();
            die();
        }
        $approvalUrl = $payment->getApprovalLink();
        $id = $payment->getId();
        $this->redirect($approvalUrl);