From 453c76e9de1fcebbed0a23671b1e0eb51e529f4a Mon Sep 17 00:00:00 2001 From: WhatTheServer Date: Sun, 11 Apr 2021 20:57:09 -0400 Subject: [PATCH] Update main.controller.php Fixed the subid not being properly saved into the tblcustomfields which causes it to not register as created. Should resolve: https://github.com/vultr/whmcs-vultr/issues/91 https://github.com/vultr/whmcs-vultr/issues/92 https://github.com/vultr/whmcs-vultr/issues/105 https://github.com/vultr/whmcs-vultr/issues/106 Tested with latest WHMCS Version: 8.1.3 PHP 7.3 and PHP7.4 Centos 7 --- servers/vultr/controller/main.controller.php | 42 +++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/servers/vultr/controller/main.controller.php b/servers/vultr/controller/main.controller.php index ad97a50..8a0d4fa 100755 --- a/servers/vultr/controller/main.controller.php +++ b/servers/vultr/controller/main.controller.php @@ -1,6 +1,6 @@ vultrAPI->create($vmParams); - - if (is_array($vm)) + + // For debugging only. Can comment out the create statement above and then uncomment the below to simulate being passed a SUBID/instanced id to confirm the weird issues. + // $vm = [ + // "SUBID" => "46109452", + // "v2_id" => "6593a82e-bd1f-4a5f-9396-322ffdd4bd22" + // ]; + + // die(var_dump($vm)); + // using if/else to detect v1 api subid/instance id or v2api + if (isset($vm['SUBID']) && mb_strlen($vm['SUBID']) == 8 ) { + $vmid = $vm['SUBID']; // $vm['SUBID'] or $vm['v2_id'] https://www.vultr.com/api/v1/#server_create + } elseif (isset($vm['v2_id'])) { + $vmid = $vm['v2_id']; // + } elseif (isset($vm['instance']['id'])) { + $vmid = $vm['instance']['id']; // $vm['instance']['id'] new v2API responses https://www.vultr.com/api/#operation/create-instance + } else { + $vmid = false; // no instance or server ID was found. + } + //die(var_dump($vmid)); + if ($vmid !== false) // Bail and show an error to contact support if no server id created or returned { - $this->addVMCustomFields($vm['SUBID']); + $this->addVMCustomFields($vmid); SessionHelper::setFlashMessage('success', LangHelper::T('main.create.created_success')); $this->redirect('clientarea.php?action=productdetails&id=' . $this->serviceID); } else { - SessionHelper::setFlashMessage('danger', $vm); + SessionHelper::setFlashMessage('danger', 'Server creation was unsuccessful! Please contact Support!. Do not try to recreate. It will fail.'); } } } @@ -372,27 +390,31 @@ public function createAction() } private function addVMCustomFields($SUBID) - { + { + $customField = Capsule::table('tblcustomfields') ->where('type', 'product') ->where('relid', $this->params['packageid']) ->where('fieldname', 'LIKE', 'subid|%')->get(); - if (count($customField) > 0) + if ($customField) { $customFieldValue = Capsule::table('tblcustomfieldsvalues') ->where('fieldid', $customField[0]->id) ->where('relid', $this->serviceID)->get(); - if (count($customFieldValue) > 0) - { + if ($customFieldValue) + { $customFieldValue = Capsule::table('tblcustomfieldsvalues') ->where('fieldid', $customField[0]->id) ->where('relid', $this->serviceID)->update(array('value' => $SUBID)); } else - { + { // die(" Die before insert normal :" . var_dump($this->serviceID).PHP_EOL); never hits here which was the old fallback and seems worthless?? Capsule::table('tblcustomfieldsvalues')->insert(array('fieldid' => $customField[0]->id, 'relid' => $this->serviceID, 'value' => $SUBID)); } + } + // die('Die before inserting tblcustomfieldsvalues '); // This is what was actually needed to fix the missing subid issue not being entered into tblcustomfieldsvalues. + Capsule::table('tblcustomfieldsvalues')->insert(array('fieldid' => $this->params['packageid'], 'relid' => $this->serviceID, 'value' => $SUBID)); } public function deleteAction()