PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.22.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.22.1
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 / packages / blocks / Controllers / Middleware / UpdateSubscriptionMiddleware.php
UpdateSubscriptionMiddleware.php
74 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCartBlocks\Controllers\Middleware;
3
4 use Closure;
5 use SureCart\Models\PaymentIntent;
6 use SureCart\Models\Subscription;
7
8 /**
9 * Middleware for handling model archiving.
10 */
11 class UpdateSubscriptionMiddleware {
12 /**
13 * Handle the middleware.
14 *
15 * @param string $action Action.
16 * @param Closure $next Next.
17 * @return function
18 */
19 public function handle( string $action, Closure $next ) {
20 // check for the payment intent.
21 $payment_intent = $_GET['payment_intent'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
22 if ( empty( $payment_intent ) ) {
23 return $next();
24 }
25
26 $intent = PaymentIntent::where( [ 'refresh_status' => true ] )->find( $payment_intent );
27 if ( is_wp_error( $intent ) ) {
28 return wp_die( wp_kses_post( $intent->get_error_message() ) );
29 }
30 if ( empty( $intent->payment_method ) ) {
31 return wp_die( esc_html__( 'Payment method not found.', 'surecart' ) );
32 }
33
34 // update the subscription.
35 $subscription = Subscription::with(
36 [
37 'price',
38 'price.product',
39 'product.product_group',
40 'current_period',
41 'period.checkout',
42 'purchase',
43 'discount',
44 'discount.coupon',
45 'purchase.license',
46 'license.activations',
47 ]
48 )->update(
49 [
50 'id' => sanitize_text_field( wp_unslash( $_GET['id'] ?? '' ) ),
51 'payment_method' => $intent->payment_method,
52 ]
53 );
54 if ( is_wp_error( $subscription ) ) {
55 return wp_die( wp_kses_post( $subscription->get_error_message() ) );
56 }
57
58 return $next();
59 }
60
61 /**
62 * Get the intent from the url.
63 *
64 * @return \WP_Error|\SureCart\Models\PaymentIntent;
65 */
66 public function getIntentFromUrl() {
67 $intent_id = sanitize_text_field( wp_slash( $_GET['payment_intent'] ?? null ) );
68 if ( empty( $intent_id ) ) {
69 return false;
70 }
71 return ! empty( $intent_id ) ? PaymentIntent::find( $intent_id ) : null;
72 }
73 }
74