PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce-gateway-stripe/includes/admin/class-wc-rest-stripe-account-keys-controller.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 232 lines | 159 code | 21 blank | 52 comment | 14 complexity | 1b394a8847cec76fbbc303c9a4669562 MD5 | raw file
  1. <?php
  2. /**
  3. * Class WC_REST_Stripe_Account_Keys_Controller
  4. */
  5. defined( 'ABSPATH' ) || exit;
  6. /**
  7. * REST controller for saving Stripe's test/live account keys.
  8. *
  9. * This includes Live Publishable Key, Live Secret Key, Webhook Secret.
  10. *
  11. * @since 5.6.0
  12. */
  13. class WC_REST_Stripe_Account_Keys_Controller extends WC_Stripe_REST_Base_Controller {
  14. const STRIPE_GATEWAY_SETTINGS_OPTION_NAME = 'woocommerce_stripe_settings';
  15. /**
  16. * Endpoint path.
  17. *
  18. * @var string
  19. */
  20. protected $rest_base = 'wc_stripe/account_keys';
  21. /**
  22. * The instance of the Stripe account.
  23. *
  24. * @var WC_Stripe_Account
  25. */
  26. private $account;
  27. /**
  28. * Constructor.
  29. *
  30. * @param WC_Stripe_Account $account The instance of the Stripe account.
  31. */
  32. public function __construct( WC_Stripe_Account $account ) {
  33. $this->account = $account;
  34. }
  35. /**
  36. * Configure REST API routes.
  37. */
  38. public function register_routes() {
  39. register_rest_route(
  40. $this->namespace,
  41. '/' . $this->rest_base,
  42. [
  43. 'methods' => WP_REST_Server::READABLE,
  44. 'callback' => [ $this, 'get_account_keys' ],
  45. 'permission_callback' => [ $this, 'check_permission' ],
  46. ]
  47. );
  48. register_rest_route(
  49. $this->namespace,
  50. '/' . $this->rest_base,
  51. [
  52. 'methods' => WP_REST_Server::EDITABLE,
  53. 'callback' => [ $this, 'set_account_keys' ],
  54. 'permission_callback' => [ $this, 'check_permission' ],
  55. 'args' => [
  56. 'publishable_key' => [
  57. 'description' => __( 'Your Stripe API Publishable key, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  58. 'type' => 'string',
  59. 'validate_callback' => [ $this, 'validate_publishable_key' ],
  60. ],
  61. 'secret_key' => [
  62. 'description' => __( 'Your Stripe API Secret, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  63. 'type' => 'string',
  64. 'validate_callback' => [ $this, 'validate_secret_key' ],
  65. ],
  66. 'webhook_secret' => [
  67. 'description' => __( 'Your Stripe webhook endpoint URL, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  68. 'type' => 'string',
  69. 'validate_callback' => 'rest_validate_request_arg',
  70. ],
  71. 'test_publishable_key' => [
  72. 'description' => __( 'Your Stripe testing API Publishable key, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  73. 'type' => 'string',
  74. 'validate_callback' => [ $this, 'validate_test_publishable_key' ],
  75. ],
  76. 'test_secret_key' => [
  77. 'description' => __( 'Your Stripe testing API Secret, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  78. 'type' => 'string',
  79. 'validate_callback' => [ $this, 'validate_test_secret_key' ],
  80. ],
  81. 'test_webhook_secret' => [
  82. 'description' => __( 'Your Stripe testing webhook endpoint URL, obtained from your Stripe dashboard.', 'woocommerce-gateway-stripe' ),
  83. 'type' => 'string',
  84. 'validate_callback' => 'rest_validate_request_arg',
  85. ],
  86. ],
  87. ]
  88. );
  89. }
  90. /**
  91. * Retrieve flag status.
  92. *
  93. * @return WP_REST_Response
  94. */
  95. public function get_account_keys() {
  96. $allowed_params = [ 'publishable_key', 'secret_key', 'webhook_secret', 'test_publishable_key', 'test_secret_key', 'test_webhook_secret' ];
  97. $stripe_settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
  98. // Filter only the fields we want to return
  99. $account_keys = array_intersect_key( $stripe_settings, array_flip( $allowed_params ) );
  100. return new WP_REST_Response( $account_keys );
  101. }
  102. /**
  103. * Validate stripe publishable keys and secrets. Allow empty string to erase key.
  104. * Also validates against explicit key prefixes based on live/test environment.
  105. *
  106. * @param mixed $value
  107. * @param WP_REST_Request $request
  108. * @param string $param
  109. * @param array $validate_options
  110. * @return true|WP_Error
  111. */
  112. private function validate_stripe_param( $param, $request, $key, $validate_options ) {
  113. if ( empty( $param ) ) {
  114. return true;
  115. }
  116. $result = rest_validate_request_arg( $param, $request, $key );
  117. if ( ! empty( $result ) && ! preg_match( $validate_options['regex'], $param ) ) {
  118. return new WP_Error( 400, $validate_options['error_message'] );
  119. }
  120. return true;
  121. }
  122. public function validate_publishable_key( $param, $request, $key ) {
  123. return $this->validate_stripe_param(
  124. $param,
  125. $request,
  126. $key,
  127. [
  128. 'regex' => '/^pk_live_/',
  129. 'error_message' => __( 'The "Live Publishable Key" should start with "pk_live", enter the correct key.', 'woocommerce-gateway-stripe' ),
  130. ]
  131. );
  132. }
  133. public function validate_secret_key( $param, $request, $key ) {
  134. return $this->validate_stripe_param(
  135. $param,
  136. $request,
  137. $key,
  138. [
  139. 'regex' => '/^[rs]k_live_/',
  140. 'error_message' => __( 'The "Live Secret Key" should start with "sk_live" or "rk_live", enter the correct key.', 'woocommerce-gateway-stripe' ),
  141. ]
  142. );
  143. }
  144. public function validate_test_publishable_key( $param, $request, $key ) {
  145. return $this->validate_stripe_param(
  146. $param,
  147. $request,
  148. $key,
  149. [
  150. 'regex' => '/^pk_test_/',
  151. 'error_message' => __( 'The "Test Publishable Key" should start with "pk_test", enter the correct key.', 'woocommerce-gateway-stripe' ),
  152. ]
  153. );
  154. }
  155. public function validate_test_secret_key( $param, $request, $key ) {
  156. return $this->validate_stripe_param(
  157. $param,
  158. $request,
  159. $key,
  160. [
  161. 'regex' => '/^[rs]k_test_/',
  162. 'error_message' => __( 'The "Test Secret Key" should start with "sk_test" or "rk_test", enter the correct key.', 'woocommerce-gateway-stripe' ),
  163. ]
  164. );
  165. }
  166. /**
  167. * Update the data.
  168. *
  169. * @param WP_REST_Request $request Full data about the request.
  170. */
  171. public function set_account_keys( WP_REST_Request $request ) {
  172. $publishable_key = $request->get_param( 'publishable_key' );
  173. $secret_key = $request->get_param( 'secret_key' );
  174. $webhook_secret = $request->get_param( 'webhook_secret' );
  175. $test_publishable_key = $request->get_param( 'test_publishable_key' );
  176. $test_secret_key = $request->get_param( 'test_secret_key' );
  177. $test_webhook_secret = $request->get_param( 'test_webhook_secret' );
  178. $settings = get_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, [] );
  179. // If all keys were empty, then is a new account; we need to set the test/live mode.
  180. $new_account = ! trim( $settings['publishable_key'] )
  181. && ! trim( $settings['secret_key'] )
  182. && ! trim( $settings['test_publishable_key'] )
  183. && ! trim( $settings['test_secret_key'] );
  184. // If all new keys are empty, then account is being disconnected. We should disable the payment gateway.
  185. $is_deleting_account = ! trim( $publishable_key )
  186. && ! trim( $secret_key )
  187. && ! trim( $test_publishable_key )
  188. && ! trim( $test_secret_key );
  189. $settings['publishable_key'] = is_null( $publishable_key ) ? $settings['publishable_key'] : $publishable_key;
  190. $settings['secret_key'] = is_null( $secret_key ) ? $settings['secret_key'] : $secret_key;
  191. $settings['webhook_secret'] = is_null( $webhook_secret ) ? $settings['webhook_secret'] : $webhook_secret;
  192. $settings['test_publishable_key'] = is_null( $test_publishable_key ) ? $settings['test_publishable_key'] : $test_publishable_key;
  193. $settings['test_secret_key'] = is_null( $test_secret_key ) ? $settings['test_secret_key'] : $test_secret_key;
  194. $settings['test_webhook_secret'] = is_null( $test_webhook_secret ) ? $settings['test_webhook_secret'] : $test_webhook_secret;
  195. if ( $new_account ) {
  196. $settings['enabled'] = 'yes';
  197. if ( trim( $settings['publishable_key'] ) && trim( $settings['secret_key'] ) ) {
  198. $settings['testmode'] = 'no';
  199. } elseif ( trim( $settings['test_publishable_key'] ) && trim( $settings['test_secret_key'] ) ) {
  200. $settings['testmode'] = 'yes';
  201. }
  202. } elseif ( $is_deleting_account ) {
  203. $settings['enabled'] = 'no';
  204. }
  205. update_option( self::STRIPE_GATEWAY_SETTINGS_OPTION_NAME, $settings );
  206. $this->account->clear_cache();
  207. // Gives an instant reply if the connection was succesful or not + rebuild the cache for the next request
  208. $account = $this->account->get_cached_account_data();
  209. return new WP_REST_Response( $account, 200 );
  210. }
  211. }