Guest User

Rewritten code based on order class instead of strange arrays.

a guest
Mar 4th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | Help | 0 0
  1. <?php
  2. // we assume the order class and all the other classes are stored
  3. // in a file that we can then include where we need it
  4. require_once "./orderclass.php";
  5.  
  6. // tax you pay or not, should be per order not per item
  7. $tax = $_POST['with_gst'] === "1" ? new tax("GST", 5) : new tax();
  8. $order = new order($tax);  
  9.  
  10. if (isset($_POST["submit"])){
  11.     $order->store_id = "store3";
  12.     $order->api_token = "yesguy";
  13.     $order->checkout_id = "chktGLFZQtore3";
  14.     $order->environment = "qa";
  15.     $order->action = "preload";
  16.    
  17.     foreach($_POST['quantity'] as $key => $quantity) {
  18.         if($quantity === 0) {
  19.             continue; // we will get the next product
  20.         }
  21.        
  22.         // All products cost 60
  23.         $item = new item($_POST['id'][$key], $_POST['description'][$key], 60, $_POST['quantity'][$key]);
  24.         $order-AddItem($item);
  25.     }
  26.  
  27.     // Add contact details
  28.     $order->contact_details->first_name = $_POST['bill_first_name'];
  29.     $order->contact_details->last_name = $_POST['bill_last_name'];
  30.     $order->contact_details->email = $_POST['email'];
  31.     $order->contact_details->phone = $_POST['bill_phone'];
  32.    
  33.     // Add billing details
  34.     $address = new address();
  35.     $address->address_1 = $_POST['bill_address_one'];
  36.     $address->address_2 = $_POST['bill_address_two'];
  37.     $address->city = $_POST['bill_city'];
  38.     $address->province = $_POST['bill_state_or_province'];
  39.     $address->country = "CA"; // only ship to Canada
  40.     $address->postal_code = $_POST['bill_postal_code'];
  41.    
  42.     $order->billing_details = $address;
  43.    
  44.     if($_POST['ship_to_billing'] === "1")
  45.     {
  46.         $order->shipping_details = $address;
  47.     } else {
  48.         $order->shipping_details->address_1 = $_POST['ship_address_one'];
  49.         $order->shipping_details->address_2 = $_POST['ship_address_two'];
  50.         $order->shipping_details->city = $_POST['ship_city'];
  51.         $order->shipping_details->province = $_POST['ship_state_or_province'];
  52.         $order->shipping_details->country = "CA";
  53.         $order->shipping_details->postal_code = $_POST['ship_postal_code'];
  54.     }
  55. }
  56.  
  57. $myObj = $arr;
  58. $myJSON =  json_encode($order);
  59.  
  60. echo $myJSON;
  61.  
  62. // Prepare new cURL resource
  63. $ch = curl_init();
  64. curl_setopt($ch, CURLOPT_URL, "https://gatewayt.moneris.com/chkt/request/request.php");
  65. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  66. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  67. curl_setopt($ch, CURLOPT_POST, true);
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $myJSON);
  69.  
  70. $array_length = count($arr, COUNT_RECURSIVE);
  71.  
  72. // Set HTTP Header for POST request
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  74.     'Content-Type: application/json' ,'Content-Length: ' . strlen($myJSON)
  75.  )
  76. );
  77.  
  78. // Submit the POST request
  79. $result = curl_exec($ch);
  80.  
  81. // Close cURL session handle
  82. curl_close($ch);
  83. print_r($arr);
  84. print_r($result);
  85. $response_data = json_decode($result);
  86. print_r($response_data);
  87.  
  88.  
  89. $result_data = json_decode($result, true);
  90. print_r($result_data);
  91.  
  92. if( isset($result_data['response']['error']) &&  ( ! (bool) $result_data['response']['success'] ) ){
  93.     echo 'error message';
  94. }
  95. $token = $result_data['response']['ticket'];
  96. ?>
Tags: JSON php
Advertisement
Add Comment
Please, Sign In to add comment