'Net 10', 21 => 'Net 120', 1 => 'Net 15', 2 => 'Net 30', 16 => 'Net 45', 3 => 'Net 60', 7 => 'Net 7', 20 => 'Net 90', ]; public function __construct( CartRepositoryInterface $cartRepository, NetSuiteAPI $netSuiteAPI, NetSuiteLogger $netSuiteLogger, protected CustomerRepository $customerRepository ) { $this->netSuiteAPI = $netSuiteAPI; $this->netSuiteLogger = $netSuiteLogger; $this->cartRepository = $cartRepository; } protected function updateCustomer(CartInterface $quote, int $netsuiteId, $cseg_ncg_rep_office) { $customer = $quote->getCustomer(); $old_netsuite_cseg_ncg_rep_office = $customer->getExtensionAttributes()?->getNetsuiteCsegNcgRepOffice(); if (!$old_netsuite_cseg_ncg_rep_office) { $service = sprintf('record/v1/customer/%d', $netsuiteId); try { if (!$cseg_ncg_rep_office) { $response = $this->netSuiteAPI->get($service); $cseg_ncg_rep_office = isset($response['response']['cseg_ncg_rep_office']['id'])? $response['response']['cseg_ncg_rep_office']['id']:0; } // $this->netSuiteLogger->warning('netsuite_cseg_ncg_rep_office', [ // 'netsuite_cseg_ncg_rep_office' => $cseg_ncg_rep_office, // '!$customer->getData(\'netsuite_cseg_ncg_rep_office\')' => var_export(!$old_netsuite_cseg_ncg_rep_office, true) // ] // ); if ($cseg_ncg_rep_office) { $customer->getExtensionAttributes()?->setNetsuiteCsegNcgRepOffice($cseg_ncg_rep_office); $this->customerRepository->save($customer); } // $this->netSuiteLogger->warning('netsuite_cseg_ncg_rep_office after save', [ // '$customer->getExtensionAttributes()?->getNetsuiteCsegNcgRepOffice()' => $customer->getExtensionAttributes()?->getNetsuiteCsegNcgRepOffice() // ] // ); } catch (\Exception $e) { $this->netSuiteLogger->error( sprintf('update customer cseg_ncg_rep_office %s exception: ', $quote->getCustomerEmail()). $e->getMessage(), $e->getTrace() ); } } return $this; } /** * @param CartInterface $quote * @param int $netsuiteId * @return bool */ protected function isNettermAvailable(CartInterface $quote, int $netsuiteId) { $result = false; $cseg_ncg_rep_office = null; if (!$quote->getNetsuitePaymentId()) { $service = sprintf('record/v1/customer/%d', $netsuiteId); try { $response = $this->netSuiteAPI->get($service); $this->netSuiteLogger->warning('netsuite customer api response', $response); $nettermId = isset($response['response']['terms']['id'])? intval($response['response']['terms']['id']):0; $cseg_ncg_rep_office = isset($response['response']['cseg_ncg_rep_office']['id'])? $response['response']['cseg_ncg_rep_office']['id']:0; if ($nettermId && in_array($nettermId, array_keys($this->nettermPayments)) ) { $result = true; $quote->setNetsuitePaymentId($nettermId); $this->cartRepository->save($quote); } } catch (\Exception $e) { $this->netSuiteLogger->error( sprintf('check customer %s exception: ', $quote->getCustomerEmail()). $e->getMessage(), $e->getTrace() ); } } else { $result = in_array(intval($quote->getNetsuitePaymentId()), array_keys($this->nettermPayments)); } $this->updateCustomer($quote, $netsuiteId, $cseg_ncg_rep_office); return $result; } public function execute(Observer $observer) { /* @var $checkResult DataObject */ $checkResult = $observer->getData('result'); /* @var $methodInstance Netterm */ $methodInstance = $observer->getData('method_instance'); /* @var $quote CartInterface */ $quote = $observer->getData('quote'); if ($methodInstance->getCode()==Netterm::PAYMENT_METHOD_NETTERM_CODE) { $checkResult->setData('is_available', false); $dataCustomer = $quote->getCustomer(); $netsuiteId = $dataCustomer->getCustomAttribute('netsuite_id')?->getValue(); if ($netsuiteId) { $checkResult->setData('is_available', $this->isNettermAvailable($quote, $netsuiteId)); } } } }