UpdateSubscriptionMiddleware.php
| 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 |