I am trying to send invoices from my web app for school instrumental music lessons. The amount can vary depending on institution rates and grouping.
This is what I have been trying:
function createStripeLessonInvoice($parentName, $email, $childName, $instrument, $grouping, $cost, $lessonCount) {
StripeStripe::setApiKey('sk_test_[MYKEY]');
try {
//Get or create customer
$customer = getOrCreateStripeCustomer($email, $parentName, null, $childName);
error_log("COST IS $cost");
error_log("Invoice creation with cost: " . ($cost * 100)); // Check the amount in pence
// Create the invoice
$invoiceItem = StripeInvoiceItem::create([
'customer' => $customer->id,
'amount' => $cost * 100, // amount in pence
'currency' => 'gbp',
'description' => 'Lesson Fee',
'metadata' => [
'student_name' => $childName,
'instrument' => $instrument,
'grouping' => $grouping,
'lesson_count' => $lessonCount,
'total' => $cost,
],
]);
$invoice = StripeInvoice::create([
'customer' => $customer->id,
'collection_method' => 'send_invoice',
'days_until_due' => 7,
]);
//Finalize the invoice
$finalized = $invoice->finalizeInvoice();
//Return the invoice URL or log
return $finalized->hosted_invoice_url;
error_log("Invoice item created: " . print_r($invoiceItem, true)); // Log the invoice item creation
//Finalize the invoice
$invoice->finalizeInvoice();
error_log("Invoice finalized: " . print_r($invoice, true));
// Return the hosted invoice URL
return $invoice->hosted_invoice_url;
} catch (StripeExceptionApiErrorException $e) {
// Handle Stripe API errors
error_log('Stripe error: ' . $e->getMessage());
return null; // or handle this error more gracefully
}
}
Logging shows that the cost is £90, but I get a £0 invoice to the correct customer each time (which obviously doesn’t send).
Is what I am trying to do not possible with Stripe, or have I missed something?
Thanks,
Chris