How can I pass shipping address to PayPal Express checkout

Pamulapati
Contributor
Contributor

 

I am using PayPal Express checkout Client-side REST for our eCommerce solution. I am wondering how I can pass shipping address from our website to PayPal, so we can have the shipping address confirmed by PayPal and be eligible for Seller Protection. For your reference I am using below code:

 

<div id="paypal-button"></div>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<script>
    paypal.Button.render({

        env: 'production', // Or 'sandbox'

        client: {
            sandbox:    'xxxxxxxxx',
            production: 'xxxxxxxxx'
        },

        commit: true, // Show a 'Pay Now' button

        payment: function(data, actions) {
            return actions.payment.create({
                payment: {
                    transactions: [
                        {
                            amount: { total: '1.00', currency: 'USD' }
                        }
                    ]
                }
            });
        },

        onAuthorize: function(data, actions) {
            return actions.payment.execute().then(function(payment) {

                // The payment is complete!
                // You can now show a confirmation message to the customer
            });
        }

    }, '#paypal-button');
</script>

Thanks for your help in advance.

Login to Me Too
1 ACCEPTED SOLUTION

Accepted Solutions
Solved

Pamulapati
Contributor
Contributor

Finally It is worked. The issue is redirection is not working, So I replaced the redirection with HTML message. i.e.

 

Instead of:

window.location="https://mydomain.com/success.php";

 

Replaced code:

$("#message").html("Payment Successful!");

View solution in original post

Login to Me Too
3 REPLIES 3

angelleye
Advisor
Advisor

You need to add the shipping_address node to the transactions node in your JSON request.  PayPal's API doc for creating a payment shows an example of this.

 

curl -v -X POST https://api.sandbox.paypal.com/v1/payments/payment \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
  "intent": "sale",
  "payer": {
  "payment_method": "paypal"
  },
  "transactions": [
  {
    "amount": {
    "total": "30.11",
    "currency": "USD",
    "details": {
      "subtotal": "30.00",
      "tax": "0.07",
      "shipping": "0.03",
      "handling_fee": "1.00",
      "shipping_discount": "-1.00",
      "insurance": "0.01"
    }
    },
    "description": "The payment transaction description.",
    "custom": "EBAY_EMS_90048630024435",
    "invoice_number": "48787589673",
    "payment_options": {
    "allowed_payment_method": "INSTANT_FUNDING_SOURCE"
    },
    "soft_descriptor": "ECHI5786786",
    "item_list": {
    "items": [
      {
      "name": "hat",
      "description": "Brown hat.",
      "quantity": "5",
      "price": "3",
      "tax": "0.01",
      "sku": "1",
      "currency": "USD"
      },
      {
      "name": "handbag",
      "description": "Black handbag.",
      "quantity": "1",
      "price": "15",
      "tax": "0.02",
      "sku": "product34",
      "currency": "USD"
      }
    ],
    "shipping_address": {
      "recipient_name": "Brian Robinson",
      "line1": "4th Floor",
      "line2": "Unit #34",
      "city": "San Jose",
      "country_code": "US",
      "postal_code": "95131",
      "phone": "011862212345678",
      "state": "CA"
    }
    }
  }
  ],
  "note_to_payer": "Contact us for any questions on your order.",
  "redirect_urls": {
  "return_url": "https://example.com/return",
  "cancel_url": "https://example.com/cancel"
  }
}'

 

 

Angell EYE - www.angelleye.com
PayPal Partner and Certified Developer - Kudos are Greatly Appreciated!
Login to Me Too

Pamulapati
Contributor
Contributor

Thank you for your help, however a small problem. I modified the code and added shipping_address to my code. Now I am able to pass the shipping address to PayPal page.

 

Now the problem is: On success or Failure onAuthorize, onCancel, onError functions are not working/fired. PayPal popup is closed and my old page is displayed without PayPal button and without any redirection.

 

Below is my modified code:

 

<div id="paypal-button"></div>

<script src="https://www.paypalobjects.com/api/checkout.js"></script>

<script>
paypal.Button.render({

env: 'production', // Or 'sandbox'

client: {
sandbox: 'xxxxxxxxx',
production: 'xxxxxxxxx'
},

commit: true, // Show a 'Pay Now' button

payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' },
invoice_number: '48787589673',
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '5',
price: '3',
tax: '0.01',
sku: '1',
currency: 'USD'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '15',
tax: '0.02',
sku: 'product34',
currency: 'USD'
}
],
shipping_address: {
recipient_name: 'Brian Robinson',
line1: '4th Floor',
line2: 'Unit #34',
city: 'San Jose',
country_code: 'US',
postal_code: '95131',
phone: '011862212345678',
state: 'CA'
}
}
}
]
}
});
},


onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(payment) {

// The payment is complete!
// You can now show a confirmation message to the customer
window.location="https://mydomain.com/success.php";
});
},

onCancel: function(data, actions) {
/*
* Buyer cancelled the payment
*/
window.location="https://mydomain.com/failure.php";
},

onError: function(err) {
/*
* An error occurred during the transaction
*/
window.location="https://mydomain.com/failure.php";
}

}, '#paypal-button');
</script>

Login to Me Too
Solved

Pamulapati
Contributor
Contributor

Finally It is worked. The issue is redirection is not working, So I replaced the redirection with HTML message. i.e.

 

Instead of:

window.location="https://mydomain.com/success.php";

 

Replaced code:

$("#message").html("Payment Successful!");

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.