From 43095c62414eb0b2dc9e351e9f39c0af7a43a079 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 11 Apr 2019 11:54:37 -0600 Subject: [PATCH] Prevent empty registration, send Stripe email receipts --- public/actions/submit.php | 41 ++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/public/actions/submit.php b/public/actions/submit.php index 60e5626..93702d7 100644 --- a/public/actions/submit.php +++ b/public/actions/submit.php @@ -25,6 +25,8 @@ $database->action(function($database) { $dueusd = 0.0; + $emails = []; + try { $people = $_POST['people']; @@ -40,6 +42,10 @@ $database->action(function($database) { "sex" => ["M", "F"] ]; + if (count($people['ids']) == 0) { + errorBack("You need to register at least one person."); + } + foreach ($people['ids'] as $pid) { // Clear these out $camperid = null; @@ -173,6 +179,10 @@ $database->action(function($database) { "shirt" => $people["shirt"][$pid], "sex" => $people["sex"][$pid] ]); + + if (!empty($people["email"][$pid])) { + $emails[] = $people["email"][$pid]; + } } } catch (Exception $ex) { errorBack($ex->getMessage()); @@ -203,29 +213,34 @@ $database->action(function($database) { try { \Stripe\Stripe::setApiKey($SETTINGS["stripe"]["seckey"]); - $charge = \Stripe\Charge::create([ - 'amount' => $duecard * 100.0, - 'currency' => 'usd', - 'description' => 'Day Camp', - 'source' => $_POST['stripeToken'], - 'statement_descriptor' => 'PPD Day Camp', - ]); + $chargedata = [ + 'amount' => $duecard * 100.0, + 'currency' => 'usd', + 'description' => 'Day Camp ' . date('Y'), + 'source' => $_POST['stripeToken'] + ]; + + if (count($emails) > 0) { + $chargedata['receipt_email'] = $emails[0]; + } + + $charge = \Stripe\Charge::create($chargedata); } catch (\Stripe\Error\Card $e) { $body = $e->getJsonBody(); $err = $body['error']; errorBack("We couldn't process your card because it was declined. Your card issuer or bank sent us this message: " . $err["message"] . " That's all we know."); } catch (\Stripe\Error\RateLimit $e) { - errorBack("We couldn't process your card because things are happening too fast. Please try again in a minute. (Error code: STRIPE_RATELIMIT)"); + errorBack("We couldn't process your card because things are happening too fast. Please try again in a minute. Your card was not charged. (Error code: STRIPE_RATELIMIT)"); } catch (\Stripe\Error\InvalidRequest $e) { - errorBack("We couldn't process your card because of a technical issue. Please try again later. (Error code: STRIPE_INVREQ)"); + errorBack("We couldn't process your card because of a technical issue. Please try again later. Your card was not charged. (Error code: STRIPE_INVREQ)"); } catch (\Stripe\Error\Authentication $e) { - errorBack("We can't connect to the card processor. Please try again later. (Error code: STRIPE_AUTH)"); + errorBack("We can't connect to the card processor. Please try again later. Your card was not charged. (Error code: STRIPE_AUTH)"); } catch (\Stripe\Error\ApiConnection $e) { - errorBack("We can't connect to the card processor. Please try again later. (Error code: STRIPE_NOAPI)"); + errorBack("We can't connect to the card processor. Please try again later. Your card was not charged. (Error code: STRIPE_NOAPI)"); } catch (\Stripe\Error\Base $e) { - errorBack("An unknown payment error occurred. Please try again later."); + errorBack("An unknown payment error occurred. Please try again later. Your card was not charged."); } catch (Exception $e) { - errorBack("An unknown error occurred. Please try again later."); + errorBack("An unknown error occurred. Please try again later. Your card was not charged."); } }