{strtolower($type)} = $endPoint; return $this; } /** * @param Model\Persona $persona * @return array * @throws EmptyResponse */ public function sendPersona(Model\Persona|Model\Venta\Propietario $persona): array { $rut = implode('', [$persona->rut, strtoupper($persona->dv)]); try { return $this->customer->getById($rut); } catch (EmptyResponse $exception) { $datos = $persona->datos; $customerData = [ 'rut' => $rut, 'nombreCompleto' => $persona->nombreCompleto(), 'email' => $datos?->email ?? '', 'telefono' => $datos?->telefono ?? '' ]; if (!$this->customer->add($customerData)) { throw new EmptyResponse("Could not save Customer for Persona {$rut}", $exception); } return $this->customer->getById($rut); } } /** * @param Model\Venta $venta * @return bool * @throws EmptyResponse */ public function sendVenta(Model\Venta $venta): array { $customer = $this->sendPersona($venta->propietario()); try { return $this->subscription->getById($venta->id); } catch (EmptyResponse $exception) { $subscriptionData = [ 'customer' => $customer['toku_id'], 'product_id' => $venta->id, 'venta' => $venta ]; if (!$this->subscription->add($subscriptionData)) { throw new EmptyResponse("Could not save Subscription for Venta {$venta->id}", $exception); } return $this->subscription->getById($venta->id); } } /** * @param Model\Venta $venta * @return array * @throws EmptyResponse */ public function sendCuotas(Model\Venta $venta): array { $customer = $this->sendPersona($venta->propietario()); $subscription = $this->sendVenta($venta); $invoices = []; $errors = []; foreach ($venta->formaPago()->pie->cuotas() as $cuota) { try { $invoices []= $this->invoice->getById($cuota->id); } catch (EmptyResponse $exception) { try { $invoiceData = [ 'customer' => $customer['toku_id'], 'product_id' => $venta->id, 'subscription' => $subscription['toku_id'], 'cuota' => $cuota ]; if (!$this->invoice->add($invoiceData)) { throw new EmptyResponse("Could not add Invoice for Cuota {$cuota->id}", $exception); } $invoices []= $this->invoice->getById($cuota->id); } catch (EmptyResponse $exception) { $this->logger->warning($exception); $errors []= $exception; } } } if (count($errors) > 0) { $this->logger->warning("Revisar el envĂ­o de cuotas de la Venta {$venta->id}"); } return $invoices; } }