Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class token {
- function __construct($data_key = null, $issuer_id = null) {
- $this->data_key = $data_key;
- $this->issuer_id = $issuer_id;
- }
- public $data_key;
- public $issuer_id;
- }
- class recur {
- public $bill_now;
- public $recur_amount;
- public $start_date;
- public $recur_unit;
- public $recur_period;
- public $number_of_recurs;
- }
- class item {
- function __construct($produc_code = null, $description = null, $unit_cost = null, $quantity = null) {
- $this->product_code = $produc_code;
- $this->description = $description;
- $this->unit_cost = $unit_cost;
- $this->quantity = $quantity;
- // We assume that items have the same page url, just the product code will tell which
- // item will be shown. if you need to change this, you can always do it with
- // $item->url = "https://custom.url.exmple.com";
- $this->url = "https://example.net/item?id=${produc_code}";
- }
- public $url;
- public $description;
- public $product_code;
- public $unit_cost;
- public $quantity;
- }
- class tax {
- function __construct($description = null, $rate = null, $amount = null) {
- $this->description = $description;
- $this->rate = $rate;
- $this->amount = $amount;
- }
- public $amount;
- public $description;
- public $rate;
- }
- class cart {
- public $items = array(); // for item objects
- public $subtotal;
- public $tax;
- }
- class contactdetails {
- public $first_name;
- public $last_name;
- public $email;
- public $phone;
- }
- class address {
- public $address_1;
- public $address_2;
- public $city;
- public $province;
- public $country;
- public $postal_code;
- }
- class order {
- function __construct(tax $tax) {
- // just to simply init things for you
- $this->recur = new recur();
- $this->cart = new cart();
- $this->cart->tax = $tax; // assign the tax to cart
- if(is_numeric($tax->rate)) {
- // update _tax_rate if we have a numeric value
- $this->_tax_rate = floatval($tax->rate) / 100;
- }
- $this->contact_details = new contactdetails();
- $this->shipping_details = new address();
- $this->billing_details = new address();
- }
- // varaibles used to store internal data not part of json
- private float $_tax_rate = 0.0;
- private float $_subtotal = 0.0;
- private float $_taxtotal = 0.0;
- // public values part of the json
- public $store_id;
- public $api_token;
- public $checkout_id;
- public $txn_total;
- public $environment;
- public $action;
- public $token = array(); // for token objects
- public $ask_cvv;
- public $order_no;
- public $cust_id;
- public $dynamic_descriptor;
- public $language;
- public $recur;
- public $cart;
- public $contact_details;
- public $shipping_details;
- public $billing_details;
- // function to add items to the orders cart and update tax amount, cart subtotal and order txn_total
- // we have a type verifiction that we just have items.
- function AddItem(item $item) {
- $this->cart->items[] = $item;
- $total = intval($item->quantity) * floatval($item->unit_cost);
- $this->_subtotal += $total;
- $this->_taxtotal += $total * $this->_tax_rate;
- // store the values to strings, we don't do any rounding
- $this->cart->subtotal = number_format($this->_subtotal, 2);
- $this->cart->tax->amount = number_format($this->_taxtotal, 2);
- $this->txn_total = number_format(floatval($this->_subtotal) + floatval($this->_taxtotal), 2);
- }
- // function to add tokens to order token, we have type verification and we ensure we don't of
- // mistake replace already added tokens.
- function AddToken(token $token) {
- $this->token[] = $token;
- }
- }
- // first we need to decide the tax
- $tax = new tax("Tax", "13.00");
- // we create a new empty order with the tax we had decided for
- $order = new order($tax);
- // we add two tokens
- $order->AddToken(new token("1234", "645sddfvdrt4tefd"));
- $order->AddToken(new token("5678", "645sddfvdrt4tefd"));
- // we add two items, we do not update other values in this example
- $order->AddItem(new item("1", "item 1", "100", "1"));
- $order->AddItem(new item("2", "item 2", "200", "1"));
- // it's possible to store item as a variable and then add to the
- // order:
- $item = new item("3", "item 3", "50", "2");
- $item->url = "https://custom.url.exmple.net/olditems/33";
- $order->AddItem($item);
- echo json_encode($order, JSON_PRETTY_PRINT);
- echo "\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment