diff --git a/index.php b/index.php index bc93ecb..c9106b1 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,7 @@ 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']; diff --git a/src/.env.example b/src/.env.example index b7abc15..2b3e4f8 100644 --- a/src/.env.example +++ b/src/.env.example @@ -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" diff --git a/views/admin-guest-checkout.php b/views/admin-guest-checkout.php index 8f0cbf1..7c7dca1 100644 --- a/views/admin-guest-checkout.php +++ b/views/admin-guest-checkout.php @@ -30,7 +30,7 @@
$
- +
@@ -48,9 +48,16 @@
$
- +
+ stripe_id)) {?> +
+ + +
+