Files
Testing/payment_cc.php

191 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
$page = "payment_cc";
include 'header.php';
include './include/class_semods_geo.php';
header("Expires: -1");
header("Cache-Control: no-cache");
header("Pragma: nocache");
$cart = new semods_cart();
if($cart->cart_exists == false) {
semods::redirect("home.php");
}
$task = semods::getpost('task','main');
$paymentgw_id = semods::getpost('paymentgw_id','');
// try from session
if($paymentgw_id == '') {
$paymentgw_id = semods::g($_SESSION,'session_paymentgw_id','');
}
// save to session
if($paymentgw_id != '') {
$_SESSION['session_paymentgw_id'] = $paymentgw_id;
}
// if not secure, redirect
payment_securify();
// Credit card verification regexes. Also possible - cvv data && checksum algo
$ccverify = array( 'visa' => array( 'regex' => '/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/i' ),
'mastercard' => array( 'regex' => '/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/i' ),
'american express' => array( 'regex' => '/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/i' ),
'discover' => array( 'regex' => '/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/i' )
);
$paymentgw = semods_paymengtw_factory( $paymentgw_id );
if(!$paymentgw) {
// TODO: message admin
// silently die
semods::redirect("home.php");
}
// Load supported credit card types. Future - get from gateway
$cctypes = $paymentgw->get_supported_credit_cards();
// Load Expiration years
$cur_year = date("Y");
$countries = semods_geo::get_countries( array('US', 'CA') , true );
// Regions - only for US
$regions = semods_geo::get_regions( 254 );
// CONSTRUCT MONTH ARRAY
$month_format = "M";
$month_array = array();
for($m=1;$m<=12;$m++) {
$month_array[$m] = array( 'name' => sprintf( "%02d (%s)", $m, $datetime->cdate("$month_format", mktime(0, 0, 0, $m, 1, 1990))),
'value' => $m,
);
}
if($task == 'pay') {
// read cc details
$ccparams['cc_type'] = semods::getpost('cc_type');
$ccparams['cc_cardholder_firstname'] = semods::getpost('cc_cardholder_firstname');
$ccparams['cc_cardholder_lastname'] = semods::getpost('cc_cardholder_lastname');
$ccparams['cc_number'] = semods::getpost('cc_number');
$ccparams['cc_exp_month'] = semods::getpost('cc_exp_month');
$ccparams['cc_exp_year'] = semods::getpost('cc_exp_year');
$ccparams['cc_cvv'] = semods::getpost('cc_cvv');
$ccparams['cc_addr_country'] = semods::getpost('cc_addr_country');
$ccparams['cc_addr_state'] = semods::getpost('cc_addr_state');
$ccparams['cc_addr_street'] = semods::getpost('cc_addr_street');
$ccparams['cc_addr_city'] = semods::getpost('cc_addr_city');
$ccparams['cc_addr_zip'] = semods::getpost('cc_addr_zip');
$ccparams['cc_addr_phone'] = semods::getpost('cc_addr_phone');
// normalize stuff
str_replace( "-", "", $ccparams['cc_number'] );
$is_error = 1;
for(;;) {
// verify - cc
$ccparams['cc_type'] = strtolower($ccparams['cc_type']);
if(!in_array($ccparams['cc_type'], $cctypes)) {
$error_message = semods::get_language_text( 100015159 );
break;
}
// verify - cc regex
if(isset($ccverify[$ccparams['cc_type']]) && !preg_match( $ccverify[$ccparams['cc_type']]['regex'], $ccparams['cc_number'] )) {
$error_message = "Invalid credit card number";
break;
}
// verify - cc exp date
if( strtotime( sprintf( '%s-%s', $ccparams['cc_exp_year'], $ccparams['cc_exp_month'])) < time() ) {
$error_message = semods::get_language_text( 100015160 );
break;
}
// get our money
if(!$paymentgw->pay( $cart, $ccparams ) ) {
// application user displayable errors are >= 100
if($paymentgw->errID >= 100) {
$error_message = $paymentgw->errMsg;
} else {
$error_message = semods::get_language_text( 100015164 );
}
break;
}
// all good!
semods::redirect('payment.php?task=success');
break;
}
}
if($task == 'main') {
// try to guess cardholder details
if($user->user_exists) {
$ccparams['cc_cardholder_firstname'] = $user->user_info['user_fname'];
$ccparams['cc_cardholder_lastname'] = $user->user_info['user_lname'];
}
}
// ASSIGN VARIABLES AND INCLUDE FOOTER
$smarty->assign('is_error', $is_error);
$smarty->assign('result', $result);
$smarty->assign('error_message', $error_message);
$smarty->assign('success_message', $success_message);
$smarty->assign('paymentgw_id', $paymentgw_id);
$smarty->assign('ccparams', $ccparams);
$smarty->assign('cctypes', $cctypes);
$smarty->assign('cur_year', $cur_year);
$smarty->assign('months', $month_array);
$smarty->assign('countries', $countries);
$smarty->assign('regions', $regions);
include "footer.php";
?>