Files
Testing/include/functions_payment.php

240 lines
7.0 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
// TODO: separation
$payment_states = array( 0 => 100015005,
1 => 100015006,
2 => 100015007,
3 => 100015008,
4 => 100015009
);
/*** PAYMENT FUNCTIONS ***/
// loads paymentgw extension and instantiates class
function semods_paymengtw_factory( $paymentgw_id ) {
global $folder;
if(is_numeric( $paymentgw_id ) ) {
$paymentgw = semods::db_query_assoc("SELECT * FROM se_semods_paymentgateways WHERE paymentgateway_id = $paymentgw_id");
} else {
$paymentgw = semods::db_query_assoc("SELECT * FROM se_semods_paymentgateways WHERE paymentgateway_typename = '$paymentgw_id'");
}
if($paymentgw === false) {
// no known gateways
return null;
}
if($folder == "base") {
$serverpath = ".";
} else {
$serverpath = "..";
}
if(defined('SE_ROOT')) {
$serverpath = SE_ROOT;
}
if(!file_exists($serverpath . '/include/class_semods_paymentgw_' . $paymentgw['paymentgateway_typename'] . '.php')) {
return null;
}
include_once $serverpath . '/include/class_semods_paymentgw_' . $paymentgw['paymentgateway_typename'] . '.php';
$class_name = "semods_paymentgw_" . $paymentgw['paymentgateway_typename'];
if(!class_exists($class_name))
return null;
// call_user_function(array(&$myClass,$func),$param);
$params = unserialize( $paymentgw['paymentgateway_metadata'] );
$pgw = new $class_name ( $params );
$pgw->gateway_id = $paymentgw['paymentgateway_id'];
return $pgw;
}
// create invoice
// type:
// 0 - Sale / Cart
// 1 - recurring / subscription payment
// 2 - initial recurring / subscription payment
// state
// 0 - completed
// 1 - pending (?)
// 2 - refunded
// 3 - reversed
// 4 - failed
// 5 - unknown ?
// TBD: tax, fee
// paymentgw txn_id, paymentgw_id ?
function semods_payment_invoice( $user_id, $date, $type, $state, $text, $amount, $txn_id, $handler, $client_firstname = '', $client_lastname = '', $client_email = '', $client_street = '', $client_city = '', $client_state = '', $client_zipcode = '', $client_country = '' ) {
global $database;
// Find out if this is known client
$client_id = semods::db_query_count("SELECT client_id FROM se_semods_clients WHERE client_email = '$client_email'");
if(!$client_id) {
// New Client
$database->database_query( "INSERT INTO se_semods_clients (
client_user_id,
client_firstname,
client_lastname,
client_email,
client_street,
client_city,
client_state,
client_zipcode,
client_country
) VALUES (
$user_id,
'$client_firstname',
'$client_lastname',
'$client_email',
'$client_street',
'$client_city',
'$client_state',
'$client_zipcode',
'$client_country' )" );
$client_id = $database->database_insert_id();
}
// Check if it's a state update
$row_id = semods::db_query_count("SELECT userpaymenthistory_id
FROM se_semods_userpaymenthistory
WHERE
userpaymenthistory_user_id = $user_id AND
userpaymenthistory_handler = $handler AND
userpaymenthistory_txnid = '$txn_id'");
if($row_id) {
$database->database_query( "UPDATE se_semods_userpaymenthistory SET
userpaymenthistory_state = $state
WHERE userpaymenthistory_id = $row_id"
);
} else {
$database->database_query( "INSERT INTO se_semods_userpaymenthistory (
userpaymenthistory_date,
userpaymenthistory_user_id,
userpaymenthistory_client_id,
userpaymenthistory_type,
userpaymenthistory_state,
userpaymenthistory_text,
userpaymenthistory_amount,
userpaymenthistory_handler,
userpaymenthistory_txnid
) VALUES (
$date,
$user_id,
$client_id,
$type,
$state,
'$text',
$amount,
$handler,
'$txn_id' )"
);
}
}
/********************* SYSTEM FUNCTIONS *********************/
function payment_securify() {
//no ssl for dev
if(defined("DEV_ENVIRONMENT")) {
return;
}
// Redirect to secure version of self
$secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0;
if(!$secure) {
$location = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header("Location: $location");
exit;
}
// fix base href to start with "https://", with se_url
global $url;
// same same
$url->url_base = str_replace ( "http://", "https://", $url->url_base );
//$url->url_base = substr_replace ( $url->url_base, "https://", 0, 7 );
}
/********************* SYSTEM FUNCTIONS *********************/
function deleteuser_payment( $user_id ) {
global $database;
// Related Client Data
$database->database_query("DELETE FROM se_semods_clients WHERE client_user_id = $user_id");
// Payment History
$database->database_query("DELETE FROM se_semods_userpaymenthistory WHERE userpaymenthistory_user_id = $user_id");
// Clear cart stuff
$database->database_query("DELETE FROM se_semods_cart, se_semods_cartitems USING se_semods_cart LEFT JOIN se_semods_cartitems ON se_semods_cart.cart_id = se_semods_cartitems.cartitem_cart_id WHERE se_semods_cart.cart_user_id = $user_id");
}
/*********************** HOOKS *********************/
function payment_hook_signup_footer( $arguments = array() ) {
global $task;
if($task == "step5") {
$cart = new semods_cart();
if($cart->cart_exists && count($cart->cart_items) > 0 ) {
header("Location: payment.php");
exit;
}
}
}
?>