Period.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasCheckout; |
| 6 | use SureCart\Models\Traits\HasPrice; |
| 7 | use SureCart\Models\Traits\HasSubscription; |
| 8 | use SureCart\Support\TimeDate; |
| 9 | |
| 10 | /** |
| 11 | * Period model |
| 12 | */ |
| 13 | class Period extends Model { |
| 14 | use HasSubscription, HasCheckout, HasPrice; |
| 15 | |
| 16 | /** |
| 17 | * Rest API endpoint |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $endpoint = 'periods'; |
| 22 | |
| 23 | /** |
| 24 | * Object name |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $object_name = 'period'; |
| 29 | |
| 30 | /** |
| 31 | * Restore a subscription |
| 32 | * |
| 33 | * @param string $id Model id. |
| 34 | * @return $this|\WP_Error |
| 35 | */ |
| 36 | protected function retryPayment( $id = null ) { |
| 37 | if ( $id ) { |
| 38 | $this->setAttribute( 'id', $id ); |
| 39 | } |
| 40 | |
| 41 | if ( $this->fireModelEvent( 'retrying_payment' ) === false ) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | if ( empty( $this->attributes['id'] ) ) { |
| 46 | return new \WP_Error( 'not_saved', 'Please create the period.' ); |
| 47 | } |
| 48 | |
| 49 | $restored = \SureCart::request( |
| 50 | $this->endpoint . '/' . $this->attributes['id'] . '/retry_payment/', |
| 51 | [ |
| 52 | 'method' => 'PATCH', |
| 53 | 'query' => $this->query, |
| 54 | ] |
| 55 | ); |
| 56 | |
| 57 | if ( is_wp_error( $restored ) ) { |
| 58 | return $restored; |
| 59 | } |
| 60 | |
| 61 | $this->resetAttributes(); |
| 62 | |
| 63 | $this->fill( $restored ); |
| 64 | |
| 65 | $this->fireModelEvent( 'payment_retry_success' ); |
| 66 | |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the start at date. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function getStartAtDateAttribute() { |
| 76 | return ! empty( $this->start_at ) ? TimeDate::formatDate( $this->start_at ) : ''; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the end at date. |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public function getEndAtDateAttribute() { |
| 85 | return ! empty( $this->end_at ) ? TimeDate::formatDate( $this->end_at ) : ''; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the next payment retry at date time. |
| 90 | * |
| 91 | * @return string |
| 92 | */ |
| 93 | public function getNextPaymentRetryAtDateTimeAttribute() { |
| 94 | return ! empty( $this->next_payment_retry_at ) ? TimeDate::formatDateAndTime( $this->next_payment_retry_at ) : ''; |
| 95 | } |
| 96 | } |
| 97 |