| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Models; |
| 4 |
|
| 5 |
use SureCart\Models\Traits\HasCustomer; |
| 6 |
use SureCart\Models\Traits\HasPrice; |
| 7 |
use SureCart\Models\Traits\HasPurchase; |
| 8 |
|
| 9 |
/** |
| 10 |
* Subscription model |
| 11 |
*/ |
| 12 |
class Subscription extends Model { |
| 13 |
use HasCustomer, HasPrice, HasPurchase; |
| 14 |
|
| 15 |
/** |
| 16 |
* Rest API endpoint |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $endpoint = 'subscriptions'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Object name |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $object_name = 'subscription'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Update the model. |
| 31 |
* |
| 32 |
* @param array $attributes Attributes to update. |
| 33 |
* @return $this|false |
| 34 |
*/ |
| 35 |
protected function update( $attributes = [] ) { |
| 36 |
// find existing subscription with purchase record. |
| 37 |
$existing = ( new Subscription() )->with( [ 'purchase' ] )->find( $attributes['id'] ?? $this->attributes['id'] ); |
| 38 |
|
| 39 |
// do the update and also get the purchase record. |
| 40 |
$this->with( [ 'purchase' ] ); |
| 41 |
$updated = parent::update( $attributes ); |
| 42 |
if ( is_wp_error( $updated ) ) { |
| 43 |
return $updated; |
| 44 |
} |
| 45 |
|
| 46 |
// do the purchase updated event. |
| 47 |
if ( ! empty( $updated->purchase ) ) { |
| 48 |
do_action( |
| 49 |
'surecart/purchase_updated', |
| 50 |
$updated->purchase, |
| 51 |
[ |
| 52 |
'data' => [ |
| 53 |
'object' => $updated->purchase->toArray(), |
| 54 |
'previous_attributes' => array_filter( |
| 55 |
[ |
| 56 |
// conditionally have the previous product and quantity as the previous attributes. |
| 57 |
'product' => $updated->purchase->product_id !== $existing->purchase->product_id ? ( $existing->purchase->product_id ?? null ) : null, |
| 58 |
'quantity' => $updated->purchase->quantity !== $existing->purchase->quantity ? ( $existing->purchase->quantity ?? 1 ) : null, |
| 59 |
] |
| 60 |
), |
| 61 |
], |
| 62 |
] |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Cancel a subscription |
| 71 |
* |
| 72 |
* @param string $id Model id. |
| 73 |
* @return $this|\WP_Error |
| 74 |
*/ |
| 75 |
protected function cancel( $id = null ) { |
| 76 |
if ( $id ) { |
| 77 |
$this->setAttribute( 'id', $id ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( $this->fireModelEvent( 'canceling' ) === false ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
if ( empty( $this->attributes['id'] ) ) { |
| 85 |
return new \WP_Error( 'not_saved', 'Please create the subscription.' ); |
| 86 |
} |
| 87 |
|
| 88 |
$attributes = $this->attributes; |
| 89 |
unset( $attributes['id'] ); |
| 90 |
|
| 91 |
$canceled = $this->with( |
| 92 |
[ |
| 93 |
'purchase', |
| 94 |
] |
| 95 |
)->makeRequest( |
| 96 |
[ |
| 97 |
'method' => 'PATCH', |
| 98 |
'query' => $this->query, |
| 99 |
'body' => [ |
| 100 |
$this->object_name => $attributes, |
| 101 |
], |
| 102 |
], |
| 103 |
$this->endpoint . '/' . $this->attributes['id'] . '/cancel/' |
| 104 |
); |
| 105 |
|
| 106 |
if ( is_wp_error( $canceled ) ) { |
| 107 |
return $canceled; |
| 108 |
} |
| 109 |
|
| 110 |
$this->resetAttributes(); |
| 111 |
|
| 112 |
$this->fill( $canceled ); |
| 113 |
|
| 114 |
$this->fireModelEvent( 'canceled' ); |
| 115 |
|
| 116 |
// purchase revoked event. |
| 117 |
if ( ! empty( $this->purchase->revoked ) ) { |
| 118 |
do_action( 'surecart/purchase_revoked', $this->purchase ); |
| 119 |
} |
| 120 |
|
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Complete a subscription |
| 126 |
* |
| 127 |
* @param string $id Model id. |
| 128 |
* @return $this|\WP_Error |
| 129 |
*/ |
| 130 |
protected function complete( $id = null ) { |
| 131 |
if ( $id ) { |
| 132 |
$this->setAttribute( 'id', $id ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( $this->fireModelEvent( 'completeing' ) === false ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
if ( empty( $this->attributes['id'] ) ) { |
| 140 |
return new \WP_Error( 'not_saved', 'Please create the subscription.' ); |
| 141 |
} |
| 142 |
|
| 143 |
$completed = $this->with( |
| 144 |
[ |
| 145 |
'purchase', |
| 146 |
] |
| 147 |
)->makeRequest( |
| 148 |
[ |
| 149 |
'method' => 'PATCH', |
| 150 |
'query' => $this->query, |
| 151 |
], |
| 152 |
$this->endpoint . '/' . $this->attributes['id'] . '/complete/' |
| 153 |
); |
| 154 |
|
| 155 |
if ( is_wp_error( $completed ) ) { |
| 156 |
return $completed; |
| 157 |
} |
| 158 |
|
| 159 |
$this->resetAttributes(); |
| 160 |
|
| 161 |
$this->fill( $completed ); |
| 162 |
|
| 163 |
$this->fireModelEvent( 'completed' ); |
| 164 |
|
| 165 |
return $this; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Restore a subscription |
| 170 |
* |
| 171 |
* @param string $id Model id. |
| 172 |
* @return $this|\WP_Error |
| 173 |
*/ |
| 174 |
protected function restore( $id = null ) { |
| 175 |
if ( $id ) { |
| 176 |
$this->setAttribute( 'id', $id ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( $this->fireModelEvent( 'restoring' ) === false ) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
if ( empty( $this->attributes['id'] ) ) { |
| 184 |
return new \WP_Error( 'not_saved', 'Please create the subscription.' ); |
| 185 |
} |
| 186 |
|
| 187 |
$restored = \SureCart::request( |
| 188 |
$this->endpoint . '/' . $this->attributes['id'] . '/restore/', |
| 189 |
[ |
| 190 |
'method' => 'PATCH', |
| 191 |
'query' => $this->query, |
| 192 |
] |
| 193 |
); |
| 194 |
|
| 195 |
if ( is_wp_error( $restored ) ) { |
| 196 |
return $restored; |
| 197 |
} |
| 198 |
|
| 199 |
$this->resetAttributes(); |
| 200 |
|
| 201 |
$this->fill( $restored ); |
| 202 |
|
| 203 |
$this->fireModelEvent( 'restored' ); |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Renew a subscription. |
| 210 |
* |
| 211 |
* @param string $id Model id. |
| 212 |
* @return $this|\WP_Error |
| 213 |
*/ |
| 214 |
protected function renew( $id = null ) { |
| 215 |
if ( $id ) { |
| 216 |
$this->setAttribute( 'id', $id ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( $this->fireModelEvent( 'renewing' ) === false ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
if ( empty( $this->attributes['id'] ) ) { |
| 224 |
return new \WP_Error( 'not_saved', 'Please create the subscription.' ); |
| 225 |
} |
| 226 |
|
| 227 |
$renewed = \SureCart::request( |
| 228 |
$this->endpoint . '/' . $this->attributes['id'], |
| 229 |
[ |
| 230 |
'method' => 'PATCH', |
| 231 |
'query' => $this->query, |
| 232 |
'body' => [ |
| 233 |
$this->object_name => [ |
| 234 |
'cancel_at_period_end' => false, |
| 235 |
], |
| 236 |
], |
| 237 |
] |
| 238 |
); |
| 239 |
|
| 240 |
if ( is_wp_error( $renewed ) ) { |
| 241 |
return $renewed; |
| 242 |
} |
| 243 |
|
| 244 |
$this->resetAttributes(); |
| 245 |
|
| 246 |
$this->fill( $renewed ); |
| 247 |
|
| 248 |
$this->fireModelEvent( 'renewed' ); |
| 249 |
|
| 250 |
return $this; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Preserve a subscription. |
| 255 |
* |
| 256 |
* @param string $id Model id. |
| 257 |
* @return $this|\WP_Error |
| 258 |
*/ |
| 259 |
protected function preserve( $id = null ) { |
| 260 |
if ( $id ) { |
| 261 |
$this->setAttribute( 'id', $id ); |
| 262 |
} |
| 263 |
|
| 264 |
if ( $this->fireModelEvent( 'preserving' ) === false ) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
if ( empty( $this->attributes['id'] ) ) { |
| 269 |
return new \WP_Error( 'not_saved', 'Please create the subscription.' ); |
| 270 |
} |
| 271 |
|
| 272 |
$preserved = $this->makeRequest( |
| 273 |
[ |
| 274 |
'method' => 'PATCH', |
| 275 |
'query' => $this->query, |
| 276 |
], |
| 277 |
$this->endpoint . '/' . $this->attributes['id'] . '/preserve/' |
| 278 |
); |
| 279 |
|
| 280 |
if ( is_wp_error( $preserved ) ) { |
| 281 |
return $preserved; |
| 282 |
} |
| 283 |
|
| 284 |
$this->resetAttributes(); |
| 285 |
|
| 286 |
$this->fill( $preserved ); |
| 287 |
|
| 288 |
$this->fireModelEvent( 'preserved' ); |
| 289 |
|
| 290 |
return $this; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Preview the upcoming invoice. |
| 295 |
* |
| 296 |
* @param string $args Arguments |
| 297 |
* @return $this|\WP_Error |
| 298 |
*/ |
| 299 |
protected function upcomingPeriod( $args = [] ) { |
| 300 |
if ( ! empty( $args['id'] ) ) { |
| 301 |
$this->setAttribute( 'id', $args['id'] ); |
| 302 |
unset( $args['id'] ); |
| 303 |
} |
| 304 |
|
| 305 |
if ( $this->fireModelEvent( 'previewingUpcomingPeriod' ) === false ) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
|
| 309 |
if ( empty( $this->attributes['id'] ) ) { |
| 310 |
return new \WP_Error( 'not_saved', 'Please create the subscription' ); |
| 311 |
} |
| 312 |
|
| 313 |
$upcoming_period = $this->makeRequest( |
| 314 |
[ |
| 315 |
'method' => 'PATCH', |
| 316 |
'query' => $this->query, |
| 317 |
'body' => [ |
| 318 |
$this->object_name => $args, |
| 319 |
], |
| 320 |
], |
| 321 |
$this->endpoint . '/' . $this->attributes['id'] . '/upcoming_period/' |
| 322 |
); |
| 323 |
|
| 324 |
if ( is_wp_error( $upcoming_period ) ) { |
| 325 |
return $upcoming_period; |
| 326 |
} |
| 327 |
|
| 328 |
$this->resetAttributes(); |
| 329 |
|
| 330 |
$this->fill( $upcoming_period ); |
| 331 |
|
| 332 |
$this->fireModelEvent( 'previewedUpcomingPeriod' ); |
| 333 |
|
| 334 |
return $this; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Pay off a subscription |
| 339 |
* |
| 340 |
* @param string $id Model id. |
| 341 |
* @return $this|\WP_Error |
| 342 |
*/ |
| 343 |
protected function payOff ($id = null){ |
| 344 |
if($id){ |
| 345 |
$this->setAttribute('id',$id); |
| 346 |
} |
| 347 |
|
| 348 |
if($this->fireModelEvent('payingOff') === false){ |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
if(empty($this->attributes['id'])){ |
| 353 |
return new \WP_Error('not_saved','Please create the subscription'); |
| 354 |
} |
| 355 |
|
| 356 |
$paid_off = $this->makeRequest( |
| 357 |
[ |
| 358 |
'method' => 'PATCH', |
| 359 |
'query' => $this->query, |
| 360 |
], |
| 361 |
$this->endpoint . '/' . $this->attributes['id'] . '/pay_off/' |
| 362 |
); |
| 363 |
|
| 364 |
if ( is_wp_error( $paid_off ) ) { |
| 365 |
return $paid_off; |
| 366 |
} |
| 367 |
|
| 368 |
$this->resetAttributes(); |
| 369 |
|
| 370 |
$this->fill( $paid_off ); |
| 371 |
|
| 372 |
$this->fireModelEvent( 'paidOff' ); |
| 373 |
|
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Is this subscription a lifetime one? |
| 379 |
* |
| 380 |
* @return boolean |
| 381 |
*/ |
| 382 |
protected function isLifetime() { |
| 383 |
return $this->attributes['id'] && empty( $this->attributes['current_period_end_at'] ); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Can the user upgrade this subscription? |
| 388 |
* |
| 389 |
* @return boolean |
| 390 |
*/ |
| 391 |
protected function canBeSwitched() { |
| 392 |
return apply_filters( 'surecart/subscription/can_be_changed', $this->checkIfCanBeSwitched(), $this ); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Can the subscription be changed? |
| 397 |
* |
| 398 |
* @return boolean |
| 399 |
*/ |
| 400 |
private function checkIfCanBeSwitched() { |
| 401 |
// updates are not enabled for the account. |
| 402 |
if ( empty( \SureCart::account()->portal_protocol->subscription_updates_enabled ) ) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
// already set to canceling. |
| 406 |
if ( $this->attributes['cancel_at_period_end'] ) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
// can't update canceled, incomplete, or past due subscriptions. |
| 410 |
if ( in_array( $this->attributes['status'], [ 'canceled', 'incomplete', 'past_due' ] ) ) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
// must not be lifetime. |
| 414 |
if ( $this->isLifetime() ) { |
| 415 |
return false; |
| 416 |
} |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Can we cancel the subscription? |
| 422 |
* |
| 423 |
* @return boolean |
| 424 |
*/ |
| 425 |
public function canBeCanceled() { |
| 426 |
return apply_filters( 'surecart/subscription/can_be_canceled', $this->checkIfCanBeSwitched(), $this ); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Can the subscription be canceled? |
| 431 |
* |
| 432 |
* @return boolean |
| 433 |
*/ |
| 434 |
private function checkIfCanBeCanceled() { |
| 435 |
// updates are not enabled for the account. |
| 436 |
if ( empty( \SureCart::account()->portal_protocol->subscription_cancellations_enabled ) ) { |
| 437 |
return false; |
| 438 |
} |
| 439 |
|
| 440 |
// can't cancel canceled, incomplete, or past due subscriptions. |
| 441 |
if ( in_array( $this->attributes['status'], [ 'canceled', 'incomplete', 'past_due' ] ) ) { |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Can we update the quantity? |
| 450 |
* |
| 451 |
* @return boolean |
| 452 |
*/ |
| 453 |
protected function canUpdateQuantity() { |
| 454 |
return apply_filters( 'surecart/subscription/can_update_quantity', $this->checkIfCanBeSwitched(), $this ); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Check if we can update the quantity. |
| 459 |
* |
| 460 |
* @return boolean |
| 461 |
*/ |
| 462 |
private function checkIfCanUpdateQuantity() { |
| 463 |
// quantity changes are not enabled for this account. |
| 464 |
if ( empty( \SureCart::account()->portal_protocol->subscription_quantity_updates_enabled ) ) { |
| 465 |
return false; |
| 466 |
} |
| 467 |
return true; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|