Skip to content

Commit

Permalink
Infrastructure select (#15)
Browse files Browse the repository at this point in the history
* api us base variable

* function to get base url

* dropdown

* comment

* infra
rheber authored Apr 15, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent dba5a50 commit 01a6fa7
Showing 3 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ services:
- 8888:80
environment:
SPIFF_API_BASE: 'http://host.docker.internal:8080'
SPIFF_API_US_BASE: 'http://host.docker.internal:8080'
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
5 changes: 5 additions & 0 deletions replace-env-vars.py
Original file line number Diff line number Diff line change
@@ -10,5 +10,10 @@
print('define("SPIFF_API_BASE", "https://api.spiff.com.au");')
else:
print('define("SPIFF_API_BASE", "https://api.app.dev.spiff.com.au");')
elif line.startswith('define("SPIFF_API_US_BASE"'):
if (env == 'production'):
print('define("SPIFF_API_US_BASE", "https://api.us.spiffcommerce.com");')
else:
print('define("SPIFF_API_US_BASE", "https://api.app.dev.spiff.com.au");')
else:
print(line, end="")
30 changes: 25 additions & 5 deletions spiff-connect/spiff-connect.php
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
/*
Plugin Name: Spiff Connect
Plugin URI: http://spiff3d.com
Version: 2.0.67
Description: Connect your WooCommerce store to Spiff and allow customers access to Spiff Workflows to personalize their products.
Author: Spiff Pty. Ltd.
License: GPL3
@@ -11,11 +10,20 @@
require plugin_dir_path(__FILE__) . 'includes/spiff-connect-requests.php';

define("SPIFF_API_BASE", getenv("SPIFF_API_BASE"));
define("SPIFF_API_US_BASE", getenv("SPIFF_API_US_BASE"));
define("SPIFF_API_INSTALLS_PATH", "/api/installs");
define("SPIFF_API_ORDERS_PATH", "/api/v2/orders");
define("SPIFF_API_TRANSACTIONS_PATH", "/api/transactions");
define("SPIFF_GRAPHQL_PATH", "/graphql");

// Get base API URL based on infrastructure choice.
function spiff_get_base_api_url() {
if (get_option('spiff_infrastructure') === "US") {
return SPIFF_API_US_BASE;
}
return SPIFF_API_BASE;
}

/**
* Activation hook.
*/
@@ -49,7 +57,8 @@ function spiff_activation_hook() {
$headers = array(
'Content-Type' => 'application/json',
);
wp_remote_post(SPIFF_API_BASE . SPIFF_API_INSTALLS_PATH, array(
// This is expected to always be Australia because the value hasn't been set yet.
wp_remote_post(spiff_get_base_api_url() . SPIFF_API_INSTALLS_PATH, array(
'body' => $body,
'headers' => $headers
));
@@ -72,6 +81,7 @@ function spiff_register_admin_settings() {
register_setting('spiff-settings-group', 'spiff_api_key');
register_setting('spiff-settings-group', 'spiff_api_secret');
register_setting('spiff-settings-group', 'spiff_application_key');
register_setting('spiff-settings-group', 'spiff_infrastructure');

register_setting('spiff-settings-group', 'spiff_show_customer_selections_in_cart');
register_setting('spiff-settings-group', 'spiff_show_preview_images_in_cart');
@@ -130,6 +140,16 @@ function spiff_admin_menu_html() {
<td><input autocomplete=off type="text" name="spiff_application_key" value="<?php echo esc_attr(get_option('spiff_application_key')); ?>" /></td>
</tr>
</table>
<p style="font-size: 16px;margin-bottom: 30px;position: relative;">Ensure that you are using the Spiff infrastructure that your account was created against.</p>
<table class="form-table">
<tr valign="top">
<th scope="row">Infrastructure</th>
<td><select name="spiff_infrastructure">
<option value="AU" <?php echo selected("AU", get_option("spiff_infrastructure") ?? "AU", false); ?>>Australia</option>
<option value="US" <?php echo selected("US", get_option("spiff_infrastructure"), false); ?>>United States</option>
</select></td>
</tr>
</table>

<h2 style="font-size: 24px;line-height: 29px;position: relative;">Cart</h2>
<table class="form-table">
@@ -395,7 +415,7 @@ function spiff_create_cart_item() {

// Get the data associated with a transaction.
function spiff_get_transaction($transaction_id) {
$url = SPIFF_API_BASE . SPIFF_GRAPHQL_PATH;
$url = spiff_get_base_api_url() . SPIFF_GRAPHQL_PATH;
$access_key = get_option('spiff_api_key');
$secret_key = get_option('spiff_api_secret');
$body = json_encode(array(
@@ -471,7 +491,7 @@ function spiff_show_metadata_in_cart($cart_data, $cart_item) {
}

function spiff_get_transaction_image($transaction_id) {
$url = SPIFF_API_BASE . SPIFF_API_TRANSACTIONS_PATH . '/' . $transaction_id . '/image';
$url = spiff_get_base_api_url() . SPIFF_API_TRANSACTIONS_PATH . '/' . $transaction_id . '/image';
$response = wp_remote_get($url, array('redirection' => 0));
$response_location_header = wp_remote_retrieve_header($response, 'location');
if ($response_location_header === '') {
@@ -542,7 +562,7 @@ function spiff_post_order($access_key, $secret_key, $items, $woo_order_id) {
'orderItems' => $items
));
$headers = spiff_request_headers($access_key, $secret_key, $body, SPIFF_API_ORDERS_PATH);
$response = wp_remote_post(SPIFF_API_BASE . SPIFF_API_ORDERS_PATH, array(
$response = wp_remote_post(spiff_get_base_api_url() . SPIFF_API_ORDERS_PATH, array(
'body' => $body,
'headers' => $headers,
));

0 comments on commit 01a6fa7

Please sign in to comment.