'Day', 'monthly' => 'Month', 'yearly' => 'Year'); // constructor function payment() { // globals global $base_dir, $db, $domain; // connections $this->db = $db; // stripe api include_once("{$base_dir}/includes/stripe/lib/Stripe.php"); // ipn gateway $this->ipn_url = "https://{$domain}/actions/ipn.action.php"; } function init() { global $users, $template; if( !isset($users) ){ $users = new users(); } // admin test condition exception $user_type = ''; if( isset($_SESSION['login']) ){ $user_type = $users->get_user_type($_SESSION['login']); } /* if($user_type == 'admin'){ $this->IS_ONLINE = true; } //$this->IS_ONLINE = false; ///////////// REMOVE after testing ///////////// */ if( isset($_SERVER['HTTP_HOST']) ){ if($_SERVER['HTTP_HOST'] != 'www.franchiseball.com'){ if($_SERVER['HTTP_HOST'] == 'gamedev'){ $this->IS_ONLINE = false; } } } // testing credentials if( $this->IS_ONLINE == false ){ // stripe staging settings $this->secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $this->publishable_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // production credentials } else { // stripe live settings $this->secret_key = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYY'; $this->publishable_key = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyy'; } // init stripe api Stripe::setApiKey($this->secret_key); } // cancel_cycle_payment : cancels a subscription based transaction (ex : subscription/membership) function cancel_cycle_payment($customer_id = '') { if($customer_id == ''){ return; } $this->init(); $customer = Stripe_Customer::retrieve($customer_id); if( isset($customer->id) && $customer->id != '' ){ $customer->cancelSubscription(); } } function process_payment() { global $market; if( !isset($market) ){ $market = new market(); } // payment token recieved if( isset($_POST['stripeToken']) && $_POST['stripeToken'] != '' ){ // vars $errors = array(); $charge_token = $_POST['stripeToken']; $charge_value = $market->get_cart_total(); $charge_value = $charge_value + ($charge_value * $this->servicefee_pct) + $this->servicefee_additional; $charge_value = round($charge_value * 100); // create customer $customer = Stripe_Customer::create(array( 'email' => $_SESSION['email'], 'card' => $charge_token )); // seperate payment types : direct & recurring $reoccuring_set = array(); $product_set = array(); $product_data = $market->get_product_data($_SESSION['cart']['products']); foreach($product_data as $index => $product){ if($product['product_reoccuring'] != 'none'){ $reoccuring_set[] = $product; } else { $product_set[] = $product; } } if( !empty($reoccuring_set) ){ $product = $reoccuring_set[0]; $plan_id = $product['product_slug']; if( !empty($product_set) ){ $charge_amt = $product['product_price'] + ($product['product_price'] * $this->servicefee_pct) + $this->servicefee_additional; $charge_amt = round($charge_amt * 100); } $charge_value = ($charge_value - $charge_amt); $subscription = $customer->updateSubscription(array('plan' => $plan_id)); if( isset($subscription->customer) && $subscription->customer != '' ){ $product['customer_id'] = $subscription->customer; $this->on_charge_success(array($product)); } else { $errors[] = 'Error creating subscription'; } } if( !empty($product_set) ){ $products_str = ''; foreach($product_set as $datum){ if($datum['product_slug'] == 'credits-million-gc'){ $gc += $datum['product_entity']; } else { $products_str .= "{$comma}{$datum['product_name']}"; $comma = ', '; } } if($gc > 0){ $products_str .= "{$comma} \$" . number_format($gc) . " GC"; } $charge = Stripe_Charge::create(array( 'customer' => $customer->id, 'amount' => $charge_value, 'currency' => 'usd', 'description' => $products_str )); if( isset($charge->id) && $charge->id != '' ){ foreach($product_set as $index => $datum){ $product_set[$index]['charge_id'] = $charge->id; } $this->on_charge_success($product_set); } else { $errors[] = 'Error creating charge'; } } if( empty($errors) ){ $this->products_purchased = $_SESSION['cart']['products']; // redirect $ref_page = '/home.php'; if( isset($_SESSION['value']['ref_page']) && $_SESSION['value']['ref_page'] != '' ){ $ref_page = $_SESSION['value']['ref_page']; } $purchase_data = $this->get_purchase_data(); if( !empty($purchase_data) ){ $_SESSION['dialog'] = array( 'ui' => 'market', 'method' => 'payment_confirm', 'title' => 'Your payment was successful', 'data' => $purchase_data ); unset($_SESSION['value']['ref_page']); } unset($_SESSION['cart']['products']); header("Location: {$ref_page}"); } } } function on_charge_success($cart_set = array()) { global $team, $market; if( !isset($team) ){ $team = new team(); } if( !isset($market) ){ $market = new market(); } // login team if login condition exists if( !isset($_SESSION['login']) && isset($_SESSION['cond_login_team']) ){ if($_SESSION['cond_login_team'] > 0){ $ui = new ui(); $ui->autologin_team($_SESSION['cond_login_team']); } } if( !empty($cart_set) ){ if( !empty($cart_set) ){ foreach($cart_set as $product){ $this->post_payment($product); if( $team->add_product($product) ){ $team->enable_product($product); } } } } } function get_purchase_data() { global $market; if( !isset($market) ){ $market = new market(); } if( !empty($this->products_purchased) ){ $product_data = $market->get_product_data($this->products_purchased); $product_data = $market->quantity_group_product_data($product_data); } return $product_data; } function post_payment(&$product, $manual_team_id = '') { global $team, $schedule; if( !isset($team) ){ $team = new team(); } if( !isset($schedule) ){ $schedule = new schedule(); } $team_id = $team->get_primary_team(); if($manual_team_id != ''){ $team_id = $manual_team_id; } $time = $schedule->time(); $transaction_id_sql = ''; if( !empty($product) ){ if( isset($product['charge_id']) ){ $transaction_id_sql = " payment_charge_id = '{$product['charge_id']}',"; } if( isset($product['customer_id']) ){ $transaction_id_sql = " payment_customer_id = '{$product['customer_id']}',"; } $sql = "INSERT INTO payments SET team_id = {$team_id}, payment_amount = {$product['product_price']},{$transaction_id_sql} payment_time = {$time}"; $this->db->query($sql); $product['payment_id'] = mysql_insert_id(); } } function payment_form() { global $generate, $market, $domain; if( !isset($generate) ){ $generate = new generate(); } if( !isset($market) ){ $market = new market(); } $this->init(); //$domain = 'gamedev'; // todo : remove $order_amount = $market->get_cart_total(); $charge_total = $order_amount + ($order_amount * $this->servicefee_pct) + $this->servicefee_additional; ?>