/classes/gateways/mijireh/includes/Order.php

https://github.com/markjaquith/woocommerce · PHP · 343 lines · 269 code · 32 blank · 42 comment · 45 complexity · 600d78808c266d70386227e375209d95 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. class Mijireh_Order extends Mijireh_Model {
  4. private function _init() {
  5. $this->_data = array(
  6. 'partner_id' => null,
  7. 'order_number' => null,
  8. 'mode' => null,
  9. 'status' => null,
  10. 'order_date' => null,
  11. 'ip_address' => null,
  12. 'checkout_url' => null,
  13. 'total' => '',
  14. 'return_url' => '',
  15. 'items' => array(),
  16. 'email' => '',
  17. 'first_name' => '',
  18. 'last_name' => '',
  19. 'meta_data' => array(),
  20. 'tax' => '',
  21. 'shipping' => '',
  22. 'discount' => '',
  23. 'shipping_address' => array(),
  24. 'billing_address' => array()
  25. );
  26. }
  27. public function __construct($order_number=null) {
  28. $this->_init();
  29. if(isset($order_number)) {
  30. $this->load($order_number);
  31. }
  32. }
  33. public function load($order_number) {
  34. if(strlen(Mijireh::$access_key) < 5) {
  35. throw new Mijireh_Exception('missing mijireh access key');
  36. }
  37. $rest = new Mijireh_RestJSON(Mijireh::$url);
  38. $rest->setupAuth(Mijireh::$access_key, '');
  39. try {
  40. $order_data = $rest->get("orders/$order_number");
  41. $this->copy_from($order_data);
  42. return $this;
  43. }
  44. catch(Mijireh_Rest_BadRequest $e) {
  45. throw new Mijireh_BadRequest($e->getMessage());
  46. }
  47. catch(Mijireh_Rest_Unauthorized $e) {
  48. throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key");
  49. }
  50. catch(Mijireh_Rest_NotFound $e) {
  51. throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']);
  52. }
  53. catch(Mijireh_Rest_ClientError $e) {
  54. throw new Mijireh_ClientError($e->getMessage());
  55. }
  56. catch(Mijireh_Rest_ServerError $e) {
  57. throw new Mijireh_ServerError($e->getMessage());
  58. }
  59. }
  60. public function copy_from($order_data) {
  61. foreach($order_data as $key => $value) {
  62. if($key == 'items') {
  63. if(is_array($value)) {
  64. $this->clear_items(); // Clear current items before adding new items.
  65. foreach($value as $item_array) {
  66. $item = new Mijireh_Item();
  67. $item->copy_from($item_array);
  68. $this->add_item($item);
  69. }
  70. }
  71. }
  72. elseif($key == 'shipping_address') {
  73. if(is_array($value)) {
  74. $address = new Mijireh_Address();
  75. $address->copy_from($value);
  76. $this->set_shipping_address($address);
  77. }
  78. }
  79. elseif($key == 'billing_address') {
  80. if(is_array($value)) {
  81. $address = new Mijireh_Address();
  82. $address->copy_from($value);
  83. $this->set_billing_address($address);
  84. }
  85. }
  86. elseif($key == 'meta_data') {
  87. if(is_array($value)) {
  88. $this->clear_meta_data(); // Clear current meta data before adding new meta data
  89. $this->_data['meta_data'] = $value;
  90. }
  91. }
  92. else {
  93. $this->$key = $value;
  94. }
  95. }
  96. if(!$this->validate()) {
  97. throw new Mijireh_Exception('invalid order hydration: ' . $this->get_errors_lines());
  98. }
  99. return $this;
  100. }
  101. public function create() {
  102. if(strlen(Mijireh::$access_key) < 5) {
  103. throw new Mijireh_Exception('missing mijireh access key');
  104. }
  105. if(!$this->validate()) {
  106. $error_message = 'unable to create order: ' . $this->get_error_lines();
  107. throw new Mijireh_Exception($error_message);
  108. }
  109. $rest = new Mijireh_RestJSON(Mijireh::$url);
  110. $rest->setupAuth(Mijireh::$access_key, '');
  111. try {
  112. $result = $rest->post('orders', $this->get_data());
  113. $this->copy_from($result);
  114. return $this;
  115. }
  116. catch(Mijireh_Rest_BadRequest $e) {
  117. throw new Mijireh_BadRequest($e->getMessage());
  118. }
  119. catch(Mijireh_Rest_Unauthorized $e) {
  120. throw new Mijireh_Unauthorized("Unauthorized. Please check your api access key");
  121. }
  122. catch(Mijireh_Rest_NotFound $e) {
  123. throw new Mijireh_NotFound("Mijireh resource not found: " . $rest->last_request['url']);
  124. }
  125. catch(Mijireh_Rest_ClientError $e) {
  126. throw new Mijireh_ClientError($e->getMessage());
  127. }
  128. catch(Mijireh_Rest_ServerError $e) {
  129. throw new Mijireh_ServerError($e->getMessage());
  130. }
  131. }
  132. /**
  133. * If meta_data or shipping_address are empty, exclude them altogether.
  134. */
  135. public function get_data() {
  136. $data = parent::get_data();
  137. if(count($data['meta_data']) == 0) { unset($data['meta_data']); }
  138. if(count($data['shipping_address']) == 0) { unset($data['shipping_address']); }
  139. if(count($data['billing_address']) == 0) { unset($data['billing_address']); }
  140. return $data;
  141. }
  142. /**
  143. * Add the specified item and price to the order.
  144. *
  145. * Return the total number of items in the order (including the one that was just added)
  146. *
  147. * @return int
  148. */
  149. public function add_item($name, $price=0, $quantity=1, $sku='') {
  150. $item = '';
  151. if(is_object($name) && get_class($name) == 'Mijireh_Item') {
  152. $item = $name;
  153. }
  154. else {
  155. $item = new Mijireh_Item();
  156. $item->name = $name;
  157. $item->price = $price;
  158. $item->quantity = $quantity;
  159. $item->sku = $sku;
  160. }
  161. if($item->validate()) {
  162. $this->_data['items'][] = $item->get_data();
  163. return $this->item_count();
  164. }
  165. else {
  166. $errors = implode(' ', $item->get_errors());
  167. throw new Mijireh_Exception('unable to add invalid item to order :: ' . $errors);
  168. }
  169. }
  170. public function add_meta_data($key, $value) {
  171. if(!is_array($this->_data['meta_data'])) {
  172. $this->_data['meta_data'] = array();
  173. }
  174. $this->_data['meta_data'][$key] = $value;
  175. }
  176. /**
  177. * Return the value associated with the given key in the order's meta data.
  178. *
  179. * If the key does not exist, return false.
  180. */
  181. public function get_meta_value($key) {
  182. $value = false;
  183. if(isset($this->_data['meta_data'][$key])) {
  184. $value = $this->_data['meta_data'][$key];
  185. }
  186. return $value;
  187. }
  188. public function item_count() {
  189. $item_count = 0;
  190. if(is_array($this->_data['items'])) {
  191. $item_count = count($this->_data['items']);
  192. }
  193. return $item_count;
  194. }
  195. public function get_items() {
  196. $items = array();
  197. foreach($this->_data['items'] as $item_data) {
  198. $item = new Mijireh_Item();
  199. $item->copy_from($item_data);
  200. }
  201. }
  202. public function clear_items() {
  203. $this->_data['items'] = array();
  204. }
  205. public function clear_meta_data() {
  206. $this->_data['meta_data'] = array();
  207. }
  208. public function validate() {
  209. $this->_check_total();
  210. $this->_check_return_url();
  211. $this->_check_items();
  212. return count($this->_errors) == 0;
  213. }
  214. /**
  215. * Alias for set_shipping_address()
  216. */
  217. public function set_address(Mijireh_Address $address){
  218. $this->set_shipping_address($address);
  219. }
  220. public function set_shipping_address(Mijireh_Address $address) {
  221. if($address->validate()) {
  222. $this->_data['shipping_address'] = $address->get_data();
  223. }
  224. else {
  225. throw new Mijireh_Exception('invalid shipping address');
  226. }
  227. }
  228. public function set_billing_address(Mijireh_Address $address) {
  229. if($address->validate()) {
  230. $this->_data['billing_address'] = $address->get_data();
  231. }
  232. else {
  233. throw new Mijireh_Exception('invalid shipping address');
  234. }
  235. }
  236. /**
  237. * Alias for get_shipping_address()
  238. */
  239. public function get_address() {
  240. return $this->get_shipping_address();
  241. }
  242. public function get_shipping_address() {
  243. $address = false;
  244. if(is_array($this->_data['shipping_address'])) {
  245. $address = new Mijireh_Address();
  246. $address->copy_from($this->_data['shipping_address']);
  247. }
  248. return $address;
  249. }
  250. public function get_billing_address() {
  251. $address = false;
  252. if(is_array($this->_data['billing_address'])) {
  253. $address = new Mijireh_Address();
  254. $address->copy_from($this->_data['billing_address']);
  255. }
  256. return $address;
  257. }
  258. /**
  259. * The order total must be greater than zero.
  260. *
  261. * Return true if valid, otherwise false.
  262. *
  263. * @return boolean
  264. */
  265. private function _check_total() {
  266. $is_valid = true;
  267. if($this->_data['total'] <= 0) {
  268. $this->add_error('order total must be greater than zero');
  269. $is_valid = false;
  270. }
  271. return $is_valid;
  272. }
  273. /**
  274. * The return url must be provided and must start with http.
  275. *
  276. * Return true if valid, otherwise false
  277. *
  278. * @return boolean
  279. */
  280. private function _check_return_url() {
  281. $is_valid = false;
  282. if(!empty($this->_data['return_url'])) {
  283. $url = $this->_data['return_url'];
  284. if('http' == strtolower(substr($url, 0, 4))) {
  285. $is_valid = true;
  286. }
  287. else {
  288. $this->add_error('return url is invalid');
  289. }
  290. }
  291. else {
  292. $this->add_error('return url is required');
  293. }
  294. return $is_valid;
  295. }
  296. /**
  297. * An order must contain at least one item
  298. *
  299. * Return true if the order has at least one item, otherwise false.
  300. *
  301. * @return boolean
  302. */
  303. private function _check_items() {
  304. $is_valid = true;
  305. if(count($this->_data['items']) <= 0) {
  306. $is_valid = false;
  307. $this->add_error('the order must contain at least one item');
  308. }
  309. return $is_valid;
  310. }
  311. }