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