使用PHP和PayPal API進行支付

王林
發布: 2023-06-19 16:22:01
原創
2856 人瀏覽過

隨著網路交易的日益普及,支付方式也逐漸多樣化,其中PayPal作為一種廣泛使用的支付方法備受歡迎。如果您想在您的網站或應用程式上使用PayPal來處理交易,那麼您可以使用PHP和PayPal API來輕鬆地完成付款流程。

PayPal API是一組程式設計接口,用於與PayPal進行互動。透過API,您可以接收來自PayPal的通知、查詢最新的交易資訊、發起付款請求和更多。在本文中,我們將介紹如何使用PHP和PayPal API進行付款。讓我們開始吧!

步驟1:取得PayPal API憑證

在使用PayPal API之前,您需要取得API憑證。這將允許您透過API與PayPal進行通訊並進行交易。要取得憑證,請遵循以下步驟:

1.造訪PayPal開發者網站,登入您的帳戶,然後點擊" 帳戶中心"。

2.點選" 個人資料"。

3.點擊"我的應用程式和憑證"。

4.點選" 建立憑證"。

5.選擇API憑證類型(以此類推):

-「付款」代表您將接收付款的應用程式。
-「Payouts」代表您的應用程式將執行向用戶的付款。
-「訂閱和協議」代表您的應用程式將執行重複付款。

6.輸入您的應用程式名稱和可選的描述,然後按一下" 建立憑證"。

7.您將看到您的API憑證。請妥善保存它們,因為您將需要在程式碼中使用它們。

步驟2:連接到PayPal

現在,您需要在程式碼中連接到PayPal API。若要連接到PayPal,請使用以下程式碼:

$api_endpoint = 'https://api.sandbox.paypal.com'; // Sandbox endpoint
// $api_endpoint = 'https://api.paypal.com'; // Live endpoint
$client_id = 'YOUR_CLIENT_ID_HERE';
$client_secret = 'YOUR_CLIENT_SECRET_HERE';

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $api_endpoint."/v1/oauth2/token");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
 
 $result = curl_exec($ch);
 $response = json_decode($result);
 
 $access_token = $response->access_token;
登入後複製

這將在測試環境中連接到PayPal API。若要連接到生產環境,請取消註解第5行,註第4行。

步驟3:發起付款請求

現在您已連接到PayPal API,您可以發起付款請求了。要發起付款請求,請使用以下程式碼:

$post_data = '{
   "intent": "sale",
   "payer": {
     "payment_method": "paypal"
   },
   "transactions": [{
     "amount": {
       "total": "10.00",
       "currency": "USD"
     }
   }],
   "redirect_urls": {
     "return_url": "https://example.com/success",
     "cancel_url": "https://example.com/cancel"
   }
 }';

$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $api_endpoint."/v1/payments/payment");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 
 $headers = array(
   "Content-Type: application/json",
   "Authorization: Bearer ".$access_token
 );
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
 $result = curl_exec($ch);
 $response = json_decode($result);
登入後複製

這將發起一筆10美元的付款請求,並將用戶重新導向到成功或取消頁面。您可以根據需要更改金額和貨幣。

步驟4:取得付款憑證

付款成功後,您需要取得PayPal付款憑證,以便將其儲存到資料庫或以其他方式處理它。要取得付款憑證,請使用以下程式碼:

$payment_id = $response->id;
 $payer_id = $_GET['PayerID'] // Payer ID from the URL parameter
 $execute_payment = '{
   "payer_id": "'.$payer_id.'"
 }';
 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $api_endpoint."/v1/payments/payment/".$payment_id."/execute");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $execute_payment);
 
 $headers = array(
   "Content-Type: application/json",
   "Authorization: Bearer ".$access_token
 );
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
 $result = curl_exec($ch);
 $response = json_decode($result);
 $transaction_id = $response->transactions[0]->related_resources[0]->sale->id;
登入後複製

這將取得付款ID和交易ID,您可以將它們儲存到資料庫或以其他方式處理它們。

步驟5:處理PayPal通知

最後,當交易發生時,PayPal將向您的應用程式發送通知。您可以使用以下程式碼處理通知:

$http_body = file_get_contents('php://input');
 $notification = json_decode($http_body);
 
 if($notification->event_type == "PAYMENT.SALE.COMPLETED") {
   $payment_id = $notification->resource->parent_payment;
   $sale_id = $notification->resource->id;
   // Process the completed sale
 } elseif($notification->event_type == "PAYMENT.SALE.REFUNDED") {
   $payment_id = $notification->resource->parent_payment;
   $refund_id = $notification->resource->id;
   // Process the refunded sale
 } else {
   // Handle other event types
 }
登入後複製

您可以根據需要處理付款完成和退款通知。

總結

透過使用PHP和PayPal API,您可以輕鬆處理付款請求、取得付款憑證和處理通知。使用這些程式碼片段,您可以透過PayPal接受付款並在自己的應用程式中輕鬆處理付款資訊。

以上是使用PHP和PayPal API進行支付的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!