PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.6.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.6.1
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 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / packages / blocks / Controllers / SubscriptionController.php
surecart / packages / blocks / Controllers Last commit date
BaseController.php 3 years ago ChargeController.php 3 years ago CustomerController.php 3 years ago DownloadController.php 3 years ago InvoiceController.php 3 years ago OrderController.php 3 years ago PaymentMethodController.php 2 years ago SubscriptionController.php 3 years ago UserController.php 3 years ago
SubscriptionController.php
553 lines
1 <?php
2 namespace SureCartBlocks\Controllers;
3
4 use SureCart\Models\Component;
5 use SureCart\Models\Price;
6 use SureCart\Models\Subscription;
7 use SureCart\Models\SubscriptionProtocol;
8 use SureCart\Models\User;
9
10 /**
11 * The subscription controller.
12 */
13 class SubscriptionController extends BaseController {
14 /**
15 * Render the block
16 *
17 * @param array $attributes Block attributes.
18 * @return function
19 */
20 public function preview( $attributes = [] ) {
21 return wp_kses_post(
22 Component::tag( 'sc-subscriptions-list' )
23 ->id( 'customer-subscriptions-preview' )
24 ->with(
25 [
26 'heading' => $attributes['title'] ?? null,
27 'isCustomer' => User::current()->isCustomer(),
28 'allLink' => add_query_arg(
29 [
30 'tab' => $this->getTab(),
31 'model' => 'subscription',
32 'action' => 'index',
33 ],
34 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
35 ),
36 'query' => [
37 'customer_ids' => array_values( User::current()->customerIds() ),
38 'status' => [ 'active', 'trialing', 'past_due', 'canceled' ],
39 'page' => 1,
40 'per_page' => 5,
41 ],
42 ]
43 )->render( $attributes['title'] ? "<span slot='heading'>" . $attributes['title'] . '</span>' : '' )
44 );
45 }
46
47 /**
48 * Render the block
49 *
50 * @return function
51 */
52 public function index() {
53 \SureCart::assets()->addComponentData(
54 'sc-subscriptions-list',
55 '#customer-subscriptions-index',
56 [
57 'heading' => $attributes['title'] ?? __( 'Plans', 'surecart' ),
58 'isCustomer' => User::current()->isCustomer(),
59 'query' => [
60 'customer_ids' => array_values( User::current()->customerIds() ),
61 'status' => [ 'active', 'trialing', 'canceled' ],
62 'page' => 1,
63 'per_page' => 20,
64 ],
65 ]
66 );
67 ob_start();
68 ?>
69 <sc-spacing style="--spacing: var(--sc-spacing-large)">
70 <sc-breadcrumbs>
71 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
72 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
73 </sc-breadcrumb>
74 <sc-breadcrumb>
75 <?php esc_html_e( 'Plans', 'surecart' ); ?>
76 </sc-breadcrumb>
77 </sc-breadcrumbs>
78 <sc-subscriptions-list id="customer-subscriptions-index"></sc-subscriptions-list>
79 </sc-spacing>
80 <?php
81 return ob_get_clean();
82 }
83
84 /**
85 * Show and individual checkout session.
86 *
87 * @return function
88 */
89 public function edit() {
90 $id = $this->getId();
91
92 if ( ! $id ) {
93 return $this->notFound();
94 }
95
96 // fetch subscription.
97 $subscription = Subscription::with(
98 [
99 'price',
100 'price.product',
101 'current_period',
102 'period.checkout',
103 'purchase',
104 'discount',
105 'discount.coupon',
106 'purchase.license',
107 'license.activations',
108 ]
109 )->find( $id );
110
111 ob_start();
112 ?>
113
114 <sc-spacing style="--spacing: var(--sc-spacing-large)">
115 <sc-breadcrumbs>
116 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
117 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
118 </sc-breadcrumb>
119 <sc-breadcrumb href="
120 <?php
121 echo esc_url(
122 add_query_arg(
123 [
124 'tab' => $this->getTab(),
125 'action' => 'index',
126 'model' => 'subscription',
127 ],
128 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
129 )
130 );
131 ?>
132 ">
133 <?php esc_html_e( 'Plans', 'surecart' ); ?>
134 </sc-breadcrumb>
135 <sc-breadcrumb>
136 <?php esc_html_e( 'Plan', 'surecart' ); ?>
137 </sc-breadcrumb>
138 </sc-breadcrumbs>
139
140 <?php
141 echo wp_kses_post(
142 Component::tag( 'sc-subscription' )
143 ->id( 'customer-subscription-edit' )
144 ->with(
145 [
146 'heading' => __( 'Current Plan', 'surecart' ),
147 'showCancel' => \SureCart::account()->portal_protocol->subscription_cancellations_enabled && ! $subscription->remaining_period_count,
148 'protocol' => SubscriptionProtocol::with( [ 'preservation_coupon' ] )->find(), // \SureCart::account()->subscription_protocol,
149 'subscription' => $subscription,
150 ]
151 )->render()
152 );
153 ?>
154
155 <?php
156 // show switch if we can change it.
157 if ( $subscription->canBeSwitched() ) :
158 echo wp_kses_post(
159 Component::tag( 'sc-subscription-switch' )
160 ->id( 'customer-subscription-switch' )
161 ->with(
162 [
163 'heading' => __( 'Update Plan', 'surecart' ),
164 'productId' => $subscription->price->product->id,
165 'productGroupId' => $subscription->price->product->product_group,
166 'subscription' => $subscription,
167 ]
168 )->render()
169 );
170 endif;
171 ?>
172
173 </sc-spacing>
174
175 <?php
176 return ob_get_clean();
177 }
178
179 /**
180 * Update the subscription payment method
181 *
182 * @return string
183 */
184 public function update_payment_method() {
185 $id = $this->getId();
186
187 if ( ! $id ) {
188 return $this->notFound();
189 }
190
191 // fetch subscription.
192 $subscription = Subscription::with(
193 [
194 'price',
195 'price.product',
196 'current_period',
197 'period.checkout',
198 'discount',
199 'discount.coupon',
200 ]
201 )->find( $id );
202
203 ob_start();
204 ?>
205
206 <sc-spacing style="--spacing: var(--sc-spacing-large)">
207 <sc-breadcrumbs>
208 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
209 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
210 </sc-breadcrumb>
211 <sc-breadcrumb href="
212 <?php
213 echo esc_url(
214 add_query_arg(
215 [
216 'tab' => $this->getTab(),
217 'action' => 'index',
218 'model' => 'subscription',
219 ],
220 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
221 )
222 );
223 ?>
224 ">
225 <?php esc_html_e( 'Plans', 'surecart' ); ?>
226 </sc-breadcrumb>
227 <sc-breadcrumb href="
228 <?php
229 echo esc_url(
230 add_query_arg(
231 [
232 'tab' => $this->getTab(),
233 'action' => 'edit',
234 'model' => 'subscription',
235 'id' => $this->getId(),
236 ],
237 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
238 )
239 );
240 ?>
241 ">
242 <?php esc_html_e( 'Plan', 'surecart' ); ?>
243 </sc-breadcrumb>
244 <sc-breadcrumb>
245 <?php esc_html_e( 'Update Payment Method', 'surecart' ); ?>
246 </sc-breadcrumb>
247 </sc-breadcrumbs>
248
249 <?php
250 echo wp_kses_post(
251 Component::tag( 'sc-subscription' )
252 ->id( 'customer-subscription-edit' )
253 ->with(
254 [
255 'heading' => __( 'Current Plan', 'surecart' ),
256 'showCancel' => false,
257 'subscription' => $subscription,
258 ]
259 )->render()
260 );
261 ?>
262
263 <?php
264 echo wp_kses_post(
265 Component::tag( 'sc-subscription-payment-method' )
266 ->id( 'customer-subscription-payment-method' )
267 ->with(
268 [
269 'heading' => __( 'Change Payment Method', 'surecart' ),
270 'subscription' => $subscription,
271 ]
272 )->render()
273 );
274 ?>
275
276 </sc-spacing>
277
278 <?php
279 return ob_get_clean();
280 }
281
282
283 /**
284 * Get the terms text.
285 */
286 public function getTermsText() {
287 $account = \SureCart::account();
288 $privacy_url = $account->portal_protocol->privacy_url ?? \get_privacy_policy_url();
289 $terms_url = $account->portal_protocol->terms_url ?? '';
290
291 if ( ! empty( $privacy_url ) && ! empty( $terms_url ) ) {
292 return sprintf( __( 'By updating or canceling your plan, you agree to the <a href="%1$1s" target="_blank">%2$2s</a> and <a href="%3$3s" target="_blank">%4$4s</a>', 'surecart' ), esc_url( $terms_url ), __( 'Terms', 'surecart' ), esc_url( $privacy_url ), __( 'Privacy Policy', 'surecart' ) );
293 }
294
295 if ( ! empty( $privacy_url ) ) {
296 return sprintf( __( 'By updating or canceling your plan, you agree to the <a href="%1$1s" target="_blank">%2$2s</a>', 'surecart' ), esc_url( $privacy_url ), __( 'Privacy Policy', 'surecart' ) );
297 }
298
299 if ( ! empty( $terms_url ) ) {
300 return sprintf( __( 'By updating or canceling your plan, you agree to the <a href="%1$1s" target="_blank">%2$2s</a>', 'surecart' ), esc_url( $terms_url ), __( 'Terms', 'surecart' ) );
301 }
302
303 return '';
304 }
305
306 /**
307 * Confirm the ad_hoc amount.
308 *
309 * @return void
310 */
311 public function confirm_amount() {
312 $price = Price::find( $this->getParam( 'price_id' ) );
313 ob_start();
314 ?>
315
316 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
317 <sc-breadcrumbs>
318 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
319 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
320 </sc-breadcrumb>
321 <sc-breadcrumb href="
322 <?php
323 echo esc_url(
324 add_query_arg(
325 [
326 'tab' => $this->getTab(),
327 'action' => 'edit',
328 'model' => 'subscription',
329 'id' => $this->getId(),
330 ],
331 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
332 )
333 );
334 ?>
335 ">
336 <?php esc_html_e( 'Plan', 'surecart' ); ?>
337 </sc-breadcrumb>
338 <sc-breadcrumb>
339 <?php esc_html_e( 'Enter Amount', 'surecart' ); ?>
340 </sc-breadcrumb>
341 </sc-breadcrumbs>
342
343 <?php
344
345 echo wp_kses_post(
346 Component::tag( 'sc-subscription-ad-hoc-confirm' )
347 ->id( 'subscription-ad-hoc-confirm' )
348 ->with(
349 [
350 'heading' => __( 'Enter An Amount', 'surecart' ),
351 'price' => $price,
352 ]
353 )->render()
354 );
355 ?>
356
357 </sc-spacing>
358
359 <?php
360 return ob_get_clean();
361 }
362
363 /**
364 * Confirm changing subscription
365 *
366 * @return function
367 */
368 public function confirm() {
369 $back = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
370 ob_start();
371 ?>
372
373 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
374 <sc-breadcrumbs>
375 <sc-breadcrumb href="<?php echo esc_url( add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>">
376 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
377 </sc-breadcrumb>
378 <sc-breadcrumb href="
379 <?php
380 echo esc_url(
381 add_query_arg(
382 [
383 'tab' => $this->getTab(),
384 'action' => 'edit',
385 'model' => 'subscription',
386 'id' => $this->getId(),
387 ],
388 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
389 )
390 );
391 ?>
392 ">
393 <?php esc_html_e( 'Plan', 'surecart' ); ?>
394 </sc-breadcrumb>
395 <sc-breadcrumb>
396 <?php esc_html_e( 'Confirm', 'surecart' ); ?>
397 </sc-breadcrumb>
398 </sc-breadcrumbs>
399
400 <?php
401 $terms = $this->getTermsText();
402 $quantity_enabled = (bool) \SureCart::account()->portal_protocol->subscription_quantity_updates_enabled;
403 if ( $this->getParam( 'ad_hoc_amount' ) ) {
404 $quantity_enabled = false;
405 }
406
407 echo wp_kses_post(
408 Component::tag( 'sc-upcoming-invoice' )
409 ->id( 'customer-upcoming-invoice' )
410 ->with(
411 [
412 'heading' => __( 'New Plan', 'surecart' ),
413 'subscriptionId' => $this->getId(),
414 'priceId' => $this->getParam( 'price_id' ),
415 'adHocAmount' => $this->getParam( 'ad_hoc_amount' ),
416 'successUrl' => esc_url_raw( $back ),
417 'quantityUpdatesEnabled' => (bool) $quantity_enabled,
418 'quantity' => 1,
419 ]
420 )->render( $terms ? '<span slot="terms">' . wp_kses_post( $terms ) . '</span>' : '' )
421 );
422 ?>
423
424
425 </sc-spacing>
426
427 <?php
428 return ob_get_clean();
429
430 }
431
432 /**
433 * Confirm cancel subscription
434 *
435 * @return function
436 */
437 public function cancel() {
438 $back_url = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
439 $edit_subscription_url = add_query_arg(
440 [
441 'tab' => $this->getTab(),
442 'action' => 'edit',
443 'model' => 'subscription',
444 'id' => $this->getId(),
445 ],
446 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
447 );
448 ob_start();
449 ?>
450 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
451 <sc-breadcrumbs>
452 <sc-breadcrumb href="<?php echo esc_url( $back_url ); ?>">
453 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
454 </sc-breadcrumb>
455 <sc-breadcrumb href="<?php echo esc_url( $edit_subscription_url ); ?>" >
456 <?php esc_html_e( 'Plan', 'surecart' ); ?>
457 </sc-breadcrumb>
458 <sc-breadcrumb>
459 <?php esc_html_e( 'Cancel', 'surecart' ); ?>
460 </sc-breadcrumb>
461 </sc-breadcrumbs>
462
463 <?php
464 echo wp_kses_post(
465 Component::tag( 'sc-subscription-cancel' )
466 ->id( 'customer-subscription-cancel' )
467 ->with(
468 [
469 'subscriptionId' => $this->getId(),
470 'backUrl' => esc_url_raw( $edit_subscription_url ),
471 'successUrl' => esc_url_raw( $back_url ),
472 ]
473 )->render()
474 );
475 ?>
476
477 </sc-spacing>
478 <?php
479 return ob_get_clean();
480 }
481
482 /**
483 * Update payment
484 *
485 * @return function
486 */
487 public function payment() {
488 $back_url = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
489
490 $edit_subscription_url = add_query_arg(
491 [
492 'tab' => $this->getTab(),
493 'action' => 'edit',
494 'model' => 'subscription',
495 'id' => $this->getId(),
496 ],
497 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
498 );
499
500 $confirm_subscription_url = add_query_arg(
501 [
502 'tab' => $this->getTab(),
503 'action' => 'confirm',
504 'model' => 'subscription',
505 'ad_hoc_amount' => $this->getParam( 'ad_hoc_amount' ),
506 'id' => $this->getId(),
507 'price_id' => $this->getParam( 'price_id' ),
508 ],
509 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
510 );
511
512 $subscription = Subscription::find( $this->getId() );
513 ob_start();
514 ?>
515
516 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
517 <sc-breadcrumbs>
518 <sc-breadcrumb href="<?php echo esc_url( $back_url ); ?>">
519 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
520 </sc-breadcrumb>
521 <sc-breadcrumb href="<?php echo esc_url( $edit_subscription_url ); ?>">
522 <?php esc_html_e( 'Plan', 'surecart' ); ?>
523 </sc-breadcrumb>
524 <sc-breadcrumb href="<?php echo esc_url( $confirm_subscription_url ); ?>">
525 <?php esc_html_e( 'Confirm', 'surecart' ); ?>
526 </sc-breadcrumb>
527 <sc-breadcrumb>
528 <?php esc_html_e( 'Payment Method', 'surecart' ); ?>
529 </sc-breadcrumb>
530 </sc-breadcrumbs>
531
532 <?php
533 echo wp_kses_post(
534 Component::tag( 'sc-subscription-payment' )
535 ->id( 'customer-subscription-payment' )
536 ->with(
537 [
538 'customerIds' => $this->customerIds(),
539 'subscription' => $subscription,
540 'backUrl' => esc_url_raw( $confirm_subscription_url ),
541 'successUrl' => esc_url_raw( $confirm_subscription_url ),
542 'quantity' => 1,
543 ]
544 )->render()
545 );
546 ?>
547 </sc-spacing>
548
549 <?php
550 return ob_get_clean();
551 }
552 }
553