I'm trying to implement paypal checkout. I found this bit of code which is near perfect for me but not quite :
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';
});
});
});
}
I would like to redirect the user on an other php page when payment is Authorized. Will I still be able to call functions actions.payment.get() and actions.payment.execute() ? If yes, how should I proceed to call them ? If not, why ?
You can configure your php url when you create the payment passing the urls to the create request payment with the redirect_urls property
{
intent: "sale",
...
redirect_urls :
{
return_url : "http://youreturnurl.com/finished",
cancel_url : "http://youreturnurl.com/canceled"
},
...
transactions: [ {
...
}]
...
}
and you just need to call actions.redirect()
inside your then method after the get()
.
onAuthorize: function(data, actions) {
return actions.payment.get().then(function(data) {
//do extra actions
...
return actions.redirect();
}}
You can do the same for the onCancel()
or simply close the popup with actions.close()
.