woocommerce /
woocommerce-gateway-stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 = '2017-12-14'; |
||
| 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 | $customer = ! empty( $request['customer'] ) ? $request['customer'] : ''; |
||
| 104 | $source = ! empty( $request['source'] ) ? $request['source'] : $customer; |
||
| 105 | |||
| 106 | if ( 'charges' === $api && 'POST' === $method ) { |
||
| 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 | View Code Duplication | if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
|
|
0 ignored issues
–
show
|
|||
| 121 | WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) ); |
||
| 122 | throw new Exception( __( '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 | $ua = self::get_user_agent(); |
||
|
0 ignored issues
–
show
$ua is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 139 | |||
| 140 | $response = wp_safe_remote_get( |
||
| 141 | self::ENDPOINT . $api, |
||
| 142 | array( |
||
| 143 | 'method' => 'GET', |
||
| 144 | 'headers' => self::get_headers(), |
||
| 145 | 'timeout' => 70, |
||
| 146 | ) |
||
| 147 | ); |
||
| 148 | |||
| 149 | View Code Duplication | if ( is_wp_error( $response ) || empty( $response['body'] ) ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 150 | WC_Stripe_Logger::log( 'Error Response: ' . print_r( $response, true ) ); |
||
| 151 | return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the Stripe API endpoint.', 'woocommerce-gateway-stripe' ) ); |
||
| 152 | } |
||
| 153 | |||
| 154 | return json_decode( $response['body'] ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.