The new SDK doesn't support the onShippingChange function for Paypal Checkout Integration

I recently followed the steps outlined in this tutorial from PayPal's developer website here and I am now trying to customize the shipping costs based on the destination country, following the instructions provided here

My code for the "Paypal-Button" appears as follows:

<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXX&currency=EUR">
</script>
</head>
<body>

<h2>Checkout</h2>

<div id="paypal-button-container"></div>
<script>
  const baseOrderAmount = '15.00';
  const addFloats = (...floats) => floats.reduce((v, t) => parseFloat(t) + parseFloat(v), 0).toFixed(2);

  paypal.Buttons({

    createOrder: function(data, actions) {
      // Set up the transaction
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: baseOrderAmount
          }
        }]
      });
    },

    onApprove: function(data, actions) {
      // Capture the funds from the transaction
      return actions.order.capture().then(function(details) {
        // Show a success message to your buyer
        alert('Transaction completed by ' + details.payer.name.given_name);
        // Call your server to save the transaction
        return fetch('/paypal-transaction-complete.php', {
          method: 'post',
          body: JSON.stringify({
            orderID: data.orderID
          })
        });
      });
    },

    onShippingChange: function(data, actions) {

        // Reject all others
        if ((data.shipping_address.country !== 'DE') || (data.shipping_address.country !== 'AT') || (data.shipping_address.country !== 'CH')) {
            return actions.reject();
        }

        // Patch the shipping amount
        var shippingAmount = 10.0;
        if (data.shipping_address.country == 'AT') {
            shippingAmount = 20.0;
        }
        if (data.shipping_address.country == 'CH') {
            shippingAmount = 30.0;
        }

        return actions.order.patch([
        {
            op: 'replace',
            path: "/purchase_units/@reference_id=='default'/amount",
            value: {
                currency_code: 'EUR',
                value: addFloats(baseOrderAmount, shippingAmount),
                breakdown: {
                    item_total: {
                        currency_code: 'EUR',
                        value: baseOrderAmount
                    },
                    shipping: {
                        currency_code: 'EUR',
                        value: shippingAmount
                    }
                }
            }
        }]);
    }
  }).render('#paypal-button-container');
</script>
</body>
</html>

Everything else is functioning correctly, but when implementing the onShippingChange() function, I keep receiving an error message from PayPal stating that the seller does not ship to this country, despite providing a German address.

Answer №1

Here is the revised line:

data.shipping_address.country

Change it to this:

data.shipping_address.country_code

Warm regards, Thomas

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the page is loaded, ensure a condition is met before displaying a modal pop-up using AngularJS

Just starting out with AngularJS and looking to implement a modal pop up? I've tried using the modal on button click from the Angular dialog demo tutorial. Now, I want to show the pop up based on a condition when the page loads. The idea of automatic ...

Struggling with overlaying Bootstrap modals on top of each other

This idea is inspired by the topic Multiple modals overlay I am working on developing a folder modal that allows users to either 1) open an existing file or 2) create a new file within each folder modal. To see how it functions, please run the code below ...

Enhance your websites' search functionality with jQuery autocomplete using AJAX

I am attempting to implement dynamic autocomplete values for a text box. Here is my solution: echo json_encode($res) 0: {type_name: "name1"} 1: {type_name: "name2"} 2: {type_name: "name3"} 3: {type_name: "name4"} 4: {type_name: "name5"} Below is the co ...

The onchange function is activated when the value is being established using jQuery

During testing of my HTML5 app, I encountered the following issue: After the app finishes loading, a jQuery function is executed as follows: // Sets the Dropdown value and sliders based on local storage values function setDropdownAndSliders() { // Se ...

Tips for Updating HTML Table Content Dynamically Using JavaScript without jQuery

I am in the process of developing a web application that requires me to dynamically change the content of an HTML table based on user input, all without relying on jQuery or any other external libraries. Adding rows to the table is not an issue for me, but ...

Tips for setting up listeners across multiple modules using socket.io

Last year, I created a multiplayer game using node.js and socket.io. Now, as part of my efforts to enhance the game, I am working on breaking down the code into modules. Currently, I am utilizing expressjs 4.4 along with socket.io 1.0. One challenge I enco ...

Error: authentication failed during npm installation due to an incorrect URL

After executing npm install @types/js-cookie@^2.2.0, an error occurred: npm install @types/js-cookie@^2.2.0 npm ERR! code E401 npm ERR! Unable to authenticate, need: Basic realm="https://pkgsprodsu3weu.app.pkgs.visualstudio.com/" npm ERR! A com ...

The webpack-dev-server is not being acknowledged

When attempting to run "npm run dev," I encountered a web pack related error stating 'webpack-dev-server' is not recognized as an internal or external command, operable program, or batch file. I have included screenshots of the error from the te ...

What is the best way to incorporate styling and structure into a jQuery plugin?

I have created a custom jQuery plugin that enhances table operations, similar to a grid plugin. The plugin applies styles such as overflow:hidden; to cells and adds borders to the table and its cells. To ensure consistent border widths, I included the fol ...

Steps for replacing the firestore document ID with user UID in a document:

I've been attempting to retrieve the user UID instead of using the automatically generated document ID in Firebase/Firestore, but I'm encountering this error: TypeError: firebase.auth(...).currentUser is null This is the content of my index.js ...

Battle between Comet and Ajax polling

I'm looking to develop a chat similar to Facebook's chat feature. Using Comet would require more memory to maintain the connection. There seems to be a latency issue when using Ajax polling if requests are sent every 3-4 seconds. Considering t ...

Deleting list items by clicking a button

I'm working on adding a functional "x" button to each list item (li) so that users can click on it to remove the item. I've successfully added the button, but I'm unsure about what code to use in the onClick event to delete the corresponding ...

The functionality of the d3 Bar chart with a tool tip seems to be malfunctioning

Working on a D3 svg chart with built-in tooltips using the d3-tip library. Check out the original code here. Utilizing Django as the back end to populate log count per year from datetime. Successfully populated axis and labels except for the bars. Here i ...

How can you reset the chosen option in a Vue.js dropdown menu?

Within my dropdown menu, I have included a button that is intended to clear the selected city. Despite adding an event, the selected option is not being cleared. Can anyone provide insights on what I might be missing? Thank you in advance. Below is the bu ...

Sending data via an AJAX POST request in the request parameter

I have a question regarding the method of making a POST request in my code: $.ajax({ url :"/clientCredentials.json", type: "POST", data: { "clientEmail": email, "clientName":clientName, "orgName":orgName, "l ...

Avoiding the Popover Component from Covering the Screen When the Menu Component Opens in React Material UI

I have implemented a Menu component to display a menu upon hovering over an element. However, I have encountered an issue where the menu includes a Popover component that opens up and covers the entire screen as an overlay, preventing interaction with th ...

What is the process for defining a global variable within a module in Typescript?

I've already included a global value in my global JavaScript context: const fs = require('fs') For a specific reason, I need to include it in the global scope. Now, I want to create a .d.ts file to declare the global variable with a stron ...

Tips on transferring data using indices to separate tables

To retrieve tables dynamically, the code logic will depend on the number of items selected in the left window visible in the image provided below. I am uncertain about the exact code that needs to be written within the onClick function of the button to shi ...

Developing a perpetually scrolling container within a webpage

I am attempting to implement a scrollable div on my webpage that can continuously load content. I am currently using the following code snippet for this --> http://jsfiddle.net/cyrus2013/Qq85d/ $(document).ready(function(){ function loadMoreContent() ...

Adjusting loop sizes according to category identification

Can anyone help me troubleshoot my code? I am using the category_id where 1 represents shoe sizes and 2 represents apparel sizes. Currently, it is only displaying values for category_id(1) and for category_id(2) it is showing only "Large". Any suggestion ...