METRC (Marijuana Enforcement Tracking Reporting Compliance) is a state-mandated system for tracking cannabis inventory and sales from seed to sale. For developers working on integrating METRC with POS systems, passing the METRC test and successfully integrating their APIs is critical. Below are key tips for passing the test and a guide to integrating the METRC API using PHP in a POS system.
Tips for Passing METRC Testing
- Understand METRC API Requirements:
- METRC offers a RESTful API to integrate third-party software with their system. Familiarize yourself with their API documentation, which includes endpoints for handling sales, inventory, plants, packages, and more.
- METRC’s sandbox environment mirrors the production environment but uses fake data. Start by setting up in the sandbox to test your API calls.
- Ensure that you understand compliance regulations specific to the state you’re working with, as METRC rules and required data fields can vary by state.
- Use Valid API Keys:
- To authenticate with METRC, you will need valid API keys. Each user accessing the system needs a unique API key. For testing in the sandbox environment, use the sandbox API keys provided by METRC.
- Test for All Required Endpoints:
- METRC requires you to test various endpoints during their certification process. This includes:
- Sales: Recording transactions accurately.
- Packages: Handling inventory and adjusting package data.
- Plants: Tracking growth stages of plants.
- Transfers: Managing cannabis transfers between facilities.
- Ensure that each endpoint is fully functional, and test all required scenarios (e.g., recording, modifying, and deleting data).
- METRC requires you to test various endpoints during their certification process. This includes:
- Error Handling:
- Proper error handling is essential. METRC has strict validation checks, and if the data submitted is incomplete or incorrect, the system will reject the API request. Implement robust error handling to catch validation errors, rate limit issues, or other exceptions.
- Logging and Reporting:
- Ensure your POS system logs all interactions with the METRC API for auditing purposes. METRC compliance often requires maintaining a clear record of all activities.
- Stay Updated with API Changes:
- METRC frequently updates its API. Be sure to monitor any changes in the API endpoints or data requirements, and make the necessary adjustments to your integration.
Integrating METRC API in PHP for POS
Below is a step-by-step guide on how to integrate the METRC API in a PHP-based POS system. In this example, we will demonstrate how to authenticate and record a sales transaction.
Step 1: Set Up API Credentials
To begin, you’ll need your API key (available from the METRC sandbox or production environment) and the license number associated with your facility. Store these securely as they’ll be used for authentication in API requests.
TO generate token and call facilities API. Here you get the license number which have access of METRC API.
<?php
// Software vendor's API key
$softwareApiKey = 'hjbwshj';
// User's API key
$userApiKey = 'wdjnj';
// Combine the software API key and user API key
$combinedKeys = $softwareApiKey.':'.$userApiKey;
// Base64 encode the combined keys
$encodedKeys = base64_encode($combinedKeys);
// METRC endpoint with additional date parameters
$url = "https://sandbox-api-ca.metrc.com/facilities/v2/";
// Create a new cURL resource
$ch = curl_init($url);
// Set the authorization header using the encoded keys
$headers = [
'Authorization: Basic ' . $encodedKeys,
'Content-Type: application/json',
'Accept: application/json'
];
// Set cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Check for errors and HTTP status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Status Code: " . $httpCode . "<br>";
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Decode the JSON response
$data = json_decode($response, true);
echo "<pre>";
print_r($data);
echo "</pre>";
}
// Close cURL resource
curl_close($ch);
?>
Call the API Sales=> receipt
<?php
// Software vendor's API key
$softwareApiKey = 'ygyu';
// User's API key
$userApiKey = 'yyugu';
// Combine the software API key and user API key
$combinedKeys = $softwareApiKey.':'.$userApiKey;
// Base64 encode the combined keys
$encodedKeys = base64_encode($combinedKeys);
$licenseNumber = "C12-1002-LIC";
// METRC endpoint with additional date parameters
$url = 'https://sandbox-api-ca.metrc.com/packages/v2/active?licenseNumber='.$licenseNumber;
//$url = "https://sandbox-api-ca.metrc.com/facilities/v2/";
// Create a new cURL resource
$ch = curl_init($url);
// Set the authorization header using the encoded keys
$headers = [
'Authorization: Basic ' . $encodedKeys,
'Content-Type: application/json',
'Accept: application/json'
];
// Set cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
// Check for errors and HTTP status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Status Code: " . $httpCode . "<br>";
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Decode the JSON response
$data = json_decode($response, true);
echo "<pre>";
print_r($data);
echo "</pre>";
}
// Close cURL resource
curl_close($ch);
?>
Step 4: Handling Responses and Errors
The response will provide information about the success or failure of the request. Always handle these responses to log successful transactions and debug any errors.
Step 5: Testing in Sandbox
Once the integration is complete, thoroughly test the solution in the sandbox environment. Test all possible sales scenarios, including different product types, quantities, and discounts, to ensure compliance with METRC’s requirements.
Conclusion
Integrating METRC into your POS system requires careful attention to compliance and technical detail. By following the steps above and ensuring robust error handling, logging, and state-specific compliance, you can successfully pass the METRC test and create a seamless integration with the METRC API using PHP.