CartEntity.php
4 years ago
OrderEntity.php
4 years ago
OrderFactory.php
4 years ago
OrderMode.php
4 years ago
OrderStatus.php
4 years ago
OrderType.php
4 years ago
index.php
4 years ago
OrderEntity.php
535 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Models\Order; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\AbstractModel; |
| 6 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 7 | use ProfilePress\Core\Membership\Models\ModelInterface; |
| 8 | use ProfilePress\Core\Membership\PaymentMethods\AbstractPaymentMethod; |
| 9 | use ProfilePress\Core\Membership\PaymentMethods\PaymentMethods; |
| 10 | use ProfilePress\Core\Membership\Repositories\OrderRepository; |
| 11 | use ProfilePress\Core\Membership\Services\OrderService; |
| 12 | |
| 13 | /** |
| 14 | * @property int $id |
| 15 | * @property string $order_key |
| 16 | * @property int $plan_id |
| 17 | * @property int $customer_id |
| 18 | * @property int $subscription_id |
| 19 | * @property string $order_type |
| 20 | * @property string $transaction_id |
| 21 | * @property string $payment_method |
| 22 | * @property string $status |
| 23 | * @property string $coupon_code |
| 24 | * @property string $subtotal |
| 25 | * @property string $tax |
| 26 | * @property string $tax_rate |
| 27 | * @property string $discount |
| 28 | * @property string $total |
| 29 | * @property array $billing_address |
| 30 | * @property array $billing_city |
| 31 | * @property array $billing_state |
| 32 | * @property array $billing_postcode |
| 33 | * @property array $billing_country |
| 34 | * @property array $billing_phone |
| 35 | * @property string $mode |
| 36 | * @property string $currency |
| 37 | * @property string $ip_address |
| 38 | * @property string $date_created |
| 39 | * @property string $date_completed |
| 40 | */ |
| 41 | class OrderEntity extends AbstractModel implements ModelInterface |
| 42 | { |
| 43 | const EU_VAT_NUMBER = 'eu_vat_number'; |
| 44 | const EU_VAT_COUNTRY_CODE = 'eu_vat_country_code'; |
| 45 | const EU_VAT_COMPANY_NAME = 'eu_vat_company_name'; |
| 46 | const EU_VAT_COMPANY_ADDRESS = 'eu_vat_company_address'; |
| 47 | const EU_VAT_NUMBER_IS_VALID = 'eu_vat_number_is_valid'; |
| 48 | const EU_VAT_IS_REVERSE_CHARGED = 'eu_vat_is_reverse_charged'; |
| 49 | |
| 50 | /** |
| 51 | * Order ID |
| 52 | * |
| 53 | * @var int |
| 54 | */ |
| 55 | protected $id = 0; |
| 56 | |
| 57 | protected $plan_id = 0; |
| 58 | |
| 59 | protected $subscription_id = 0; |
| 60 | |
| 61 | /** |
| 62 | * The payment method mode the order was made in |
| 63 | * |
| 64 | * @var string |
| 65 | */ |
| 66 | protected $mode = OrderMode::LIVE; |
| 67 | |
| 68 | protected $order_type = OrderType::NEW_ORDER; |
| 69 | |
| 70 | /** |
| 71 | * The Unique order Key |
| 72 | * |
| 73 | * @var string |
| 74 | */ |
| 75 | protected $order_key = ''; |
| 76 | |
| 77 | protected $discount = '0'; |
| 78 | |
| 79 | protected $tax = '0'; |
| 80 | |
| 81 | protected $tax_rate = '0'; |
| 82 | |
| 83 | protected $subtotal = '0'; |
| 84 | |
| 85 | protected $total = '0'; |
| 86 | |
| 87 | protected $coupon_code = ''; |
| 88 | |
| 89 | /** |
| 90 | * The date the order was created |
| 91 | * |
| 92 | * @var string |
| 93 | */ |
| 94 | protected $date_created = ''; |
| 95 | |
| 96 | /** |
| 97 | * The date the payment was marked as 'complete' |
| 98 | * |
| 99 | * @var string |
| 100 | */ |
| 101 | protected $date_completed = ''; |
| 102 | |
| 103 | /** |
| 104 | * The status of the payment |
| 105 | * |
| 106 | * @var string |
| 107 | */ |
| 108 | protected $status = OrderStatus::PENDING; |
| 109 | |
| 110 | /** |
| 111 | * The customer ID that made the order |
| 112 | * |
| 113 | * @var int |
| 114 | */ |
| 115 | protected $customer_id = 0; |
| 116 | |
| 117 | protected $ip_address = ''; |
| 118 | |
| 119 | protected $billing_address = ''; |
| 120 | |
| 121 | protected $billing_city = ''; |
| 122 | |
| 123 | protected $billing_state = ''; |
| 124 | |
| 125 | protected $billing_country = ''; |
| 126 | |
| 127 | protected $billing_postcode = ''; |
| 128 | |
| 129 | protected $billing_phone = ''; |
| 130 | |
| 131 | /** |
| 132 | * The transaction ID returned by the payment method |
| 133 | * |
| 134 | * @var string |
| 135 | */ |
| 136 | protected $transaction_id = ''; |
| 137 | |
| 138 | /** |
| 139 | * The payment method used to process the order |
| 140 | * |
| 141 | * @var string |
| 142 | */ |
| 143 | protected $payment_method = ''; |
| 144 | |
| 145 | /** |
| 146 | * The currency the order was made with |
| 147 | * |
| 148 | * @var string |
| 149 | */ |
| 150 | protected $currency = ''; |
| 151 | |
| 152 | public function __construct($data = []) |
| 153 | { |
| 154 | if (is_array($data) && ! empty($data)) { |
| 155 | |
| 156 | foreach ($data as $key => $value) { |
| 157 | $this->$key = $value; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @return bool |
| 164 | */ |
| 165 | public function exists() |
| 166 | { |
| 167 | return ! empty($this->id); |
| 168 | } |
| 169 | |
| 170 | public function get_id() |
| 171 | { |
| 172 | return absint($this->id); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param $transaction_id |
| 177 | * |
| 178 | * @return false|int |
| 179 | */ |
| 180 | public function complete_order($transaction_id = '') |
| 181 | { |
| 182 | $this->status = OrderStatus::COMPLETED; |
| 183 | |
| 184 | if ( ! empty($transaction_id)) { |
| 185 | $this->transaction_id = $transaction_id; |
| 186 | } |
| 187 | |
| 188 | $order_id = $this->save(); |
| 189 | |
| 190 | do_action('ppress_order_completed', $this); |
| 191 | |
| 192 | return $order_id; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return false|int |
| 197 | */ |
| 198 | public function refund_order() |
| 199 | { |
| 200 | $this->status = OrderStatus::REFUNDED; |
| 201 | $response = $this->save(); |
| 202 | |
| 203 | if ($response) { |
| 204 | $this->add_note(sprintf(__('Payment %s has been fully refunded in Stripe.', 'wp-user-avatar'), $this->transaction_id)); |
| 205 | do_action('ppress_order_order_refunded', $this); |
| 206 | } |
| 207 | |
| 208 | return $response; |
| 209 | } |
| 210 | |
| 211 | public function update_status($order_status) |
| 212 | { |
| 213 | $old_status = $this->status; |
| 214 | |
| 215 | $this->status = $order_status; |
| 216 | |
| 217 | $response = $this->save(); |
| 218 | |
| 219 | $user = is_user_logged_in() ? wp_get_current_user()->user_login : esc_html__('payment method', 'wp-user-avatar'); |
| 220 | |
| 221 | $this->add_note( |
| 222 | sprintf(__('Order changed from %s to %s by %s', 'wp-user-avatar'), $old_status, $this->status, $user) |
| 223 | ); |
| 224 | |
| 225 | do_action('ppress_order_status_updated', $order_status, $old_status, $this); |
| 226 | |
| 227 | return $response; |
| 228 | } |
| 229 | |
| 230 | public function set_status($status) |
| 231 | { |
| 232 | $valid_statuses = (new \ReflectionClass(OrderStatus::class))->getConstants(); |
| 233 | |
| 234 | if (in_array($status, $valid_statuses)) { |
| 235 | $this->status = $status; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public function set_mode($mode) |
| 240 | { |
| 241 | $valid_modes = (new \ReflectionClass(OrderMode::class))->getConstants(); |
| 242 | |
| 243 | if (in_array($mode, $valid_modes)) { |
| 244 | $this->mode = $mode; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public function set_order_key($value) |
| 249 | { |
| 250 | // ensures order key doesn't exceed 64 chars (DB max length) |
| 251 | $this->order_key = substr($value, 0, 64); |
| 252 | } |
| 253 | |
| 254 | public function set_currency($currency) |
| 255 | { |
| 256 | $currencies = array_keys(ppress_get_currencies()); |
| 257 | |
| 258 | if (in_array($currency, $currencies)) { |
| 259 | $this->currency = $currency; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return false|int |
| 265 | */ |
| 266 | public function save() |
| 267 | { |
| 268 | if ($this->id > 0) { |
| 269 | |
| 270 | $result = OrderRepository::init()->update($this); |
| 271 | |
| 272 | do_action('ppress_order_updated', $result, $this); |
| 273 | |
| 274 | return $result; |
| 275 | } |
| 276 | |
| 277 | $result = OrderRepository::init()->add($this); |
| 278 | |
| 279 | do_action('ppress_order_added', $result, $this); |
| 280 | |
| 281 | return $result; |
| 282 | } |
| 283 | |
| 284 | public function get_customer_full_address() |
| 285 | { |
| 286 | $billing_address = $this->billing_address; |
| 287 | |
| 288 | if (empty($billing_address)) return ''; |
| 289 | |
| 290 | $state = ppress_var(ppress_array_of_world_states($this->billing_country), $this->billing_state, $this->billing_state, true); |
| 291 | |
| 292 | $address = [trim($billing_address)]; |
| 293 | $address[] = trim($this->billing_city . ' ' . $state); |
| 294 | $address[] = $this->billing_postcode; |
| 295 | $address[] = ppress_array_of_world_countries($this->billing_country); |
| 296 | |
| 297 | return implode(', ', array_filter($address)); |
| 298 | } |
| 299 | |
| 300 | public function get_customer_tax_id() |
| 301 | { |
| 302 | return $this->get_meta(self::EU_VAT_NUMBER); |
| 303 | } |
| 304 | |
| 305 | public function get_subtotal() |
| 306 | { |
| 307 | return (string)$this->subtotal; |
| 308 | } |
| 309 | |
| 310 | public function get_tax() |
| 311 | { |
| 312 | return (string)$this->tax; |
| 313 | } |
| 314 | |
| 315 | public function get_tax_rate() |
| 316 | { |
| 317 | return (string)$this->tax_rate; |
| 318 | } |
| 319 | |
| 320 | public function get_total() |
| 321 | { |
| 322 | return (string)$this->total; |
| 323 | } |
| 324 | |
| 325 | public function get_discount() |
| 326 | { |
| 327 | return (string)$this->discount; |
| 328 | } |
| 329 | |
| 330 | public function get_order_number() |
| 331 | { |
| 332 | return (string)apply_filters('ppress_order_number', $this->get_id(), $this); |
| 333 | } |
| 334 | |
| 335 | public function get_order_key() |
| 336 | { |
| 337 | return $this->order_key; |
| 338 | } |
| 339 | |
| 340 | public function get_reduced_order_key() |
| 341 | { |
| 342 | return strtoupper(substr($this->order_key, 0, 8)); |
| 343 | } |
| 344 | |
| 345 | public function get_order_id() |
| 346 | { |
| 347 | return $this->get_reduced_order_key(); |
| 348 | } |
| 349 | |
| 350 | public function get_plan_id() |
| 351 | { |
| 352 | return absint($this->plan_id); |
| 353 | } |
| 354 | |
| 355 | public function get_subscription_id() |
| 356 | { |
| 357 | return absint($this->subscription_id); |
| 358 | } |
| 359 | |
| 360 | public function get_customer_id() |
| 361 | { |
| 362 | return absint($this->customer_id); |
| 363 | } |
| 364 | |
| 365 | public function get_customer_email() |
| 366 | { |
| 367 | return CustomerFactory::fromId($this->customer_id)->get_email(); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * @return string |
| 372 | */ |
| 373 | public function get_transaction_id() |
| 374 | { |
| 375 | return $this->transaction_id; |
| 376 | } |
| 377 | |
| 378 | public function get_linked_transaction_id() |
| 379 | { |
| 380 | return PaymentMethods::get_instance()->get_by_id($this->payment_method)->link_transaction_id($this->transaction_id); |
| 381 | } |
| 382 | |
| 383 | public function key_is_valid($key) |
| 384 | { |
| 385 | return hash_equals($this->get_order_key(), $key); |
| 386 | } |
| 387 | |
| 388 | public function needs_payment() |
| 389 | { |
| 390 | $valid_order_statuses = apply_filters('woocommerce_valid_order_statuses_for_payment', array('pending', 'failed'), $this); |
| 391 | |
| 392 | return apply_filters('woocommerce_order_needs_payment', ($this->has_status($valid_order_statuses) && $this->get_total() > 0), $this, $valid_order_statuses); |
| 393 | } |
| 394 | |
| 395 | public function is_new_order() |
| 396 | { |
| 397 | return $this->order_type == OrderType::NEW_ORDER; |
| 398 | } |
| 399 | |
| 400 | public function is_renewal_order() |
| 401 | { |
| 402 | return $this->order_type == OrderType::RENEWAL_ORDER; |
| 403 | } |
| 404 | |
| 405 | public function is_completed() |
| 406 | { |
| 407 | return $this->status == OrderStatus::COMPLETED; |
| 408 | } |
| 409 | |
| 410 | public function is_failed() |
| 411 | { |
| 412 | return $this->status == OrderStatus::FAILED; |
| 413 | } |
| 414 | |
| 415 | public function is_pending() |
| 416 | { |
| 417 | return $this->status == OrderStatus::PENDING; |
| 418 | } |
| 419 | |
| 420 | public function is_refunded() |
| 421 | { |
| 422 | return $this->status == OrderStatus::REFUNDED; |
| 423 | } |
| 424 | |
| 425 | public function is_refundable() |
| 426 | { |
| 427 | $payment_method = PaymentMethods::get_instance()->get_by_id($this->payment_method); |
| 428 | |
| 429 | if ( ! $payment_method instanceof AbstractPaymentMethod) { |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | if ($this->status !== OrderStatus::COMPLETED) { |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | return $payment_method->supports($payment_method::REFUNDS); |
| 438 | } |
| 439 | |
| 440 | public function get_refund_url() |
| 441 | { |
| 442 | $url = esc_url(wp_nonce_url(add_query_arg(array('ppress_order_action' => 'refund_order', 'id' => $this->id)), 'ppress-cancel-order')); |
| 443 | |
| 444 | return apply_filters('ppress_cancel_order_url', $url, $this); |
| 445 | } |
| 446 | |
| 447 | public function update_transaction_id($transaction_id) |
| 448 | { |
| 449 | return OrderRepository::init()->updateColumn($this->id, 'transaction_id', $transaction_id); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * @return array |
| 454 | */ |
| 455 | public function get_notes() |
| 456 | { |
| 457 | return OrderService::init()->get_order_notes($this->id); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * @param $note |
| 462 | * |
| 463 | * @return false|int |
| 464 | */ |
| 465 | public function add_note($note) |
| 466 | { |
| 467 | return OrderService::init()->add_order_note($this->id, $note); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @param $note_id |
| 472 | * |
| 473 | * @return bool |
| 474 | */ |
| 475 | public function delete_note($note_id) |
| 476 | { |
| 477 | return OrderService::init()->delete_order_note_by_id($note_id); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @param $meta_key |
| 482 | * |
| 483 | * @return array|false|mixed |
| 484 | */ |
| 485 | public function get_meta($meta_key) |
| 486 | { |
| 487 | return OrderRepository::init()->get_meta_data( |
| 488 | $this->get_id(), |
| 489 | $meta_key |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @param $meta_key |
| 495 | * @param $meta_value |
| 496 | * |
| 497 | * @return false|int |
| 498 | */ |
| 499 | public function add_meta($meta_key, $meta_value) |
| 500 | { |
| 501 | return OrderRepository::init()->add_meta_data( |
| 502 | $this->get_id(), |
| 503 | $meta_key, |
| 504 | $meta_value |
| 505 | ); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * @param $meta_key |
| 510 | * @param $meta_value |
| 511 | * |
| 512 | * @return bool|int |
| 513 | */ |
| 514 | public function update_meta($meta_key, $meta_value) |
| 515 | { |
| 516 | return OrderRepository::init()->update_meta_data( |
| 517 | $this->get_id(), |
| 518 | $meta_key, |
| 519 | $meta_value |
| 520 | ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @param $meta_key |
| 525 | * |
| 526 | * @return bool |
| 527 | */ |
| 528 | public function delete_meta($meta_key) |
| 529 | { |
| 530 | return OrderRepository::init()->delete_meta_data( |
| 531 | $this->get_id(), |
| 532 | $meta_key |
| 533 | ); |
| 534 | } |
| 535 | } |