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