Chapter 10 [ 233 ] Order item attributes Because some products can have selectable attributes, we need to also select these and store them in the database. Field Type Description Order_item_id Integer The product item record for an order Attribute_id Integer The attribute of this product which is being ordered Payment methods Finally, we have payment methods. This database table stores a list of payment methods. We will come on to this properly in the next chapter; however, for now we do need a table, because we reference payment methods in the order table. Field Type Description ID Integer, Primary key, Auto Increment A unique ID for the payment method Name Varchar The name of the payment method Key Varchar Refers to the payment method code les Type Enum(ofine, online) Indicates if the payment method is for online or ofine payment. Summary In this chapter we have taken our framework and extended the order process, so that the customer is now ready to make their payment. We now check that the customer is logged into the store, and if they are not we allow them to log in or create an account. The customer can then set a delivery address, and their default delivery address is shown. So, if they choose to use that, they don't need to change anything. The customer can then select the payment method they wish to use to pay for the order. Finally, the customer can review their order, before conrming and placing the order. At any time during this process, the customer can move backwards and forwards between stages if they wish. The next stage is to actually take payments from our customers! This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Taking Payment for Orders Now that our checkout process has been implemented, we are left with the nal stage: payment. Up to this stage, our customers can place orders, go right through the order process, including selecting a payment method, but they cannot yet make payment for their order. In this chapter, you will learn: How to take payment online Using PayPal Using other methods How to take payment ofine for our store How an order progresses once payment has been made Taking payment Taking payment is a fundamental aspect for any business; without payment, the business would not be able to function. With e-commerce stores, one of the benets is that we can take and process payment instantly online, giving the customer reassurances that their order has been placed and will be processed, and also giving assurance to the store that payment has been made. Our payment system For our payment system to work, we will need to separate logic for each payment method out into a different set of les. For instance, if the customer opts to pay online using PayPal, we need to generate a PayPal payment button. If they opt to pay online, we need to display payment details to them. If we want to add new payment methods in the future, we need to be able to easily slot in a new payment method. Different payment gateways have different ways of receiving payments and of sending notication back to us that a payment has been made. This logic needs to be encapsulated in the separate gateway les. • ° ° • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Taking Payment for Orders [ 236 ] The best way to do this would be to create a single payment processor object, and have each payment method extend and inherit from this object. However, this would require more research and investigation into other payment methods to get the optimal method. Instead, we will use the factory pattern; this will create a new payment gateway object depending on the payment method the customer chooses. The Factory pattern implementation is highlighted in the following code: // get the order details $orderId = intval( $urlBits[2] ); // we should really abstract this out into an order object $sql = "SELECT o.*, p.`key` AS payment FROM orders o, payment_methods p WHERE p.ID=o.payment_method AND o.ID={$orderId}"; $this->registry->getObject('db')->executeQuery( $sql ); if( $this->registry->getObject('db')->numRows() > 0 ) { $data = $this->registry->getObject('db')->getRows(); $method = $data['payment']; require_once FRAMEWORK_PATH . 'controllers/payment/methods/' . $method .'.php'; $this->paymentMethod = new Paymentmethod(); switch( $urlBits[1] ) { case 'process-payment': $this->paymentCallback(); break; case 'make-payment': $this->displayMakePayment(); break; } } To display a payment page, we need to take our payment method object, and either display a form for credit card details, a PayPal payment button, or details for sending a payment through check in the post. The following code takes out the payment method object, assigns appropriate template tags, and builds the template from a template specic to the payment method: private function displayMakePayment() { $paymentKey = $this->paymentMethod->getKey(); // template tags please $this->paymentMethod->makePaymentScreen(); $this->registry->getObject('template')-> This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 11 [ 237 ] buildFromTemplates('header.tpl.php', 'payment/'.$paymentKey.'.tpl.php','footer.tpl.php'); $tags = $this->paymentMethod->getTags(); $this->registry->getObject('template')->dataToTags( $tags, ''); } When it comes to processing the payment, we simply call a payment processing method for our paymentMethod object. $this->paymentMethod->processPayment(); Taking payment online There are a number of different ways in which we can take payment online, using a number of different gateways, which we will discuss later in this chapter. However, for our Juniper Theatricals store, the owner wants to take payment through PayPal, because it is quick and easy to sign up to, you don't need a PayPal account to send payment, and it is well known. So let's look at integrating PayPal functionality into the store. PayPal PayPal is one of the most well-known payment gateways available, primarily through use with its parent company eBay. One of the main benets of PayPal is the low cost barrier of entry to use it, and its simple standard payment system for website integration. The payment button PayPal's standard payment options rely on a payment button. We generate a form and a button on our site, which store details such as the order number, seller's PayPal details, and other information as hidden elds. When the customer clicks on the button, the data is posted to PayPal. Our PayPal payment method object sets many of the variables required for this button to work. private function makePaymentScreen() { $this->registry->getObject('template')->getPage()-> addTag('payment.email', $this->registry-> getSetting('payment.paypal.email') ); $this->registry->getObject('template')->getPage()-> addTag('payment.currency', $this->registry-> getSetting('payment.currency') ); This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 11 [ 237 ] buildFromTemplates('header.tpl .php& apos;, 'payment/'.$paymentKey.'.tpl .php& apos;,'footer.tpl .php& apos;); . tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Taking. material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Taking Payment for Orders [ 236 ] The best way to do this would