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