{strtolower($type)} = $endPoint; return $this; } /** * @param Persona|Propietario $persona * @return array * @throws InvalidResult */ public function sendPersona(Model\Persona|Model\Venta\Propietario $persona): array { $rut = implode('', [$persona->rut, strtoupper($persona->dv)]); try { return $this->customer->getById($rut); } catch (InvalidResult $exception) { $datos = $persona->datos; $customerData = [ 'rut' => $rut, 'nombreCompleto' => $persona->nombreCompleto(), 'email' => $datos?->email ?? '', 'telefono' => $datos?->telefono ?? '' ]; try { if (!$this->customer->add($customerData)) { throw new InvalidResult("Could not save Customer for Persona {$rut}", 409, $exception); } } catch (EmptyResponse $exception) { throw new InvalidResult("Could not save Customer for Persona {$rut}", 409, $exception); } return $this->customer->getById($rut); } } /** * @param Model\Venta $venta * @return array * @throws InvalidResult */ public function sendVenta(Model\Venta $venta): array { $customer = $this->sendPersona($venta->propietario()); try { return $this->subscription->getById($venta->id); } catch (InvalidResult $exception) { $subscriptionData = [ 'customer' => $customer['toku_id'], 'product_id' => $venta->id, 'venta' => $venta ]; try { if (!$this->subscription->add($subscriptionData)) { throw new InvalidResult("Could not save Subscription for Venta {$venta->id}", 409, $exception); } } catch (EmptyResponse $exception) { throw new InvalidResult("Could not save Subscription for Venta {$venta->id}", 409, $exception); } return $this->subscription->getById($venta->id); } } /** * @param Model\Venta $venta * @param array $cuotas_ids * @return array * @throws InvalidResult */ public function sendCuotas(Model\Venta $venta, array $cuotas_ids = []): array { $customer = $this->sendPersona($venta->propietario()); $subscription = $this->sendVenta($venta); $invoices = []; $errors = []; foreach ($venta->formaPago()->pie->cuotas() as $cuota) { if (count($cuotas_ids) > 0 and !in_array($cuota->id, $cuotas_ids)) { continue; } try { $invoices []= $this->invoice->getById($cuota->id); } catch (InvalidResult $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; } /** * @param array $request * @return bool * @throws InvalidResult */ public function updatePago(array $request): bool { # If $customer is not found, it will throw an exception and stop $customer = $this->customer->getByExternalId($request['customer']); $invoice = $this->invoice->getByExternalId($request['invoice']); return $this->invoice->update($invoice['id'], $request); } }