Completed
Push — master ( 1968f4...2723f5 )
by Roy
02:26
created

WC_Stripe_API::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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
18
	/**
19
	 * Secret API Key.
20
	 * @var string
21
	 */
22
	private static $secret_key = '';
23
24
	/**
25
	 * Set secret API Key.
26
	 * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
	 */
28
	public static function set_secret_key( $secret_key ) {
29
		self::$secret_key = $secret_key;
30
	}
31
32
	/**
33
	 * Get secret key.
34
	 * @return string
35
	 */
36
	public static function get_secret_key() {
37
		if ( ! self::$secret_key ) {
38
			$options = get_option( 'woocommerce_stripe_settings' );
39
40
			if ( isset( $options['testmode'], $options['secret_key'], $options['test_secret_key'] ) ) {
41
				self::set_secret_key( 'yes' === $options['testmode'] ? $options['test_secret_key'] : $options['secret_key'] );
42
			}
43
		}
44
		return self::$secret_key;
45
	}
46
47
	/**
48
	 * Send the request to Stripe's API
49
	 *
50
	 * @param array $request
51
	 * @param string $api
52
	 * @return array|WP_Error
53
	 */
54
	public static function request( $request, $api = 'charges', $method = 'POST' ) {
55
		self::log( "{$api} request: " . print_r( $request, true ) );
56
57
		$response = wp_safe_remote_post(
58
			self::ENDPOINT . $api,
59
			array(
60
				'method'        => $method,
61
				'headers'       => array(
62
					'Authorization'  => 'Basic ' . base64_encode( self::get_secret_key() . ':' ),
63
					'Stripe-Version' => '2016-03-07',
64
				),
65
				'body'       => apply_filters( 'woocommerce_stripe_request_body', $request, $api ),
66
				'timeout'    => 70,
67
				'user-agent' => 'WooCommerce ' . WC()->version,
68
			)
69
		);
70
71
		if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
72
			self::log( 'Error Response: ' . print_r( $response, true ) );
73
			return new WP_Error( 'stripe_error', __( 'There was a problem connecting to the payment gateway.', 'woocommerce-gateway-stripe' ) );
74
		}
75
76
		$parsed_response = json_decode( $response['body'] );
77
		// Handle response
78
		if ( ! empty( $parsed_response->error ) ) {
79
			if ( ! empty( $parsed_response->error->param ) ) {
80
				$code = $parsed_response->error->param;
81
			} elseif ( ! empty( $parsed_response->error->code ) ) {
82
				$code = $parsed_response->error->code;
83
			} else {
84
				$code = 'stripe_error';
85
			}
86
			return new WP_Error( $code, $parsed_response->error->message );
87
		} else {
88
			return $parsed_response;
89
		}
90
	}
91
92
	/**
93
	 * Logs
94
	 *
95
	 * @since 3.1.0
96
	 * @version 3.1.0
97
	 *
98
	 * @param string $message
99
	 */
100
	public static function log( $message ) {
101
		$options = get_option( 'woocommerce_stripe_settings' );
102
103
		if ( 'yes' === $options['logging'] ) {
104
			WC_Stripe::log( $message );
105
		}
106
	}
107
}
108