I choose Shapeways from a quick search for 3D print suppliers who offer an API to place orders and below I created with the help of AI a PHP script that shall take the Reflow Webhook, process it and send it to Shapeways for printing.
In this example I have chosen the SKU in Reflow to specify to Shapeways the existing model (MODEL_ID
) on the account.
Although not implemented here, another feature you could use in Reflow and the Shapeways API is allowing customers to specify what material their models would be printed with (MATERIAL_ID
) using the personalisation options in Reflow.
Shapeways would bill you with the CREDIT_CARD
on your account, so you would still be able to manage billing customers through Reflow.
<?php
// Replace with your actual Shapeways API access token
$accessToken = 'YOUR_ACCESS_TOKEN';
// Shapeways API endpoint for placing orders
$url = 'https://api.shapeways.com/orders/v1';
// Read the raw POST data from the webhook
$rawData = file_get_contents('php://input');
// Decode the JSON into an associative array
$reflowWebhook = json_decode($rawData, true);
// Initialize items list
$items = [];
// Function to split the full name from Reflow into first and last name, using LAST NAME as backup if none is found
function splitName($fullName) {
// Check if there's a space in the name
$nameParts = explode(' ', $fullName, 2);
$firstName = $nameParts[0];
$lastName = isset($nameParts[1]) ? $nameParts[1] : 'LAST NAME';
return [$firstName, $lastName];
}
// Map Reflow webhook data to Shapeways order format
foreach ($reflowWebhook['data']['object']['line_items'] as $lineItem) {
$items[] = [
'materialId' => $lineItem['MATERIAL_ID'],
'modelId' => $lineItem['sku'], //SKU must correlate to the model ID from Shapeways
'quantity' => $lineItem['quantity'],
];
}
// Extract the customer's name and split it into first and last name
list($firstName, $lastName) = splitName($reflowWebhook['data']['object']['customer']['name']);
// Prepare order data
$orderData = [
'items' => $items,
'firstName' => $firstName,
'lastName' => $lastName, // Use the split last name
'country' => $reflowWebhook['data']['object']['shipping_address']['country'],
'state' => '', // State not shown in the Reflow Webhook example, so leaving blank
'city' => $reflowWebhook['data']['object']['shipping_address']['city'],
'address1' => $reflowWebhook['data']['object']['shipping_address']['address'],
'address2' => '', // Not provided in the Reflow Webhook example, unsure if it exists
'zipCode' => $reflowWebhook['data']['object']['shipping_address']['postcode'],
'phoneNumber' => $reflowWebhook['data']['object']['customer']['phone'],
'paymentMethod' => 'credit_card', // Credit card on file with Shapeways
'shippingOption' => 'Cheapest', // Tells Shapeways to use the cheapest shipping option
];
// Convert order data to JSON
$postData = json_encode($orderData);
// Initialize cURL session
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request and handle response
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Handle successful response
echo 'Order placed successfully: ' . $response;
}
// Close cURL session
curl_close($ch);
?>