Guest User

Expanded exmple of the order/cart

a guest
Mar 4th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | Help | 0 0
  1. <?php
  2.  
  3. class token {
  4.         function __construct($data_key = null, $issuer_id = null) {
  5.                 $this->data_key = $data_key;
  6.                 $this->issuer_id = $issuer_id;
  7.         }
  8.         public $data_key;
  9.         public $issuer_id;
  10. }
  11.  
  12. class recur {
  13.         public $bill_now;
  14.         public $recur_amount;
  15.         public $start_date;
  16.         public $recur_unit;
  17.         public $recur_period;
  18.         public $number_of_recurs;
  19. }
  20.  
  21. class item {
  22.         function __construct($produc_code = null, $description = null, $unit_cost = null, $quantity = null) {
  23.                 $this->product_code = $produc_code;
  24.                 $this->description = $description;
  25.                 $this->unit_cost = $unit_cost;
  26.                 $this->quantity = $quantity;
  27.                 // We assume that items have the same page url, just the product code will tell which
  28.                 // item will be shown. if you need to change this, you can always do it with
  29.                 // $item->url = "https://custom.url.exmple.com";
  30.                 $this->url = "https://example.net/item?id=${produc_code}";
  31.         }
  32.  
  33.         public $url;
  34.         public $description;
  35.         public $product_code;
  36.         public $unit_cost;
  37.         public $quantity;
  38. }
  39.  
  40. class tax {
  41.         function __construct($description = null, $rate = null, $amount = null) {
  42.                 $this->description = $description;
  43.                 $this->rate = $rate;
  44.                 $this->amount = $amount;
  45.         }
  46.  
  47.         public $amount;
  48.         public $description;
  49.         public $rate;
  50. }
  51.  
  52. class cart {
  53.         public $items = array(); // for item objects
  54.         public $subtotal;
  55.         public $tax;
  56. }
  57.  
  58. class contactdetails {
  59.         public $first_name;
  60.         public $last_name;
  61.         public $email;
  62.         public $phone;
  63. }
  64.  
  65. class address {
  66.         public $address_1;
  67.         public $address_2;
  68.         public $city;
  69.         public $province;
  70.         public $country;
  71.         public $postal_code;
  72. }
  73.  
  74. class order {
  75.         function __construct(tax $tax) {
  76.                 // just to simply init things for you
  77.                 $this->recur = new recur();
  78.                 $this->cart = new cart();
  79.                 $this->cart->tax = $tax; // assign the tax to cart
  80.                 if(is_numeric($tax->rate)) {
  81.                         // update _tax_rate if we have a numeric value
  82.                         $this->_tax_rate = floatval($tax->rate) / 100;
  83.                 }
  84.                 $this->contact_details = new contactdetails();
  85.                 $this->shipping_details = new address();
  86.                 $this->billing_details = new address();
  87.         }
  88.  
  89.         // varaibles used to store internal data not part of json
  90.         private float $_tax_rate = 0.0;
  91.         private float $_subtotal = 0.0;
  92.         private float $_taxtotal = 0.0;
  93.  
  94.         // public values part of the json
  95.         public $store_id;
  96.         public $api_token;
  97.         public $checkout_id;
  98.         public $txn_total;
  99.         public $environment;
  100.         public $action;
  101.         public $token = array(); // for token objects
  102.         public $ask_cvv;
  103.         public $order_no;
  104.         public $cust_id;
  105.         public $dynamic_descriptor;
  106.         public $language;
  107.         public $recur;
  108.         public $cart;
  109.         public $contact_details;
  110.         public $shipping_details;
  111.         public $billing_details;
  112.  
  113.  
  114.         // function to add items to the orders cart and update tax amount, cart subtotal and order txn_total
  115.         // we have a type verifiction that we just have items.
  116.         function AddItem(item $item) {
  117.                 $this->cart->items[] = $item;
  118.                 $total = intval($item->quantity) * floatval($item->unit_cost);
  119.                 $this->_subtotal += $total;
  120.                 $this->_taxtotal += $total * $this->_tax_rate;
  121.  
  122.                 // store the values to strings, we don't do any rounding
  123.                 $this->cart->subtotal = number_format($this->_subtotal, 2);
  124.                 $this->cart->tax->amount = number_format($this->_taxtotal, 2);
  125.                 $this->txn_total = number_format(floatval($this->_subtotal) + floatval($this->_taxtotal), 2);
  126.         }
  127.  
  128.         // function to add tokens to order token, we have type verification and we ensure we don't of
  129.         // mistake replace already added tokens.
  130.         function AddToken(token $token) {
  131.                 $this->token[] = $token;
  132.         }
  133.  
  134. }
  135.  
  136. // first we need to decide the tax
  137. $tax = new tax("Tax", "13.00");
  138.  
  139. // we create a new empty order with the tax we had decided for
  140. $order = new order($tax);
  141.  
  142. // we add two tokens
  143. $order->AddToken(new token("1234", "645sddfvdrt4tefd"));
  144. $order->AddToken(new token("5678", "645sddfvdrt4tefd"));
  145.  
  146. // we add two items, we do not update other values in this example
  147. $order->AddItem(new item("1", "item 1", "100", "1"));
  148. $order->AddItem(new item("2", "item 2", "200", "1"));
  149.  
  150. // it's possible to store item as a variable and then add to the
  151. // order:
  152. $item = new item("3", "item 3", "50", "2");
  153. $item->url = "https://custom.url.exmple.net/olditems/33";
  154. $order->AddItem($item);
  155.  
  156. echo json_encode($order, JSON_PRETTY_PRINT);
  157. echo "\n";
  158.  
  159. ?>
  160.  
Tags: JSON php
Advertisement
Add Comment
Please, Sign In to add comment