1. Go to this page and download the library: Download openbuildings/purchases library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
openbuildings / purchases example snippets
class Model_Product extends Jam_Model implements Sellable {
public static function initialize(Jam_Meta $meta)
{
$meta
->fields(array(
'id' => Jam::field('primary'),
'name' => Jam::field('string'),
'currency' => Jam::field('string'),
'price' => Jam::field('price'),
));
}
public function price_for_purchase_item(Model_Purchase_Item $item)
{
return $this->price;
}
public function currency()
{
return $this->currency;
}
}
class Model_User extends Kohana_Model_User {
public static function initialize(Jam_Meta $meta)
{
$meta
->behaviors(array(
'buyer' => Jam::association('buyer'),
));
// ...
}
}
$brand_purchase->items(); // return all the purchase items as an array
$brand_purchase->items('product'); // return all the purchase items with model "purchase_item_product" as an array
$brand_purchase->items(array('product', 'shipping')); // return all the purchase items with model "purchase_item_product" or "purchase_item_shipping" as an array
$brand_purchase->items(array('is_payable' => TRUE)); // return all the purchase items with flag "is_payable" set to TRUE as an array
$brand_purchase->items(array('is_payable' => TRUE, 'product')); // return all the purchase items with flag "is_payable" set to TRUE and are with model "purchase_item_product" as an array
$brand_purchase->items(array('not' => 'shipping')); // return all the purchase items that are not instance of model "purchase_item_shipping"
// This will return the quantity of all the payable items in all the brand_purchases of this purchase.
$purchase->items_quantity(array('is_payable' => TRUE));
$brand_purchase->total_price_ratio(array('is_payable' => TRUE)); // Will return e.g. 0.6
$purchase
->freeze()
->save();
$purchase->unfreeze();
$purchase->isFrozen();
class Model_Purchase extends Jam_Model {
use Clippings\Freezable\FreezableCollectionTrait {
performFreeze as freezeCollection;
performUnfreeze as unfreezeCollection;
};
public static function initialize(Jam_Meta $meta)
{
$meta
->associations(array(
'brand_purchases' => Jam::association('has_many'),
))
->fields(array(
'is_frozen' => Jam::field('boolean'),
'price' => Jam::field('serializable'),
));
}
public function price()
{
return $this->isFrozen() ? $this->price : $this->computePrice();
}
public function isFrozen()
{
return $this->is_frozen;
}
public function setFrozen($frozen)
{
$this->is_frozen = (bool) $frozen;
return $this;
}
public function performFreeze()
{
$this->freezeCollection();
$this->price = $this->price();
}
public function performUnfreeze()
{
$this->unfreezeCollection();
$this->price = NULL;
}
public function getItems()
{
return $this->books;
}
//...
}
class Controller_Payment extends Controller_Template {
public function action_index()
{
$purchase = // Load purchase from somewhere
$form = Jam::build('emp_form', array($this->post()));
if ($this->request->method() === Request::POST AND $form->check())
{
$purchase
->build('payment', array('model' => 'payment_emp'))
->execute($form->as_array());
$this->redirect('payment/complete');
}
$this->template->content = View::factory('payment/index', array('form' => Jam::form($form)))
}
}
class Controller_Payment extends Controller_Template {
public function action_index()
{
$purchase = // Load purchase from somewhere
$form = Jam::build('emp_form', array($this->post()));
if ($this->request->method() === Request::POST AND $form->check())
{
$purchase
->build('payment', array('model' => 'payment_paypal_vbv'))
->authorize($form->vbv_params('/payment/complete'));
// We need to save the form somewhere as it is later used for execute method
$this->session->set('emp_form', $form->as_array());
$this->redirect($purchase->payment->authorize_url());
}
$this->template->content = View::factory('payment/index', array('form' => Jam::form($form)));
}
public function action_complete()
{
$purchase = // Load purchase from somewhere
if ( ! $purchase->is_paid())
{
$form = Jam::build('emp_form', array($this->session->get_once('emp_form')));
$purchase
->payment
->execute($form->as_array());
}
$this->template->content = View::factory('payment/complete', array('purchase' => $purchase));
}
}