action(function($database) { $lastname = $_POST['familyname']; $father = $_POST['fathername']; $mother = $_POST['mothername']; if (empty($lastname)) { errorBack("Enter a last name."); } if (empty($father)) { errorBack("Enter a father name."); } if (empty($mother)) { errorBack("Enter a mother name."); } $phone = $_POST['phone']; $phone = preg_replace("/[^0-9]/", "", $phone); if (strlen($phone) == 11) { $phone = preg_replace("/^1/", "", $phone); } if (strlen($phone) != 10) { errorBack("Enter a valid 10-digit phone number."); } $email = $_POST['email']; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { errorBack("The email address looks wrong."); } $address = $_POST['streetaddress']; $city = $_POST['city']; $state = strtoupper($_POST['state']); $zip = $_POST['zip']; if (empty($address)) { errorBack("Enter a street address."); } if (empty($city)) { errorBack("Enter a city."); } if (!preg_match("/^[A-Z]{2}$/", $state)) { errorBack("Enter a valid two-character state (MT, WY, ID, etc)."); } if (!preg_match("/^[0-9]{5}(-?[0-9]{4})?$/", $zip)) { errorBack("Enter a valid five or nine digit US ZIP code."); } $newsletter = $_POST['newsletter_method']; $membership_cost = 2500; if (empty($newsletter)) { errorBack("Select a newsletter preference."); } switch ($newsletter) { case 1: // Email only $membership_cost = 2500; break; case 2: // Print only $membership_cost = 3500; break; case 3: // Email and print $membership_cost = 3500; break; default: errorBack("Select a valid newsletter preference."); } $photopermission = $_POST['photo_permission']; if (!empty($photopermission) && $photopermission == "1") { $photopermission = true; } else { $photopermission = false; } $database->insert("families", [ "familyname" => $lastname, "father_name" => $father, "mother_name" => $mother, "phone" => $phone, "email" => $email, "newsletter_method" => $newsletter, "address" => $address, "city" => $city, "state" => $state, "zip" => $zip, "photo_permission" => $photopermission ]); $familyid = $database->id(); $children = $_POST['child']; foreach ($children['ids'] as $cid) { if (empty($children['name'][$cid])) { continue; } if (!preg_match("/^([1-9]|1[012])$/", $children['month'][$cid])) { errorBack("Invalid birth month chosen for " . htmlentities($children['name'][$cid]) . "."); } if (!is_numeric($children['year'][$cid])) { errorBack("Invalid birth year chosen for " . htmlentities($children['name'][$cid]) . "."); } $children['year'][$cid] = $children['year'][$cid] * 1; if ($children['year'][$cid] < 1980 || $children['year'][$cid] > date("Y")) { errorBack("Invalid birth year chosen for " . htmlentities($children['name'][$cid]) . "."); } $database->insert("people", [ "familyid" => $familyid, "name" => $children['name'][$cid], "birthday" => $children['year'][$cid] . "-" . $children['month'][$cid] . "-00", "graduated" => empty($children['graduate'][$cid]) ? 0 : 1 ]); } $interests = []; foreach ($_POST['events'] as $evt) { if ($database->has("events", ['eventid' => $evt])) { $interests[] = ["familyid" => $familyid, "eventid" => $evt]; } } $database->insert("interests", $interests); try { \Stripe\Stripe::setApiKey(STRIPE_SECKEY); $charge = \Stripe\Charge::create([ 'amount' => $membership_cost, 'currency' => 'usd', 'description' => 'HACHE Membership', 'source' => $_POST['stripeToken'], 'statement_descriptor' => 'HACHE Membership 1yr', ]); } 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)"); } 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)"); } catch (\Stripe\Error\Authentication $e) { errorBack("We can't connect to the card processor. Please try again later. (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)"); } catch (\Stripe\Error\Base $e) { errorBack("An unknown payment error occurred. Please try again later."); } catch (Exception $e) { errorBack("An unknown error occurred. Please try again later."); } $database->insert("payments", [ "familyid" => $familyid, "amount" => ($membership_cost / 100.0), "paid" => 1, "date" => date("Y-m-d H:i:s") ]); header("Location: ../?page=thanks"); return true; });