PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.29.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.29.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 / Checkout.php
Checkout.php
359 lines 7.6 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\HasSubscriptions;
7 use SureCart\Models\LineItem;
8 use SureCart\Models\Traits\CanFinalize;
9 use SureCart\Models\Traits\HasBillingAddress;
10 use SureCart\Models\Traits\HasDiscount;
11 use SureCart\Models\Traits\HasPaymentIntent;
12 use SureCart\Models\Traits\HasPaymentMethod;
13 use SureCart\Models\Traits\HasProcessorType;
14 use SureCart\Models\Traits\HasPurchases;
15 use SureCart\Models\Traits\HasShippingAddress;
16
17 /**
18 * Order model
19 */
20 class Checkout extends Model {
21 use HasCustomer,
22 HasSubscriptions,
23 HasDiscount,
24 HasShippingAddress,
25 HasPaymentIntent,
26 HasPaymentMethod,
27 HasPurchases,
28 CanFinalize,
29 HasProcessorType,
30 HasBillingAddress;
31
32 /**
33 * Rest API endpoint
34 *
35 * @var string
36 */
37 protected $endpoint = 'checkouts';
38
39 /**
40 * Object name
41 *
42 * @var string
43 */
44 protected $object_name = 'checkout';
45
46 /**
47 * Need to pass the processor type on create
48 *
49 * @param array $attributes Optional attributes.
50 * @param string $type stripe, paypal, etc.
51 */
52 public function __construct( $attributes = [], $type = '' ) {
53 $this->processor_type = $type;
54 parent::__construct( $attributes );
55 }
56
57 /**
58 * Set attributes during write actions.
59 *
60 * @return void
61 */
62 protected function setWriteAttributes() {
63 $this->setAttribute( 'ip_address', $this->getIPAddress() );
64 if ( isset( $_COOKIE['sc_click_id'] ) ) {
65 $this->setAttribute( 'last_click', $_COOKIE['sc_click_id'] );
66 }
67 }
68
69 /**
70 * Set the upsell funnel attribute
71 *
72 * @param object $value The data array.
73 * @return void
74 */
75 public function setUpsellFunnelAttribute( $value ) {
76 $this->setRelation( 'upsell_funnel', $value, UpsellFunnel::class );
77 }
78
79 /**
80 * Set the upsell funnel attribute
81 *
82 * @param object $value The data array.
83 * @return void
84 */
85 public function setCurrentUpsellAttribute( $value ) {
86 $this->setRelation( 'current_upsell', $value, Upsell::class );
87 }
88
89 /**
90 * Set the recommended bumps attribute
91 *
92 * @param object $value Subscription data array.
93 * @return void
94 */
95 public function setRecommendedBumpsAttribute( $value ) {
96 $this->setCollection( 'recommended_bumps', $value, Bump::class );
97 }
98
99 /**
100 * Create a new model
101 *
102 * @param array $attributes Attributes to create.
103 *
104 * @return $this|\WP_Error|false
105 */
106 protected function create( $attributes = [] ) {
107 $this->setWriteAttributes();
108 return parent::create( $attributes );
109 }
110
111 /**
112 * Update the model
113 *
114 * @param array $attributes Attributes to create.
115 *
116 * @return $this|\WP_Error|false
117 */
118 protected function update( $attributes = [] ) {
119 $this->setWriteAttributes();
120 return parent::update( $attributes );
121 }
122
123 /**
124 * Get the IP address of the user
125 *
126 * TOD0: Move this to a helper class.
127 *
128 * @return string
129 */
130 protected function getIPAddress() {
131 return $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '';
132 }
133
134 /**
135 * Set the product attribute
136 *
137 * @param object $value Product properties.
138 * @return void
139 */
140 public function setLineItemsAttribute( $value ) {
141 $this->setCollection( 'line_items', $value, LineItem::class );
142 }
143
144 /**
145 * Finalize the session for checkout.
146 *
147 * @return $this|\WP_Error
148 */
149 protected function manuallyPay() {
150 if ( $this->fireModelEvent( 'manually_paying' ) === false ) {
151 return false;
152 }
153
154 if ( empty( $this->attributes['id'] ) ) {
155 return new \WP_Error( 'not_saved', 'Please create the checkout session.' );
156 }
157
158 $finalized = \SureCart::request(
159 $this->endpoint . '/' . $this->attributes['id'] . '/manually_pay/',
160 [
161 'method' => 'PATCH',
162 'query' => $this->query,
163 'body' => [
164 $this->object_name => $this->getAttributes(),
165 ],
166 ]
167 );
168
169 if ( is_wp_error( $finalized ) ) {
170 return $finalized;
171 }
172
173 $this->resetAttributes();
174
175 $this->fill( $finalized );
176
177 $this->fireModelEvent( 'manually_paid' );
178
179 return $this;
180 }
181
182 /**
183 * Cancel an checkout
184 *
185 * @return $this|\WP_Error
186 */
187 protected function cancel() {
188 if ( $this->fireModelEvent( 'cancelling' ) === false ) {
189 return false;
190 }
191
192 if ( empty( $this->attributes['id'] ) ) {
193 return new \WP_Error( 'not_saved', 'Please create the order.' );
194 }
195
196 $cancelled = $this->makeRequest(
197 [
198 'method' => 'PATCH',
199 'query' => $this->query,
200 'body' => [
201 $this->object_name => $this->getAttributes(),
202 ],
203 ],
204 $this->endpoint . '/' . $this->attributes['id'] . '/cancel/'
205 );
206
207 if ( is_wp_error( $cancelled ) ) {
208 return $cancelled;
209 }
210
211 $this->resetAttributes();
212
213 $this->fill( $cancelled );
214
215 $this->fireModelEvent( 'cancelled' );
216
217 return $this;
218 }
219
220 /**
221 * Offer a bump.
222 *
223 * @param string|\SureCart\Models\Bump $bump The bump object.
224 *
225 * @return true|\WP_Error
226 */
227 protected function offerBump( $bump ) {
228 if ( $this->fireModelEvent( 'offering_bump' ) === false ) {
229 return false;
230 }
231
232 if ( empty( $this->attributes['id'] ) ) {
233 return new \WP_Error( 'not_saved', 'Please create the checkout.' );
234 }
235
236 $id = is_a( $bump, Bump::class ) ? $bump->id : $bump;
237
238 if ( empty( $id ) ) {
239 return new \WP_Error( 'not_saved', 'Please pass an upsell.' );
240 }
241
242 $offered = $this->makeRequest(
243 [
244 'method' => 'PATCH',
245 'query' => $this->query,
246 'body' => [
247 $this->object_name => $this->getAttributes(),
248 ],
249 ],
250 $this->endpoint . '/' . $this->attributes['id'] . '/offer_bump/' . $id
251 );
252
253 if ( is_wp_error( $offered ) ) {
254 return $offered;
255 }
256
257 return true;
258 }
259
260 /**
261 * Offer an upsell.
262 *
263 * @param string|\SureCart\Models\Upsell $upsell The upsell object.
264 *
265 * @return true|\WP_Error
266 */
267 protected function offerUpsell( $upsell ) {
268 if ( $this->fireModelEvent( 'offering_upsell' ) === false ) {
269 return false;
270 }
271
272 if ( empty( $this->attributes['id'] ) ) {
273 return new \WP_Error( 'not_saved', 'Please create the checkout.' );
274 }
275
276 $id = is_a( $upsell, Upsell::class ) ? $upsell->id : $upsell;
277
278 if ( empty( $id ) ) {
279 return new \WP_Error( 'not_saved', 'Please pass an upsell.' );
280 }
281
282 $offered = $this->makeRequest(
283 [
284 'method' => 'PATCH',
285 'query' => $this->query,
286 'body' => [
287 $this->object_name => $this->getAttributes(),
288 ],
289 ],
290 $this->endpoint . '/' . $this->attributes['id'] . '/offer_upsell/' . $id
291 );
292
293 if ( is_wp_error( $offered ) ) {
294 return $offered;
295 }
296
297 return true;
298 }
299
300 /**
301 * Decline an upsell.
302 *
303 * @param string|\SureCart\Models\Upsell $upsell The upsell object.
304 *
305 * @return $this|\WP_Error
306 */
307 protected function declineUpsell( $upsell ) {
308 if ( $this->fireModelEvent( 'declining_upsell' ) === false ) {
309 return false;
310 }
311
312 if ( empty( $this->attributes['id'] ) ) {
313 return new \WP_Error( 'not_saved', 'Please create the checkout.' );
314 }
315
316 $id = is_a( $upsell, Upsell::class ) ? $upsell->id : $upsell;
317
318 if ( empty( $id ) ) {
319 return new \WP_Error( 'not_saved', 'Please pass an upsell.' );
320 }
321
322 $declined = $this->makeRequest(
323 [
324 'method' => 'PATCH',
325 'query' => $this->query,
326 'body' => [
327 $this->object_name => $this->getAttributes(),
328 ],
329 ],
330 $this->endpoint . '/' . $this->attributes['id'] . '/decline_upsell/' . $id
331 );
332
333 if ( is_wp_error( $declined ) ) {
334 return $declined;
335 }
336
337 $this->resetAttributes();
338
339 $this->fill( $declined );
340
341 $this->fireModelEvent( 'declined' );
342
343 return $this;
344 }
345
346 /**
347 * Get the billing address attribute
348 *
349 * @return array|null The billing address.
350 */
351 public function getBillingAddressDisplayAttribute() {
352 if ( $this->billing_matches_shipping ) {
353 return $this->shipping_address;
354 }
355
356 return $this->attributes['billing_address'] ?? null;
357 }
358 }
359