admin charge and view in stripe

This commit is contained in:
Chris Smith
2019-05-31 16:12:18 -05:00
parent 328ccf6ca6
commit 94a7a80b50
4 changed files with 98 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
<?php
use Bramus\Router\Router;
use Postmark\PostmarkClient;
use RedBeanPHP\R;
use Stripe\Charge;
use Stripe\Customer;
@@ -176,6 +177,89 @@ $router->get('/admin/guest/checkout/{id}', function ($id) {
include 'views/common/footer.php';
});
$router->post('/admin/guest/checkout/{id}', function ($id) {
$guest = R::load('guests', $id);
$totalChargeCents = convertPossibleFloatToCents($_POST['totalCharge']);
$guest->checkout_cents = $totalChargeCents;
Stripe::setApiKey($_SERVER['STRIPE_API_SECRET_KEY']);
if (isset($_POST['sendViaStripe']) && $_POST['sendViaStripe'] == 'on') {
if (empty($guest->stripe_id)) {
$customer = Customer::create([
'description' => $guest->name . ' - ' . $guest->email,
'email' => $guest->email,
]);
} else {
$customer = $guest->stripe_id;
}
$invoiceItem = \Stripe\InvoiceItem::create([
'customer' => $customer,
'amount' => $guest->checkout_cents,
'currency' => 'usd',
'description' => 'Dinner in the Woods ' . date('Y'),
]);
/** @var $invoice \Stripe\Invoice*/
$invoice = \Stripe\Invoice::create([
'customer' => $customer,
'billing' => 'send_invoice',
'days_until_due' => 1,
'description' => 'Dinner in the Woods ' . date('Y'),
]);
$invoice->finalizeInvoice();
$client = new Postmark\PostmarkClient($_SERVER['POSTMARK_API_KEY']);
$client->sendEmailWithTemplate(
$_SERVER['POSTMARK_FROM'],
$guest->email,
$_SERVER['POSTMARK_TEMPLATE_STRIPE_INVOICE'],
[
'name' => $guest->name,
'product_name' => 'Dinner in the Woods ' . date('Y'),
'action_receipt_url' => $invoice->hosted_invoice_url,
],
true, //inline css
null, //tag
true, //track opens
null, //reply to
null //cc
);
R::store($guest);
header('Location: /admin/guest/list/?action=success&msg=Invoice created for ' . $guest->name);
} else {
if (!empty($guest->stripe_id)) {
$charge = \Stripe\Charge::create([
'amount' => $guest->checkout_cents,
'currency' => 'usd',
'customer' => $guest->stripe_id,
]);
}
$client = new Postmark\PostmarkClient($_SERVER['POSTMARK_API_KEY']);
$client->sendEmailWithTemplate(
$_SERVER['POSTMARK_FROM'],
$guest->email,
$_SERVER['POSTMARK_TEMPLATE_STRIPE_RECEIPT'],
[
'name' => $guest->name,
'total' => $guest->checkout_cents/100,
'product_name' => 'Dinner in the Woods ' . date('Y'),
],
true, //inline css
null, //tag
true, //track opens
null, //reply to
null //cc
);
header('Location: /admin/guest/list/?action=success&msg=Charge created for ' . $guest->name);
}
});
$router->post('/admin/guest/add-card/{id}', function ($id) {
$guest = R::load('guests', $_POST['guestId']);
$guest->name = $_POST['name'];

View File

@@ -5,6 +5,7 @@ TABLE_TICKET_PRICE_2="450"
EVENT_TICKET_PRICE_3="75"
TABLE_TICKET_PRICE_3="500"
ENHANCER_TICKET_PRICE="20"
ENHANCER_TICKET_PRICE_SINGLE="2"
CHILDCARE_PRICE="25"
CABANA_PRICE="250"
DB_HOST="localhost"
@@ -13,6 +14,8 @@ DB_USER="root"
DB_PASS="root"
POSTMARK_API_KEY="special"
POSTMARK_TEMPLATE="identifier_for_template"
POSTMARK_TEMPLATE_STRIPE_INVOICE="identifier_for_template"
POSTMARK_TEMPLATE_STRIPE_RECEIPT="id_for_template"
POSTMARK_REMINDER_TEMPLATE="identifier_for_reminder_template"
POSTMARK_GUEST_TEMPLATE="identifier_for_template"
POSTMARK_FROM="sender@email.com"

View File

@@ -30,7 +30,7 @@
<div class="input-group-prepend">
<div class="input-group-text">$</div>
</div>
<input name="enhancerCharge" type="text" class="form-control txtCal" id="inlineFormInputGroup" value="<?=$guest->enhancer_qty * $_SERVER['ENHANCER_TICKET_PRICE']?>">
<input name="enhancerCharge" type="text" class="form-control txtCal" id="inlineFormInputGroup" value="<?=$guest->enhancer_qty * $_SERVER['ENHANCER_TICKET_PRICE_SINGLE']?>">
</div>
</div>
<div class="mb-3">
@@ -48,9 +48,16 @@
<div class="input-group-prepend">
<div class="input-group-text">$</div>
</div>
<input name="totalCharge" type="text" class="form-control" id="totalCharge" value="<?=$guest->enhancer_qty * $_SERVER['ENHANCER_TICKET_PRICE']?>" readonly>
<input name="totalCharge" type="text" class="form-control" id="totalCharge" value="<?=$guest->enhancer_qty * $_SERVER['ENHANCER_TICKET_PRICE_SINGLE']?>" readonly>
</div>
</div>
<?php
if (empty($guest->stripe_id)) {?>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="sendViaStripe" name="sendViaStripe">
<label class="form-check-label" for="sendViaStripe">Send invoice via stripe?</label>
</div>
<?php } ?>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Checkout</button>

View File

@@ -10,7 +10,7 @@
</div>
<div class="modal-body">
<div class="form-group">
<label for="inputEnhancers">How many enhancers packs are given (10 per pack)?</label>
<label for="inputEnhancers">How many enhancers sold (these are single tickets)?</label>
<input type="number" class="form-control" id="inputEnhancers" name="enhancerQty" value="0">
<input type="hidden" class="form-control" id="inputGuestId" name="guestId" value="0">
</div>
@@ -79,6 +79,7 @@
<a class="dropdown-item" href="/admin/guest/add-card/<?=$guest->id?>">Add Credit Card</a>
<?php } else { ?>
<a class="dropdown-item" href="/admin/guest/delete-card/<?=$guest->id?>">Delete Credit Card</a>
<a class="dropdown-item" href="https://dashboard.stripe.com/customers/<?=$guest->stripe_id?>">View in Stripe</a>
<?php } ?>
<a class="dropdown-item" href="/admin/guest/checkout/<?=$guest->id?>">Checkout</a>