function process($parameters) { $extras = new Extras(); $fakturoid = new FakturoidWrapper(); $action = $extras->sanitize($parameters[0]); switch ($action) { case 'add': $paymentId = $extras->sanitize($_POST['paymentId']); $price = $extras->sanitize($_POST['price']); $description = $extras->sanitize($_POST['description']); $result = $extras->checkAddValues($paymentId, $price, $description); if ($result['s'] == 'success') { $status = $extras->getStatusOfPayment($paymentId); //allow add extra only when new payment will be generated if (!in_array($status, ['unpaid', 'refund', 'timeout'])) { $this->messages[] = ['s' => 'error', 'cs' => 'Bohužel, položka nebyla přidána; platba se právě platí nebo je již zaplacená', 'en' => 'Sorry, we cannot add an extra; payment is processing']; } else { $invoiceFakturoidId = $fakturoid->getFakturoidInvoiceIdFromPaymentId($paymentId); $extraFakturoidId = $fakturoid->addExtra($invoiceFakturoidId, $price, $description); $result = $extras->addExtra($paymentId, $price, $description, $extraFakturoidId); $this->messages[] = $result; } } $this->redirect('checkUsers'); break; case 'addBlank': $userId = $extras->sanitize($_POST['userId']); $price = $extras->sanitize($_POST['price']); $description = $extras->sanitize($_POST['description']); $result = $extras->checkAddBlankValues($userId, $price, $description); if ($result['s'] == 'success') { $this->messages[] = $extras->addBlankExtra($userId, $price, $description); } else { $this->messages[] = $result; } $this->redirect('checkUsers'); break; case 'delete': $extraId = $parameters[1]; $status = $extras->getStatusOfPaymentFromExtraId($extraId); //allow add extra only when new payment will be generated or is blank if (!in_array($status, ['unpaid', 'refund', 'timeout', null])) { $this->messages[] = ['s' => 'error', 'cs' => 'Bohužel, položka nebyla zrušena; platba se právě platí nebo je již zaplacená', 'en' => 'Sorry, we cannot cancel an extra; payment is processing']; } else { $extraFakturoidId = $fakturoid->getExtraFakturoidId($extraId); $invoiceFakturoidId = $fakturoid->getInvoiceFakturoidIdFromExtraId($extraId); $fakturoid->deleteExtra($invoiceFakturoidId, $extraFakturoidId); $result = $extras->deleteExtra($extraId); $this->messages[] = $result; } $this->redirect('checkUsers'); break; default: $this->redirect('error'); } }
private function createPayment($user, $tariff, $beginningDate, $lang) { $userId = $user['id_user']; $tariffId = $tariff['id_tariff']; $tariffName = $this->getTariffName($tariffId, 'cs'); //invoice is in czech only $priceCZK = $tariff['priceCZK']; $fakturoid = new FakturoidWrapper(); $fakturoidInvoice = $fakturoid->createInvoice($user, $tariff['priceCZK'], $tariffName, $beginningDate, $lang); if (!$fakturoidInvoice) { return ['s' => 'error', 'cs' => 'Nepovedlo se spojení s fakturoid.cz. Zkuste to prosím za pár minut', 'en' => 'We are unable to connect to fakturoid.cz. Try again in a few minutes']; } $fakturoidInvoiceId = $fakturoidInvoice->id; $fakturoidInvoiceNumber = $fakturoidInvoice->number; $now = date('Y-m-d H-i-s'); Db::queryModify(' INSERT INTO `payments` ( `id_payer`, `payment_first_date`, `status`, `time_generated`, `tariff_id`, `price_CZK`, `invoice_fakturoid_id`, `invoice_fakturoid_number` ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [$userId, date('Y-m-d', $beginningDate), 'unpaid', $now, $tariffId, $priceCZK, $fakturoidInvoiceId, $fakturoidInvoiceNumber]); //add blank extras $extras = new Extras(); $blankExtras = $extras->getBlankExtras($user['id_user']); if (!empty($blankExtras)) { foreach ($blankExtras as $extra) { $extraId = $extra['id_extra']; $price = $extra['priceCZK']; $description = $extra['description']; $fakturoidExtraId = $fakturoid->addExtra($fakturoidInvoiceId, $extra['priceCZK'], $extra['description']); $paymentId = $this->getPaymentIdFromFakturoidInvoiceId($fakturoidInvoiceId); $extras->assignBlankExtra($paymentId, $price, $description, $fakturoidExtraId, $extraId); } } //send email to user $subject = NAME . ' Paralelní Polis - nová faktura'; $link = ROOT . '/cs/payments'; $message = 'Ahoj,<br/> <br/> vystavili jsem ti fakturu za členství / pronájem v Paper Hub v Paralelní Polis.<br/> <a href="' . $link . '">' . $link . '</a><br/> Platbu uhradíš jednoduše na odkazu výše.<br/> <br/> Díky za rychlou platbu!<br/> Paper Hub'; $this->sendEmail(EMAIL, $user['email'], $subject, $message); //and send copy of email to hub manager //TODO refractor $this->sendEmail(EMAIL, EMAIL_HUB_MANAGER, NAME . ' - Poslána výzva o nové faktuře na email ' . $user['email'], $message); return ['s' => 'success']; }