03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

E-Commerce 275<br />

85 }<br />

.<br />

This was extracted from app/controllers/OrderController.php.<br />

There are a few steps taking place here:<br />

1. We validate that the account and items details have been provided.<br />

2. We create an order item, and assign it to the prodded account.<br />

3. We json_decode() the provided items and return an error if an invalid format has been<br />

provided.<br />

4. We create individual order items for each provided item, and add the total value up.<br />

5. We pass this value, and the order to a GateWayInterface class (which we’ll create in a bit).<br />

6. If this pay() method returns true; we create a document (with the DocumentInterface we’re<br />

about to make) and send it (with the MessengerInterface we’re also about to make).<br />

7. Finally we return a status of ok.<br />

The main purpose of this endpoint is to create the order (and order items) while passing the payment<br />

off to the service provider classes.<br />

.<br />

It would obviously be better to separate these tasks into their own classes/methods but there’s<br />

simply not time for that sort of thing. Feel free to do it in your own application!<br />

Working The Service Provider<br />

This leaves us with the service-provider part of things. I’ve gone through the motions to hook<br />

everything up (as you might have done following on from the chapter which covered this); and<br />

here is a list of the changes:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!