PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.0.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.0.4
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / Purchase.php
Purchase.php
151 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Models;
4
5 use SureCart\Models\Traits\HasCustomer;
6 use SureCart\Models\Traits\HasProduct;
7 use SureCart\Models\Traits\HasRefund;
8 use SureCart\Models\Traits\HasSubscription;
9
10 /**
11 * Purchase model.
12 */
13 class Purchase extends Model {
14 use HasCustomer, HasProduct, HasSubscription, HasRefund;
15
16 /**
17 * Rest API endpoint
18 *
19 * @var string
20 */
21 protected $endpoint = 'purchases';
22
23 /**
24 * Object name
25 *
26 * @var string
27 */
28 protected $object_name = 'purchase';
29
30 /**
31 * Revoke the purchase.
32 *
33 * @param string $id Model id.
34 *
35 * @return $this|\WP_Error
36 */
37 protected function revoke( $id = 0 ) {
38 $id = $id ? $id : $this->id;
39
40 if ( $this->fireModelEvent( 'revoking' ) === false ) {
41 return false;
42 }
43
44 if ( empty( $id ) ) {
45 return new \WP_Error( 'not_saved', 'You can only revoke an existing purchase.' );
46 }
47
48 $model = $this->makeRequest(
49 [
50 'method' => 'PATCH',
51 'query' => $this->query,
52 ],
53 $this->endpoint . '/' . $id . '/revoke/',
54 );
55
56 if ( is_wp_error( $model ) ) {
57 return $model;
58 }
59
60 $this->resetAttributes();
61
62 $this->fill( $model );
63
64 $this->fireModelEvent( 'revoked' );
65
66 // purchase revoked event.
67 do_action( 'surecart/purchase_revoked', $this );
68
69 return $this;
70 }
71
72 /**
73 * Invoke the purchase.
74 *
75 * @param string $id Model id.
76 *
77 * @return $this|\WP_Error
78 */
79 protected function invoke( $id = 0 ) {
80 $id = $id ? $id : $this->id;
81
82 if ( $this->fireModelEvent( 'invoking' ) === false ) {
83 return false;
84 }
85
86 if ( empty( $id ) ) {
87 return new \WP_Error( 'not_saved', 'You can only invoke an existing purchase.' );
88 }
89
90 $model = $this->makeRequest(
91 [
92 'method' => 'PATCH',
93 'query' => $this->getQuery(),
94 ],
95 $this->endpoint . '/' . $id . '/invoke/',
96 );
97
98 if ( is_wp_error( $model ) ) {
99 return $model;
100 }
101
102 $this->resetAttributes();
103
104 $this->fill( $model );
105
106 $this->fireModelEvent( 'invoked' );
107
108 // purchase invoked event.
109 do_action( 'surecart/purchase_invoked', $this );
110
111 return $this;
112 }
113
114 /**
115 * Has the product changed?
116 */
117 protected function getHasProductChangedAttribute() {
118 return (bool) $this->getPreviousProductIdAttribute();
119 }
120
121 /**
122 * Get the previous product ID.
123 *
124 * @return string
125 */
126 protected function getPreviousProductIdAttribute() {
127 if ( empty( $this->attributes['previous_attributes']['product'] ) ) {
128 return false;
129 }
130 if ( is_string( $this->attributes['previous_attributes']['product'] ) ) {
131 return $this->attributes['previous_attributes']['product'];
132 }
133 if ( ! empty( $this->attributes['previous_attributes']['product']['id'] ) ) {
134 return $this->attributes['previous_attributes']['product']['id'];
135 }
136 return false;
137 }
138
139 /**
140 * Get the previous quantity
141 *
142 * @return integer
143 */
144 protected function getPreviousQuantityAttribute() {
145 if ( empty( $this->attributes['previous_attributes']['quantity'] ) ) {
146 return false;
147 }
148 return $this->attributes['previous_attributes']['quantity'];
149 }
150 }
151