mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 00:59:03 +00:00
186 lines
11 KiB
PHP
186 lines
11 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Autopay from an invoice's link - no sign-in. The Set up autopay button in
|
|
* an invoice's email and on its page opens it (autopayOffer(),
|
|
* functions/billing_pay.php): what autopay does for the recurring invoice
|
|
* the invoice came from - how much, how often, the card fee and whether this
|
|
* invoice is charged too - then their name, email and agreement, and
|
|
* Stripe's form for the card, on the page (js/guest_autopay_stripe.js).
|
|
* Stripe sends them back through guest_post.php (guest_autopay_done), which
|
|
* turns autopay on and comes back here. Once it is on, this says so.
|
|
*/
|
|
|
|
require_once "includes/inc_all_guest.php";
|
|
|
|
$invoice = billingRow('invoice', intval($_GET['invoice_id'] ?? 0), false);
|
|
$url_key = (string) ($_GET['url_key'] ?? '');
|
|
|
|
if (!$invoice || (string) $invoice['invoice_url_key'] === '' || !hash_equals((string) $invoice['invoice_url_key'], $url_key)) {
|
|
echo guestDeadEndHtml('fa-file-invoice-dollar', "This invoice link isn't working", "It may have been mistyped or replaced. Ask whoever sent it for a new link.");
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
// Paid long enough ago - its link has stopped working (functions/guest_link.php)
|
|
if (guestLinkExpired('invoice', $invoice['invoice_id'])) {
|
|
echo guestLinkExpiredHtml('invoice');
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
$invoice_id = intval($invoice['invoice_id']);
|
|
$client_id = intval($invoice['invoice_client_id']);
|
|
$number = billingNumber('invoice', $invoice);
|
|
$company = billingCompany();
|
|
$invoice_url = "guest_view_invoice.php?invoice_id=$invoice_id&url_key=" . rawurlencode($url_key);
|
|
$back = '<a class="btn btn-outline-primary" href="' . escapeHtml($invoice_url) . '"><i class="fas fa-file-invoice-dollar me-2"></i>View invoice ' . escapeHtml($number) . '</a>';
|
|
|
|
$recurring = intval($invoice['invoice_recurring_invoice_id'] ?? 0) ? billingRow('recurring', $invoice['invoice_recurring_invoice_id'], false) : null;
|
|
if (!$recurring || intval($recurring['recurring_invoice_client_id']) !== $client_id) {
|
|
echo str_replace('</p></div>', '</p>' . $back . '</div>', guestDeadEndHtml('fa-redo-alt', "Autopay isn't for this invoice", "Autopay pays the invoices that come regularly, and this one doesn't. Get in touch if you'd like to pay another way."));
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
$title = trim((string) $recurring['recurring_invoice_scope']) !== '' ? trim((string) $recurring['recurring_invoice_scope']) : billingNumber('recurring', $recurring);
|
|
|
|
// On already - what pays it, and whether that will go through
|
|
$autopay = intval($recurring['recurring_payment_id'] ?? 0) ? invoiceAutopay($invoice) : null;
|
|
if ($autopay) { ?>
|
|
<div class="itflow-asset-empty mt-5" data-autopay-on>
|
|
<i class="fas fa-check-circle fa-3x mb-3 text-success opacity-75"></i>
|
|
<h1 class="h4">Autopay is on</h1>
|
|
<p>Each invoice for <?= escapeHtml($title) ?> <?= $autopay['card'] ? 'is charged to ' . escapeHtml($autopay['method']) : 'is paid by ' . escapeHtml($autopay['method']) ?> on its due date - nothing to do.</p>
|
|
<?php if ($autopay['problem'] !== '') { ?>
|
|
<p class="text-warning-emphasis"><i class="fas fa-exclamation-triangle me-1"></i>It won't go through just now - <?= escapeHtml($autopay['problem']) ?>. Get in touch to change the card.</p>
|
|
<?php } ?>
|
|
<p class="small text-body-secondary"><?= escapeHtml(autopayChangeText()) ?></p>
|
|
<?= $back ?>
|
|
</div>
|
|
<?php
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
// Not offered - cards off, the offer turned off, the recurring invoice ended, or over the card limit
|
|
if (autopayOfferBlocked($recurring) !== '') {
|
|
echo str_replace('</p></div>', '</p>' . $back . '</div>', guestDeadEndHtml('fa-credit-card', "Autopay can't be set up here just now", "Get in touch and we'll set it up with you, or pay this invoice from its page."));
|
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
|
exit();
|
|
}
|
|
|
|
$offer = autopayOfferDetails($invoice, $recurring);
|
|
$state = invoiceState($invoice, invoicePaid($invoice_id));
|
|
$owed = in_array($state['state'], ['open', 'overdue'], true) && $state['balance'] > 0;
|
|
$money = function ($amount) use ($offer) {
|
|
return escapeHtml(billingMoney($amount, $offer['currency']));
|
|
};
|
|
|
|
// Signed in to the client portal as one of this client's contacts - their name and email to start with
|
|
$name = '';
|
|
$email = '';
|
|
if (!empty($_SESSION['client_logged_in']) && intval($_SESSION['client_id'] ?? 0) === $client_id) {
|
|
$me = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_name, contact_email FROM contacts WHERE contact_id = " . intval($_SESSION['contact_id'] ?? 0) . " AND contact_client_id = $client_id"));
|
|
$name = (string) ($me['contact_name'] ?? '');
|
|
$email = (string) ($me['contact_email'] ?? '');
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="row justify-content-center my-4">
|
|
<div class="col-lg-7 col-xl-6">
|
|
|
|
<div class="card mb-3" data-autopay>
|
|
<div class="card-body p-4">
|
|
|
|
<div class="d-flex align-items-center gap-3 mb-3">
|
|
<span class="itflow-asset-icon bg-primary-subtle text-primary-emphasis" aria-hidden="true"><i class="fas fa-sync-alt"></i></span>
|
|
<div>
|
|
<h1 class="h4 mb-0">Set up autopay</h1>
|
|
<div class="small text-body-secondary"><?= escapeHtml($recurring['client_name']) ?></div>
|
|
</div>
|
|
</div>
|
|
|
|
<p>Each invoice for <strong><?= escapeHtml($offer['title']) ?></strong> is charged to your card on its due date, with a receipt by email - no paying each one by hand.</p>
|
|
|
|
<ul class="list-group mb-4">
|
|
<li class="list-group-item d-flex justify-content-between gap-3">
|
|
<span class="text-body-secondary">Recurring invoice</span><span class="text-end"><?= escapeHtml($offer['number']) ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between gap-3">
|
|
<span class="text-body-secondary">Each <?= escapeHtml($offer['period']) ?></span><span class="itflow-money fw-semibold"><?= $money($offer['amount']) ?></span>
|
|
</li>
|
|
<li class="list-group-item d-flex justify-content-between gap-3">
|
|
<span class="text-body-secondary">Next invoice</span><span><?= escapeHtml(formatDate($recurring['recurring_invoice_next_date'])) ?></span>
|
|
</li>
|
|
<?php if ($owed) { ?>
|
|
<li class="list-group-item d-flex justify-content-between gap-3" data-autopay-this-invoice>
|
|
<span class="text-body-secondary">Invoice <?= escapeHtml($number) ?></span>
|
|
<span class="text-end"><?= $offer['this_invoice'] ? 'Charged too, on its due date, ' . escapeHtml(formatDate($invoice['invoice_due'])) : "Not charged - pay it as usual" ?></span>
|
|
</li>
|
|
<?php } ?>
|
|
<?php if ($offer['fee'] !== '') { ?>
|
|
<li class="list-group-item d-flex justify-content-between gap-3">
|
|
<span class="text-body-secondary">Card fee</span><span class="text-end"><?= escapeHtml(ucfirst($offer['fee'])) ?>, added to each charge</span>
|
|
</li>
|
|
<?php } ?>
|
|
</ul>
|
|
|
|
<form data-autopay-form data-stripe-key="<?= escapeHtml((string) billingStripeProvider()['payment_provider_public_key']) ?>" novalidate>
|
|
<input type="hidden" name="invoice_id" value="<?= $invoice_id ?>">
|
|
<input type="hidden" name="url_key" value="<?= escapeHtml($url_key) ?>">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label" for="autopayName">Your name</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text"><i class="fas fa-fw fa-user"></i></span>
|
|
<input type="text" class="form-control" id="autopayName" name="name" maxlength="80" autocomplete="name" required value="<?= escapeHtml($name) ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label" for="autopayEmail">Your email</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text"><i class="fas fa-fw fa-envelope"></i></span>
|
|
<input type="email" class="form-control" id="autopayEmail" name="email" maxlength="120" autocomplete="email" required value="<?= escapeHtml($email) ?>">
|
|
</div>
|
|
<div class="form-text">For the confirmation.</div>
|
|
</div>
|
|
|
|
<div class="form-check mb-3">
|
|
<input class="form-check-input" type="checkbox" id="autopayAgree" name="agree" value="1" required>
|
|
<label class="form-check-label small" for="autopayAgree"><?= escapeHtml(autopayConsentText($offer)) ?></label>
|
|
</div>
|
|
|
|
<div class="alert alert-danger small py-2" data-autopay-error role="alert" hidden></div>
|
|
|
|
<button type="submit" class="btn btn-primary btn-lg w-100" data-autopay-submit>
|
|
<span data-autopay-idle><i class="fas fa-credit-card me-2"></i>Continue to card details</span>
|
|
<span data-autopay-busy hidden><span class="spinner-border spinner-border-sm me-2" aria-hidden="true"></span>One moment</span>
|
|
</button>
|
|
</form>
|
|
|
|
<div data-autopay-checkout hidden>
|
|
<div id="autopay-checkout"></div>
|
|
</div>
|
|
|
|
<div class="alert alert-warning small mb-0" data-autopay-unavailable hidden>The card form couldn't load - check your connection and reload this page, or get in touch.</div>
|
|
|
|
<p class="small text-body-secondary mb-0 mt-3"><i class="fas fa-lock me-1"></i>Your card goes straight to Stripe, our payment processor - we never see its number. <?= escapeHtml(autopayChangeText()) ?></p>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="text-center mb-4">
|
|
<a class="small" href="<?= escapeHtml($invoice_url) ?>"><i class="fas fa-arrow-left me-1"></i>Back to invoice <?= escapeHtml($number) ?></a>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://js.stripe.com/v3/"></script>
|
|
<script src="<?= staticFileUrl('/js/guest_autopay_stripe.js') ?>"></script>
|
|
|
|
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|