Guest User

A way to get a valid json for the order api call

a guest
Mar 4th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | Source Code | 0 0
  1. <?php
  2.  
  3. class token {
  4.         public $data_key;
  5.         public $issuer_id;
  6. }
  7.  
  8. class recur {
  9.         public $bill_now;
  10.         public $recur_amount;
  11.         public $start_date;
  12.         public $recur_unit;
  13.         public $recur_period;
  14.         public $number_of_recurs;
  15. }
  16.  
  17. class item {
  18.         public $url;
  19.         public $description;
  20.         public $product_code;
  21.         public $unit_cost;
  22.         public $quantity;
  23. }
  24.  
  25. class tax {
  26.         public $amount;
  27.         public $description;
  28.         public $rate;
  29. }
  30.  
  31. class cart {
  32.         function __construct() {
  33.                 $this->tax = new tax();
  34.         }
  35.  
  36.         public $items = array(); // for item objects
  37.         public $subtotal;
  38.         public $tax;
  39. }
  40.  
  41. class contactdetails {
  42.         public $first_name;
  43.         public $last_name;
  44.         public $email;
  45.         public $phone;
  46. }
  47.  
  48. class address {
  49.         public $address_1;
  50.         public $address_2;
  51.         public $city;
  52.         public $province;
  53.         public $country;
  54.         public $postal_code;
  55. }
  56.  
  57. class order {
  58.         function __construct() {
  59.                 // just to simply init things for you
  60.                 $this->recur = new recur();
  61.                 $this->cart = new cart();
  62.                 $this->contact_details = new contactdetails();
  63.                 $this->shipping_details = new address();
  64.                 $this->billing_details = new address();
  65.         }
  66.  
  67.         public $store_id;
  68.         public $api_token;
  69.         public $checkout_id;
  70.         public $txn_total;
  71.         public $environment;
  72.         public $action;
  73.         public $token = array(); // for token objects
  74.         public $ask_cvv;
  75.         public $order_no;
  76.         public $cust_id;
  77.         public $dynamic_descriptor;
  78.         public $language;
  79.         public $recur;
  80.         public $cart;
  81.         public $contact_details;
  82.         public $shipping_details;
  83.         public $billing_details;
  84. }
  85.  
  86. $order = new order();
  87.  
  88. // To add new items do, we assume $itemid is posted together with $quantity
  89. // $item = new item();
  90. // $item->url = "https://domain.example.net/item/$itemid";
  91. // $item->description = fetch_item_description_from_db($itemid);
  92. // $item->product_code = $itemid;
  93. // $item->unit_cost = fetch_item_unit_cost_from_db($itmeid);
  94. // $item->quantity = $quantity;
  95. // don't forget to calculate the tax and add it to the $order->cart->tax->amount
  96. // update also $order->txn_total and also $order->cart->subtotal
  97. // $order->cart->items[] = $item;
  98.  
  99. // we add two tokens
  100. $token1 = new token();
  101. $token1->data_key = "1234";
  102. $token1->issuer_id = "645sddfvdrt4tefd";
  103. $order->token[] = $token1;
  104. $token2 = new token();
  105. $token2->data_key = "5678";
  106. $token2->issuer_id = "645sddfvdrt4tefd";
  107. $order->token[] = $token2;
  108.  
  109. // we add two items, we do not update other values in this example
  110. $item1 = new item();
  111. $item1->product_code = "1";
  112. $item1->url = "https://example.net/item?id=1";
  113. $item1->description = "item 1";
  114. $item1->unit_cost = "100";
  115. $item1->quantity = "1";
  116. $order->cart->items[] = $item1;
  117. $item2 = new item();
  118. $item2->product_code = "2";
  119. $item2->url = "https://example.net/item?id=2";
  120. $item2->description = "item 2";
  121. $item2->unit_cost = "200";
  122. $item2->quantity = "1";
  123. $order->cart->items[] = $item2;
  124.  
  125. echo json_encode($order, JSON_PRETTY_PRINT);
  126. echo "\n";
  127.  
  128. ?>
  129.  
Tags: JSON
Advertisement
Add Comment
Please, Sign In to add comment