PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.2
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 / app / src / Middleware / InvoiceRedirectMiddleware.php
InvoiceRedirectMiddleware.php
58 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCart\Middleware;
3
4 use Closure;
5 use SureCart\Models\Invoice;
6 use SureCartCore\Requests\RequestInterface;
7 use SureCartCore\Responses\RedirectResponse;
8
9 /**
10 * Middleware for handling invoice redirects.
11 */
12 class InvoiceRedirectMiddleware {
13 /**
14 * Handle the invoice redirect.
15 *
16 * @param RequestInterface $request Request.
17 * @param Closure $next Next middleware.
18 *
19 * @return RedirectResponse|\SureCartVendors\Psr\Http\Message\ResponseInterface
20 */
21 public function handle( RequestInterface $request, Closure $next ) {
22 $id = $request->query( 'invoice_id' );
23
24 // no invoice id, next request.
25 if ( empty( $id ) ) {
26 return $next( $request );
27 }
28
29 // Find the invoice and redirect to the invoice's checkout
30 $invoice = Invoice::find($id);
31
32 // show error if the invoice is not found.
33 if ( is_wp_error( $invoice ) ) {
34 return wp_die( wp_kses_post( $invoice->get_error_message() ) );
35 }
36
37 // show error if the invoice does not have a checkout id.
38 if ( empty( $invoice->id ) ) {
39 return wp_die( esc_html__( 'Invoice not found.', 'surecart' ) );
40 }
41
42 // show error if the invoice checkout is not found.
43 if ( empty( $invoice->checkout_id ) ) {
44 return wp_die( esc_html__( 'Invoice checkout not found.', 'surecart' ) );
45 }
46
47 // redirect to the invoice's checkout.
48 return ( new RedirectResponse( $request ) )->to(
49 add_query_arg(
50 [
51 'checkout_id' => $invoice->checkout_id,
52 ],
53 \SureCart::getUrl()->checkout()
54 )
55 );
56 }
57 }
58