mirror of
https://github.com/itflow-org/itflow
synced 2026-08-31 11:55:11 +00:00
Stripe ID creation/linking for guest flows (wip)
This commit is contained in:
@@ -61,15 +61,17 @@ if (!$stripe_public_key || !$stripe_secret_key) {
|
||||
<div class="col-md-6">
|
||||
|
||||
<?php if (!$stripe_customer_id) { ?>
|
||||
In order to set up automatic payments, you must create a customer record in Stripe.
|
||||
First, you must authorize Stripe to store your card details for the purpose of automatic payment.
|
||||
<br><br>
|
||||
In order to set up automatic payments, you must create a Stripe customer record.
|
||||
<br>
|
||||
By saving your card details, you grant consent for automatic payments.
|
||||
<small class="text-muted d-block mt-2">Stripe processes your information in accordance with its Privacy Policy and Terms.</small>
|
||||
<br>
|
||||
|
||||
<form action="post.php" method="POST">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-success" name="create_stripe_customer"><strong><i class="fas fa-check me-2"></i>I grant consent for automatic payments</strong></button>
|
||||
<button type="submit" class="btn btn-success" name="create_stripe_customer"><strong><i class="fas fa-check me-2"></i>Continue</strong></button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -121,7 +123,8 @@ if (!$stripe_public_key || !$stripe_secret_key) {
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<b>Add a new payment method</b>
|
||||
<p>Adding a new payment method will authorize us to charge your card for future invoices automatically.</p><br><br>
|
||||
<p>If you save payment details, you grant consent for automatic payments.</p>
|
||||
<br><br>
|
||||
|
||||
<input type="hidden" id="stripe_publishable_key" value="<?= $stripe_public_key ?>">
|
||||
<script src="https://js.stripe.com/v3/"></script>
|
||||
|
||||
@@ -64,14 +64,24 @@ if (isset($_GET['stripe_create_pi'])) {
|
||||
}
|
||||
|
||||
// Setup Stripe from payment_providers
|
||||
$stripe_provider = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT payment_provider_private_key FROM payment_providers WHERE payment_provider_name = 'Stripe' LIMIT 1"));
|
||||
$stripe_provider = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT payment_provider_id, payment_provider_private_key FROM payment_providers WHERE payment_provider_name = 'Stripe' LIMIT 1"));
|
||||
if (!$stripe_provider) {
|
||||
exit("Stripe not enabled / configured");
|
||||
}
|
||||
$stripe_provider_id = intval($stripe_provider['payment_provider_id']);
|
||||
$stripe_secret_key = $stripe_provider['payment_provider_private_key'];
|
||||
|
||||
require_once '../includes/stripe_init.php';
|
||||
|
||||
// Get client's Stripe customer ID
|
||||
$stripe_customer_query = mysqli_query($mysqli, "
|
||||
SELECT payment_provider_client FROM client_payment_provider
|
||||
WHERE client_id = $session_client_id AND payment_provider_id = $stripe_provider_id
|
||||
LIMIT 1
|
||||
");
|
||||
$stripe_customer = mysqli_fetch_assoc($stripe_customer_query);
|
||||
$stripe_customer_id = $stripe_customer ? escapeSql($stripe_customer['payment_provider_client']) : null;
|
||||
|
||||
$pi_description = "ITFlow: $client_name payment of $invoice_currency_code $balance_to_pay for $invoice_prefix$invoice_number";
|
||||
|
||||
try {
|
||||
@@ -88,6 +98,7 @@ if (isset($_GET['stripe_create_pi'])) {
|
||||
'itflow_invoice_id' => $invoice_id,
|
||||
],
|
||||
'payment_method_types' => ['card'],
|
||||
'customer' => $stripe_customer_id,
|
||||
]);
|
||||
|
||||
$output = [
|
||||
|
||||
116
guest/guest_pay_setup_stripe_customer.php
Normal file
116
guest/guest_pay_setup_stripe_customer.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
require_once 'includes/inc_all_guest.php';
|
||||
|
||||
// --- Get Stripe config from payment_providers table ---
|
||||
$stripe_provider = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT payment_provider_id, payment_provider_account, payment_provider_private_key, payment_provider_public_key FROM payment_providers"));
|
||||
|
||||
$stripe_provider_id = intval($stripe_provider['payment_provider_id']);
|
||||
$stripe_publishable = escapeHtml($stripe_provider['payment_provider_public_key']);
|
||||
$stripe_secret = escapeHtml($stripe_provider['payment_provider_private_key']);
|
||||
$stripe_account = intval($stripe_provider['payment_provider_account']);
|
||||
|
||||
// Show setup form
|
||||
if (isset($_GET['invoice_id'], $_GET['url_key'])) {
|
||||
|
||||
$invoice_url_key = escapeSql($_GET['url_key']);
|
||||
$invoice_id = intval($_GET['invoice_id']);
|
||||
|
||||
// Query invoice details
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT client_id, client_name, invoice_amount, invoice_currency_code, invoice_date,
|
||||
invoice_discount_amount, invoice_due, invoice_id, invoice_number, invoice_prefix,
|
||||
invoice_status FROM invoices
|
||||
LEFT JOIN clients ON invoice_client_id = client_id
|
||||
WHERE invoice_id = $invoice_id
|
||||
AND invoice_url_key = '$invoice_url_key'
|
||||
AND invoice_status NOT IN ('Draft', 'Paid', 'Cancelled')
|
||||
LIMIT 1"
|
||||
);
|
||||
|
||||
// Ensure valid invoice
|
||||
if (!$sql || mysqli_num_rows($sql) !== 1) {
|
||||
echo "<br><h2>Oops, something went wrong! Please ensure you have the correct URL and have not already paid this invoice.</h2>";
|
||||
require_once 'includes/guest_footer.php';
|
||||
error_log("Stripe payment error - Invoice with ID $invoice_id not found or not eligible.");
|
||||
exit();
|
||||
}
|
||||
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$invoice_id = intval($row['invoice_id']);
|
||||
$invoice_prefix = escapeHtml($row['invoice_prefix']);
|
||||
$invoice_number = intval($row['invoice_number']);
|
||||
$client_id = intval($row['client_id']);
|
||||
$client_name = escapeHtml($row['client_name']);
|
||||
|
||||
// Company info for currency formatting, etc
|
||||
$sql_company = mysqli_query($mysqli, "SELECT * FROM companies WHERE company_id = 1");
|
||||
$company_row = mysqli_fetch_assoc($sql_company);
|
||||
$company_locale = escapeHtml($company_row['company_locale']);
|
||||
|
||||
// Get client's Stripe customer ID
|
||||
$stripe_customer_query = mysqli_query($mysqli, "
|
||||
SELECT payment_provider_client FROM client_payment_provider
|
||||
WHERE client_id = $client_id AND payment_provider_id = $stripe_provider_id
|
||||
LIMIT 1
|
||||
");
|
||||
$stripe_customer = mysqli_fetch_assoc($stripe_customer_query);
|
||||
$stripe_customer_id = $stripe_customer ? escapeSql($stripe_customer['payment_provider_client']) : null;
|
||||
|
||||
if (!$stripe_customer_id) { ?>
|
||||
<br><br>
|
||||
<h2>Setup Stripe payments for <?php echo $client_name; ?></h2>
|
||||
In order to make online payments, please create a Stripe customer record for <?php echo $client_name; ?>.
|
||||
<p>If you save payment details in future, you also grant consent for automatic payments.</p>
|
||||
|
||||
<div class="row g-3">
|
||||
<div class="col-12 col-md-8 col-lg-6">
|
||||
<form action="guest_post.php" method="POST">
|
||||
<input type="hidden" name="invoice_id" value="<?php echo $invoice_id; ?>">
|
||||
<input type="hidden" name="url_key" value="<?php echo escapeHtml($invoice_url_key); ?>">
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-floating">
|
||||
<input type="text" class="form-control" id="stripe_cust_name" disabled value="<?php echo $client_name; ?>">
|
||||
<label for="stripe_cust_name">Client</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-floating">
|
||||
<input type="text" class="form-control" id="stripe_name" name="name" placeholder="Name" autocomplete="name" required>
|
||||
<label for="stripe_name">Your Name</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-floating">
|
||||
<input type="email" class="form-control" id="stripe_email" name="email" placeholder="Email" autocomplete="email" required>
|
||||
<label for="stripe_email">Your Email</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<small class="text-muted d-block mt-2">Stripe processes your information in accordance with its Privacy Policy and Terms.</small>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<button type="submit" class="btn btn-success" name="create_stripe_customer"><strong><i class="fas fa-check me-2"></i>Continue</strong></button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php }
|
||||
else {
|
||||
echo "<br><h2>Stripe customer record already exists.</h2>";
|
||||
echo "<p>You can now proceed to pay your invoice.</p>";
|
||||
echo "<a href='guest_view_invoice.php?invoice_id=$invoice_id&url_key=" . urlencode($invoice_url_key) . "' class='btn btn-primary'>View Invoice</a>";
|
||||
}
|
||||
|
||||
} else {
|
||||
exit("Error.");
|
||||
}
|
||||
|
||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
|
||||
@@ -824,3 +824,120 @@ if (isset($_POST['guest_quote_upload_file'])) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['create_stripe_customer'])) {
|
||||
|
||||
$invoice_url_key = escapeSql($_POST['url_key']);
|
||||
$invoice_id = intval($_POST['invoice_id']);
|
||||
$name = escapeSql($_POST['name']);
|
||||
$email = escapeSql($_POST['email']);
|
||||
|
||||
// Query invoice details
|
||||
$sql = mysqli_query(
|
||||
$mysqli,
|
||||
"SELECT client_id, client_name, invoice_amount, invoice_currency_code, invoice_date,
|
||||
invoice_discount_amount, invoice_due, invoice_id, invoice_number, invoice_prefix,
|
||||
invoice_status FROM invoices
|
||||
LEFT JOIN clients ON invoice_client_id = client_id
|
||||
WHERE invoice_id = $invoice_id
|
||||
AND invoice_url_key = '$invoice_url_key'
|
||||
AND invoice_status NOT IN ('Draft', 'Paid', 'Cancelled')
|
||||
LIMIT 1"
|
||||
);
|
||||
|
||||
// Ensure valid invoice
|
||||
if (!$sql || mysqli_num_rows($sql) !== 1) {
|
||||
echo "<br><h2>Oops, something went wrong! Please ensure you have the correct URL and have not already paid this invoice.</h2>";
|
||||
require_once 'includes/guest_footer.php';
|
||||
error_log("Stripe payment error - Invoice with ID $invoice_id not found or not eligible.");
|
||||
exit();
|
||||
}
|
||||
|
||||
$row = mysqli_fetch_assoc($sql);
|
||||
$invoice_id = intval($row['invoice_id']);
|
||||
$invoice_prefix = escapeHtml($row['invoice_prefix']);
|
||||
$invoice_number = intval($row['invoice_number']);
|
||||
$client_id = intval($row['client_id']);
|
||||
$client_name = escapeHtml($row['client_name']);
|
||||
|
||||
// Get Stripe provider config
|
||||
$stripe_provider_result = mysqli_query($mysqli, "
|
||||
SELECT payment_provider_id, payment_provider_private_key
|
||||
FROM payment_providers
|
||||
WHERE payment_provider_name = 'Stripe'
|
||||
AND payment_provider_active = 1
|
||||
LIMIT 1
|
||||
");
|
||||
|
||||
$stripe_provider = mysqli_fetch_assoc($stripe_provider_result);
|
||||
if (!$stripe_provider) {
|
||||
flashAlert("Stripe provider is not configured in the system.", 'danger');
|
||||
redirect("saved_payment_methods.php");
|
||||
}
|
||||
|
||||
$stripe_provider_id = intval($stripe_provider['payment_provider_id']);
|
||||
$stripe_secret_key = escapeHtml($stripe_provider['payment_provider_private_key']);
|
||||
|
||||
if (empty($stripe_secret_key)) {
|
||||
flashAlert("Stripe credentials missing. Please contact support.", 'danger');
|
||||
redirect("saved_payment_methods.php");
|
||||
}
|
||||
|
||||
// Check if client already has a Stripe customer
|
||||
$existing_customer = mysqli_fetch_assoc(mysqli_query($mysqli, "
|
||||
SELECT payment_provider_client
|
||||
FROM client_payment_provider
|
||||
WHERE client_id = $client_id
|
||||
AND payment_provider_id = $stripe_provider_id
|
||||
LIMIT 1
|
||||
"));
|
||||
|
||||
if (!$existing_customer) {
|
||||
try {
|
||||
// Initialize Stripe
|
||||
require_once '../includes/stripe_init.php';
|
||||
$stripe = new \Stripe\StripeClient($stripe_secret_key);
|
||||
|
||||
// Create new customer in Stripe
|
||||
$customer = $stripe->customers->create([
|
||||
'name' => $client_name,
|
||||
'email' => $email,
|
||||
'metadata' => [
|
||||
'itflow_client_id' => $client_id,
|
||||
'consent_by' => $name,
|
||||
]
|
||||
]);
|
||||
|
||||
$stripe_customer_id = escapeSql($customer->id);
|
||||
|
||||
// Insert customer into client_payment_provider
|
||||
mysqli_query($mysqli, "
|
||||
INSERT INTO client_payment_provider
|
||||
SET client_id = $client_id,
|
||||
payment_provider_id = $stripe_provider_id,
|
||||
payment_provider_client = '$stripe_customer_id',
|
||||
client_payment_provider_created_at = NOW()
|
||||
");
|
||||
|
||||
logAudit("Stripe", "Create", "Guest $name created Stripe customer for $client_name as $stripe_customer_id and authorized future automatic payments", $client_id);
|
||||
|
||||
flashAlert("Stripe customer created. Thank you for your consent.");
|
||||
|
||||
} catch (Exception $e) {
|
||||
$error = $e->getMessage();
|
||||
|
||||
error_log("Stripe error while creating customer for $client_name: $error");
|
||||
|
||||
logApp("Stripe", "error", "Failed to create Stripe customer for $client_name: $error");
|
||||
|
||||
flashAlert("An error occurred while creating your Stripe customer. Please try again.", 'danger');
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
flashAlert("Stripe customer already exists for your account.", 'danger');
|
||||
}
|
||||
|
||||
redirect('guest_view_invoice.php?invoice_id=' . $invoice_id . '&url_key=' . urlencode($invoice_url_key));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user