JezK
Edit File: process_payment.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'; \Stripe\Stripe::setApiKey('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'); // Retrieve JSON from POST body $jsonStr = file_get_contents('php://input'); $jsonObj = json_decode($jsonStr); if ($jsonObj === null) { http_response_code(400); echo json_encode(['error' => 'Invalid JSON input']); exit; } // Switch-case for handling different request types switch ($jsonObj->request_type) { case 'create_payment_intent': createPaymentIntent($jsonObj); break; case 'update_payment_intent': updatePaymentIntent($jsonObj); break; case 'create_customer': createCustomer($jsonObj); break; case 'payment_insert': insertPayment($jsonObj); break; default: http_response_code(400); echo json_encode(['error' => 'Invalid request type']); } function createPaymentIntent($data) { try { $amount = $data->event_price; $eventName = $data->event_name; $itemPriceCents = round($amount * 100); $description = $eventName; $paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => $itemPriceCents, 'currency' => 'eur', 'description' => $description, 'payment_method_types' => ['card'] ]); echo json_encode([ 'id' => $paymentIntent->id, 'clientSecret' => $paymentIntent->client_secret ]); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } } function updatePaymentIntent($data) { try { $payment_intent_id = !empty($data->payment_intent_id) ? $data->payment_intent_id : ''; $new_price = !empty($data->new_price) ? $data->new_price : ''; $payment_intent_obj = \Stripe\PaymentIntent::retrieve($payment_intent_id); $payment_intent_obj->amount = round($new_price * 100); $payment_intent_obj->save(); echo json_encode(['success' => true]); } catch (\Stripe\Exception\ApiErrorException $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } } function createCustomer($data) { try { $payment_intent_id = !empty($data->payment_intent_id) ? $data->payment_intent_id : ''; $email = !empty($data->email) ? $data->email : ''; $fname = !empty($data->name) ? $data->name : ''; // Additional metadata $metadata = [ 'first_name' => !empty($data->name) ? $data->name : '', 'last_name' => !empty($data->last_name) ? $data->last_name : '', 'position' => !empty($data->position) ? $data->position : '', 'company_name' => !empty($data->company_name) ? $data->company_name : '', 'city' => !empty($data->city) ? $data->city : '', 'country' => !empty($data->country) ? $data->country : '', 'vat_number' => !empty($data->vat_number) ? $data->vat_number : '', 'countryCode' => !empty($data->countryCode) ? $data->countryCode : '', 'mobile' => !empty($data->mobile) ? $data->mobile : '', 'eventPrice' => !empty($data->eventPrice) ? $data->eventPrice : '', 'event_title' => !empty($data->event_title) ? $data->event_title : '', 'couponCode' => !empty($data->couponCode) ? $data->couponCode : '', 'comments' => !empty($data->comments) ? $data->comments : '', 'foodItemsString' => !empty($data->foodItemsString) ? json_encode($data->foodItemsString) : '' ]; $customer = \Stripe\Customer::create([ 'email' => $email, 'name' => $fname, 'metadata' => $metadata ]); if ($customer && $customer->id) { $paymentIntent = \Stripe\PaymentIntent::update($payment_intent_id, [ 'customer' => $customer->id ]); echo json_encode([ 'id' => $payment_intent_id, 'customer_id' => $customer->id ]); } else { throw new Exception('Customer ID is null after creation.'); } } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } } function insertPayment($data) { global $conn; try { // Extract necessary data from $data $payment_intent = !empty($data->payment_intent) ? $data->payment_intent : ''; $customer_id = !empty($data->customer_id) ? $data->customer_id : ''; // Retrieve payment intent object from Stripe $payment_intent_obj = \Stripe\PaymentIntent::retrieve($payment_intent); // Check if payment was successful if ($payment_intent_obj->status === 'succeeded') { // Extract metadata from customer object $customer = \Stripe\Customer::retrieve($customer_id); $metadata = $customer->metadata; // Generate unique booking ID $sql = "SELECT MAX(id) AS max_id FROM payments"; $result = $conn->query($sql); if (!$result) { throw new Exception('Error retrieving max ID from payments table.'); } $data = $result->fetch_assoc(); $unique_id = "REIA-B00" . ($data['max_id'] + 1); // Check if payment ID already exists in database $check_payment_id = "SELECT COUNT(*) AS count FROM payments WHERE payment_intent_id = '$payment_intent'"; $check_result = $conn->query($check_payment_id); if (!$check_result) { throw new Exception('Error checking payment ID existence.'); } $row = $check_result->fetch_assoc(); $row_count = $row['count']; if ($row_count == 0) { // Insert payment data into database $date = date('Y-m-d'); $transaction_id = $payment_intent_obj->id; $paid_amount = $payment_intent_obj->amount_received; $new_price = ($paid_amount / 100); $paid_currency = $payment_intent_obj->currency; $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', '{$metadata['first_name']}', '{$metadata['last_name']}', '{$metadata['position']}', '{$metadata['company_name']}', '{$metadata['city']}', '{$metadata['country']}', '{$metadata['vat_number']}', '{$customer->email}', '{$metadata['countryCode']}', '{$metadata['mobile']}', '{$metadata['comments']}', '{$metadata['couponCode']}', '$transaction_id', '{$payment_intent_obj->status}', '$date', '{$metadata['event_title']}', '$new_price', '$paid_currency', '$payment_intent')"; if ($conn->query($sql) === TRUE) { // Send payment confirmation emails send_payment_confirmation_email($customer->email, $metadata['first_name'], $unique_id, $transaction_id, $new_price, $metadata['event_id'], $date); send_payment_confirmation_client($customer->email, $metadata['first_name'], $unique_id, $transaction_id, $new_price, $metadata['event_id'], $date, $metadata['company_name'], $metadata['city'], $metadata['country'], $metadata['position'], $metadata['mobile'], $metadata['coupon_code'], $metadata['comments'], $metadata['vat_number']); echo json_encode(['payment_txn_id' => $transaction_id, 'booking_id' => $unique_id]); } else { throw new Exception('Error inserting payment data into database.'); } } } else { http_response_code(400); echo json_encode(['error' => 'Payment not succeeded']); } } catch (\Stripe\Exception\ApiErrorException $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); } } ?>