Skip to content

Commit

Permalink
Started working on payment redirect controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
knit-pay committed Feb 10, 2024
1 parent da19613 commit ce19a4a
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 0 deletions.
241 changes: 241 additions & 0 deletions src/PaymentRedirectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/**
* Payment Redirect controller
*
* @author Pronamic <[email protected]>
* @copyright 2005-2023 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay
*/

namespace Pronamic\WordPress\Pay;

use Pronamic\WordPress\Http\Facades\Http;
use Pronamic\WordPress\Money\Money;
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
use WP_REST_Request;

/**
* Payment Redirect controller
*
* @author GautamMKGarg
* @version 1.0.0
* @since 1.0.0
*/
class PaymentRedirectController {
/**
* Plugin object.
*
* @var Plugin
*/
private $plugin;

/**
* Construct Payment Redirect controller.
*
* @param Plugin $plugin Plugin object.
*/
public function __construct( Plugin $plugin ) {
$this->plugin = $plugin;
}

/**
* Setup.
*
* @return void
*/
public function setup() {
\add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );

add_action( 'init', function() {
/**
* ([^/]*) - taxonomy
* ([^/]*) - term
* ([0-9]{4}) - year 2021
* ([0-9]{2}) - month 11
*
*/
add_rewrite_rule(
'knit-pay/payments/(\d+)/redirect/(\w+)',
'index.php?payment_redirect=$matches[1]&key=$matches[2]',
'top'
);
} );

add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'payment_redirect';
$query_vars[] = 'key';
return $query_vars;
} );

add_filter( 'template_include', function( $template ) {return $template;
if ( get_query_var( 'payment_redirect' ) == false || get_query_var( 'payment_redirect' ) == '' ) {

Check warning on line 73 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed

Check failure on line 73 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Unreachable statement - code above always terminates.
return $template;

Check warning on line 74 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed
}

// Get payment.
$payment_id = get_query_var( 'payment_redirect' );

Check warning on line 78 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed

$payment = get_pronamic_payment( $payment_id );

Check warning on line 80 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed

if ( null === $payment ) {

Check warning on line 82 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed
return;

Check warning on line 83 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed
}

// Validate key.
$key = \sanitize_text_field( \wp_unslash( get_query_var( 'key' ) ) );

Check warning on line 87 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed

if ( $key !== $payment->key || empty( $payment->key ) ) {

Check warning on line 89 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed
return;

Check warning on line 90 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed
}

// phpcs:enable WordPress.Security.NonceVerification.Recommended

Core_Util::no_cache();

Check warning on line 95 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Code after the RETURN statement on line 72 cannot be executed

$gateway = $payment->get_gateway();

if ( null !== $gateway ) {
// Give gateway a chance to handle redirect.
$gateway->payment_redirect( $payment );

// Handle HTML form redirect.
if ( $gateway->is_html_form() ) {
$gateway->redirect( $payment );
}
}

// Redirect to payment action URL.
$action_url = $payment->get_action_url();

if ( ! empty( $action_url ) ) {
wp_redirect( $action_url );

exit;
}

} );
}

/**
* REST API init.
*
* @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
* @link https://developer.wordpress.org/reference/hooks/rest_api_init/
* @return void
*/
public function rest_api_init() {
\register_rest_route(
$this->plugin->rest_base . '/v1',
'payments/(?P<payment_id>\d+)/redirect/(?P<key>\w+)',
[
/**
* IPN and PDT variables.
*
* @link https://developer.paypal.com/docs/api-basics/notifications/ipn/IPNandPDTVariables/
*/
'args' => [
'hash' => [
'description' => \__( 'Hash.', 'pronamic-ideal' ),
'type' => 'string',
],
'payment_id' => [
'description' => \__( 'Payment ID.', 'pronamic-ideal' ),
'type' => 'string',
],
],
'methods' => 'GET',
'callback' => [ $this, 'rest_api_payment_redirect' ],
'permission_callback' => '__return_true',
]
);
}

/**
* REST API PayPal cancel return permission handler.
*
* @param WP_REST_Request $request Request.
* @return bool
*/
public function rest_api_paypal_cancel_return_permission( WP_REST_Request $request ) {
$payment_id = $request->get_param( 'payment_id' );

if ( empty( $payment_id ) ) {
return false;
}

$hash = $request->get_param( 'hash' );

if ( empty( $hash ) ) {
return false;
}

return \wp_hash( $payment_id ) === $hash;
}

/**
* REST API PayPal cancel return handler.
*
* @param WP_REST_Request $request Request.
* @return object
*/
public function rest_api_payment_redirect( WP_REST_Request $request ) {
$payment_id = $request->get_param( 'payment_id' );

$payment = \get_pronamic_payment( $payment_id );

if ( null === $payment ) {
return new \WP_Error(
'rest_paypal_no_payment',
\sprintf(
/* translators: %s: Value of PayPayl `custom` parameter. */
\__( 'No payment found by `payment_id` variable: %s.', 'pronamic-ideal' ),
(string) $payment_id
),
[
'status' => 404,
]
);
}

// Validate key.
$key = $request->get_param( 'key' );

if ( $key !== $payment->key || empty( $payment->key ) ) {
return;

Check failure on line 206 in src/PaymentRedirectController.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Method Pronamic\WordPress\Pay\PaymentRedirectController::rest_api_payment_redirect() should return object but empty return statement found.
}

// phpcs:enable WordPress.Security.NonceVerification.Recommended

Core_Util::no_cache();

$gateway = $payment->get_gateway();

if ( null !== $gateway ) {
// Give gateway a chance to handle redirect.
$gateway->payment_redirect( $payment );

// Handle HTML form redirect.
if ( $gateway->is_html_form() ) {
$gateway->redirect( $payment );
}
}

// Redirect to payment action URL.
$action_url = $payment->get_action_url();

if ( ! empty( $action_url ) ) {
wp_redirect( $action_url );

exit;
}

/**
* 303 See Other.
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303
*/
return new \WP_REST_Response( null, 303, [ 'Location' => $payment->get_return_redirect_url() ] );
}
}
14 changes: 14 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ class Plugin {
public static $dirname;

/**
* Rest Base for Rest APIs.

Check failure on line 65 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
*

Check failure on line 66 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
* @var string

Check failure on line 67 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
*/

Check failure on line 68 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
public $rest_base;

Check failure on line 69 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 70 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
/**

Check failure on line 71 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
* The timezone
*
* @var string
Expand Down Expand Up @@ -257,6 +264,9 @@ public function __construct( $args = [] ) {
self::$file = $args['file'];
self::$dirname = dirname( self::$file );

// Rest API base.

Check failure on line 267 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed
$this->rest_base = array_key_exists('rest_base', $args)?$args['rest_base']:'pronamic-pay';

Check failure on line 268 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 268 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / phpcs / phpcs

Expected 1 spaces after opening parenthesis; 0 found

// Options.
$this->options = $args['options'];

Expand Down Expand Up @@ -293,6 +303,10 @@ public function __construct( $args = [] ) {

require_once $args['action_scheduler'];

// Notifications controller.
$payment_redirect_controller = new PaymentRedirectController($this);
$payment_redirect_controller->setup();

/**
* Payment methods.
*/
Expand Down

0 comments on commit ce19a4a

Please sign in to comment.