2017-05-05 104 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()关闭弹出。