2017-07-24 165 views
0

我正在從NVP/SOAP PayPal API集成到更新的REST API。如何獲取PayPal REST API中的用戶信息

我的舊網站代碼使用「快速結賬」流程。

一言以蔽之:

  • 你看到「購物車」彙總頁面,包含的產品購買清單;

  • 您點擊'Pay with PayPal'按鈕(無需填寫用戶數據表單);

  • 網站發起付款,用戶重定向到PayPal;

  • 用戶登錄並確認發起的付款;

  • 用戶被重定向到網站,該網站調用PayPal以獲取用戶個人信息以保存在交易記錄中(例如姓名,地址,...);

  • 付款終於執行;

我在這裏簽出的集成模式:https://developer.paypal.com/demo/checkout/#/pattern/server

...但無法弄清楚如何實施與新的API完全相同的工作流程。

我的意思是,裏面的onAuthorize回調方法我確實有用戶授權付款的paymentID和payerID,等待執行..但沒有他的個人數據,爲了我的訂單保存。

是否有某種調用來檢索該數據?或者將其包含在回覆中?

回答

1

首先,請讓我知道你想讓我走多遠深入細節。如果您想了解工作流程,僞代碼或代碼示例的總體概述,我會盡我所能提供這些內容。現在,我將堅持參考一般概述。

查看貝寶參考文檔中的Identity API call。這是一個GET請求返回「用戶信息」的對象,又包含用戶的個人資料,如姓名,地址等

不過,我懷疑你會想,如果使用Invoice API相反,如果你的屬性買家指定與其配置文件不同的細節。在這種情況下,您應該執行GET/v1/invoicing/invoices/invoice_id請求,它將您的發票ID作爲參數並返回用戶指定的地址,名稱等。它還包含您需要構建的要執行的請求的模板。

編輯:我已經研究了一些,我想我找到了一個更好的解決方案。您可以通過撥打return actions.payment.get().then(function(data)並指定所需的數據,從Paypal按鈕回叫中返回任何交易詳情。我提供我發現下面的例子:

onAuthorize: function(data, actions) { 

     // Get the payment details 

     return actions.payment.get().then(function(data) { 

      // Display the payment details and a confirmation button. You may want to save the queried data to your server, make sure to do this here 

      var shipping = data.payer.payer_info.shipping_address; 

      document.querySelector('#recipient').innerText = shipping.recipient_name; 
      document.querySelector('#line1').innerText  = shipping.line1; 
      document.querySelector('#city').innerText  = shipping.city; 
      document.querySelector('#state').innerText  = shipping.state; 
      document.querySelector('#zip').innerText  = shipping.postal_code; 
      document.querySelector('#country').innerText = shipping.country_code; 

      document.querySelector('#paypal-button-container').style.display = 'none'; 
      document.querySelector('#confirm').style.display = 'block'; 

      // Listen for click on confirm button 

      document.querySelector('#confirmButton').addEventListener('click', function() { 

       // Button click event code 

       // Execute the payment 

       return actions.payment.execute().then(function() { 

        // payment executed code 
        // display confirmation, thank you notes and whatnot 
       }); 
      }); 
     }); 
    } 

這樣,你得到你需要自動而不創建請求額外的(和不必要的)大部分的信息。

+0

謝謝你的迴應。概述對我有好處。 無論如何,我仍然不明白Identity API。 我的意思是,我應該使用.... no ID調用/ v1/identity/openidconnect/userinfo?什麼用戶信息將被返回?這聽起來像你查詢當前登錄的用戶,但我在服務器上下文,而不是客戶端上下文。 鑑於支付者ID /交易ID,是否有某種方式要求提供身份數據? –

+1

我已經更新了我的答案,我想我找到了一個更好的方法來使用。在任何情況下,關於您評論中的問題:/ v1/identity/openidconnect/userinfo將openID作爲參數,通過[OAuth]獲取(https://developer.paypal.com/docs/integration/admin/manage -apps /?標記=開放%20ID)。這裏有兩個鏈接,詳細說明如何在PayPal中設置和維護OAuth,這將有助於:[1](https://developer.paypal.com/docs/integration/direct/make-your-first-call/)[2 ](https://developer.paypal.com/docs/integration/direct/paypal-oauth2/) – altskop

+0

我終於選擇了另一種解決方案。 我正在尋找'顯示付款詳情'API:https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/advanced-payments-api/show-payment-details/ 我沒有注意到響應中的'payerInfo'對象,這正是我所期待的。現在我可以在執行付款之前獲取用戶信息,沒關係。 您的答案對於實現我的客戶端代碼也非常有用。謝謝。 –

相關問題