← All changes
|
core/module/stripe-addon/handler/stripe_handler.php
+84
-94
1.0.1
→
1.0.28
View file →
| @@ -1,95 +1,85 @@ | ||
| 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 | - $response = wp_remote_get( STRIPE_CONNECT_DOMAIN . '/wp-json/stripe/secret-key?type=' . Stripe_Payment_Method::account_type() ); | |
| 27 | - if ( is_wp_error( $response ) ) { | |
| 28 | - return ''; | |
| 29 | - } | |
| 30 | - | |
| 31 | - return json_decode( wp_remote_retrieve_body( $response ) ); | |
| 32 | - } | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Get the Stripe Publishable key. | |
| 36 | - * | |
| 37 | - * @return string The Stripe publishable key | |
| 38 | - */ | |
| 39 | - public static function get_stripe_publishable_key() { | |
| 40 | - $response = wp_remote_get( STRIPE_CONNECT_DOMAIN . '/wp-json/stripe/publishable-key?type=' . Stripe_Payment_Method::account_type() ); | |
| 41 | - if ( is_wp_error( $response ) ) { | |
| 42 | - return ''; | |
| 43 | - } | |
| 44 | - | |
| 45 | - return json_decode( wp_remote_retrieve_body( $response ) ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Handle the OAuth callback from Stripe. | |
| 50 | - * | |
| 51 | - * @param array $request The request data | |
| 52 | - * | |
| 53 | - * @return \WP_REST_Response The response object | |
| 54 | - */ | |
| 55 | - public static function handle_callback( $request ) { | |
| 56 | - if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'booktics_stripe_callback' ) ) { | |
| 57 | - return rest_ensure_response( [ | |
| 58 | - 'status' => 'error', | |
| 59 | - 'message' => __( 'Invalid security token', 'booktics' ) | |
| 60 | - ] ); | |
| 61 | - } | |
| 62 | - | |
| 63 | - $account_id = isset( $_GET['account_id'] ) ? sanitize_text_field( wp_unslash( $_GET['account_id'] ) ) : ''; | |
| 64 | - $type = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : 'test'; | |
| 65 | - | |
| 66 | - if ( ! $account_id ) { | |
| 67 | - return rest_ensure_response( [ | |
| 68 | - 'status' => 'error', | |
| 69 | - 'message' => __( 'Missing account_id parameter', 'booktics' ) | |
| 70 | - ] ); | |
| 71 | - } | |
| 72 | - | |
| 73 | - if ( ! empty( $account_id ) ) { | |
| 74 | - Settings::update( 'stripe_account_id', $account_id ); | |
| 75 | - Settings::update( 'stripe_account_type', $type ); | |
| 76 | - | |
| 77 | - wp_redirect( admin_url( 'admin.php?page=booktics#/settings?tab=payment' ) ); | |
| 78 | - exit; | |
| 79 | - } | |
| 80 | - | |
| 81 | - return rest_ensure_response( [ | |
| 82 | - 'status' => 'error', | |
| 83 | - 'message' => __( 'Stripe connection failed', 'booktics' ) | |
| 84 | - ] ); | |
| 85 | - } | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * Get the Stripe Connect authorization URL. | |
| 89 | - * | |
| 90 | - * @return string The authorization URL | |
| 91 | - */ | |
| 92 | - public function get_connect_url( $type = 'test' ) { | |
| 93 | - return STRIPE_CONNECT_DOMAIN . '/stripe/connect?type=' . $type . '&redirect_uri=' . base64_encode( $this->get_redirect_uri( $type ) ); | |
| 94 | - } | |
| 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 | + } | |
| 95 | 85 | } |