1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* WC_Stripe_API class. |
8
|
|
|
* |
9
|
|
|
* Communicates with Stripe API. |
10
|
|
|
*/ |
11
|
|
|
class WC_Stripe_API { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Stripe API Endpoint |
15
|
|
|
*/ |
16
|
|
|
const ENDPOINT = 'https://api.stripe.com/v1/'; |
17
|
|
|
const STRIPE_API_VERSION = '2018-01-23'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Secret API Key. |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $secret_key = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set secret API Key. |
27
|
|
|
* @param string $key |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public static function set_secret_key( $secret_key ) { |
30
|
|
|
self::$secret_key = $secret_key; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get secret key. |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function get_secret_key() { |
38
|
|
|
if ( ! self::$secret_key ) { |
39
|
|
|
$options = get_option( 'woocommerce_stripe_settings' ); |
40
|
|
|
|
41
|
|
|
if ( isset( $options['testmode'], $options['secret_key'], $options['test_secret_key'] ) ) { |
42
|
|
|
self::set_secret_key( 'yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key'] ); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
return self::$secret_key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generates the user agent we use to pass to API request so |
50
|
|
|
* Stripe can identify our application. |
51
|
|
|
* |
52
|
|
|
* @since 4.0.0 |
53
|
|
|
* @version 4.0.0 |
54
|
|
|
*/ |
55
|
|
|
public static function get_user_agent() { |
56
|
|
|
$app_info = array( |
57
|
|
|
'name' => 'WooCommerce Stripe Gateway', |
58
|
|
|
'version' => WC_STRIPE_VERSION, |
59
|
|
|
'url' => 'https://woocommerce.com/products/stripe/', |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return array( |
63
|
|
|
'lang' => 'php', |
64
|
|
|
'lang_version' => phpversion(), |
65
|
|
|
'publisher' => 'woocommerce', |
66
|
|
|
'uname' => php_uname(), |
67
|
|
|
'application' => $app_info, |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generates the headers to pass to API request. |
73
|
|
|
* |
74
|
|
|
* @since 4.0.0 |
75
|
|
|
* @version 4.0.0 |
76
|
|
|
*/ |
77
|
|
|
public static function get_headers() { |
78
|
|
|
$user_agent = self::get_user_agent(); |
79
|
|
|
$app_info = $user_agent['application']; |
80
|
|
|
|
81
|
|
|
return apply_filters( 'woocommerce_stripe_request_headers', array( |
82
|
|
|
'Authorization' => 'Basic ' . base64_encode( self::get_secret_key() . ':' ), |
83
|
|
|
'Stripe-Version' => self::STRIPE_API_VERSION, |
84
|
|
|
'User-Agent' => $app_info['name'] . '/' . $app_info['version'] . ' (' . $app_info['url'] . ')', |
85
|
|
|
'X-Stripe-Client-User-Agent' => json_encode( $user_agent ), |
86
|
|
|
) ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Send the request to Stripe's API |
91
|
|
|
* |
92
|
|
|
* @since 3.1.0 |
93
|
|
|
* @version 4.0.0 |
94
|
|
|
* @param array $request |
95
|
|
|
* @param string $api |
96
|
|
|
* @return array|WP_Error |
97
|
|
|
*/ |
98
|
|
|
public static function request( $request, $api = 'charges', $method = 'POST' ) { |
99
|
|
|
WC_Stripe_Logger::log( "{$api} request: " . print_r( $request, true ) ); |
100
|
|
|
|
101
|
|
|
$headers = self::get_headers(); |
102
|
|
|
|
103
|
|
|
if ( 'charges' === $api && 'POST' === $method ) { |
104
|
|
|
$customer = ! empty( $request['customer'] ) ? $request['customer'] : ''; |
105
|
|
|
$source = ! empty( $request['source'] ) ? $request['source'] : $customer; |
106
|
|
|
|
107
|
|
|
$headers['Idempotency-Key'] = $request['metadata']['order_id'] . '-' . $source; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$response = wp_safe_remote_post( |
111
|
|
|
self::ENDPOINT . $api, |
112
|
|
|
array( |
113
|
|
|
'method' => $method, |
114
|
|
|
'headers' => $headers, |
115
|
|
|
'body' => apply_filters( 'woocommerce_stripe_request_body', $request, $api ), |
116
|
|
|
'timeout' => 70, |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
121
|
|
|
WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) ); |
122
|
|
|
throw new WC_Stripe_Exception( print_r( $response, true ), __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return json_decode( $response['body'] ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieve API endpoint. |
130
|
|
|
* |
131
|
|
|
* @since 4.0.0 |
132
|
|
|
* @version 4.0.0 |
133
|
|
|
* @param string $api |
134
|
|
|
*/ |
135
|
|
|
public static function retrieve( $api ) { |
136
|
|
|
WC_Stripe_Logger::log( "{$api}" ); |
137
|
|
|
|
138
|
|
|
$response = wp_safe_remote_get( |
139
|
|
|
self::ENDPOINT . $api, |
140
|
|
|
array( |
141
|
|
|
'method' => 'GET', |
142
|
|
|
'headers' => self::get_headers(), |
143
|
|
|
'timeout' => 70, |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
148
|
|
|
WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) ); |
149
|
|
|
return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return json_decode( $response['body'] ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.