$client_id, 'itflow_client_name' => $client_name, 'itflow_invoice_number' => $invoice_prefix . $invoice_number, 'itflow_invoice_id' => $invoice_id, ]; if ($fee_cents > 0) { $pi_metadata['itflow_fees'] = "$invoice_id:$fee_cents"; } $pi_description = "ITFlow: $client_name payment of $invoice_currency_code " . number_format(($balance_cents + $fee_cents) / 100, 2, '.', '') . ($fee_cents > 0 ? ' (with a ' . number_format($fee_cents / 100, 2, '.', '') . ' card fee)' : '') . " for $invoice_prefix$invoice_number"; try { \Stripe\Stripe::setApiKey($stripe_secret_key); $paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => $balance_cents + $fee_cents, // Stripe expects cents 'currency' => $invoice_currency_code, 'description' => $pi_description, 'metadata' => $pi_metadata, 'payment_method_types' => ['card'], 'customer' => $stripe_customer_id, ]); $output = [ 'clientSecret' => $paymentIntent->client_secret, ]; echo json_encode($output); } catch (\Throwable $e) { http_response_code(500); error_log("Stripe PI create failed (invoice $invoice_id): " . $e->getMessage()); echo json_encode(['error' => $e->getMessage()]); } exit; }