Paypal smart payments - PATCH

natewallis
Contributor
Contributor

Hi, 

 

I am having issues with using the smart payments buttons and having the patch function update the paypal total.  The code is working, but the value reflected to the user seems to always be out of sync. 

 

I have two test addresses saved against my sandbox user account.  One with a post code of 7000 and one with a post code of 3350.   After detecting a shipping change, I make a call to my server to update the users post code, which will in turn update the shipping total.  I then follow the documentation and return actions.order.patch() with appropriate parameters.  However, the value reflected in the paypal window is out of sync with the selected shipping address.  

 

E.g.  When I select my 7000 shipping address, I can see my cart total changing in the background on our ecommerce platform (so I know that is working) and paypal says "Address has been updated" , but the total charged to the customer does not change.  However if I then go and select the 3350 address, again, I see my website update to the correct value and in a grey box I see the order total that relates to when the 7000 post code was selected. 

 

This suggests to me that the patch is working (on some level), but it isn't reflecting immediately in the paypal payments window.. 

 

Any thoughts?

 

Here is my code:

 

      paypal.Buttons({

        style: {
          branding: true,
          label: 'buynow',
          layout: 'horizontal',
          tagline: false
        },

        createOrder: (data, actions) => {
          let order_data = {
            purchase_units: [
              {
                amount: this.generate_purchase_units_amount()          
              }
            ]
          };
          return actions.order.create(order_data);
        },

        onCancel: function (data) {
          //alert('order cancelled');
        },

        onError: function (err) {
          //alert ("there was a paypal error");
        },

        onShippingChange: (data, actions) => {

          if (data.shipping_address.country_code !== 'AU') {
            return actions.reject();
          }

          this.$store.dispatch('update_user_data', {
            ['s_zipcode']: data.shipping_address.postal_code 
          }).then(response => {

            console.log(this.generate_purchase_units_amount());

            return actions.order.patch([
              {
                op: 'replace',
                path: "/purchase_units/@reference_id=='default'/amount",
                value: this.generate_purchase_units_amount()
              }
            ]);

          });

        }

      }).render('#paypal-button-container');
    generate_purchase_units_amount(){
      return {
        currency_code: 'AUD',
        value: this.$store.getters.cart_total,
        breakdown: {
          item_total: {
            currency_code: 'AUD',
            value: this.$store.getters.cart_subtotal
          },
          shipping: {
            currency_code: 'AUD',
            value: this.$store.getters.cart_shipping_total
          }
        }
      };
    },
Login to Me Too
4 REPLIES 4

MikeKrzyzaniak
Contributor
Contributor

I have the exact same problem. When I calculate a new shipping address and consequently a new total, PayPal always displays the *previous* total, corresponding to the previous shipping address. For the first address entered, i.e. when the user first logs in, the total is displayed without any shipping charges, although the onShippingChange handler was called and I do patch the correct shipping charge to the order. When the user completes the purchase, the transaction is for the correct amount for the *current* shipping address, which is not the amount that is displayed to the user immediately prior to checking out. Any solution would be helpful.

 

Login to Me Too

boceadacian
Contributor
Contributor

I seem to have exactly the same problem, the entire amount breakdown except the total amount does not seem to be updated. One simple example: 

Create an order with items with some taxes and shipping values:

 

 

 

createOrder: function(data, actions) {
        // This function sets up the details of the transaction, including the amount and line item details.
        return actions.order.create({
          purchase_units: [{
            amount: {
              value: '11.00',
              currency_code: 'CAD',
              breakdown: {
                item_total: {
                  currency_code: 'CAD',
                  value: "3.00"
                },
                shipping: {
                  currency_code: 'CAD',
                  value: "5.00"
                },
                tax_total: {
                  currency_code: 'CAD',
                  value: "3.00"
                }
              }
            },
            items: [{
              name: 'a',
              quantity: '1',
              sku: '1223',
              tax: {
                currency_code: 'CAD',
                value: '3.00'
              },
              unit_amount: {
                currency_code: 'CAD',
                value: '3.00'
              }
            }]
          }]
        });
      },

 

 

 

Patch the order on shipping address change callback and update these values(taxes, shipping cost etc - region based taxes),

 

 

 

 

     onShippingChange: function(data, actions) {
        // Patch the shipping amount
        return actions.order.patch([
          {
            op: 'replace',
            path: '/purchase_units/@reference_id==\'default\'',
            value: {
              amount: {
                currency_code: 'CAD',
                value: "19.00",
                breakdown: {
                  item_total: {
                    currency_code: 'CAD',
                    value: "3.00"
                  },
                  shipping: {
                    currency_code: 'CAD',
                    value: "10.00"
                  },
                  tax_total: {
                    currency_code: 'CAD',
                    value: "6.00"
                  }
                }
              },
              items: [{
                name: 'a',
                quantity: '1',
                sku: '1223',
                tax: {
                  currency_code: 'CAD',
                  value: '6.00'
                },
                unit_amount: {
                  currency_code: 'CAD',
                  value: '3.00'
                }
              }]
            }
          }
        ]);
      },

 

 

 

 

Only the total amount is changed, the breakdown is ignored in the payment sheet.

In this example i tried replacing the entire purchase unit, replacing just the amount seems behave exactly the same.

Any solutions?

 

Login to Me Too

speedyjoe
New Community Member

I have the same problem. The patch of the order is ignored after recalculating the shipping cost when the users logs in to paypal. Is there any solution?

Login to Me Too

boceadacian
Contributor
Contributor

For me, the issue seems to have fixed by itself after testing the shipping change with discounts..  I'm sure that is irrelevant and it might be some issue with the test environment

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.