2018-11-17 19:56:10 -07:00
< ? php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http :// mozilla . org / MPL / 2.0 /.
*/
2018-11-30 15:17:31 -07:00
require_once __DIR__ . " /../../lib/requiredpublic.php " ;
2018-12-09 20:20:08 -07:00
require_once __DIR__ . " /../../lib/Email.lib.php " ;
2018-12-04 19:07:19 -07:00
2018-11-30 15:17:31 -07:00
function errorBack ( string $errormsg ) {
2019-03-11 14:56:29 -06:00
global $familyid ;
2020-02-20 17:43:14 -07:00
//header("Location: ../?page=signup&error=" . htmlentities($errormsg));
2019-03-11 14:56:29 -06:00
$database -> delete ( " families " , [ " familyid " => $familyid ]);
2018-11-30 15:17:31 -07:00
die ( $errormsg );
}
2019-05-30 21:40:10 -06:00
if ( ! empty ( $SETTINGS [ " disable_registration " ]) && $SETTINGS [ " disable_registration " ] == true ) {
die ( " Online registration is now closed. " );
}
2018-11-30 15:17:31 -07:00
$database -> action ( function ( $database ) {
2019-03-11 14:56:29 -06:00
global $SETTINGS ;
2018-11-30 15:17:31 -07:00
2019-03-11 14:56:29 -06:00
$database -> insert ( " families " , []);
$familyid = $database -> id ();
2018-11-30 15:17:31 -07:00
2019-03-11 14:56:29 -06:00
$dueusd = 0.0 ;
2018-11-30 15:17:31 -07:00
2019-04-11 11:54:37 -06:00
$emails = [];
2019-03-11 14:56:29 -06:00
try {
2018-11-30 15:17:31 -07:00
2019-03-11 14:56:29 -06:00
$people = $_POST [ 'people' ];
$requiredfields = [
" firstname " => " .+ " ,
" lastname " => " .+ " ,
" address " => " .+ " ,
" zip " => " [0-9] { 5}(-?[0-9] { 4})? " ,
" phone1 " => " [0-9] { 10} " ,
" email " => " _EMAIL_ " ,
2019-05-30 21:00:32 -06:00
" shirt " => [ " NO " , " YS " , " YM " , " YL " , " AS " , " AM " , " AL " , " AX " , " A2 " , " A3 " ],
2019-03-11 14:56:29 -06:00
" sex " => [ " M " , " F " ]
];
2019-04-11 11:54:37 -06:00
if ( count ( $people [ 'ids' ]) == 0 ) {
errorBack ( " You need to register at least one person. " );
}
2020-02-20 17:43:14 -07:00
$campercount = 0 ;
foreach ( $people [ 'ids' ] as $pid ) {
if ( $people [ " type " ][ $pidd ] == " camper " ) {
$campercount ++ ;
}
}
2019-03-11 14:56:29 -06:00
foreach ( $people [ 'ids' ] as $pid ) {
2019-03-18 18:06:10 -06:00
// Clear these out
$camperid = null ;
$adultid = null ;
$youthid = null ;
2019-03-11 14:56:29 -06:00
switch ( $people [ " type " ][ $pid ]) {
case " camper " :
$checkfields = array_merge ( $requiredfields , [
" parentname " => " .+ " ,
" unit " => " [0-9] { 3,4} " ,
" rank " => [ " Tiger " , " Wolf " , " Bear " , " Webelos " , " Arrow of Light " ]
]);
break ;
case " adult " :
$checkfields = array_merge ( $requiredfields , [
" position " => [
" None " ,
" Den Walker " ,
" Station Leader " ,
" Tot Lot " ,
" First Aid " ,
" Floater "
]
]);
break ;
case " youth " :
$checkfields = array_merge ( $requiredfields , [
" position " => [
" None " ,
" Den Chief " ,
" Station " ,
" Tot Lot " ,
" Floater "
]
]);
break ;
default :
errorBack ( " Invalid person type. " );
2018-12-08 11:06:54 -07:00
}
2019-03-11 14:56:29 -06:00
foreach ( $checkfields as $name => $regex ) {
$validatefunction = function ( $str ) use ( $regex ) {
return preg_match ( " / $regex / " , $str );
};
if ( is_array ( $regex )) {
// Array of options
$validatefunction = function ( $str ) use ( $regex ) {
return in_array ( $str , $regex );
};
} else if ( strpos ( $regex , " _ " ) === 0 ) {
// Special cases
switch ( $regex ) {
case " _EMAIL_ " :
$validatefunction = function ( $str ) {
return filter_var ( $str , FILTER_VALIDATE_EMAIL );
};
break ;
}
}
2018-12-04 19:07:19 -07:00
2019-03-11 14:56:29 -06:00
// Validate
if ( ! $validatefunction ( $people [ $name ][ $pid ])) {
errorBack ( " Please check your input and try again ( $name ). " );
}
2018-12-11 20:42:41 -07:00
}
2018-12-04 19:07:19 -07:00
2019-03-11 14:56:29 -06:00
$days = " " ;
if ( is_array ( $people [ " days " ][ $pid ])) {
2020-02-20 17:43:14 -07:00
foreach ( $SETTINGS [ " camp_days " ] as $short => $long ) {
$validdays [] = $short ;
}
2019-03-11 14:56:29 -06:00
$days = " " ;
foreach ( $people [ " days " ][ $pid ] as $day ) {
if ( in_array ( $day , $validdays )) {
$days .= $day ;
}
}
2018-12-04 19:07:19 -07:00
}
2019-03-11 14:56:29 -06:00
switch ( $people [ " type " ][ $pid ]) {
case " camper " :
$dueusd += 50.0 ;
echo " \n Adding $ 50 to the total for a camper, dueusd is $dueusd\n " ;
$database -> insert ( " campers " , [
" parentname " => $people [ " parentname " ][ $pid ],
" rank " => $people [ " rank " ][ $pid ]
]);
$camperid = $database -> id ();
break ;
case " adult " :
$discount = 10.0 * ( strlen ( $days ) / 2 );
$dueusd -= $discount ;
2019-03-19 13:40:09 -06:00
echo " Subtracting $ $discount from the total for an adult volunteer, dueusd is $dueusd\n " ;
2020-02-20 17:43:14 -07:00
// Add shirt charge if not working all days
if ( $SETTINGS [ " prices " ][ " alone_adult_free_tshirt " ] === true && $campercount == 0 ) {
// No shirt cost
} else if ( $SETTINGS [ " prices " ][ " adult_tshirt " ] !== false ) {
if ( $people [ " shirt " ][ $pid ] != " NO " && ( strlen ( $days ) / 2 ) < $SETTINGS [ " prices " ][ " adult_tshirt " ]) {
echo " Adding $ 10 for a tshirt. \n " ;
$dueusd += 10.0 ;
}
2019-03-19 13:40:09 -06:00
}
2020-02-20 18:07:41 -07:00
if ( ! empty ( $people [ " child_care " ][ $pid ])) {
$items = preg_split ( " /[^ \ d]+/ " , $people [ " child_care " ][ $pid ]);
$ages = [];
foreach ( $items as $it ) {
$ages [] = $it ;
}
$people [ " child_care " ][ $pid ] = implode ( " , " , $ages );
}
2019-03-11 14:56:29 -06:00
$database -> insert ( " adults " , [
" position " => $people [ " position " ][ $pid ],
2020-02-20 17:43:14 -07:00
" days " => $days ,
" child_care " => ( empty ( $people [ " child_care " ][ $pid ]) ? null : $people [ " child_care " ][ $pid ])
2019-03-11 14:56:29 -06:00
]);
$adultid = $database -> id ();
break ;
case " youth " :
2020-02-20 17:43:14 -07:00
if ( $SETTINGS [ " prices " ][ " youth_tshirt " ] !== false ) {
if ( $people [ " shirt " ][ $pid ] != " NO " && ( strlen ( $days ) / 2 ) < $SETTINGS [ " prices " ][ " youth_tshirt " ]) {
echo " Adding $ 10 for a tshirt. \n " ;
$dueusd += 10.0 ;
}
2019-03-19 13:40:09 -06:00
}
2019-03-11 14:56:29 -06:00
$database -> insert ( " youth " , [
" position " => $people [ " position " ][ $pid ],
" days " => $days
]);
$youthid = $database -> id ();
break ;
2018-12-04 19:07:19 -07:00
}
2018-11-30 15:17:31 -07:00
2019-03-11 14:56:29 -06:00
$database -> insert ( " people " , [
" familyid " => $familyid ,
" camperid " => $camperid ,
" adultid " => $adultid ,
" youthid " => $youthid ,
" firstname " => $people [ " firstname " ][ $pid ],
" lastname " => $people [ " lastname " ][ $pid ],
" address " => $people [ " address " ][ $pid ],
" zip " => $people [ " zip " ][ $pid ],
2019-03-11 16:20:21 -06:00
" phone1 " => empty ( $people [ " phone1 " ][ $pid ]) ? " " : $people [ " phone1 " ][ $pid ],
" phone2 " => empty ( $people [ " phone2 " ][ $pid ]) ? " " : $people [ " phone2 " ][ $pid ],
" email " => empty ( $people [ " email " ][ $pid ]) ? " " : $people [ " email " ][ $pid ],
2019-03-11 14:56:29 -06:00
" unit " => $people [ " unit " ][ $pid ],
" shirt " => $people [ " shirt " ][ $pid ],
" sex " => $people [ " sex " ][ $pid ]
]);
2019-04-11 11:54:37 -06:00
if ( ! empty ( $people [ " email " ][ $pid ])) {
$emails [] = $people [ " email " ][ $pid ];
}
2018-11-30 16:54:02 -07:00
}
2018-12-04 19:07:19 -07:00
} catch ( Exception $ex ) {
errorBack ( $ex -> getMessage ());
2018-11-30 15:17:31 -07:00
}
2018-12-04 19:07:19 -07:00
//
// Payment
//
2018-11-30 16:54:02 -07:00
2019-03-11 14:56:29 -06:00
$campcoupons = ( ! empty ( $_POST [ 'campcoupons' ]) && preg_match ( " /[0-9]+/ " , $_POST [ 'campcoupons' ])) ? $_POST [ 'campcoupons' ] * 1 : 0 ;
2018-11-30 15:17:31 -07:00
2019-03-18 18:06:10 -06:00
if ( $campcoupons < 0 ) {
$campcoupons = 0 ;
}
$dueusd = max ( 0 , $dueusd );
$duecard = max ( 0 , $dueusd - $campcoupons );
2018-11-30 15:17:31 -07:00
2019-03-11 14:56:29 -06:00
echo " \n Cost $dueusd total: $duecard to Stripe, $campcoupons as coupons \n " ;
2018-12-09 20:20:08 -07:00
2019-03-18 18:06:10 -06:00
if ( $dueusd != $_POST [ 'totalcharge' ]) {
2019-03-11 14:56:29 -06:00
errorBack ( " There was a discrepency between the total you saw and the total the server calculated. The transaction has been cancelled and you were not charged. " );
2018-12-09 20:20:08 -07:00
}
2019-03-11 14:56:29 -06:00
if ( $duecard > 0 ) {
try {
\Stripe\Stripe :: setApiKey ( $SETTINGS [ " stripe " ][ " seckey " ]);
2019-04-11 11:54:37 -06:00
$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 );
2019-03-11 14:56:29 -06:00
} 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 ) {
2019-04-11 11:54:37 -06:00
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) " );
2019-03-11 14:56:29 -06:00
} catch ( \Stripe\Error\InvalidRequest $e ) {
2019-04-11 11:54:37 -06:00
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) " );
2019-03-11 14:56:29 -06:00
} catch ( \Stripe\Error\Authentication $e ) {
2019-04-11 11:54:37 -06:00
errorBack ( " We can't connect to the card processor. Please try again later. Your card was not charged. (Error code: STRIPE_AUTH) " );
2019-03-11 14:56:29 -06:00
} catch ( \Stripe\Error\ApiConnection $e ) {
2019-04-11 11:54:37 -06:00
errorBack ( " We can't connect to the card processor. Please try again later. Your card was not charged. (Error code: STRIPE_NOAPI) " );
2019-03-11 14:56:29 -06:00
} catch ( \Stripe\Error\Base $e ) {
2019-04-11 11:54:37 -06:00
errorBack ( " An unknown payment error occurred. Please try again later. Your card was not charged. " );
2019-03-11 14:56:29 -06:00
} catch ( Exception $e ) {
2019-04-11 11:54:37 -06:00
errorBack ( " An unknown error occurred. Please try again later. Your card was not charged. " );
2018-12-09 20:20:08 -07:00
}
}
2018-12-07 11:17:44 -07:00
2019-03-11 14:56:29 -06:00
$database -> insert ( " payments " , [
" familyid " => $familyid ,
2019-03-18 18:06:10 -06:00
" amount " => $dueusd ,
" amountpaid " => $duecard ,
2019-03-11 14:56:29 -06:00
" date " => date ( " Y-m-d H:i:s " ),
" type " => " Online "
]);
2018-11-30 15:17:31 -07:00
header ( " Location: ../?page=thanks " );
return true ;
});