Cart work for payments and Paypal work to test

This commit is contained in:
Deon George
2013-01-15 17:07:54 +11:00
parent 133ae4d5c6
commit 69645c4eea
42 changed files with 968 additions and 801 deletions

View File

@@ -11,137 +11,85 @@
* @license http://dev.osbill.net/license.html
*/
class Controller_Checkout extends Controller_TemplateDefault {
protected $auth_required = TRUE;
protected $noauth_redirect = 'login/register';
protected $auth_required = FALSE;
protected $secure_actions = array(
'before'=>TRUE,
'after'=>TRUE,
'cancel'=>TRUE,
);
/**
* This is the main call to export, providing a list of items to export and
* setting up the page to call the export plugin when submitted.
*/
public function action_index() {
if ($_POST)
return $this->checkout();
HTTP::redirect('cart');
}
// @todo - this should be a global config item
$mediapath = Route::get('default/media');
public function action_before() {
// If we are not here by a POST operation, we'll redirect to the cart.
if (! $cid=Request::current()->post('checkout_id'))
HTTP::redirect('cart');
// @todo Items in the cart dont have account_id if they were put in the cart when the user was not logged in
$co = ORM::factory('Checkout',$cid);
// If the cart is empty, we'll return here.
if (! Cart::instance()->contents()->count_all())
Block::add(array(
'title'=>_('Empty Cart'),
'body'=>_('The cart is empty')
));
else {
Style::add(array(
'type'=>'file',
'data'=>'css/checkout_cartlist.css',
));
// Show a list of items in the cart
$output = '<table class="checkout_cartlist" border="0">';
foreach (Cart::instance()->contents()->find_all() as $item) {
$ppa = $item->product->get_price_array();
$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
$output .= View::factory('cart/checkout_list')
->set('price_firstinvoice',$item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata'])
->set('price_setup',$item->quantity*$ppa[$item->recurr_schedule]['price_setup'])
->set('service_start',$pdata['date'])
->set('service_end',$pdata['end'])
->set('price_recurring',$item->quantity*$ppa[$item->recurr_schedule]['price_base'])
->set('item',$item)
->set('mediapath',$mediapath);
}
$output .= '</table>';
Block::add(array(
'title'=>_('Your Items'),
'body'=>$output,
));
$po = ORM::factory('Checkout')
->payment_options_cart();
// @todo Country value should come from somewhere?
Block::add(array(
'title'=>_('Order Total'),
'body'=>View::factory('cart/checkout_total')
->set('cart',Cart::instance())
->set('country',61),
));
$output = Form::open();
$output .= '<table class="payment_options_box" border="0">';
foreach ($po as $payment) {
$output .= View::factory('checkout/payment_option')
->set('payment',$payment);
}
// @todo Add Javascript to stop submission if something not selected
$output .= '<tr><td>&nbsp;</td></tr>';
$output .= '<tr>';
$output .= sprintf('<td>%s</td>',Form::submit('submit',_('Submit Order')));
$output .= '</tr>';
$output .= '</table>';
$output .= Form::close();
Block::add(array(
'title'=>_('Available Payment Methods'),
'body'=>$output,
));
}
Block::add(array(
'title'=>'Checkout',
'body'=>$co->plugin()->before(Cart::instance()),
));
// Suppress our right hand tab
$this->template->right = ' ';
}
/**
* Process checkout
*/
private function checkout() {
$invoice = ORM::factory('Invoice');
public function action_after() {
$co = ORM::factory('Checkout',$this->request->param('id'));
// Add our individual items to the invoice
foreach (Cart::instance()->contents()->find_all() as $item) {
$invoice_item = $invoice->add_item();
if (! $co->loaded())
HTTP::redirect('/');
$invoice_item->product_id = $item->product_id;
$invoice_item->product_attr = $item->product_attr;
$invoice_item->product_attr_cart = $item->product_attr;
$invoice_item->quantity = $item->quantity;
$invoice_item->recurring_schedule = $item->recurr_schedule;
return method_exists($co->plugin(),'after') ? $co->plugin()->after(Cart::instance()) : HTTP::redirect('/');
}
$ppa = $item->product->get_price_array();
$period = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
// @todo rounding should be a global config
$invoice_item->price_base = round($item->quantity*$ppa[$item->recurr_schedule]['price_base']*$period['prorata'],2);
$invoice_item->price_setup = round($item->quantity*$ppa[$item->recurr_schedule]['price_setup'],2);
public function action_cancel() {
$co = ORM::factory('Checkout',$this->request->param('id'));
if (! $co->loaded())
HTTP::redirect('cart');
return method_exists($co->plugin(),'cancel') ? $co->plugin()->cancel(Cart::instance()) : HTTP::redirect('cart');
}
public function action_notify() {
$test_id = FALSE;
$co = ORM::factory('Checkout',$this->request->param('id'));
if ((! $co->loaded() OR ! Request::current()->post()) AND ! $test_id=Kohana::$config->load('debug')->checkout_notify)
throw HTTP_Exception::factory(404,'Payment not found!');
$this->auto_render = FALSE;
$cno = ORM::factory('Checkout_Notify');
if (! $test_id) {
$cno->checkout_id = $co->id;
$cno->status = 1;
$cno->data = Request::current()->post();
$cno->save();
} else {
$cno->where('id','=',$test_id)->find();
}
$invoice->account_id = Auth::instance()->get_user()->id;
$invoice->type = 2; // INVOICED VIA CHECKOUT
$invoice->status = 1; // INVOICE IS NOT CANCELLED
$invoice->due_date = time(); // DATE INVOICE MUST BE PAID
$invoice->billed_currency_id = 6; // @todo This should come from the site config or the currency selected
/*
$invoice->process_status = NULL; // TO BE PROCESSED
$invoice->billing_status = NULL; // UNPAID
$invoice->refund_status = NULL; // NOT REFUNDED
$invoice->print_status = NULL; // NOT YET PRINTED
$invoice->discount_amt = NULL; // @todo CALCULATE DISCOUNTS
$invoice->checkout_plugin_id = NULL; // @todo Update the selected checkout plugin
$invoice->checkout_plugin_data = NULL; // @todo Data required for the checkout plugin
*/
if (! $cno->loaded())
throw HTTP_Exception::factory(500,'Unable to save!');
if ($invoice->check())
$invoice->save();
else
throw new Kohana_Exception('Problem saving invoice - Failed check()');
// Process our Notify
try {
$this->response->body($cno->process());
} catch (Exception $e) {
$this->response->body('Received, thank you!');
}
$this->response->headers('Content-Type','text/plain');
$this->response->headers('Content-Length',(string)$this->response->content_length());
$this->response->headers('Last-Modified',time());
}
}
?>