JezK
Edit File: transferfunction.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'); include '../membership/vendor/autoload.php'; $even_price = $_POST['event_price']; // if($even_price!=0) { // Set API key \Stripe\Stripe::setApiKey('sk_test_51HqxNhLB5kw3RTIUhauZZsIPMfsAcXCvdUdIjIF9b99Xlcpjf831CmqNB9Y0mCwcBU2iGFiwe571HERp2zZXtStk0003TA8OwV'); // Retrieve JSON from POST body $jsonStr = file_get_contents('php://input'); $jsonObj = json_decode($jsonStr); //echo "Request Type: " . $jsonObj->request_type . "\n"; if ($jsonObj->request_type == 'create_payment_intent') { $amount = $jsonObj->event_price; // 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'] ]); $output = [ 'id' => $paymentIntent->id, 'clientSecret' => $paymentIntent->client_secret ]; echo json_encode($output); } catch (Error $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } } elseif ($jsonObj->request_type == 'create_customer') { $payment_intent_id = !empty($jsonObj->payment_intent_id) ? $jsonObj->payment_intent_id : ''; $first_name = !empty($jsonObj->name) ? $jsonObj->name : ''; $last_name = !empty($jsonObj->last_name) ? $jsonObj->last_name : ''; $email = !empty($jsonObj->email) ? $jsonObj->email : ''; $position = !empty($jsonObj->position) ? $jsonObj->position : ''; $company_name = !empty($jsonObj->company_name) ? $jsonObj->company_name : ''; $city = !empty($jsonObj->city) ? $jsonObj->city : ''; $country = !empty($jsonObj->country) ? $jsonObj->country : ''; $vat_number = !empty($jsonObj->vat_number) ? $jsonObj->vat_number : ''; $countryCode = !empty($jsonObj->countryCode) ? $jsonObj->countryCode : ''; $mobile = !empty($jsonObj->mobile) ? $jsonObj->mobile : ''; $eventPrice = !empty($jsonObj->eventPrice) ? $jsonObj->eventPrice : ''; $event_title = !empty($jsonObj->event_title) ? $jsonObj->event_title : ''; $couponCode = !empty($jsonObj->couponCode) ? $jsonObj->couponCode : ''; $comments = !empty($jsonObj->comments) ? $jsonObj->comments : ''; // Add customer to stripe try { $customer = \Stripe\Customer::create([ 'name' => $name, 'email' => $email ]); $date = date('Y-m-d'); $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; $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`,`payment_intent_id`) VALUES ('$unique_id','$first_name','$last_name','$position','$company_name','$city','$country','$vat_number', '$email', '$countryCode', '$mobile', '[]', '$comments','$couponCode','1', '', 'succeeded','$date', '$event_title', '$eventPrice','euro', '$payment_intent_id')"; $insert_data = $conn->query($sqlQ); if($insert_data) { $name = $first_name; $booking_id = $unique_id; $transaction_id = $payment_intent_id; $price = $eventPrice; $event_id =$event_title; $date = date('Y-m-d'); // 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); } } 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($jsonObj->payment_intent) ? $jsonObj->payment_intent : ''; $customer_id = !empty($jsonObj->customer_id) ? $jsonObj->customer_id : ''; //echo $transaction_id = $payment_intent->id; exit; // Retrieve customer info try { $customer = \Stripe\Customer::retrieve($customer_id); } catch (Exception $e) { $api_error = $e->getMessage(); } // Check whether the charge was successful if (!empty($payment_intent)) { // Transaction details $date = date('Y-m-d'); $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 = "UPDATE `payments` SET `payment_intent_id`='$transaction_id',`payment_status`='$payment_status',`currency`='$paid_currency' WHERE `payment_intent_id`='$payment_intent'"; print_r($sqlQ); 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']; // header('Location:payment_success.php?trans_id=' . $transaction_id); } } else { http_response_code(500); echo json_encode(['error' => 'Transaction has been failed!']); } } // } else { //echo 'adasd'; exit; // $date = date('Y-m-d'); // $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`,`payment_intent_id`) 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'); // } // } ?>