推薦:《PHP視頻教程》
環(huán)境準(zhǔn)備
注冊paypal賬號
注冊paypal開發(fā)者賬號
創(chuàng)建沙盒應(yīng)用APP
下載SDK
composer require paypal/rest-api-sdk-php
實現(xiàn)支付
以下是生成支付的代碼片段,偽代碼
function createPayPal(){ $shippingPrice = 2; $taxPrice = 0; $subTotal = 26; $item1 = new PayPalApiItem(); $item1->setName("產(chǎn)品2")->setCurrency("USD")->setQuantity(1)->setPrice(10); $item2 = new PayPalApiItem(); $item2->setName("產(chǎn)品1")->setCurrency("USD")->setQuantity(2)->setPrice(8); $itemList = new PayPalApiItemList(); $itemList->addItems([$item1,$item2]); // Set payment details $details = new PayPalApiDetails(); $details->setShipping($shippingPrice)->setTax($taxPrice)->setSubtotal($subTotal); // Set payment amount //注意,此處的subtotal,必須是產(chǎn)品數(shù)*產(chǎn)品價格,所有值必須是正確的,否則會報錯 $total = $shippingPrice + $subTotal + $taxPrice; $amount = new PayPalApiAmount(); $amount->setCurrency("USD")->setTotal($total)->setDetails($details); // Set transaction object $transaction = new PayPalApiTransaction(); $transaction->setAmount($amount)->setItemList($itemList)->setDescription("這是交易描述") ->setInvoiceNumber(uniqid());//setInvoiceNumber為支付唯一標(biāo)識符,在使用時建議改成訂單號 $payer = new PayPalApiPayer(); $payer->setPaymentMethod('paypal');//["credit_card", "paypal"] $redirectUrls = new PayPalApiRedirectUrls(); $redirectUrl = "http://test.com/redirect/success";//支付成功跳轉(zhuǎn)的回調(diào) $cancelUrl = "http://test.com/redirect/cancel";//取消支付的回調(diào) $redirectUrls->setReturnUrl($redirectUrl)->setCancelUrl($cancelUrl); // Create the full payment object $payment = new PayPalApiPayment(); $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->addTransaction($transaction); try { $clientId = "xxxxxx";//上面應(yīng)用的clientId和secret $secret = "XXXXXX"; $oAuth = new PayPalAuthOAuthTokenCredential($clientId, $secret); $apiContext = new PayPalRestApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設(shè)置線上環(huán)境,默認(rèn)是sandbox } $payment->create($apiContext); $approvalUrl = $payment->getApprovalLink(); dd($approvalUrl);//這個是請求支付的鏈接,在瀏覽器中請求此鏈接就會跳轉(zhuǎn)到支付頁面 } catch (Exception $e) { dd($e->getMessage());//錯誤提示 } }
以下是支付成功的回調(diào)代碼片段,偽代碼
function payRedirect(Request $request) { $paymentID = $request->get('paymentId'); $payerId = $request->get('PayerID'); $clientId = "xxxxxx";//上面應(yīng)用的clientId和secret $secret = "XXXXXX"; $oAuth = new PayPalAuthOAuthTokenCredential($clientId, $secret); $apiContext = new PayPalRestApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設(shè)置線上環(huán)境,默認(rèn)是sandbox } $payment = PayPalApiPayment::get($paymentID, $apiContext); $execute = new PayPalApiPaymentExecution(); $execute->setPayerId($payerId); try{ $payment = $payment->execute($execute, $apiContext);//執(zhí)行,從paypal獲取支付結(jié)果 $paymentState = $payment->getState();//Possible values: created, approved, failed. $invoiceNum = $payment->getTransactions()[0]->getInvoiceNumber(); $payNum = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getId();//這是支付的流水單號,必須保存,在退款時會使用到 $total = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getAmount()->getTotal();//支付總金額 $transactionState = $payment->getTransactions()[0]->getRelatedResources()[0]->getSale()->getState();//Possible values: completed, partially_refunded, pending, refunded, denied. if($paymentState == 'approved' && $transactionState == 'completed'){ //處理成功的邏輯,例如:判斷支付金額與訂單金額,更新訂單狀態(tài)等 return "success";//返回成功標(biāo)識 }else{ //paypal回調(diào)錯誤,paypal狀態(tài)不正確 return "error";//返回錯誤標(biāo)識 } }catch(Exception $e){ dd($e->getMessage()); } }
退款
退款必須要有支付成功的流水單號
一個流水單號可以進(jìn)行多次退款
退款成功之后,在賬號的交易記錄中不體現(xiàn),測試時請登錄測試賬號自己記錄一下余額的變化情況,以確定退款是否成功
商家賬號會減少余額并增加手續(xù)費(手續(xù)費是之前付款多收的),用戶賬號會增加余額
function refundOrder() { try{ $refundRequest = new PayPalApiRefundRequest(); $amount = new PayPalApiAmount(); $amount->setCurrency("USD")->setTotal(10);//退總金額 $refundRequest->setAmount($amount); $refundRequest->setDescription("退款測試"); $sale = new PayPalApiSale(); $sale->setId("XXXXXXXXX");//支付單號,支付成功時保存的支付流水單號 $clientId = "xxxxxx";//上面應(yīng)用的clientId和secret $secret = "XXXXXX"; $oAuth = new PayPalAuthOAuthTokenCredential($clientId, $secret); $apiContext = new PayPalRestApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設(shè)置線上環(huán)境,默認(rèn)是sandbox } $detailedRefund = $sale->refundSale($refundRequest, $apiContext);//調(diào)接口 $refundState = $detailedRefund->getState();//Possible values: pending, completed, cancelled, failed. //var_dump($refundedSale); if($refundState == 'completed'){ //退款成功,返回 }else{ dd('paypal 退款失敗, 狀態(tài)不正確'); } }catch (Exception $exception){ dd($exception->getMessage());//發(fā)生異常 } }
更多接口代碼示例
http://paypal.github.io/PayPal-PHP-SDK/sample/
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com