/** * @param Customer $customer * @return bool */ public function createOrderForContract($customer) { //check if user have one order item $this->authService->logMessage('Start add order for contractId ' . $customer->getBillerfoxContractId()); if (!$this->authService->accessToken) { $this->authService->setConfigDefault()->auth(); } if (($orders = $this->getOrdersForOneContract($customer->getBillerfoxContractId(), true)) && (int) $orders['count'] != 0) { if (isset($orders['items'])) { foreach ($orders['items'] as $order) { if (isset($order['order_items']) && count($order['order_items']) > 0) { if (isset($order['order_items'][0]['id']) && ($orderItemId = $order['order_items'][0]['id'])) { $options['form_params'] = ['access_token' => $this->authService->accessToken]; $response = $this->authService->getClient()->delete(Billing::UPDATE_ORDERS_PATH . Billing::API_DELIMITER . $order['id'] . Billing::UPDATE_ITEMS_PATH . Billing::API_DELIMITER . $orderItemId, $this->authService->getConfig() + $options); } } } } } $options['form_params'] = $this->prepareCustomerDataForOrder($customer) + ['access_token' => $this->authService->accessToken]; $order = $this->authService->getClient()->post(Billing::CONTRACT_PATH . Billing::API_DELIMITER . $customer->getBillerfoxContractId() . Billing::ADD_ORDERS_PATH, $this->authService->getConfig() + $options); if ($order->getStatusCode() == 201) { $this->authService->initLastLogs(); $this->authService->logMessage('Customer was created successfully. Order Id: ' . $order->getHeader('resource_id')[0], null, null, '', false, true); return []; } else { $orderResponseContent = json_decode($order->getBody()->getContents()); $this->authService->logMessage('Contract Id was not returned and user was not created: ', $customer, '', '', false, true); $this->authService->setLastLogs($order); } return ['Create order error: ' . $orderResponseContent]; }
/** * @param Customer $customer * @param Form $form * @param bool $lastLogLine * @return bool */ public function updateContract(Customer $customer, $form = null, $lastLogLine = false) { if (!$this->authService->accessToken) { $this->authService->setConfigDefault()->auth(); } if (!$customer->getBillerfoxContractId()) { return $this->createContract($customer, $form); } $this->authService->logMessage('STARTING to UPDATE user data'); $options['form_params'] = $this->prepareCustomerData($customer) + ['access_token' => $this->authService->accessToken]; $contract = $this->authService->getClient()->put(Billing::CONTRACT_PATH . '/' . $customer->getBillerfoxContractId(), $this->authService->getConfig() + $options); if ($contract->getStatusCode() === 204) { $this->authService->initLastLogs(); $this->authService->logMessage('Customer was UPDATED successfully. Contract Id: ' . $contract->getHeader('resource_id')[0], null, null, '', false, $lastLogLine); return $contract->getHeader('resource_id')[0]; } else { $contractResponseContent = json_decode($contract->getBody()->getContents()); $this->authService->setFormErrors($form, $contractResponseContent); $this->authService->logMessage('Contract Id was not returned and user was not created: ', $customer, $form->getErrorsAsString(), '', false, $lastLogLine); $this->authService->setLastLogs($contract); } return false; }