Allows guests to add credit cards and receive a welcome email

This commit is contained in:
Chris Smith
2019-03-14 22:25:33 -05:00
parent 0e895c212c
commit 91d69b6f0e
3 changed files with 162 additions and 7 deletions

104
index.php
View File

@@ -26,12 +26,13 @@ $dotenv->required([
'STRIPE_API_PUBLIC_KEY',
]);
$_SERVER['receipt-url'] = $_SERVER['HTTP_HOST']."/thank-you/";
$_SERVER['manage-url'] = $_SERVER['HTTP_HOST']."/manage/";
$_SERVER['receipt-url'] = $_SERVER['HTTP_HOST'] . "/thank-you/";
$_SERVER['manage-url'] = $_SERVER['HTTP_HOST'] . "/manage/";
$_SERVER['manage-guest-url'] = $_SERVER['HTTP_HOST'] . "/guest/";
$router = new Router();
$r = R::setup('mysql:host=' . $_SERVER['DB_HOST'] . ';dbname=' . $_SERVER['DB_NAME'], $_SERVER['DB_USER'], $_SERVER['DB_PASS']);
R::freeze(true);
// Custom 404 Handler
$router->set404(function () {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
@@ -59,6 +60,39 @@ $router->get('/notify', function () {
include 'views/common/footer.php';
});
$router->get('/admin/orders', function () {
$orders = R::findAll('orders');
include 'views/common/head.php';
include 'views/admin.php';
include 'views/common/footer.php';
});
$router->get('/admin/order/{id}', function ($id) {
$order = R::load('orders', $id);
$guests = R::findAll('guests', ' order_id = ?', [$order->id]);
include 'views/common/head.php';
include 'views/admin-order-details.php';
include 'views/common/footer.php';
});
$router->post('/admin/order/{id}', function ($id) {
$order = R::load('orders', $id);
$parametersToSearch = $_POST['guestsArray'];
array_push($parametersToSearch, $order->id);
$guests = R::findAll('guests', ' id IN(' . R::genSlots($_POST['guestsArray']) . ') AND order_id = ?', $parametersToSearch);
foreach ($guests as $id => $guest) {
$guest->table = (int)$_POST['table'][$id];
$guest->paddle = (int)$_POST['paddle'][$id];
R::store($guest);
}
header('Location: /admin/order/' . $order->id . '?alert=success');
});
$router->get('/step-1', function () {
checkIfTicketsAreOnSale();
include 'views/common/head.php';
@@ -165,7 +199,6 @@ $router->post('/checkout', function () {
unset($guest, $uuid);
}
$client = new Postmark\PostmarkClient($_SERVER['POSTMARK_API_KEY']);
$orderedItems = [];
if ($eventTicketQty > 0) {
@@ -188,11 +221,12 @@ $router->post('/checkout', function () {
if (isset($stripeCustomerToken)) {
$paymentMethod = 'check_payment';
$paymentNote = $stripeCustomerToken;
}else{
} else {
$paymentMethod = 'credit_payment';
$paymentNote = true;
}
$client = new Postmark\PostmarkClient($_SERVER['POSTMARK_API_KEY']);
$client->sendEmailWithTemplate(
$_SERVER['POSTMARK_FROM'],
$order->email,
@@ -204,8 +238,8 @@ $router->post('/checkout', function () {
'receipt_id' => $order->id,
'receipt_details' => $orderedItems,
'total' => '$' . number_format(($order->total_cents / 100), 2),
'action_manage_guests_url' => 'https://'.$_SERVER['manage-url'] . $order->uuid,
'action_receipt_url' => 'https://'.$_SERVER['receipt-url'] . $order->uuid,
'action_manage_guests_url' => 'https://' . $_SERVER['manage-url'] . $order->uuid,
'action_receipt_url' => 'https://' . $_SERVER['receipt-url'] . $order->uuid,
$paymentMethod => $paymentNote
]
);
@@ -221,6 +255,38 @@ $router->get('/manage/{uuid}', function ($uuid) {
include 'views/common/footer.php';
});
$router->get('/guest/{uuid}', function ($uuid) {
$guest = R::findOne('guests', ' uuid = ?', [$uuid]);
include 'views/common/head.php';
include 'views/guest-manage.php';
include 'views/common/footer.php';
});
$router->post('/guest/{uuid}', function ($uuid) {
if ($uuid !== $_POST['uuid']) {
throw new Exception('Invalid form submission', 400);
}
$guest = R::findOne('guests', ' uuid = ?', [$uuid]);
// Check if credit checkout and valid
if (isset($_POST['stripeToken'])) {
Stripe::setApiKey($_SERVER['STRIPE_API_SECRET_KEY']);
$customer = Customer::create([
"description" => $guest->name . ' - ' . $guest->email,
"source" => $_POST['stripeToken'], // obtained with Stripe.js
]);
// make payment
$guest->stripe_id = $customer->id;
}
$guest->phone = $_POST['phone'];
$guest->childcare = $_POST['childcare'];
$guest->restrictions = $_POST['restrictions'];
R::store($guest);
header('Location: /guest/' . $guest->uuid . '?alert=success');
});
$router->get('/thank-you/{uuid}', function ($uuid) {
$order = R::findOne('orders', ' uuid = ?', [$uuid]);
$guests = R::findAll('guests', ' order_id = ?', [$order->id]);
@@ -244,15 +310,39 @@ $router->post('/manage/{uuid}', function ($uuid) {
$parametersToSearch = $_POST['guestsArray'];
array_push($parametersToSearch, $order->id);
$guests = R::findAll('guests', ' id IN(' . R::genSlots($_POST['guestsArray']) . ') AND order_id = ?', $parametersToSearch);
$client = new Postmark\PostmarkClient($_SERVER['POSTMARK_API_KEY']);
foreach ($guests as $id => $guest) {
if (isset($_POST['guests'][$id]['name'])) {
$guest->name = $_POST['guests'][$id]['name'];
if ($guest->email !== $_POST['guests'][$id]['email'] && !empty($_POST['guests'][$id]['email'])) {
$guestUuid = \Ramsey\Uuid\Uuid::uuid1();
$emailGuestInfo = true;
$guest->stripe_id = ''; // Clear stripe id if email changes
$guest->uuid = $guestUuid->toString(); // get new UUID if email changes
} else {
$emailGuestInfo = false;
}
$guest->email = $_POST['guests'][$id]['email'];
$guest->phone = $_POST['guests'][$id]['phone'];
$guest->childcare = $_POST['guests'][$id]['childcare'];
$guest->restrictions = $_POST['guests'][$id]['restrictions'];
R::store($guest);
if ($emailGuestInfo) {
$client->sendEmailWithTemplate(
$_SERVER['POSTMARK_FROM'],
$guest->email,
$_SERVER['POSTMARK_GUEST_TEMPLATE'],
[
'from_name' => $order->first_name . ' ' . $order->last_name,
'from_email' => $order->email,
'guest_name' => $guest->name,
'product_name' => 'Dinner in the Woods ' . date('Y'),
'action_manage_guests_url' => 'https://' . $_SERVER['manage-guest-url'] . $guest->uuid,
]
);
}
}
}

View File

@@ -9,6 +9,7 @@ DB_USER="root"
DB_PASS="root"
POSTMARK_API_KEY="special"
POSTMARK_TEMPLATE="identifier_for_template"
POSTMARK_GUEST_TEMPLATE="identifier_for_template"
POSTMARK_FROM="sender@email.com"
STRIPE_API_SECRET_KEY="sk"
STRIPE_API_PUBLIC_KEY="pk"

64
views/guest-manage.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
if (isset($_GET['alert']) && $_GET['alert'] == 'success') { ?>
<div class="alert alert-success" role="alert">Thank You for submitting your guest information.</div>
<?php }?>
<div class="row">
<div class="col-md-12 order-md-1">
<h3 class="mb-3">Your information</h3>
<p>Note: childcare is only available for NCM families for $25/child. Payment will be taken on the day of the event.</p>
<form class="needs-validation" novalidate method="POST" id="payment-form">
<input type="hidden" name="uuid" value="<?=$guest->uuid?>">
<h5 class="mb-3"><?=$guest->name?> <small><?=(empty($guest->table)) ? '' : 'Table #' . $guest->table; ?><?=(empty($guest->paddle)) ? '' : ', Paddle #' . $guest->paddle; ?></small></h5>
<div class="row">
<div class="col-md-3 mb-3">
<label for="lastName">Phone</label>
<input name="phone" type="text" class="form-control" id="phone" value="<?=$guest->phone?>">
</div>
<div class="col-md-2 mb-3">
<label for="childcare">Childcare?</label>
<select class="form-control" name="childcare">
<option value="0" <?php if (false == $guest->childcare) { echo 'selected'; } ?>>No</option>
<option value="1" <?php if (true == $guest->childcare) { echo 'selected'; } ?>>Yes</option>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="restrictions">Food preferences?</label>
<select class="form-control" name="restrictions">
<option value="0" <?php if (0 == $guest->restrictions) { echo 'selected'; } ?>>None</option>
<option value="1" <?php if (1 == $guest->restrictions) { echo 'selected'; } ?>>Vegetarian</option>
<option value="2" <?php if (2 == $guest->restrictions) { echo 'selected'; } ?>>Vegan</option>
</select>
</div>
</div>
<h5>Credit Card Details</h5>
<?php
if (empty($guest->stripe_id)) {
?>
<p>Adding your credit card number will make it quicker for you to buy drink tickets, egg tickets, and checkout
quicker with live auction items. <i>It is not required to save.</i></p>
<div id="creditDetails">
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div>
</div><br/>
<div>
<input id="check" type="checkbox" value="1" > Check here if you do not want to store your credit card.
<input id="credit" type="hidden" value="1" >
</div>
<?php }else {
echo '<p>Your credit card details are stored safely with our payment processor Stripe</p>';
} ?>
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block" type="submit">Save</button>
</form>
<br/><br/>
</div>
</div>