initial commit
This commit is contained in:
54
src/Counterpoint.php
Normal file
54
src/Counterpoint.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Bobs\Dropshipemail;
|
||||
|
||||
use Hpz937\Phpvaultclient\PhpVaultClient;
|
||||
|
||||
class Counterpoint
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$vault = new PhpVaultClient();
|
||||
$vault->login($_ENV['VAULT_USER'], $_ENV['VAULT_PASS']);
|
||||
|
||||
$dbCredentials = $vault->getSecret($_ENV['VAULT_PATH'], $_ENV['VAULT_ENCRYTPION_KEY']);
|
||||
|
||||
$this->db = sqlsrv_connect($dbCredentials['server'], [
|
||||
'Database' => $dbCredentials['database'],
|
||||
'UID' => $dbCredentials['username'],
|
||||
'PWD' => $dbCredentials['password'],
|
||||
'Encrypt' => true,
|
||||
'TrustServerCertificate' => true,
|
||||
]);
|
||||
if ($this->db === false) {
|
||||
echo "Failed to connect to CP database.\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
public function getOrder($orderNumber)
|
||||
{
|
||||
$sql = 'select TKT_NO,BILL_EMAIL_ADRS_1,BILL_PHONE_1,BILL_NAM,SHIP_ADRS_1,SHIP_ADRS_2,SHIP_CITY,SHIP_STATE,SHIP_ZIP_COD from VI_PS_DOC_HDR where TKT_NO=?';
|
||||
$stmt = sqlsrv_query($this->db, $sql, [$orderNumber]);
|
||||
$order = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
public function getTracking($orderNumber)
|
||||
{
|
||||
$sql = 'select h.TKT_NO,PKG_TRK_NO from PS_DOC_PKG_TRK_NO p join PS_DOC_HDR h on p.DOC_ID=h.DOC_ID where h.TKT_NO=?';
|
||||
$stmt = sqlsrv_query($this->db, $sql, [$orderNumber]);
|
||||
//get all tracking numbers for the order
|
||||
$tracking = [];
|
||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
$tracking[] = $row['PKG_TRK_NO'];
|
||||
}
|
||||
sqlsrv_free_stmt($stmt);
|
||||
|
||||
return $tracking;
|
||||
}
|
||||
}
|
||||
79
src/routes.php
Normal file
79
src/routes.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
// Serve index.html for root route
|
||||
$app->get('/', function ($request, $response) {
|
||||
$file = __DIR__ . '/../public/index.html';
|
||||
$response->getBody()->write(file_get_contents($file));
|
||||
return $response->withHeader('Content-Type', 'text/html');
|
||||
});
|
||||
|
||||
// Fetch order details
|
||||
$app->get('/api/orders/{order_number}', function ($request, $response, array $args) {
|
||||
$orderNumber = $args['order_number'];
|
||||
$counterPoint = new Bobs\Dropshipemail\Counterpoint();
|
||||
$order = $counterPoint->getOrder($orderNumber);
|
||||
if ($order) {
|
||||
$data['order_number'] = $order['TKT_NO'];
|
||||
$data['customer_name'] = $order['BILL_NAM'];
|
||||
$data['email'] = $order['BILL_EMAIL_ADRS_1'];
|
||||
$data['phone'] = $order['BILL_PHONE_1'];
|
||||
$data['shipping_address'] = [
|
||||
'address1' => $order['SHIP_ADRS_1'],
|
||||
'address2' => $order['SHIP_ADRS_2'],
|
||||
'city' => $order['SHIP_CITY'],
|
||||
'state' => $order['SHIP_STATE'],
|
||||
'zip' => $order['SHIP_ZIP_COD']
|
||||
];
|
||||
|
||||
$response->getBody()->write(json_encode($data));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
|
||||
} else {
|
||||
$response->getBody()->write(json_encode(['error' => 'Order not found']));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
$app->get('/api/tracking/{order_number}', function ($request, $response, array $args) {
|
||||
$orderNumber = $args['order_number'];
|
||||
$counterPoint = new Bobs\Dropshipemail\Counterpoint();
|
||||
$tracking = $counterPoint->getTracking($orderNumber);
|
||||
if ($tracking) {
|
||||
$trackingDetails = [];
|
||||
|
||||
foreach ($tracking as $trackingNumber) {
|
||||
$trackingDetails[] = [
|
||||
'tracking_number' => $trackingNumber,
|
||||
'carrier' => determineCarrier($trackingNumber)
|
||||
];
|
||||
}
|
||||
|
||||
$response->getBody()->write(json_encode([
|
||||
'order_number' => $orderNumber,
|
||||
'tracking_details' => $trackingDetails
|
||||
]));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
|
||||
} else {
|
||||
$response->getBody()->write(json_encode(['error' => 'Tracking information not found']));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
// Helper function to determine the carrier based on the tracking number
|
||||
function determineCarrier($trackingNumber) {
|
||||
if (strpos($trackingNumber, 'SP') === 0) {
|
||||
return 'Speedee';
|
||||
} elseif (strpos($trackingNumber, '1Z') === 0) {
|
||||
return 'UPS';
|
||||
} elseif (preg_match('/^\d{12}$/', $trackingNumber)) {
|
||||
return 'FedEx';
|
||||
} elseif (preg_match('/^(94|93|92|95)\d{20}$/', $trackingNumber) || strlen($trackingNumber) >= 20) {
|
||||
return 'USPS';
|
||||
} elseif (preg_match('/^\d{10}$/', $trackingNumber)) {
|
||||
return 'GLS';
|
||||
} elseif (preg_match('/^C\d+$/', $trackingNumber)) {
|
||||
return 'OnTrac';
|
||||
} else {
|
||||
return 'Unknown Carrier';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user