| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Module\Stripe_Addon\Handler; |
| 4 |
|
| 5 |
use Booktics\Module\Stripe_Addon\Service\Stripe_Payment_Method; |
| 6 |
use Booktics\Settings\Settings; |
| 7 |
|
| 8 |
class Stripe_Handler { |
| 9 |
/** |
| 10 |
* Get the redirect URI for Stripe OAuth callback. |
| 11 |
* |
| 12 |
* @return string The redirect URI |
| 13 |
*/ |
| 14 |
private function get_redirect_uri( $type = 'test' ) { |
| 15 |
$nonce = wp_create_nonce( 'booktics_stripe_callback' ); |
| 16 |
|
| 17 |
return admin_url( 'admin-ajax.php?action=booktics_stripe_callback&type=' . $type . '&_wpnonce=' . $nonce ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get the Stripe secret key. |
| 22 |
* |
| 23 |
* @return string The Stripe secret key |
| 24 |
*/ |
| 25 |
public static function get_stripe_secret_key() { |
| 26 |
return booktics_get_option('stripe_secret_key', ''); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get the Stripe Publishable key. |
| 31 |
* |
| 32 |
* @return string The Stripe publishable key |
| 33 |
*/ |
| 34 |
public static function get_stripe_publishable_key() { |
| 35 |
return booktics_get_option('stripe_publishable_key', ''); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Handle the OAuth callback from Stripe. |
| 40 |
* |
| 41 |
* @param array $request The request data |
| 42 |
* |
| 43 |
* @return \WP_REST_Response The response object |
| 44 |
*/ |
| 45 |
public static function handle_callback( $request ) { |
| 46 |
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'booktics_stripe_callback' ) ) { |
| 47 |
return rest_ensure_response( [ |
| 48 |
'status' => 'error', |
| 49 |
'message' => __( 'Invalid security token', 'booktics' ) |
| 50 |
] ); |
| 51 |
} |
| 52 |
|
| 53 |
$account_id = isset( $_GET['account_id'] ) ? sanitize_text_field( wp_unslash( $_GET['account_id'] ) ) : ''; |
| 54 |
$type = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : 'test'; |
| 55 |
|
| 56 |
if ( ! $account_id ) { |
| 57 |
return rest_ensure_response( [ |
| 58 |
'status' => 'error', |
| 59 |
'message' => __( 'Missing account_id parameter', 'booktics' ) |
| 60 |
] ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! empty( $account_id ) ) { |
| 64 |
Settings::update( 'stripe_account_id', $account_id ); |
| 65 |
Settings::update( 'stripe_account_type', $type ); |
| 66 |
|
| 67 |
wp_safe_redirect( admin_url( 'admin.php?page=booktics#/settings?tab=payment' ) ); |
| 68 |
exit; |
| 69 |
} |
| 70 |
|
| 71 |
return rest_ensure_response( [ |
| 72 |
'status' => 'error', |
| 73 |
'message' => __( 'Stripe connection failed', 'booktics' ) |
| 74 |
] ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the Stripe Connect authorization URL. |
| 79 |
* |
| 80 |
* @return string The authorization URL |
| 81 |
*/ |
| 82 |
public function get_connect_url( $type = 'test' ) { |
| 83 |
return BOOKTICS_STRIPE_CONNECT_DOMAIN . '/stripe/connect?type=' . $type . '&redirect_uri=' . base64_encode( $this->get_redirect_uri( $type ) ); |
| 84 |
} |
| 85 |
} |