wp-pay-gateways /
omnikassa-2
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Client |
||||
| 4 | * |
||||
| 5 | * @author Pronamic <[email protected]> |
||||
| 6 | * @copyright 2005-2018 Pronamic |
||||
| 7 | * @license GPL-3.0-or-later |
||||
| 8 | * @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
||||
| 12 | |||||
| 13 | use WP_Error; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * Client |
||||
| 17 | * |
||||
| 18 | * @author Remco Tolsma |
||||
| 19 | * @version 2.0.2 |
||||
| 20 | * @since 1.0.0 |
||||
| 21 | */ |
||||
| 22 | class Client { |
||||
| 23 | /** |
||||
| 24 | * URL OmniKassa API. |
||||
| 25 | * |
||||
| 26 | * @var string |
||||
| 27 | */ |
||||
| 28 | const URL_PRODUCTION = 'https://betalen.rabobank.nl/omnikassa-api/'; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * URL OmniKassa sandbox API. |
||||
| 32 | * |
||||
| 33 | * @var string |
||||
| 34 | */ |
||||
| 35 | const URL_SANDBOX = 'https://betalen.rabobank.nl/omnikassa-api-sandbox/'; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * Error |
||||
| 39 | * |
||||
| 40 | * @var WP_Error |
||||
| 41 | */ |
||||
| 42 | private $error; |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * The URL. |
||||
| 46 | * |
||||
| 47 | * @var string |
||||
| 48 | */ |
||||
| 49 | private $url; |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * Error. |
||||
| 53 | * |
||||
| 54 | * @return WP_Error |
||||
| 55 | */ |
||||
| 56 | public function get_error() { |
||||
| 57 | return $this->error; |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * Get the URL. |
||||
| 62 | * |
||||
| 63 | * @return string |
||||
| 64 | */ |
||||
| 65 | public function get_url() { |
||||
| 66 | return $this->url; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * Set the action URL |
||||
| 71 | * |
||||
| 72 | * @param string $url URL. |
||||
| 73 | */ |
||||
| 74 | public function set_url( $url ) { |
||||
| 75 | $this->url = $url; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Get refresh token. |
||||
| 80 | * |
||||
| 81 | * @return string |
||||
| 82 | */ |
||||
| 83 | public function get_refresh_token() { |
||||
| 84 | return $this->refresh_token; |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Set refresh token. |
||||
| 89 | * |
||||
| 90 | * @param string $refresh_token Refresh token. |
||||
| 91 | */ |
||||
| 92 | public function set_refresh_token( $refresh_token ) { |
||||
| 93 | $this->refresh_token = $refresh_token; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * Get signing key. |
||||
| 98 | * |
||||
| 99 | * @return string |
||||
| 100 | */ |
||||
| 101 | public function get_signing_key() { |
||||
| 102 | return $this->signing_key; |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | /** |
||||
| 106 | * Set signing key. |
||||
| 107 | * |
||||
| 108 | * @param string $signing_key Signing key. |
||||
| 109 | */ |
||||
| 110 | public function set_signing_key( $signing_key ) { |
||||
| 111 | $this->signing_key = $signing_key; |
||||
|
0 ignored issues
–
show
|
|||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Request URL with specified method, token. |
||||
| 116 | * |
||||
| 117 | * @param string $method HTTP request method. |
||||
| 118 | * @param string $endpoint URL endpoint to request. |
||||
| 119 | * @param string $token Authorization token. |
||||
| 120 | * @param object|null $object Object. |
||||
| 121 | */ |
||||
| 122 | private function request( $method, $endpoint, $token, $object = null ) { |
||||
| 123 | // URL. |
||||
| 124 | $url = $this->get_url() . $endpoint; |
||||
| 125 | |||||
| 126 | // Arguments. |
||||
| 127 | $args = array( |
||||
| 128 | 'method' => $method, |
||||
| 129 | 'headers' => array( |
||||
| 130 | 'Authorization' => 'Bearer ' . $token, |
||||
| 131 | ), |
||||
| 132 | ); |
||||
| 133 | |||||
| 134 | if ( null !== $object ) { |
||||
| 135 | $args['headers']['Content-Type'] = 'application/json'; |
||||
| 136 | |||||
| 137 | $args['body'] = wp_json_encode( $object ); |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | // Request. |
||||
| 141 | $response = wp_remote_request( $url, $args ); |
||||
| 142 | |||||
| 143 | if ( is_wp_error( $response ) ) { |
||||
| 144 | $this->error = $response; |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type array. However, the property $error is declared as type WP_Error. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||||
| 145 | |||||
| 146 | $this->error->add( 'omnikassa_2_error', 'HTTP Request Failed' ); |
||||
| 147 | |||||
| 148 | return false; |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | // Body. |
||||
| 152 | $body = wp_remote_retrieve_body( $response ); |
||||
|
0 ignored issues
–
show
It seems like
$response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 153 | |||||
| 154 | $data = json_decode( $body ); |
||||
| 155 | |||||
| 156 | if ( ! is_object( $data ) ) { |
||||
| 157 | $this->error = new \WP_Error( 'omnikassa_2_error', 'Could not parse response.', $data ); |
||||
| 158 | |||||
| 159 | return false; |
||||
| 160 | } |
||||
| 161 | |||||
| 162 | // Error. |
||||
| 163 | if ( isset( $data->errorCode ) ) { |
||||
| 164 | $message = 'Unknown error.'; |
||||
| 165 | |||||
| 166 | if ( isset( $data->consumerMessage ) ) { |
||||
| 167 | $message = $data->consumerMessage; |
||||
| 168 | } elseif ( isset( $data->errorMessage ) ) { |
||||
| 169 | $message = $data->errorMessage; |
||||
| 170 | } |
||||
| 171 | |||||
| 172 | $this->error = new \WP_Error( 'omnikassa_2_error', $message, $data ); |
||||
| 173 | |||||
| 174 | return false; |
||||
| 175 | } |
||||
| 176 | |||||
| 177 | // Ok. |
||||
| 178 | return $data; |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | /** |
||||
| 182 | * Get access token. |
||||
| 183 | * |
||||
| 184 | * @return string |
||||
| 185 | */ |
||||
| 186 | public function get_access_token_data() { |
||||
| 187 | return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() ); |
||||
|
0 ignored issues
–
show
|
|||||
| 188 | } |
||||
| 189 | |||||
| 190 | /** |
||||
| 191 | * Order announce. |
||||
| 192 | * |
||||
| 193 | * @param Config $config Config. |
||||
| 194 | * @param Order $order Order. |
||||
| 195 | * @return object|bool |
||||
| 196 | */ |
||||
| 197 | public function order_announce( $config, Order $order ) { |
||||
| 198 | $object = $order->get_json(); |
||||
| 199 | |||||
| 200 | $object->signature = Security::get_signature( $order, $config->signing_key ); |
||||
| 201 | |||||
| 202 | return $this->request( 'POST', 'order/server/api/order', $config->access_token, $object ); |
||||
| 203 | } |
||||
| 204 | |||||
| 205 | /** |
||||
| 206 | * Get order results by the notification token. |
||||
| 207 | * |
||||
| 208 | * @param string $notification_token Notification token. |
||||
| 209 | * @return OrderResults |
||||
| 210 | */ |
||||
| 211 | public function get_order_results( $notification_token ) { |
||||
| 212 | $result = $this->request( 'GET', 'order/server/api/events/results/merchant.order.status.changed', $notification_token ); |
||||
| 213 | |||||
| 214 | return OrderResults::from_object( $result ); |
||||
|
0 ignored issues
–
show
It seems like
$result can also be of type false; however, parameter $object of Pronamic\WordPress\Pay\G...rResults::from_object() does only seem to accept stdClass, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 215 | } |
||||
| 216 | } |
||||
| 217 |