|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Stripe Apple Pay Registration Class. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 4.0.6 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
9
|
|
|
exit; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
class WC_Stripe_Apple_Pay_Registration { |
|
13
|
|
|
/** |
|
14
|
|
|
* Enabled. |
|
15
|
|
|
* |
|
16
|
|
|
* @var |
|
17
|
|
|
*/ |
|
18
|
|
|
public $stripe_settings; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Apple Pay Domain Set. |
|
22
|
|
|
* |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
public $apple_pay_domain_set; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Stores Apple Pay domain verification issues. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
public $apple_pay_verify_notice; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct() { |
|
35
|
|
|
add_action( 'init', array( $this, 'add_domain_association_rewrite_rule' ) ); |
|
36
|
|
|
add_filter( 'query_vars', array( $this, 'whitelist_domain_association_query_param' ), 10, 1 ); |
|
37
|
|
|
add_action( 'parse_request', array( $this, 'parse_domain_association_request' ), 10, 1 ); |
|
38
|
|
|
|
|
39
|
|
|
add_action( 'woocommerce_stripe_updated', array( $this, 'verify_domain_if_configured' ) ); |
|
40
|
|
|
add_action( 'add_option_woocommerce_stripe_settings', array( $this, 'verify_domain_on_new_settings' ), 10, 2 ); |
|
41
|
|
|
add_action( 'update_option_woocommerce_stripe_settings', array( $this, 'verify_domain_on_updated_settings' ), 10, 2 ); |
|
42
|
|
|
add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
|
43
|
|
|
|
|
44
|
|
|
$this->stripe_settings = get_option( 'woocommerce_stripe_settings', array() ); |
|
45
|
|
|
$this->apple_pay_domain_set = 'yes' === $this->get_option( 'apple_pay_domain_set', 'no' ); |
|
46
|
|
|
$this->apple_pay_verify_notice = ''; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Gets the Stripe settings. |
|
51
|
|
|
* |
|
52
|
|
|
* @since 4.0.6 |
|
53
|
|
|
* @param string $setting |
|
54
|
|
|
* @param string default |
|
55
|
|
|
* @return string $setting_value |
|
56
|
|
|
*/ |
|
57
|
|
|
public function get_option( $setting = '', $default = '' ) { |
|
58
|
|
|
if ( empty( $this->stripe_settings ) ) { |
|
59
|
|
|
return $default; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ( ! empty( $this->stripe_settings[ $setting ] ) ) { |
|
63
|
|
|
return $this->stripe_settings[ $setting ]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $default; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Whether the gateway and Payment Request Button (prerequisites for Apple Pay) are enabled. |
|
71
|
|
|
* |
|
72
|
|
|
* @since 4.5.4 |
|
73
|
|
|
* @return string Whether Apple Pay required settings are enabled. |
|
74
|
|
|
*/ |
|
75
|
|
|
private function is_enabled() { |
|
76
|
|
|
$stripe_enabled = 'yes' === $this->get_option( 'enabled', 'no' ); |
|
77
|
|
|
$payment_request_button_enabled = 'yes' === $this->get_option( 'payment_request', 'yes' ); |
|
78
|
|
|
|
|
79
|
|
|
return $stripe_enabled && $payment_request_button_enabled; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Gets the Stripe secret key for the current mode. |
|
84
|
|
|
* |
|
85
|
|
|
* @since 4.5.3 |
|
86
|
|
|
* @return string Secret key. |
|
87
|
|
|
*/ |
|
88
|
|
|
private function get_secret_key() { |
|
89
|
|
|
$testmode = 'yes' === $this->get_option( 'testmode', 'no' ); |
|
90
|
|
|
return $testmode ? $this->get_option( 'test_secret_key' ) : $this->get_option( 'secret_key' ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Adds a rewrite rule for serving the domain association file from the proper location. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function add_domain_association_rewrite_rule() { |
|
97
|
|
|
$regex = '^\.well-known\/apple-developer-merchantid-domain-association$'; |
|
98
|
|
|
$redirect = 'index.php?apple-developer-merchantid-domain-association=1'; |
|
99
|
|
|
|
|
100
|
|
|
add_rewrite_rule( $regex, $redirect, 'top' ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add to the list of publicly allowed query variables. |
|
105
|
|
|
* |
|
106
|
|
|
* @param array $query_vars - provided public query vars. |
|
107
|
|
|
* @return array Updated public query vars. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function whitelist_domain_association_query_param( $query_vars ) { |
|
110
|
|
|
$query_vars[] = 'apple-developer-merchantid-domain-association'; |
|
111
|
|
|
return $query_vars; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Serve domain association file when proper query param is provided. |
|
116
|
|
|
* |
|
117
|
|
|
* @param WP WordPress environment object. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function parse_domain_association_request( $wp ) { |
|
120
|
|
|
if ( |
|
121
|
|
|
! isset( $wp->query_vars['apple-developer-merchantid-domain-association'] ) || |
|
122
|
|
|
'1' !== $wp->query_vars['apple-developer-merchantid-domain-association'] |
|
123
|
|
|
) { |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$path = WC_STRIPE_PLUGIN_PATH . '/apple-developer-merchantid-domain-association'; |
|
128
|
|
|
header( 'Content-Type: application/octet-stream' ); |
|
129
|
|
|
echo esc_html( file_get_contents( $path ) ); |
|
130
|
|
|
exit; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Makes request to register the domain with Stripe/Apple Pay. |
|
135
|
|
|
* |
|
136
|
|
|
* @since 3.1.0 |
|
137
|
|
|
* @version 4.5.4 |
|
138
|
|
|
* @param string $secret_key |
|
139
|
|
|
*/ |
|
140
|
|
|
private function make_domain_registration_request( $secret_key ) { |
|
141
|
|
|
if ( empty( $secret_key ) ) { |
|
142
|
|
|
throw new Exception( __( 'Unable to verify domain - missing secret key.', 'woocommerce-gateway-stripe' ) ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$endpoint = 'https://api.stripe.com/v1/apple_pay/domains'; |
|
146
|
|
|
|
|
147
|
|
|
$data = array( |
|
148
|
|
|
'domain_name' => $_SERVER['HTTP_HOST'], |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
$headers = array( |
|
152
|
|
|
'User-Agent' => 'WooCommerce Stripe Apple Pay', |
|
153
|
|
|
'Authorization' => 'Bearer ' . $secret_key, |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$response = wp_remote_post( |
|
157
|
|
|
$endpoint, |
|
158
|
|
|
array( |
|
159
|
|
|
'headers' => $headers, |
|
160
|
|
|
'body' => http_build_query( $data ), |
|
161
|
|
|
) |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
if ( is_wp_error( $response ) ) { |
|
165
|
|
|
/* translators: error message */ |
|
166
|
|
|
throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $response->get_error_message() ) ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if ( 200 !== $response['response']['code'] ) { |
|
170
|
|
|
$parsed_response = json_decode( $response['body'] ); |
|
171
|
|
|
|
|
172
|
|
|
$this->apple_pay_verify_notice = $parsed_response->error->message; |
|
173
|
|
|
|
|
174
|
|
|
/* translators: error message */ |
|
175
|
|
|
throw new Exception( sprintf( __( 'Unable to verify domain - %s', 'woocommerce-gateway-stripe' ), $parsed_response->error->message ) ); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Processes the Apple Pay domain verification. |
|
181
|
|
|
* |
|
182
|
|
|
* @since 3.1.0 |
|
183
|
|
|
* @version 4.5.4 |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $secret_key |
|
186
|
|
|
*/ |
|
187
|
|
|
public function register_domain_with_apple( $secret_key ) { |
|
188
|
|
|
try { |
|
189
|
|
|
$this->make_domain_registration_request( $secret_key ); |
|
190
|
|
|
|
|
191
|
|
|
// No errors to this point, verification success! |
|
192
|
|
|
$this->stripe_settings['apple_pay_domain_set'] = 'yes'; |
|
193
|
|
|
$this->apple_pay_domain_set = true; |
|
194
|
|
|
|
|
195
|
|
|
update_option( 'woocommerce_stripe_settings', $this->stripe_settings ); |
|
196
|
|
|
|
|
197
|
|
|
WC_Stripe_Logger::log( 'Your domain has been verified with Apple Pay!' ); |
|
198
|
|
|
|
|
199
|
|
|
} catch ( Exception $e ) { |
|
200
|
|
|
$this->stripe_settings['apple_pay_domain_set'] = 'no'; |
|
201
|
|
|
$this->apple_pay_domain_set = false; |
|
202
|
|
|
|
|
203
|
|
|
update_option( 'woocommerce_stripe_settings', $this->stripe_settings ); |
|
204
|
|
|
|
|
205
|
|
|
WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Process the Apple Pay domain verification if proper settings are configured. |
|
212
|
|
|
* |
|
213
|
|
|
* @since 4.5.4 |
|
214
|
|
|
* @version 4.5.4 |
|
215
|
|
|
*/ |
|
216
|
|
|
public function verify_domain_if_configured() { |
|
217
|
|
|
$secret_key = $this->get_secret_key(); |
|
218
|
|
|
|
|
219
|
|
|
if ( ! $this->is_enabled() || empty( $secret_key ) ) { |
|
220
|
|
|
return; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
// Ensure that domain association file will be served. |
|
224
|
|
|
flush_rewrite_rules(); |
|
225
|
|
|
|
|
226
|
|
|
// Register the domain with Apple Pay. |
|
227
|
|
|
$this->register_domain_with_apple( $secret_key ); |
|
228
|
|
|
|
|
229
|
|
|
// Show/hide failure note if necessary. |
|
230
|
|
|
WC_Stripe_Inbox_Notes::notify_of_apple_pay_domain_verification_if_needed(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Conditionally process the Apple Pay domain verification after settings are initially set. |
|
235
|
|
|
* |
|
236
|
|
|
* @since 4.5.4 |
|
237
|
|
|
* @version 4.5.4 |
|
238
|
|
|
*/ |
|
239
|
|
|
public function verify_domain_on_new_settings( $option, $settings ) { |
|
240
|
|
|
$this->verify_domain_on_updated_settings( array(), $settings ); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Conditionally process the Apple Pay domain verification after settings are updated. |
|
245
|
|
|
* |
|
246
|
|
|
* @since 4.5.3 |
|
247
|
|
|
* @version 4.5.4 |
|
248
|
|
|
*/ |
|
249
|
|
|
public function verify_domain_on_updated_settings( $prev_settings, $settings ) { |
|
250
|
|
|
// Grab previous state and then update cached settings. |
|
251
|
|
|
$this->stripe_settings = $prev_settings; |
|
252
|
|
|
$prev_secret_key = $this->get_secret_key(); |
|
253
|
|
|
$prev_is_enabled = $this->is_enabled(); |
|
254
|
|
|
$this->stripe_settings = $settings; |
|
255
|
|
|
|
|
256
|
|
|
// If Stripe or Payment Request Button wasn't enabled (or secret key was different) then might need to verify now. |
|
257
|
|
|
if ( ! $prev_is_enabled || ( $this->get_secret_key() !== $prev_secret_key ) ) { |
|
258
|
|
|
$this->verify_domain_if_configured(); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Display any admin notices to the user. |
|
264
|
|
|
* |
|
265
|
|
|
* @since 4.0.6 |
|
266
|
|
|
*/ |
|
267
|
|
|
public function admin_notices() { |
|
268
|
|
|
if ( ! $this->is_enabled() ) { |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
if ( ! current_user_can( 'manage_woocommerce' ) ) { |
|
273
|
|
|
return; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$empty_notice = empty( $this->apple_pay_verify_notice ); |
|
277
|
|
|
if ( $empty_notice && ( $this->apple_pay_domain_set || empty( $this->secret_key ) ) ) { |
|
|
|
|
|
|
278
|
|
|
return; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Apple pay is enabled by default and domain verification initializes |
|
283
|
|
|
* when setting screen is displayed. So if domain verification is not set, |
|
284
|
|
|
* something went wrong so lets notify user. |
|
285
|
|
|
*/ |
|
286
|
|
|
$allowed_html = array( |
|
287
|
|
|
'a' => array( |
|
288
|
|
|
'href' => array(), |
|
289
|
|
|
'title' => array(), |
|
290
|
|
|
), |
|
291
|
|
|
); |
|
292
|
|
|
$verification_failed_without_error = __( 'Apple Pay domain verification failed.', 'woocommerce-gateway-stripe' ); |
|
293
|
|
|
$verification_failed_with_error = __( 'Apple Pay domain verification failed with the following error:', 'woocommerce-gateway-stripe' ); |
|
294
|
|
|
$check_log_text = sprintf( |
|
295
|
|
|
/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */ |
|
296
|
|
|
esc_html__( 'Please check the %1$slogs%2$s for more details on this issue. Logging must be enabled to see recorded logs.', 'woocommerce-gateway-stripe' ), |
|
297
|
|
|
'<a href="' . admin_url( 'admin.php?page=wc-status&tab=logs' ) . '">', |
|
298
|
|
|
'</a>' |
|
299
|
|
|
); |
|
300
|
|
|
|
|
301
|
|
|
?> |
|
302
|
|
|
<div class="error stripe-apple-pay-message"> |
|
303
|
|
|
<?php if ( $empty_notice ) : ?> |
|
304
|
|
|
<p><?php echo esc_html( $verification_failed_without_error ); ?></p> |
|
305
|
|
|
<?php else : ?> |
|
306
|
|
|
<p><?php echo esc_html( $verification_failed_with_error ); ?></p> |
|
307
|
|
|
<p><i><?php echo wp_kses( make_clickable( esc_html( $this->apple_pay_verify_notice ) ), $allowed_html ); ?></i></p> |
|
308
|
|
|
<?php endif; ?> |
|
309
|
|
|
<p><?php echo $check_log_text; ?></p> |
|
310
|
|
|
</div> |
|
311
|
|
|
<?php |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
new WC_Stripe_Apple_Pay_Registration(); |
|
316
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: