PHP對接 paypal 支付的方法?這個問題可能是我們?nèi)粘W習或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!
專注于為中小企業(yè)提供成都網(wǎng)站設計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)微山免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。環(huán)境準備
注冊paypal賬號
注冊paypal開發(fā)者賬號
創(chuàng)建沙盒應用APP
下載SDK
composer require paypal/rest-api-sdk-php
實現(xiàn)支付
以下是生成支付的代碼片段,偽代碼
function createPayPal(){ $shippingPrice = 2; $taxPrice = 0; $subTotal = 26; $item1 = new PayPal\Api\Item(); $item1->setName("產(chǎn)品2")->setCurrency("USD")->setQuantity(1)->setPrice(10); $item2 = new PayPal\Api\Item(); $item2->setName("產(chǎn)品1")->setCurrency("USD")->setQuantity(2)->setPrice(8); $itemList = new PayPal\Api\ItemList(); $itemList->addItems([$item1,$item2]); // Set payment details $details = new PayPal\Api\Details(); $details->setShipping($shippingPrice)->setTax($taxPrice)->setSubtotal($subTotal); // Set payment amount //注意,此處的subtotal,必須是產(chǎn)品數(shù)*產(chǎn)品價格,所有值必須是正確的,否則會報錯 $total = $shippingPrice + $subTotal + $taxPrice; $amount = new PayPal\Api\Amount(); $amount->setCurrency("USD")->setTotal($total)->setDetails($details); // Set transaction object $transaction = new PayPal\Api\Transaction(); $transaction->setAmount($amount)->setItemList($itemList)->setDescription("這是交易描述") ->setInvoiceNumber(uniqid());//setInvoiceNumber為支付唯一標識符,在使用時建議改成訂單號 $payer = new PayPal\Api\Payer(); $payer->setPaymentMethod('paypal');//["credit_card", "paypal"] $redirectUrls = new PayPal\Api\RedirectUrls(); $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 PayPal\Api\Payment(); $payment->setIntent("sale")->setPayer($payer)->setRedirectUrls($redirectUrls)->addTransaction($transaction); try { $clientId = "xxxxxx";//上面應用的clientId和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clientId, $secret); $apiContext = new \PayPal\Rest\ApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設置線上環(huá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";//上面應用的clientId和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clientId, $secret); $apiContext = new \PayPal\Rest\ApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設置線上環(huán)境,默認是sandbox } $payment = \PayPal\Api\Payment::get($paymentID, $apiContext); $execute = new \PayPal\Api\PaymentExecution(); $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";//返回成功標識 }else{ //paypal回調(diào)錯誤,paypal狀態(tài)不正確 return "error";//返回錯誤標識 } }catch(\Exception $e){ dd($e->getMessage()); } }
退款
退款必須要有支付成功的流水單號
一個流水單號可以進行多次退款
退款成功之后,在賬號的交易記錄中不體現(xiàn),測試時請登錄測試賬號自己記錄一下余額的變化情況,以確定退款是否成功
商家賬號會減少余額并增加手續(xù)費(手續(xù)費是之前付款多收的),用戶賬號會增加余額
function refundOrder() { try{ $refundRequest = new \PayPal\Api\RefundRequest(); $amount = new \PayPal\Api\Amount(); $amount->setCurrency("USD")->setTotal(10);//退總金額 $refundRequest->setAmount($amount); $refundRequest->setDescription("退款測試"); $sale = new \PayPal\Api\Sale(); $sale->setId("XXXXXXXXX");//支付單號,支付成功時保存的支付流水單號 $clientId = "xxxxxx";//上面應用的clientId和secret $secret = "XXXXXX"; $oAuth = new \PayPal\Auth\OAuthTokenCredential($clientId, $secret); $apiContext = new \PayPal\Rest\ApiContext($oAuth); if(env('APP_DEBUG') === false ){ $apiContext->setConfig(['mode' => 'live']);//設置線上環(huá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ā)生異常 } }
感謝各位的閱讀!看完上述內(nèi)容,你們對PHP對接 paypal 支付的方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道。