PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.2.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 3 years ago SubscriptionController.php 3 years ago UserController.php 3 years ago
SubscriptionController.php
551 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'] ?? __( 'Subscriptions', '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 return '<sc-subscriptions-list id="customer-subscriptions-index"></sc-subscriptions-list>';
68 }
69
70 /**
71 * Show and individual checkout session.
72 *
73 * @return function
74 */
75 public function edit() {
76 $id = $this->getId();
77
78 if ( ! $id ) {
79 return $this->notFound();
80 }
81
82 // fetch subscription.
83 $subscription = Subscription::with(
84 [
85 'price',
86 'price.product',
87 'current_period',
88 'period.checkout',
89 'purchase',
90 'purchase.license',
91 'license.activations',
92 ]
93 )->find( $id );
94
95 ob_start(); ?>
96
97 <sc-spacing style="--spacing: var(--sc-spacing-large)">
98 <sc-breadcrumbs>
99 <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 ?>">
100 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
101 </sc-breadcrumb>
102 <sc-breadcrumb>
103 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
104 </sc-breadcrumb>
105 </sc-breadcrumbs>
106
107 <?php
108 echo wp_kses_post(
109 Component::tag( 'sc-subscription' )
110 ->id( 'customer-subscription-edit' )
111 ->with(
112 [
113 'heading' => __( 'Current Plan', 'surecart' ),
114 'showCancel' => \SureCart::account()->portal_protocol->subscription_cancellations_enabled && ! $subscription->remaining_period_count,
115 'subscription' => $subscription,
116 ]
117 )->render()
118 );
119 ?>
120
121 <?php
122 // show switch if we can change it.
123 if ( $subscription->canBeSwitched() ) :
124 echo wp_kses_post(
125 Component::tag( 'sc-subscription-switch' )
126 ->id( 'customer-subscription-switch' )
127 ->with(
128 [
129 'heading' => __( 'Update Plan', 'surecart' ),
130 'productId' => $subscription->price->product->id,
131 'productGroupId' => $subscription->price->product->product_group,
132 'subscription' => $subscription,
133 ]
134 )->render()
135 );
136 endif;
137 ?>
138
139 </sc-spacing>
140
141 <?php
142 return ob_get_clean();
143 }
144
145 /**
146 * Update the subscription payment method
147 *
148 * @return string
149 */
150 public function update_payment_method() {
151 $id = $this->getId();
152
153 if ( ! $id ) {
154 return $this->notFound();
155 }
156
157 // fetch subscription.
158 $subscription = Subscription::with(
159 [
160 'price',
161 'price.product',
162 'current_period',
163 'period.checkout',
164 ]
165 )->find( $id );
166
167 ob_start();
168 ?>
169
170 <sc-spacing style="--spacing: var(--sc-spacing-large)">
171 <sc-breadcrumbs>
172 <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 ?>">
173 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
174 </sc-breadcrumb>
175 <sc-breadcrumb href="
176 <?php
177 echo esc_url(
178 add_query_arg(
179 [
180 'tab' => $this->getTab(),
181 'action' => 'edit',
182 'model' => 'subscription',
183 'id' => $this->getId(),
184 ],
185 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
186 )
187 );
188 ?>
189 ">
190 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
191 </sc-breadcrumb>
192 <sc-breadcrumb>
193 <?php esc_html_e( 'Update Payment Method', 'surecart' ); ?>
194 </sc-breadcrumb>
195 </sc-breadcrumbs>
196
197 <?php
198 echo wp_kses_post(
199 Component::tag( 'sc-subscription' )
200 ->id( 'customer-subscription-edit' )
201 ->with(
202 [
203 'heading' => __( 'Current Plan', 'surecart' ),
204 'showCancel' => false,
205 'subscription' => $subscription,
206 ]
207 )->render()
208 );
209 ?>
210
211 <?php
212 echo wp_kses_post(
213 Component::tag( 'sc-subscription-payment-method' )
214 ->id( 'customer-subscription-payment-method' )
215 ->with(
216 [
217 'heading' => __( 'Change Payment Method', 'surecart' ),
218 'subscription' => $subscription,
219 ]
220 )->render()
221 );
222 ?>
223
224 </sc-spacing>
225
226 <?php
227 return ob_get_clean();
228 }
229
230
231 /**
232 * Get the terms text.
233 */
234 public function getTermsText() {
235 $account = \SureCart::account();
236 $privacy_url = $account->portal_protocol->privacy_url ?? \get_privacy_policy_url();
237 $terms_url = $account->portal_protocol->terms_url ?? '';
238
239 if ( ! empty( $privacy_url ) && ! empty( $terms_url ) ) {
240 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' ) );
241 }
242
243 if ( ! empty( $privacy_url ) ) {
244 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' ) );
245 }
246
247 if ( ! empty( $terms_url ) ) {
248 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' ) );
249 }
250
251 return '';
252 }
253
254 /**
255 * Confirm the ad_hoc amount.
256 *
257 * @return void
258 */
259 public function confirm_amount() {
260 $price = Price::find( $this->getParam( 'price_id' ) );
261 ob_start();
262 ?>
263
264 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
265 <sc-breadcrumbs>
266 <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 ?>">
267 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
268 </sc-breadcrumb>
269 <sc-breadcrumb href="
270 <?php
271 echo esc_url(
272 add_query_arg(
273 [
274 'tab' => $this->getTab(),
275 'action' => 'edit',
276 'model' => 'subscription',
277 'id' => $this->getId(),
278 ],
279 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
280 )
281 );
282 ?>
283 ">
284 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
285 </sc-breadcrumb>
286 <sc-breadcrumb>
287 <?php esc_html_e( 'Enter Amount', 'surecart' ); ?>
288 </sc-breadcrumb>
289 </sc-breadcrumbs>
290
291 <?php
292
293 echo wp_kses_post(
294 Component::tag( 'sc-subscription-ad-hoc-confirm' )
295 ->id( 'subscription-ad-hoc-confirm' )
296 ->with(
297 [
298 'heading' => __( 'Enter An Amount', 'surecart' ),
299 'price' => $price,
300 ]
301 )->render()
302 );
303 ?>
304
305 </sc-spacing>
306
307 <?php
308 return ob_get_clean();
309 }
310
311 /**
312 * Confirm changing subscription
313 *
314 * @return function
315 */
316 public function confirm() {
317 $back = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
318 ob_start();
319 ?>
320
321 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
322 <sc-breadcrumbs>
323 <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 ?>">
324 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
325 </sc-breadcrumb>
326 <sc-breadcrumb href="
327 <?php
328 echo esc_url(
329 add_query_arg(
330 [
331 'tab' => $this->getTab(),
332 'action' => 'edit',
333 'model' => 'subscription',
334 'id' => $this->getId(),
335 ],
336 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
337 )
338 );
339 ?>
340 ">
341 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
342 </sc-breadcrumb>
343 <sc-breadcrumb>
344 <?php esc_html_e( 'Confirm', 'surecart' ); ?>
345 </sc-breadcrumb>
346 </sc-breadcrumbs>
347
348 <?php
349 $terms = $this->getTermsText();
350 $quantity_enabled = (bool) \SureCart::account()->portal_protocol->subscription_quantity_updates_enabled;
351 if ( $this->getParam( 'ad_hoc_amount' ) ) {
352 $quantity_enabled = false;
353 }
354
355 echo wp_kses_post(
356 Component::tag( 'sc-upcoming-invoice' )
357 ->id( 'customer-upcoming-invoice' )
358 ->with(
359 [
360 'heading' => __( 'New Plan', 'surecart' ),
361 'subscriptionId' => $this->getId(),
362 'priceId' => $this->getParam( 'price_id' ),
363 'adHocAmount' => $this->getParam( 'ad_hoc_amount' ),
364 'successUrl' => esc_url_raw( $back ),
365 'quantityUpdatesEnabled' => (bool) $quantity_enabled,
366 'quantity' => 1,
367 ]
368 )->render( $terms ? '<span slot="terms">' . wp_kses_post( $terms ) . '</span>' : '' )
369 );
370 ?>
371
372
373 </sc-spacing>
374
375 <?php
376 return ob_get_clean();
377
378 }
379
380 /**
381 * Confirm cancel subscription
382 *
383 * @return function
384 */
385 public function cancel() {
386 $back_url = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
387 $edit_subscription_url = add_query_arg(
388 [
389 'tab' => $this->getTab(),
390 'action' => 'edit',
391 'model' => 'subscription',
392 'id' => $this->getId(),
393 ],
394 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
395 );
396 ob_start();
397 ?>
398 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
399 <sc-breadcrumbs>
400 <sc-breadcrumb href="<?php echo esc_url( $back_url ); ?>">
401 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
402 </sc-breadcrumb>
403 <sc-breadcrumb href="<?php echo esc_url( $edit_subscription_url ); ?>" >
404 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
405 </sc-breadcrumb>
406 <sc-breadcrumb>
407 <?php esc_html_e( 'Cancel', 'surecart' ); ?>
408 </sc-breadcrumb>
409 </sc-breadcrumbs>
410
411 <?php
412 echo wp_kses_post(
413 Component::tag( 'sc-subscription-cancel' )
414 ->id( 'customer-subscription-cancel' )
415 ->with(
416 [
417 'subscriptionId' => $this->getId(),
418 'backUrl' => esc_url_raw( $edit_subscription_url ),
419 'successUrl' => esc_url_raw( $back_url ),
420 ]
421 )->render()
422 );
423 ?>
424
425 </sc-spacing>
426 <?php
427 return ob_get_clean();
428 }
429
430 /**
431 * Confirm renew subscription
432 *
433 * @return function
434 */
435 public function renew() {
436 $back_url = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
437 $edit_subscription_url = add_query_arg(
438 [
439 'tab' => $this->getTab(),
440 'action' => 'edit',
441 'model' => 'subscription',
442 'id' => $this->getId(),
443 ],
444 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
445 );
446 ob_start();
447 ?>
448 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
449 <sc-breadcrumbs>
450 <sc-breadcrumb href="<?php echo esc_url( $back_url ); ?>">
451 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
452 </sc-breadcrumb>
453 <sc-breadcrumb href="<?php echo esc_url( $edit_subscription_url ); ?>" >
454 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
455 </sc-breadcrumb>
456 <sc-breadcrumb>
457 <?php esc_html_e( 'Renew', 'surecart' ); ?>
458 </sc-breadcrumb>
459 </sc-breadcrumbs>
460
461 <?php
462 echo wp_kses_post(
463 Component::tag( 'sc-subscription-renew' )
464 ->id( 'customer-subscription-renew' )
465 ->with(
466 [
467 'subscriptionId' => $this->getId(),
468 'backUrl' => esc_url_raw( $edit_subscription_url ),
469 'successUrl' => esc_url_raw( $back_url ),
470 ]
471 )->render()
472 );
473 ?>
474
475 </sc-spacing>
476 <?php
477 return ob_get_clean();
478 }
479
480 /**
481 * Update payment
482 *
483 * @return function
484 */
485 public function payment() {
486 $back_url = add_query_arg( [ 'tab' => $this->getTab() ], remove_query_arg( array_keys( $_GET ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
487
488 $edit_subscription_url = add_query_arg(
489 [
490 'tab' => $this->getTab(),
491 'action' => 'edit',
492 'model' => 'subscription',
493 'id' => $this->getId(),
494 ],
495 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
496 );
497
498 $confirm_subscription_url = add_query_arg(
499 [
500 'tab' => $this->getTab(),
501 'action' => 'confirm',
502 'model' => 'subscription',
503 'ad_hoc_amount' => $this->getParam( 'ad_hoc_amount' ),
504 'id' => $this->getId(),
505 'price_id' => $this->getParam( 'price_id' ),
506 ],
507 remove_query_arg( array_keys( $_GET ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
508 );
509
510 $subscription = Subscription::find( $this->getId() );
511 ob_start();
512 ?>
513
514 <sc-spacing style="--spacing: var(--sc-spacing-xx-large)">
515 <sc-breadcrumbs>
516 <sc-breadcrumb href="<?php echo esc_url( $back_url ); ?>">
517 <?php esc_html_e( 'Dashboard', 'surecart' ); ?>
518 </sc-breadcrumb>
519 <sc-breadcrumb href="<?php echo esc_url( $edit_subscription_url ); ?>">
520 <?php esc_html_e( 'Subscription', 'surecart' ); ?>
521 </sc-breadcrumb>
522 <sc-breadcrumb href="<?php echo esc_url( $confirm_subscription_url ); ?>">
523 <?php esc_html_e( 'Confirm', 'surecart' ); ?>
524 </sc-breadcrumb>
525 <sc-breadcrumb>
526 <?php esc_html_e( 'Payment Method', 'surecart' ); ?>
527 </sc-breadcrumb>
528 </sc-breadcrumbs>
529
530 <?php
531 echo wp_kses_post(
532 Component::tag( 'sc-subscription-payment' )
533 ->id( 'customer-subscription-payment' )
534 ->with(
535 [
536 'customerIds' => $this->customerIds(),
537 'subscription' => $subscription,
538 'backUrl' => esc_url_raw( $confirm_subscription_url ),
539 'successUrl' => esc_url_raw( $confirm_subscription_url ),
540 'quantity' => 1,
541 ]
542 )->render()
543 );
544 ?>
545 </sc-spacing>
546
547 <?php
548 return ob_get_clean();
549 }
550 }
551