CLASS ORDER

CLASS ORDER

Finally, in this class we add something new and interesting. Class Name: Order

Extends Address

Default Properties:

◆ $items

Methods:

◆ Order. Calls the Base() method, assigning each element of the $atts array to Object properties.

◆ FetchOrder. Takes one argument, $order_id. Creates object properties for every row in the user table associated with the $order_id.

◆ LoadOrder. Takes one argument, $order_id. First runs FetchOrder and then creates an array, each element of which is an object containing address information.

◆ SaveOrder. Takes no arguments, assumes a $this->order_id exists. Will both update existing styles and create new ones as needed.

◆ DeleteOrder. Takes no arguments. Removes a user from the database. It will force confirmation if there are related addresses. It will delete the user after confirmation is provided.

◆ CalculateTotals. Takes no arguments. This method calculates total prices for each item of the order and for the order as a whole. Items that are flagged for deletion are removed from the items array property.

Chapter 14: Shopping Cart 383

◆ ValidateCard. Takes no arguments. This method does not conduct a trans- action with a credit-card processing agency. It runs through a function (seen in Appendix G) that makes sure the credit card number supplied is potentially valid. If the credit card is not in a proper format, the method will return false.

◆ ChargeCard. Takes no arguments. Communicates with the credit-card processor.

◆ PrintOrder. Takes no arguments. Prints out the results of an order. The ChargeCard method deserves some added discussion. M ETHOD CHARGECARD( ) This method will make use of the cURL functions

described earlier in this chapter. First we start by calculating the totals. function ChargeCard()

{ $this->CalculateTotals(); $total_charged = $this->total_price + $this->ship_cost; $exp = sprintf(“%02d/%04d”,

$this->cc_exp_mon, $this->cc_exp_yr); The following code prepares a URL that we will use to communicate with autho-

rize.net. For legal reasons we did not include the actual variables you will need to send to authorize.net to get a meaningful response. However, that information is available at the authorize.net site. Following that, we prepare an error message, just in case.

$authorize_net_url = “https://url.to.authorize.net?var1=FALSE&var2=foo”;

$this->error = “connection to authorize.net failed”; Now it is time to make the connection and see if the credit card is verified. The

cURL functions are a little strange at this point. They are fairly new and aren’t quite as polished as some of the other function sets. But by the time you read this, there may be a PEAR (PHP Extension and Application Repository) class that makes dealing with cURL easier. Make sure to check in at the php.net site for the latest updates to the cURL functions.

global $ch; $ch = curl_init($authorize_net_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); curl_close($ch);

384 Part IV: Not So Simple Applications

authorize.net returns a string of comma-separated values. The script will compare the results from the returned string against their known meanings (supplied by authorize.net). In the code that follows, the first element in the returned string is tested. If it has a value of 1, this method will return TRUE, meaning the transaction was successful.

$this->rvars = explode(“,”,$buffer); $this->auth_result = $this->rvars[0]; $this->error = $this->rvars[3]; if ($this->auth_result != 1) { return FALSE; } return TRUE;