Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP & The Stripe API Video Question

Ok so I watched this video and I tell it was amazing...gave me almost exactly what I needed. I only had one concern though and looking through the documentation, it was still not as clear to me. So on my site, the payment has to be dynamic based on the amount of weight of the item we will be shipping to the customer. Essentially, the customer will be charged $25 flat first for anything above say 5lb and then $3 extra per pound. Now this payment system has to be dynamic so it could be $25 or $28 or even $34. How do I integrate this system? My initial thought was I had to edit the charge.php file and just have like some sort of an if() statements checking some POST data and then changing the "amount" in the array from that...am I even on the right track?

2 Answers

What I gathered from the video is that it would depend on whether you wanted your customer to know the final price or not. If you wanted them to know the final charge up front then I think doing it in charge.php would be to late. I think charge.php is where the actual charging is going to be initiated.

I suppose you could do it in there if you displayed some kind of message to the user that "additional shipping charges will apply"

In the video it showed that when you click on the blue button it brings up that modal window. There was a pay button on there if I remember right that had the amount you were going to pay. So if you would like that to reflect the total amount including shipping costs then I think you need to do it client side.

The html5 data attribute data-amount which is in the script on your page is what you are going to have to modify I think.

I'm assuming you have some kind of form that the the user is interacting with and selecting things which then affects the weight. You can calculate the final cost based on the user input and then update that data-amount attribute so that when the modal comes up it is showing the final price.

Thanks for the response! Yeah thats what I was thinking but after some more research and I think he mentioned it in the video, that data-amount attribute is just there for show for the client. It's the amount in charge.php that matters and I get that since it could be a security issue as someone can just modify the html5 attr and make a $30 item like $0.30.

So that's why I was thinking that either I would need a whole bunch of if's in the charge.php which would determine the "amount" array attribute or there is some other way through Stripe that would be able to handle dynamic requests.

So I don't think data-amount will help as far as real dynamic transactions (It would though just for the display purpose on the modal window).