cancel
Showing results for 
Search instead for 
Did you mean: 

Paypal smart payments - PATCH

Contributor

Paypal smart payments - PATCH

Hi, 

 

I have implemented the paypal smart buttons inside a vue.js component (don't think this matters) so that I can access my global vuex data for use in creating the buttons.   Here is my code, I have a short helper function that I reuse to generate the JSON for the create order request and also in the onShippingChange handler. 

 

The issue I am having is that the "Review your payment" does not display the changes in the patch until I refresh the page.  I would have expected the page to update after the patch  request is complete.  

 

generation_paypal_buttons() is called when my vue.js component is mounted()...   

 

So basically what happens is that I have 2 test addresses, one with a post code 3350, one with a post code 7000.   3350 shipping is equal to $40 and 7000 shipping is $80. When I change addresses to 3350, I see a new order value that includes $80 and when I change to 7000, I see $40.... which suggests to me that the patch operation works (on the paypal server), but it is not reflected immediately in the client window. 

 

If you have any suggestions, please let me know... Thankyou.   

 

    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
          }
        }
      };
    },

 

 

    generate_paypal_buttons(){

      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 => {
            return actions.order.patch([
              {
                op: 'replace',
                path: "/purchase_units/@reference_id=='default'/amount",
                value: this.generate_purchase_units_amount()
              }
            ]);
          });

        }

      }).render('#paypal-button-container');
    }

It appear