class-wc-stripe-api.php
7.62 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Stripe_API class.
*
* Communicates with Stripe API.
*/
class WC_Stripe_API {
/**
* Stripe API Endpoint
*/
const ENDPOINT = 'https://api.stripe.com/v1/';
const STRIPE_API_VERSION = '2019-09-09';
/**
* Secret API Key.
*
* @var string
*/
private static $secret_key = '';
/**
* Set secret API Key.
*
* @param string $key
*/
public static function set_secret_key( $secret_key ) {
self::$secret_key = $secret_key;
}
/**
* Get secret key.
*
* @return string
*/
public static function get_secret_key() {
if ( ! self::$secret_key ) {
$options = get_option( 'woocommerce_stripe_settings' );
$secret_key = $options['secret_key'] ?? '';
$test_secret_key = $options['test_secret_key'] ?? '';
if ( isset( $options['testmode'] ) ) {
self::set_secret_key( 'yes' === $options['testmode'] ? $test_secret_key : $secret_key );
}
}
return self::$secret_key;
}
/**
* Generates the user agent we use to pass to API request so
* Stripe can identify our application.
*
* @since 4.0.0
* @version 4.0.0
*/
public static function get_user_agent() {
$app_info = [
'name' => 'WooCommerce Stripe Gateway',
'version' => WC_STRIPE_VERSION,
'url' => 'https://woocommerce.com/products/stripe/',
'partner_id' => 'pp_partner_EYuSt9peR0WTMg',
];
return [
'lang' => 'php',
'lang_version' => phpversion(),
'publisher' => 'woocommerce',
'uname' => function_exists( 'php_uname' ) ? php_uname() : PHP_OS,
'application' => $app_info,
];
}
/**
* Generates the headers to pass to API request.
*
* @since 4.0.0
* @version 4.0.0
*/
public static function get_headers() {
$user_agent = self::get_user_agent();
$app_info = $user_agent['application'];
$headers = apply_filters(
'woocommerce_stripe_request_headers',
[
'Authorization' => 'Basic ' . base64_encode( self::get_secret_key() . ':' ),
'Stripe-Version' => self::STRIPE_API_VERSION,
]
);
// These headers should not be overridden for this gateway.
$headers['User-Agent'] = $app_info['name'] . '/' . $app_info['version'] . ' (' . $app_info['url'] . ')';
$headers['X-Stripe-Client-User-Agent'] = wp_json_encode( $user_agent );
return $headers;
}
/**
* Send the request to Stripe's API
*
* @since 3.1.0
* @version 4.0.6
* @param array $request
* @param string $api
* @param string $method
* @param bool $with_headers To get the response with headers.
* @return stdClass|array
* @throws WC_Stripe_Exception
*/
public static function request( $request, $api = 'charges', $method = 'POST', $with_headers = false ) {
WC_Stripe_Logger::log( "{$api} request: " . print_r( $request, true ) );
$headers = self::get_headers();
$idempotency_key = '';
if ( 'charges' === $api && 'POST' === $method ) {
$customer = ! empty( $request['customer'] ) ? $request['customer'] : '';
$source = ! empty( $request['source'] ) ? $request['source'] : $customer;
$idempotency_key = apply_filters( 'wc_stripe_idempotency_key', $request['metadata']['order_id'] . '-' . $source, $request );
$headers['Idempotency-Key'] = $idempotency_key;
}
$response = wp_safe_remote_post(
self::ENDPOINT . $api,
[
'method' => $method,
'headers' => $headers,
'body' => apply_filters( 'woocommerce_stripe_request_body', $request, $api ),
'timeout' => 70,
]
);
if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
WC_Stripe_Logger::log(
'Error Response: ' . print_r( $response, true ) . PHP_EOL . PHP_EOL . 'Failed request: ' . print_r(
[
'api' => $api,
'request' => $request,
'idempotency_key' => $idempotency_key,
],
true
)
);
throw new WC_Stripe_Exception( print_r( $response, true ), __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
}
if ( $with_headers ) {
return [
'headers' => wp_remote_retrieve_headers( $response ),
'body' => json_decode( $response['body'] ),
];
}
return json_decode( $response['body'] );
}
/**
* Retrieve API endpoint.
*
* @since 4.0.0
* @version 4.0.0
* @param string $api
*/
public static function retrieve( $api ) {
WC_Stripe_Logger::log( "{$api}" );
$response = wp_safe_remote_get(
self::ENDPOINT . $api,
[
'method' => 'GET',
'headers' => self::get_headers(),
'timeout' => 70,
]
);
if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) );
return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) );
}
return json_decode( $response['body'] );
}
/**
* Send the request to Stripe's API with level 3 data generated
* from the order. If the request fails due to an error related
* to level3 data, make the request again without it to allow
* the payment to go through.
*
* @since 4.3.2
* @version 5.1.0
*
* @param array $request Array with request parameters.
* @param string $api The API path for the request.
* @param array $level3_data The level 3 data for this request.
* @param WC_Order $order The order associated with the payment.
*
* @return stdClass|array The response
*/
public static function request_with_level3_data( $request, $api, $level3_data, $order ) {
// 1. Do not add level3 data if the array is empty.
// 2. Do not add level3 data if there's a transient indicating that level3 was
// not accepted by Stripe in the past for this account.
// 3. Do not try to add level3 data if merchant is not based in the US.
// https://stripe.com/docs/level3#level-iii-usage-requirements
// (Needs to be authenticated with a level3 gated account to see above docs).
if (
empty( $level3_data ) ||
get_transient( 'wc_stripe_level3_not_allowed' ) ||
'US' !== WC()->countries->get_base_country()
) {
return self::request(
$request,
$api
);
}
// Add level 3 data to the request.
$request['level3'] = $level3_data;
$result = self::request(
$request,
$api
);
$is_level3_param_not_allowed = (
isset( $result->error )
&& isset( $result->error->code )
&& 'parameter_unknown' === $result->error->code
&& isset( $result->error->param )
&& 'level3' === $result->error->param
);
$is_level_3data_incorrect = (
isset( $result->error )
&& isset( $result->error->type )
&& 'invalid_request_error' === $result->error->type
);
if ( $is_level3_param_not_allowed ) {
// Set a transient so that future requests do not add level 3 data.
// Transient is set to expire in 3 months, can be manually removed if needed.
set_transient( 'wc_stripe_level3_not_allowed', true, 3 * MONTH_IN_SECONDS );
} elseif ( $is_level_3data_incorrect ) {
// Log the issue so we could debug it.
WC_Stripe_Logger::log(
'Level3 data sum incorrect: ' . PHP_EOL
. print_r( $result->error->message, true ) . PHP_EOL
. print_r( 'Order line items: ', true ) . PHP_EOL
. print_r( $order->get_items(), true ) . PHP_EOL
. print_r( 'Order shipping amount: ', true ) . PHP_EOL
. print_r( $order->get_shipping_total(), true ) . PHP_EOL
. print_r( 'Order currency: ', true ) . PHP_EOL
. print_r( $order->get_currency(), true )
);
}
// Make the request again without level 3 data.
if ( $is_level3_param_not_allowed || $is_level_3data_incorrect ) {
unset( $request['level3'] );
return self::request(
$request,
$api
);
}
return $result;
}
}