onShippingChange URL (server side) returns valid JSON but amount remains unchanged

skipper71_it
Contributor
Contributor

Hello community, 

I'm facing a very specific issue on the brand new (Feb. 19) Paypal Javascript SDK.

My target is to change the total amount during Paypal Checkout, if the customer want items to be delivered in some hard-to-reach Towns/ZIP Codes.

 

Since the numer of difficult places to reach is variable, and quite complex, I have to manage this on the server side.

I had no problems in making the SDK work for the standard Smartbutton events: createOrder, onShippingChange, onApprove on the client side.

I made some tests with a reduced set of ZIP codes on the client side JS, and everything worked fine.

 

Then, as I had to go for the server side. I made it work with no troubles until I got a strange problem.

Please note, that all the needed button events : createOrder, onShippingChange, onApprove are fetched from the client side, and run as URLs on the server side.

 

Create Order, for example, receives a few more parameters than it the code samples, to retrieve the shopping cart on the DB and calculate the order Amount. JSON is returned in the correct way, and the correct amount is shown on the pop-up.

 

The problem : when I change the address, and pick up (or create a new) one that has a different (=higher) shipping cost, in the pop-up I get the Paypal message "Adresse aktualisiert" (in German, corresponding to "Address updated" in English) the price remains the same, **but** if I press F5 to refresh just the pop-up and nothing else, the price changes, and the correct final amount appears.

 

Please consider, that I have used the sample code provided as a starting point, and made all the needed logging and tests on the server side, to check that the server process works. The Order Patch is applied from the server, and the client seems not to notice it. 

In fact, the total amount is calculated form the server, then returned to the client, but the client only shows:

"Address Updated", instead of "The total amount has been recalculated etc...  "

 

Shall I make some reload on the client someway ? 

This is the code of the Button event I'm using on the client side. 

 

onShippingChange: function(data, actions) {
fetch('$paypalOnShippingChangeUrl', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
data: data,
sessionModel: $sessionModel_json,
checkoutOptions: $checkoutOptions_json
})
}).then(function(res) {
if (!res || res.error) {
return actions.reject();
}
return actions.resolve();
});
},

 

Please note, that 

sessionModel: $sessionModel_json,
checkoutOptions: $checkoutOptions_json

are custom variables I need for the server process.

 

Thanks in advance for the precious help.

Best Regards,

Miguel

 

Login to Me Too
1 REPLY 1

skipper71_it
Contributor
Contributor

Hello community,

I solved the issue by myself 🙂

 

Error was:

onShippingChange: function(data, actions) {
fetch('$paypalOnShippingChangeUrl', {
method: 'post',
headers: {
'content-type': 'application/json'
},

...

 

Correct code:

onShippingChange: function(data, actions) {
return fetch('$paypalOnShippingChangeUrl', {
method: 'post',
headers: {
'content-type': 'application/json'
},

...

 

My mistake was to trust the documentation on https://developer.paypal.com/docs/checkout/integration-features/shipping-callback/

where I didn't notice the missing return was in the sample code already 😞

 

Four hours to rebuild the code in many different ways, until I noticed the bug. No issue, at the end job is done.

 

I hope my post can help somebody. 

Feel free to contact me for further info

 

All the best,

Miguel

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.