Item Variables In Smart Button

MapleLeaf4Evr
New Community Member

Hi there,

 

I am struggling through the setup of the new smart payments and javascript.  I am trying to add items to the transaction from my website's shopping cart, but I cannot understand why the variable won't work.

 

        // Render the PayPal button into #paypal-button-container
		var itemsData = '{name: "Basic Class Fee", unit_amount: {currency_code: "CAD",value: "50.00"},quantity: "1"}';
		console.log(itemsData)
        paypal.Buttons({
			
            // Set up the transaction
            createOrder: function(data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
							currency_code: "CAD",
							value: "50.00",
							breakdown: {
								item_total: {
								currency_code: "CAD",
								value: "50.00"
							}}
							},
							items: [ (itemsData) ]
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


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

 

If I replace the itemsData variable with the string itself, the button works fine, however, as soon as I use the variable, the button breaks and returns a 400 error from PayPal. 

 

Thanks for reading

Login to Me Too
2 REPLIES 2

MapleLeaf4Evr
New Community Member

Figured it out, my variable should have been using brackets and not quotes when it was declared.  Working code below:

        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({
			
            // Set up the transaction
            createOrder: function(data, actions) {
				var itemsData = ({name: "Basic Class Fee", unit_amount: {currency_code: "CAD",value: "50.00"},quantity: "1"});
                return actions.order.create({
                    purchase_units: [{
                        amount: {
							currency_code: "CAD",
							value: (subtotal),
							breakdown: {
								item_total: {
								currency_code: "CAD",
								value: (subtotal)
							}}
							},
							items:  [(itemsData)]
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


        }).render('#paypal-button-container');
Login to Me Too

mdstorrs
Contributor
Contributor

Hi @MapleLeaf4Evr 

 

This is amazing. It has taken me days to find this. But I am still looking to pass through a few more options, like Invoice Number or similar. Something I can link to the original transaction.

 

And also what values get passed back in the successful events.

 

Where did you find the information on the items variable?

 

Cheers from Australia.

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.