MissingPaymentMethodMiddleware.php
8 months ago
SubscriptionNonceVerificationMiddleware.php
2 years ago
SubscriptionPermissionsControllerMiddleware.php
2 years ago
UpdateSubscriptionMiddleware.php
2 years ago
MissingPaymentMethodMiddleware.php
54 lines
| 1 | <?php |
| 2 | namespace SureCartBlocks\Controllers\Middleware; |
| 3 | |
| 4 | use Closure; |
| 5 | use SureCart\Models\ManualPaymentMethod; |
| 6 | use SureCart\Models\Subscription; |
| 7 | use SureCartBlocks\Controllers\PaymentMethodController; |
| 8 | |
| 9 | /** |
| 10 | * Handles a showing a view for the missing payment element. |
| 11 | * If the subscription is missing it. |
| 12 | */ |
| 13 | class MissingPaymentMethodMiddleware { |
| 14 | /** |
| 15 | * Handle the middleware. |
| 16 | * |
| 17 | * @param string $action Action. |
| 18 | * @param Closure $next Next. |
| 19 | * @return function |
| 20 | */ |
| 21 | public function handle( string $action, Closure $next ) { |
| 22 | $id = sanitize_text_field( wp_unslash( $_GET['id'] ?? '' ) ); |
| 23 | $tab = sanitize_text_field( wp_unslash( $_GET['tab'] ?? '' ) ); |
| 24 | |
| 25 | // get the subscription. |
| 26 | $subscription = Subscription::find( $id ); |
| 27 | |
| 28 | // no payment method, show the payment method form. |
| 29 | if ( empty( $subscription->payment_method ) && empty( $subscription->manual_payment_method ) ) { |
| 30 | // Check if there is a manual payment method available for merchant. |
| 31 | // If available, skip redirecting to payment method form. |
| 32 | $manual_payment_methods = ManualPaymentMethod::where( |
| 33 | [ |
| 34 | 'archived' => false, |
| 35 | 'reusable' => true, |
| 36 | ] |
| 37 | )->get(); |
| 38 | if ( ! empty( $manual_payment_methods ) ) { |
| 39 | return $next(); |
| 40 | } |
| 41 | |
| 42 | $current_url = home_url( add_query_arg( [ 'tab' => esc_attr( $tab ) ] ) ); |
| 43 | |
| 44 | return ( new PaymentMethodController() )->create( |
| 45 | [ |
| 46 | 'success_url' => $current_url, |
| 47 | ] |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | return $next(); |
| 52 | } |
| 53 | } |
| 54 |