Skip to content

Commit

Permalink
Checkout process is starting to shape up.
Browse files Browse the repository at this point in the history
  • Loading branch information
myobie committed Mar 10, 2010
1 parent 327b4cf commit b72b0f6
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 204 deletions.
11 changes: 9 additions & 2 deletions app/generic/model.php
Expand Up @@ -167,7 +167,7 @@ public function save()
$this->original_values = $this->values;
}

$this->after_save();
$this->after_save($success);

return !!$success;
}
Expand All @@ -183,6 +183,8 @@ public function destroy()
global $db;
$klass = get_called_class();

$this->before_destroy();

if ($this->persisted)
{
$success = $db->delete_row($klass::$table_name, $this->id());
Expand All @@ -198,6 +200,8 @@ public function destroy()
$model->original_values = $model->values;
}

$this->after_destroy($success);

return !!$success;
}

Expand Down Expand Up @@ -301,7 +305,10 @@ public function belongs_to($model)
// --- Callbacks ---

function before_save() {}
function after_save() {}
function after_save($success) {}

function before_destroy() {}
function after_destroy($success) {}

}

Expand Down
11 changes: 11 additions & 0 deletions app/models/cart.php
Expand Up @@ -92,6 +92,17 @@ function add($variant)
return $cart_item;
}

function after_destroy($success)
{
// clean up the cart items

$cart_items = $this->cart_items();

foreach ($cart_items as $cart_item) {
$cart_item->destroy();
}
}

}

?>
94 changes: 76 additions & 18 deletions app/models/order.php
Expand Up @@ -13,26 +13,36 @@ class Order extends GenericModel
"total" => "int",
"shipping_total" => "int",
"tax_total" => "int",
"email" => "string",
"phone" => "string",
"name" => "string",
"address" => "string",
"city" => "string",
"state" => "string",
"postal_code" => "string",
"country" => "string",
"phone" => "string",
"billing_name" => "string",
"billing_address" => "string",
"billing_city" => "string",
"billing_state" => "string",
"billing_postal_code" => "string",
"billing_country" => "string",
"shipped_at" => "datetime",
"shipping_tracking_number" => "string",
"transaction_key" => "string"
"transaction_key" => "string",
"status" => "string",
"error_message" => "text"
);
public static $virtual_fields = array(
"first_name" => null,
"last_name" => null,
"billing_first_name" => null,
"billing_last_name" => null,
"billing_is_same" => false,
"billing_info" => array(),
"card" => array()
);

public $transaction_error = null;
public $errors = array();

function __construct($hash = array())
{
Expand All @@ -49,29 +59,77 @@ function order_items($hash = array())
return $this->has_many("OrderItem", $hash);
}

function before_save()
{
$first_name = $this->g("first_name");
$last_name = $this->g("last_name");
if (!empty($first_name) && !empty($last_name))
$this->update(array("name" => $first_name . " " . $last_name));

$billing_first_name = $this->g("billing_first_name");
$billing_last_name = $this->g("billing_last_name");
if (!empty($billing_first_name) && !empty($billing_last_name))
$this->update(array("billing_name" => $billing_first_name . " " . $billing_last_name));

if ($this->g("billing_is_same"))
{
$this->update(array(
"billing_name" => $this->g("name"),
"billing_address" => $this->g("address"),
"billing_city" => $this->g("city"),
"billing_state" => $this->g("state"),
"billing_postal_code" => $this->g("postal_code"),
"billing_country" => $this->g("country")
));
}
}

function validate()
{
$this->before_save();

$required_fields = array("email", "phone", "name", "address", "city", "state", "postal_code", "country", "billing_name", "billing_address", "billing_city", "billing_state", "billing_postal_code", "billing_country");

$valid = true;

foreach ($required_fields as $field) {
$value = $this->g($field);
if (empty($value))
{
$valid = false;
array_push($this->errors, ucfirst($field) . " is blank");
}
}

return $valid;
}

function process_checkout()
{
// TEMP: just for testing
$this->update(array(
"card" => array(
"number" => "4111 1111 1111 1111",
"security_code" => "123",
"month" => "1",
"year" => "2012"
)
));
if (! $this->validate())
return false;

$transaction = new Transaction($this->g("card"));

$success = $transaction->authorize();
$transaction_success = $transaction->authorize();

if ($success)
if ($transaction_success)
{
$this->update(array("transaction_key" => $transaction->key));
$this->save();
$this->update(array(
"transaction_key" => $transaction->key,
"status" => "authorized"
));
} else {
$this->transaction_error = $transaction->error_message;
$this->update(array(
"status" => "failed",
"error_message" => $transaction->error_message
));
array_push($this->errors, $transaction->error_message);
}

$save_success = $this->save();

return $transaction_success && $save_success;
}

}
Expand Down
2 changes: 1 addition & 1 deletion app/models/photo.php
Expand Up @@ -67,7 +67,7 @@ function before_save()
}
}

function after_save()
function after_save($success)
{
$attributes = $this->attributes();

Expand Down
58 changes: 44 additions & 14 deletions app/models/transaction.php
Expand Up @@ -28,28 +28,29 @@ function __construct($info = array())

public function capture()
{
$this->card_type(); // cache the card type

if ($this->is_valid())
if ($this->validate())
{
// TODO: actually hit the api to capture this transaction
// TODO: also record the transaction error, if there is one

return true;
} else {
return false;
}

return true;
}

public function authorize()
{
$this->card_type(); // cache the card type

if ($this->is_valid())
if ($this->validate())
{
// TODO: actually hit the api to authorize this transaction
// TODO: also record the transaction error, if there is one

$this->key = "1234xyz";
return true;
} else {
return false;
}
return true;
}

public function purchase()
Expand Down Expand Up @@ -81,18 +82,47 @@ public function card_type()
return $this->card_type;
}

public function is_valid()
public function validate()
{
if (!$this->is_valid)
{
$this->is_valid = $this->verify_presence() && $this->verify_date() && $this->verify_luhn();
$this->is_valid = true;

if (! $this->verify_presence()) {
$this->is_valid = false;
$this->error_message = "Not all required credit card fields are filled in.";
return $this->is_valid;
}

if (! $this->verify_known_card_type()) {
$this->is_valid = false;
$this->error_message = "Credit card doesn't appear to be one of the four types we accept or there is a typo.";
return $this->is_valid;
}

if (! $this->verify_date()) {
$this->is_valid = false;
$this->error_message = "Credit card is out of date.";
return $this->is_valid;
}

if (! $this->verify_luhn()) {
$this->is_valid = false;
$this->error_message = "Credit card number appears to have a typo.";
return $this->is_valid;
}
}

return $this->is_valid;
}

private function verify_luhn()
function verify_known_card_type()
{
return $this->card_type() != "other";
}

function verify_luhn()
{
// !!! this is the luhn algorithm
// !!! I copied it from here:
// http://blog.planzero.org/2009/08/luhn-modulus-implementation-php/
Expand Down Expand Up @@ -121,7 +151,7 @@ private function verify_luhn()
return $total % 10 == 0;
}

private function verify_date()
function verify_date()
{
$greater_month = (int)($this->info["month"]) >= (int)date("n");
$greater_year = (int)($this->info["year"]) > (int)date("Y");
Expand All @@ -130,7 +160,7 @@ private function verify_date()
return $greater_year || ($same_year && $greater_month);
}

public function verify_presence()
function verify_presence()
{
return !empty($this->info["number"]) && !empty($this->info["security_code"]) && !empty($this->info["month"]) && !empty($this->info["year"]);
}
Expand Down

0 comments on commit b72b0f6

Please sign in to comment.