| 1 |
<?php |
| 2 |
|
| 3 |
class GiftUp_API_Response { |
| 4 |
public $success = false; |
| 5 |
public $code = 0; |
| 6 |
public $body = ""; |
| 7 |
public $renderable_body = ""; |
| 8 |
|
| 9 |
function __construct( $response ) { |
| 10 |
$body = wp_remote_retrieve_body( $response ); |
| 11 |
|
| 12 |
$this->code = wp_remote_retrieve_response_code( $response ); |
| 13 |
$this->success = $this->code >= 200 && $this->code < 300; |
| 14 |
$this->body = $this->isJson( $body ) ? json_decode( $body, true ) : $body; |
| 15 |
$this->renderable_body = $this->isJson( $body ) ? $body : '<div style="word-break: break-all; overflow-x: none; overflow-y: auto; max-height: 200px;">' . htmlentities( $body ) . '</div>'; |
| 16 |
} |
| 17 |
|
| 18 |
function isJson( $string ) { |
| 19 |
json_decode($string); |
| 20 |
return (json_last_error() == JSON_ERROR_NONE); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
class GiftUp_API |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Get a gift card remaining balance |
| 28 |
* |
| 29 |
* @return balance as decimal |
| 30 |
*/ |
| 31 |
public function get_gift_card_balance( $code = null ) { |
| 32 |
if (empty($code)){ |
| 33 |
$code = GiftUp()->cache->get_accepted_gift_card_code(); |
| 34 |
} |
| 35 |
|
| 36 |
if (!empty($code)) { |
| 37 |
$giftcard = $this->get_gift_card($code); |
| 38 |
|
| 39 |
if ($giftcard !== NULL |
| 40 |
&& $giftcard['canBeRedeemed'] |
| 41 |
&& $this->get_gift_card_is_valid($code) |
| 42 |
&& isset($giftcard['remainingValue']) |
| 43 |
&& is_numeric($giftcard['remainingValue'])) { |
| 44 |
return $giftcard['remainingValue']; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return null; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return true if gift card has expired |
| 53 |
*/ |
| 54 |
public function get_gift_card_is_valid( $code = null ) { |
| 55 |
if (empty($code)){ |
| 56 |
$code = GiftUp()->cache->get_accepted_gift_card_code(); |
| 57 |
} |
| 58 |
|
| 59 |
if (!empty($code)) { |
| 60 |
$giftcard = $this->get_gift_card($code); |
| 61 |
|
| 62 |
if ($giftcard !== NULL |
| 63 |
&& ($giftcard['hasExpired'] == 1 || $giftcard['notYetValid'] == 1)) |
| 64 |
{ |
| 65 |
return false; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @return true if gift card is currency backed |
| 74 |
*/ |
| 75 |
public function get_is_currency_backed( $code = null ) { |
| 76 |
if (empty($code)){ |
| 77 |
$code = GiftUp()->cache->get_accepted_gift_card_code(); |
| 78 |
} |
| 79 |
|
| 80 |
if (!empty($code)) { |
| 81 |
$giftcard = $this->get_gift_card($code); |
| 82 |
|
| 83 |
if ($giftcard !== NULL |
| 84 |
&& $giftcard['backingType'] !== NULL) |
| 85 |
{ |
| 86 |
return strtolower( $giftcard['backingType'] ) == 'currency'; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get a gift card |
| 95 |
* |
| 96 |
* @return giftcard object |
| 97 |
*/ |
| 98 |
public function get_gift_card( $code = null ) { |
| 99 |
// Look this up in our cache |
| 100 |
if ($code != null |
| 101 |
&& GiftUp()->cache->giftcard != NULL |
| 102 |
&& strtolower($code) == strtolower(GiftUp()->cache->giftcard['code']) ) { |
| 103 |
return GiftUp()->cache->giftcard; |
| 104 |
} |
| 105 |
|
| 106 |
if (empty($code)){ |
| 107 |
$code = GiftUp()->cache->get_accepted_gift_card_code(); |
| 108 |
} |
| 109 |
|
| 110 |
if (!empty($code)) { |
| 111 |
$debug = GiftUp()->options->get_woocommerce_diagnostics_mode(); |
| 112 |
|
| 113 |
if ($debug) { |
| 114 |
GiftUp()->diagnostics->append( "├ Looking up gift card " . $code . " via API" ); |
| 115 |
} |
| 116 |
|
| 117 |
$response = $this->invoke( '/gift-cards-woocommerce/' . rawurlencode( $code ) ); |
| 118 |
|
| 119 |
if ($response->success) { |
| 120 |
GiftUp()->cache->giftcard = $response->body; |
| 121 |
return $response->body; |
| 122 |
} |
| 123 |
|
| 124 |
if ($debug) { |
| 125 |
GiftUp()->diagnostics->append( "├ Gift card " . $code . " not found (" . $response->code . ")" ); |
| 126 |
|
| 127 |
if ( $response->code != 404 && $response->renderable_body !== NULL && strlen( $response->renderable_body ) > 0 ) { |
| 128 |
GiftUp()->diagnostics->append( "├ " . $response->renderable_body ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get a list of gift cards |
| 138 |
* |
| 139 |
* @return list<giftcard> object |
| 140 |
*/ |
| 141 |
public function get_gift_cards( $offset = 0, $limit = 10 ) { |
| 142 |
$response = $this->invoke( '/gift-cards?offset=' . $offset . '&limit=' . $limit ); |
| 143 |
|
| 144 |
if ($response->success) { |
| 145 |
return $response->body; |
| 146 |
} |
| 147 |
|
| 148 |
return null; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Redeem a gift card |
| 153 |
* |
| 154 |
* @return boolean representing whether the redeem worked |
| 155 |
*/ |
| 156 |
public function redeem_gift_card( $code, $value, $order_id ) { |
| 157 |
$giftcard = $this->get_gift_card( $code ); |
| 158 |
|
| 159 |
if ($giftcard == NULL) { |
| 160 |
return -1001; |
| 161 |
} |
| 162 |
|
| 163 |
$balance = $this->get_gift_card_balance( $code ); |
| 164 |
|
| 165 |
if ( $balance < $value ) { |
| 166 |
return -1002; |
| 167 |
} |
| 168 |
|
| 169 |
$rounded_value = $value; |
| 170 |
try { |
| 171 |
$rounded_value = round($value, 2, PHP_ROUND_HALF_DOWN); |
| 172 |
} catch(exception $e) {} |
| 173 |
|
| 174 |
if ($order_id === NULL || strlen($order_id) == 0) { |
| 175 |
$order_id = "(unknown)"; |
| 176 |
} |
| 177 |
|
| 178 |
$reason = "Redeemed against WooCommerce order id " . $order_id; |
| 179 |
|
| 180 |
$payload = []; |
| 181 |
$response = $this->invoke( '/gift-cards/' . rawurlencode( $code ) . '/redeem-woocommerce?amount=' . $rounded_value . '&reason=' . rawurlencode( $reason ), 'POST', $payload ); |
| 182 |
|
| 183 |
if ( $response->success ) { |
| 184 |
return $response->body['redeemedAmount']; |
| 185 |
} |
| 186 |
|
| 187 |
// Fallback GET request to avert 411 HTTP responses |
| 188 |
if ( $response->code == 411 ) { |
| 189 |
$cache_bust = self::generate_random_string(10); |
| 190 |
$response = $this->invoke( '/gift-cards/' . rawurlencode( $code ) . '/redeem-woocommerce?amount=' . $rounded_value . '&reason=' . rawurlencode( $reason ) . '&cb=' . $cache_bust, 'GET' ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( $response->success ) { |
| 194 |
return $response->body['redeemedAmount']; |
| 195 |
} |
| 196 |
|
| 197 |
return -2000 - $response->code; |
| 198 |
} |
| 199 |
|
| 200 |
private static function generate_random_string($length = 10) { |
| 201 |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 202 |
$charactersLength = strlen($characters); |
| 203 |
$randomString = ''; |
| 204 |
for ($i = 0; $i < $length; $i++) { |
| 205 |
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
| 206 |
} |
| 207 |
return $randomString; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get the company name |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
public function get_company( $api_key = null ) { |
| 216 |
$response = $this->invoke( '/company', 'GET', null, $api_key ); |
| 217 |
|
| 218 |
if ( $response->success ) { |
| 219 |
return $response->body; |
| 220 |
} |
| 221 |
|
| 222 |
return null; |
| 223 |
} |
| 224 |
|
| 225 |
public function get_woocommerce_connection_status() |
| 226 |
{ |
| 227 |
$response = $this->invoke( '/integrations/woocommerce/test?noprobe=true' ); |
| 228 |
|
| 229 |
if ( $response->success ) { |
| 230 |
return $response->body; |
| 231 |
} |
| 232 |
|
| 233 |
return null; |
| 234 |
} |
| 235 |
|
| 236 |
public function notify_connect_woocommerce() |
| 237 |
{ |
| 238 |
$payload = []; |
| 239 |
$payload['storeUrl'] = get_site_url(); |
| 240 |
|
| 241 |
$response = $this->invoke( '/integrations/woocommerce/connect', 'POST', $payload ); |
| 242 |
|
| 243 |
if ($response->success) { |
| 244 |
GiftUp()->options->set_woocommerce_operating_mode( GIFTUP_WOO_MODE_API ); |
| 245 |
} |
| 246 |
|
| 247 |
return $response->success; |
| 248 |
} |
| 249 |
|
| 250 |
public function notify_disconnect_woocommerce() |
| 251 |
{ |
| 252 |
$response = $this->invoke( '/integrations/woocommerce/disconnect', 'POST' ); |
| 253 |
|
| 254 |
return $response->success; |
| 255 |
} |
| 256 |
|
| 257 |
public function api_root() |
| 258 |
{ |
| 259 |
return 'https://api.giftup.app'; |
| 260 |
} |
| 261 |
|
| 262 |
public function dashboard_root() |
| 263 |
{ |
| 264 |
return 'https://giftup.app'; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Invoke an API call to Gift Up! |
| 269 |
* |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
public function invoke( $endpoint, $method = "GET", $data = null, $api_key = null ) { |
| 273 |
$root = $this->api_root(); |
| 274 |
$url = esc_url_raw( $root . $endpoint ); |
| 275 |
$response = false; |
| 276 |
$json = null; |
| 277 |
|
| 278 |
if ($data !== NULL) { |
| 279 |
$json = json_encode( $data, JSON_FORCE_OBJECT ); |
| 280 |
|
| 281 |
if ($json === NULL) { |
| 282 |
$json = "{ 'error': 'Could not serialize data into JSON' }"; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if ($api_key === NULL) { |
| 287 |
$api_key = GiftUp()->options->get_api_key(); |
| 288 |
} |
| 289 |
|
| 290 |
$plugin_version = GIFTUP_VERSION; |
| 291 |
$woocommerce_version = GiftUp()->diagnostics->woocommerce_installed_version(); |
| 292 |
$php_version = phpversion(); |
| 293 |
global $wp_version; |
| 294 |
|
| 295 |
if ( $plugin_version === NULL || strlen( $plugin_version ) <= 0 ) { |
| 296 |
$plugin_version = "unknown"; |
| 297 |
} |
| 298 |
if ( $php_version === NULL || strlen( $php_version ) <= 0 ) { |
| 299 |
$php_version = "unknown"; |
| 300 |
} |
| 301 |
if ( $wp_version === NULL || strlen( $wp_version ) <= 0 ) { |
| 302 |
$wp_version = "unknown"; |
| 303 |
} |
| 304 |
if ( $woocommerce_version === NULL || strlen( $woocommerce_version ) <= 0 ) { |
| 305 |
$woocommerce_version = "unknown"; |
| 306 |
} |
| 307 |
|
| 308 |
$args = array( |
| 309 |
'timeout' => 60, |
| 310 |
'body' => $json, |
| 311 |
'headers' => array( |
| 312 |
'authorization' => 'Bearer ' . $api_key, |
| 313 |
'content-type' => 'application/json', |
| 314 |
'accept' => '*/*', |
| 315 |
'user-agent' => 'WordPress/GiftUp-WordPress-Plugin', |
| 316 |
'x-giftup-testmode' => GiftUp()->options->get_woocommerce_is_in_test_mode() ? "true" : "false", |
| 317 |
'x-giftup-wordpress-plugin-version' => $plugin_version, |
| 318 |
'x-giftup-wordpress-php-version' => $php_version, |
| 319 |
'x-giftup-wordpress-version' => $wp_version, |
| 320 |
'x-giftup-woocommerce-version' => $woocommerce_version |
| 321 |
) |
| 322 |
); |
| 323 |
|
| 324 |
if ($method === "GET") { |
| 325 |
$response = wp_remote_get( $url, $args ); |
| 326 |
} |
| 327 |
else if ($method === "POST") { |
| 328 |
$response = wp_remote_post( $url, $args ); |
| 329 |
} |
| 330 |
else { |
| 331 |
$args = array( |
| 332 |
'method' => $method |
| 333 |
); |
| 334 |
$response = wp_remote_request( $url, $args ); |
| 335 |
} |
| 336 |
|
| 337 |
if ( is_wp_error($response) ) { |
| 338 |
$error = $response->get_error_message(); |
| 339 |
|
| 340 |
echo '<div id="message" class="notice notice-error">'; |
| 341 |
echo '<p>'; |
| 342 |
echo '<strong>'; |
| 343 |
echo 'Error talking to Gift Up! at ' . $url . ' - ' . $error . '<br>'; |
| 344 |
if (strpos($error, 'tls') !== false){ |
| 345 |
echo '<br>The Gift Up! plugin requires that your PHP version is 5.6+ and cURL supports TLS1.2.<br>'; |
| 346 |
echo 'Please conduct a TLS 1.2 Compatibility Test via <a href="https://wordpress.org/plugins/tls-1-2-compatibility-test/" target="_blank">this plugin</a>'; |
| 347 |
} |
| 348 |
echo '</strong>'; |
| 349 |
echo '</p>'; |
| 350 |
echo '</div>'; |
| 351 |
} |
| 352 |
|
| 353 |
return new GiftUp_API_Response( $response ); |
| 354 |
} |
| 355 |
} |
| 356 |
|