JezK
Edit File: process_payment1.php
<?php session_start(); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(1); include 'db_connection.php'; include '../membership/email.php'; require_once('stripe-php/init.php'); // require_once('stripe_lib/vendor/autoload.php'); include '../membership/vendor/autoload.php'; // Set API key //\Stripe\Stripe::setApiKey('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'); \Stripe\Stripe::setApiKey('sk_test_51HqxNhLB5kw3RTIUhauZZsIPMfsAcXCvdUdIjIF9b99Xlcpjf831CmqNB9Y0mCwcBU2iGFiwe571HERp2zZXtStk0003TA8OwV'); print_r($_POST);exit; if(isset($_POST) && $_POST['first_name'] != "") { $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $position = trim($_POST['position']); $company_name = trim($_POST['company_name']); $city = trim($_POST['city']); $country = trim($_POST['country']); $vat_number = trim($_POST['vat_number']); $email = trim($_POST['email']); $countryCode = trim($_POST['countryCode']); $mobile = trim($_POST['mobile']); //$food_items = json_encode($_POST['food_items']); $food_items = isset($_POST['food_items']) ? json_encode($_POST['food_items']) : '[]'; $comments = trim($_POST['comments']); $coupon_code = trim($_POST['coupon_code']); $date = date("Y-m-d"); $event_id = trim($_POST['event_id']); $event_name = trim($_POST['event_name']); $sql = "SELECT MAX(id) AS max_id FROM payments"; $result = $conn->query($sql); $data = $result->fetch_assoc(); $Unique_ID = $data['max_id']+1; $unique_id = "REIA-B00".$Unique_ID; $amount = $_POST['event_price']; } if($amount!=0) { // $token = $_POST['stripe-token']; // try { // Define item price and convert to cents $itemPriceCents = round($amount*100); // Set content type to JSON header('Content-Type: application/json'); try { // Create PaymentIntent with amount and currency $paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => $itemPriceCents, 'currency' => 'eur', 'description' => 'test', 'payment_method_types' => [ 'card' ] /*'automatic_payment_methods' => [ 'enabled' => true ]*/ ]); $payment_intent_id = $paymentIntent->id; // $output = [ // 'id' => $paymentIntent->id, // 'clientSecret' => $paymentIntent->client_secret // ]; // echo "<pre>"; print_r($paymentIntent);exit; // echo json_encode($output); // } catch (Error $e) { // // http_response_code(500); // echo json_encode(['error' => $e->getMessage()]); // } // if($jsonObj->request_type == 'create_customer'){ $payment_intent_id = !empty($payment_intent_id)?$payment_intent_id:''; // $name = !empty($jsonObj->name)?$jsonObj->name:''; // $email = !empty($jsonObj->email)?$jsonObj->email:''; // Add customer to stripe try { $customer = \Stripe\Customer::create(array( 'name' => $first_name, 'email' => $email )); }catch(Exception $e) { $api_error = $e->getMessage(); } if(empty($api_error) && $customer){ try { // Update PaymentIntent with the customer ID $paymentIntent = \Stripe\PaymentIntent::update($payment_intent_id, [ 'customer' => $customer->id ]); } catch (Exception $e) { // log or do what you want } // $output = [ // 'id' => $payment_intent_id, // 'customer_id' => $customer->id // ]; // echo json_encode($output); } // else{ // http_response_code(500); // echo json_encode(['error' => $api_error]); // } // }elseif($jsonObj->request_type == 'payment_insert'){ $payment_intent = !empty($payment_intent)?$payment_intent:''; $customer_id = !empty($customer->id)?$customer->id:''; // Retrieve customer info try { $customer = \Stripe\Customer::retrieve($customer_id); }catch(Exception $e) { $api_error = $e->getMessage(); } // print_r($payment_intent);exit; // Check whether the charge was successful if(!empty($payment_intent) && $payment_intent->status == 'succeeded'){ // Transaction details $transaction_id = $payment_intent->id; $paid_amount = $payment_intent->amount; $new_price = ($paid_amount/100); $paid_currency = $payment_intent->currency; $payment_status = $payment_intent->status; $customer_name = $customer_email = ''; if(!empty($customer)){ $customer_name = !empty($customer->name)?$customer->name:''; $customer_email = !empty($customer->email)?$customer->email:''; } $sqlQ ="INSERT INTO `payments` (`booking_id`,`first_name`,`last_name`,`position`,`company_name`,`city`,`country`, `vat_number`, `email`, `countryCode`, `mobile`, `food_items`, `comments`,`coupon_code`,`gdpr`,`transaction_id`,`payment_status`,`date`,`event_id`,`price`,`currency`) VALUES ('$unique_id','$first_name','$last_name','$position','$company_name','$city','$country','$vat_number', '$email', '$countryCode', '$mobile', '$food_items', '$comments','$coupon_code','1', '$transaction_id', '$payment_status','$date', '$event_name', '$new_price','$paid_currency')"; if ($conn->query($sqlQ) === TRUE) { $last_id = $conn->insert_id; $sql_last = "SELECT * FROM `payments` WHERE `id` = '$last_id' ORDER BY `id` DESC"; $result_last = $conn->query($sql_last); $row = $result_last->fetch_assoc(); $name = $row['first_name']; $booking_id = $row['booking_id']; $transaction_id = $row['transaction_id']; $price = $row['price']; $event_id = $row['event_id']; $date = $row['date']; // send_payment_confirmation_email($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); // send_payment_confirmation_client($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); header('Location:payment_success.php?trans_id='.$transaction_id); } }else{ http_response_code(500); echo json_encode(['error' => 'Transaction has been failed!']); } } } else { $sqlQ ="INSERT INTO `payments` (`booking_id`,`first_name`,`last_name`,`position`,`company_name`,`city`,`country`, `vat_number`, `email`, `countryCode`, `mobile`, `food_items`, `comments`,`coupon_code`,`gdpr`,`transaction_id`,`payment_status`,`date`,`event_id`,`price`,`currency`) VALUES ('$unique_id','$first_name','$last_name','$position','$company_name','$city','$country','$vat_number', '$email', '$countryCode', '$mobile', '$food_items', '$comments','$coupon_code','1', '', 'succeeded','$date', '$event_name', '0','euro')"; if ($conn->query($sqlQ) === TRUE) { $last_id = $conn->insert_id; $sql_last = "SELECT * FROM `payments` WHERE `id` = '$last_id' ORDER BY `id` DESC"; $result_last = $conn->query($sql_last); $row = $result_last->fetch_assoc(); $booking_id = $row['booking_id']; $name = $row['first_name']; $lname = $row['last_name']; $position = $row['position']; $company_name = $row['company_name']; $city = $row['city']; $country = $row['country']; $vat_number = $row['vat_number']; $countryCode = $row['countryCode']; $mobile = $row['mobile']; $mobile = $row['mobile']; $transaction_id = $row['transaction_id']; $price = $row['price']; $event_id = $row['event_id']; $date = $row['date']; $payment_status = $row['payment_status']; $currency = $row['currency']; send_zero_payment_confirmation_email($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); send_zero_payment_confirmation_client($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); $_SESSION['receipt_id']=$last_id; header('Location:receipt.php'); } } //$stripe = new \Stripe\StripeClient('sk_test_51HqxNhLB5kw3RTIUhauZZsIPMfsAcXCvdUdIjIF9b99Xlcpjf831CmqNB9Y0mCwcBU2iGFiwe571HERp2zZXtStk0003TA8OwV'); // $pay=$stripe->paymentIntents->create([ // 'amount' => $amount*100, // 'currency' => 'eur', // 'automatic_payment_methods' => ['enabled' => true], // ]); //print_r($pay); exit; // $charge = \Stripe\Charge::create([ // 'amount' => $amount*100, // 'currency' => 'eur', // 'source' => $token, // 'description' => 'Event booking', // ]); //print_r($charge); exit; // $paymentIntent = \Stripe\PaymentIntent::create([ // 'amount' => $amount * 100, // 'currency' => 'eur', // 'payment_method' => $token, // 'confirmation_method' => 'manual', // 'confirm' => true, // 'description' => 'Event booking', // 'payment_method_options' => [ // 'card' => [ // 'request_three_d_secure' => 'any', // ], // ], // ]); // print_r($paymentIntent); exit; // if (!empty($charge) && $charge->status == 'succeeded') { // $transaction_id = $charge->id; // $paid_amount = $charge->amount; // $paid_currency = $charge->currency; // $payment_status = $charge->status; // $new_price = $paid_amount/100; // $sqlQ ="INSERT INTO `payments` (`booking_id`,`first_name`,`last_name`,`position`,`company_name`,`city`,`country`, `vat_number`, `email`, `countryCode`, `mobile`, `food_items`, `comments`,`coupon_code`,`gdpr`,`transaction_id`,`payment_status`,`date`,`event_id`,`price`,`currency`) VALUES ('$unique_id','$first_name','$last_name','$position','$company_name','$city','$country','$vat_number', '$email', '$countryCode', '$mobile', '$food_items', '$comments','$coupon_code','1', '$transaction_id', '$payment_status','$date', '$event_name', '$new_price','$paid_currency')"; // if ($conn->query($sqlQ) === TRUE) { // $last_id = $conn->insert_id; // $sql_last = "SELECT * FROM `payments` WHERE `id` = '$last_id' ORDER BY `id` DESC"; // $result_last = $conn->query($sql_last); // $row = $result_last->fetch_assoc(); // $name = $row['first_name']; // $booking_id = $row['booking_id']; // $transaction_id = $row['transaction_id']; // $price = $row['price']; // $event_id = $row['event_id']; // $date = $row['date']; // send_payment_confirmation_email($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); // send_payment_confirmation_client($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); // header('Location:payment_success.php?trans_id='.$transaction_id); // } // } // } catch (\Stripe\Exception\CardException $e) { // // Handle card payment errors // // echo "Error: " . $e->getMessage(); exit; // if ($e->getError()->payment_intent->status === 'requires_action') { // // The payment requires further user action, redirect to the 3DS authentication URL // $redirect_url = $e->getError()->payment_intent->next_action->redirect_to_url->url; // header("Location: $redirect_url"); exit; // } else { // echo "Error: " . $e->getMessage(); exit; // } // } // } else { // $sqlQ ="INSERT INTO `payments` (`booking_id`,`first_name`,`last_name`,`position`,`company_name`,`city`,`country`, `vat_number`, `email`, `countryCode`, `mobile`, `food_items`, `comments`,`coupon_code`,`gdpr`,`transaction_id`,`payment_status`,`date`,`event_id`,`price`,`currency`) VALUES ('$unique_id','$first_name','$last_name','$position','$company_name','$city','$country','$vat_number', '$email', '$countryCode', '$mobile', '$food_items', '$comments','$coupon_code','1', '', 'succeeded','$date', '$event_name', '0','euro')"; // if ($conn->query($sqlQ) === TRUE) { // $last_id = $conn->insert_id; // $sql_last = "SELECT * FROM `payments` WHERE `id` = '$last_id' ORDER BY `id` DESC"; // $result_last = $conn->query($sql_last); // $row = $result_last->fetch_assoc(); // $booking_id = $row['booking_id']; // $name = $row['first_name']; // $lname = $row['last_name']; // $position = $row['position']; // $company_name = $row['company_name']; // $city = $row['city']; // $country = $row['country']; // $vat_number = $row['vat_number']; // $countryCode = $row['countryCode']; // $mobile = $row['mobile']; // $mobile = $row['mobile']; // $transaction_id = $row['transaction_id']; // $price = $row['price']; // $event_id = $row['event_id']; // $date = $row['date']; // $payment_status = $row['payment_status']; // $currency = $row['currency']; // send_zero_payment_confirmation_email($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); // send_zero_payment_confirmation_client($email,$name,$booking_id,$transaction_id,$price,$event_id,$date); // $_SESSION['receipt_id']=$last_id; // header('Location:receipt.php'); // } // }