JezK
Edit File: process_payment_original.php
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); include 'db_connection.php'; require_once('stripe-php/init.php'); include '../membership/email.php'; include '../membership/vendor/autoload.php'; // Set API key \Stripe\Stripe::setApiKey('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'); // Retrieve JSON from POST body $jsonStr = file_get_contents('php://input'); $jsonObj = json_decode($jsonStr); if ($jsonObj->request_type == 'create_payment_intent') { $amount = $jsonObj->event_price; $eventName = $jsonObj->event_name; // 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' => $eventName, '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 == 'update_payment_intent') { $payment_intent_id = !empty($jsonObj->payment_intent_id) ? $jsonObj->payment_intent_id : ''; $new_price = !empty($jsonObj->new_price) ? $jsonObj->new_price : ''; try { // Retrieve payment intent directly using the Stripe API $payment_intent_obj = \Stripe\PaymentIntent::retrieve($payment_intent_id); // Update the payment intent with the new price $payment_intent_obj->amount = round($new_price * 100); $payment_intent_obj->save(); // Send success response echo json_encode(['success' => true]); } catch (\Stripe\Exception\ApiErrorException $e) { $api_error = $e->getMessage(); error_log("Stripe API Error: " . $api_error); // Send error response http_response_code(500); echo json_encode(['error' => $api_error]); } } elseif ($jsonObj->request_type == 'create_customer') { $payment_intent_id = !empty($jsonObj->payment_intent_id) ? $jsonObj->payment_intent_id : ''; $email = !empty($jsonObj->email) ? $jsonObj->email : ''; // Other parameters for metadata $first_name = !empty($jsonObj->name) ? $jsonObj->name : ''; $last_name = !empty($jsonObj->last_name) ? $jsonObj->last_name : ''; $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 : ''; // $food_items = !empty($jsonObj->foodItemsString) ? $jsonObj->foodItemsString : ''; $fooditems = !empty($jsonObj->foodItemsString) ? $jsonObj->foodItemsString : []; // Add customer to stripe try { $customer = \Stripe\Customer::create([ 'email' => $email, 'metadata' => [ 'first_name' => $first_name, 'last_name' => $last_name, 'position' => $position, 'company_name' => $company_name, 'city' => $city, 'country' => $country, 'vat_number' => $vat_number, 'countryCode' => $countryCode, 'mobile' => $mobile, 'eventPrice' => $eventPrice, 'event_title' => $event_title, 'couponCode' => $couponCode, 'comments' => $comments ] ]); if ($customer && $customer->id) { $paymentIntent = \Stripe\PaymentIntent::update($payment_intent_id, [ 'customer' => $customer->id ]); if ($paymentIntent) { $output = [ 'id' => $payment_intent_id, 'customer_id' => $customer->id ]; echo json_encode($output); } } else { // Log or echo an error message $api_error = 'Customer ID is null after creation.'; error_log("Stripe API Error: " . $api_error); http_response_code(500); echo json_encode(['error' => $api_error]); } } catch (Exception $e){ $api_error = $e->getMessage(); error_log("Stripe API Error: " . $api_error); 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 : ''; try { // Retrieve payment intent directly using the Stripe API $payment_intent_obj = \Stripe\PaymentIntent::retrieve($payment_intent); if ($payment_intent_obj->status === 'succeeded') { $date = date('Y-m-d'); $transaction_id = $payment_intent_obj->id; $paid_amount = $payment_intent_obj->amount_received; $new_price = ($paid_amount / 100); // Convert to the desired currency if needed $paid_currency = $payment_intent_obj->currency; $payment_status = $payment_intent_obj->status; $metadata = $payment_intent_obj->metadata; $customer = \Stripe\Customer::retrieve($customer_id); $email = $customer->email; $first_name = !empty($customer['metadata']['first_name']) ? $customer['metadata']['first_name'] : ''; $last_name = !empty($customer['metadata']['last_name']) ? $customer['metadata']['last_name'] : ''; $position = !empty($customer['metadata']['position']) ? $customer['metadata']['position'] : ''; $company_name = !empty($customer['metadata']['company_name']) ? $customer['metadata']['company_name'] : ''; $city = !empty($customer['metadata']['city']) ? $customer['metadata']['city'] : ''; $country = !empty($customer['metadata']['country']) ? $customer['metadata']['country'] : ''; $vat_number = !empty($customer['metadata']['vat_number']) ? $customer['metadata']['vat_number'] : ''; $countryCode = !empty($customer['metadata']['countryCode']) ? $customer['metadata']['countryCode'] : ''; $mobile = !empty($customer['metadata']['mobile']) ? $customer['metadata']['mobile'] : ''; $eventPrice = !empty($customer['metadata']['eventPrice']) ? $customer['metadata']['eventPrice'] : ''; $event_title = !empty($customer['metadata']['event_title']) ? $customer['metadata']['event_title'] : ''; $couponCode = !empty($customer['metadata']['couponCode']) ? $customer['metadata']['couponCode'] : ''; $comments = !empty($customer['metadata']['comments']) ? $customer['metadata']['comments'] : ''; // Generate booking ID $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; // Insert data into the payments table $sql = "INSERT INTO `payments` (`booking_id`, `first_name`, `last_name`, `position`, `company_name`, `city`, `country`, `vat_number`, `email`, `countryCode`, `mobile`, `comments`, `coupon_code`, `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', '$transaction_id', '$payment_status', '$date', '$event_title', '$new_price', '$paid_currency', '$payment_intent')"; $insert_data = $conn->query($sql); if ($insert_data === TRUE) { $payment_details = "SELECT * FROM `payments` WHERE `payment_intent_id` = '$transaction_id'"; $result = $conn->query($payment_details); $data = $result->fetch_assoc(); $email = $data['email']; $name = $data['first_name']; $booking_id = $data['booking_id']; $transaction_id = $data['payment_intent_id']; $price = $data['price']; $event_id = $data['event_id']; $date = $data['date']; $company_name = $data['company_name']; $city = $data['city']; $country = $data['country']; $vat_number = $data['vat_number']; $position = $data['position']; $mobile = $data['mobile']; $couponCode = $data['coupon_code']; $comments = $data['comments']; 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,$company_name,$city,$country,$position,$mobile,$couponCode,$comments,$vat_number); // Send success response echo json_encode(['payment_txn_id' => $transaction_id, 'booking_id' => $unique_id]); } else { // Send detailed error response http_response_code(500); echo json_encode(['error' => 'Error inserting payment data']); } } else { // Send detailed error response http_response_code(500); echo json_encode(['error' => 'Payment not succeeded']); } } catch (\Stripe\Exception\ApiErrorException $e) { $api_error = $e->getMessage(); error_log("Stripe API Error: " . $api_error); // Send detailed error response http_response_code(500); echo json_encode(['error' => $api_error]); } catch (Exception $e) { $generic_error = $e->getMessage(); error_log("Error: " . $generic_error); // Send detailed error response http_response_code(500); echo json_encode(['error' => $generic_error]); } }