Add an REST API call to get all keys by order-id
-
I would need the ability to get all keys generated for a given order-id, i have forked an older version to do that would be really great if you could add it to the official distribution.
includes\api\v2\Licenses.php
/** * GET licenses/by-order/{order_id} * * Retrieves a single licenses from the database. */ register_rest_route( $this->namespace, $this->rest_base . '/by-order/(?P<order_id>[\w-]+)', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'getLicensesByOrder'), 'permission_callback' => array($this, 'permissionCallback'), 'args' => array( 'order_id' => array( 'description' => 'Order ID', 'type' => 'integer', ) ) ) ) );
and
public function getLicensesByOrder(WP_REST_Request $request)
{
if (!$this->isRouteEnabled($this->settings, '010')) {
return $this->routeDisabledError();
}
if (!$this->permissionCheck('license', 'read')) {
return new WP_Error(
'lmfwc_rest_cannot_view',
__('Sorry, you cannot list resources.', 'license-manager-for-woocommerce'),
array(
'status' => $this->authorizationRequiredCode()
)
);
}
$orderId = absint($request->get_param('order_id'));
if (!$orderId) {
return new WP_Error(
'lmfwc_rest_data_error',
'Order ID invalid.',
array('status' => 404)
);
}
try {
/** @var LicenseResourceModel[] $licenses */
$licenses = LicenseResourceRepository::instance()->findAllBy(
array(
'order_id' => $orderId
)
);
} catch (Exception $e) {
return new WP_Error(
'lmfwc_rest_data_error',
$e->getMessage(),
array('status' => 404)
);
}
if (!$licenses) {
return new WP_Error(
'lmfwc_rest_data_error',
'No License Keys available',
array('status' => 404)
);
}
$response = array();
/** @var LicenseResourceModel $license */
foreach ($licenses as $license) {
$licenseData = $license->toArray();
// Remove the hash, decrypt the license key, and add it to the response
unset($licenseData['hash']);
$licenseData['licenseKey'] = $license->getDecryptedLicenseKey();
$response[] = $licenseData;
}
return $this->response(true, $response, 200, 'v2/licenses/by-order');
}
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.