2017-05-05 102 views
0

我試圖實施PayPal結帳。我發現這段代碼是近乎完美的我,但並不完全:帶重定向的PayPal結帳?

onAuthorize: function(data, actions) { 
    // Get the payment details 
    return actions.payment.get().then(function(data) { 
     // Display the payment details and a confirmation button 
     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() { 
      // Disable the button and show a loading message 
      document.querySelector('#confirm').innerText = 'Loading...'; 
      document.querySelector('#confirm').disabled = true; 
      // Execute the payment 
      return actions.payment.execute().then(function() { 
       // Show a thank-you note 
       document.querySelector('#thanksname').innerText = shipping.recipient_name; 
       document.querySelector('#confirm').style.display = 'none'; 
       document.querySelector('#thanks').style.display = 'block'; 
      }); 
     }); 
    }); 
} 

我想將用戶重定向的其他PHP頁面時,付款授權。我仍然可以調用函數actions.payment.get()和actions.payment.execute()嗎?如果是,我應該如何繼續打電話給他們?如果不是,爲什麼?

回答

0

當您創建付款傳遞的網址,以創建請求支付與redirect_urls屬性你可以配置你的php的url

{ 
    intent: "sale", 
    ... 
    redirect_urls : 
    { 
    return_url : "http://youreturnurl.com/finished", 
    cancel_url : "http://youreturnurl.com/canceled" 
    }, 
    ... 
    transactions: [ { 
    ... 
    }] 
    ... 
} 

,你只需要調用actions.redirect()你那麼方法中後get()

onAuthorize: function(data, actions) { 
    return actions.payment.get().then(function(data) { 
    //do extra actions 
    ... 
    return actions.redirect(); 
}} 

可以爲onCancel()做同樣的或只是actions.close()關閉彈出。