feat: add invoice
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Mail\PaymentInvoiceMail;
|
||||
use App\Models\PaymentTransaction;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Throwable;
|
||||
|
||||
class PaymentInvoiceEmailer
|
||||
{
|
||||
public function sendIfAvailable(PaymentTransaction $transaction): void
|
||||
{
|
||||
if ($transaction->invoice_email_sent_at !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $transaction->user || ! $transaction->user->email) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $transaction->invoice_url && ! $transaction->invoice_pdf_url) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Mail::to($transaction->user->email)->send(
|
||||
new PaymentInvoiceMail($transaction->user, $transaction->loadMissing('creditProduct'))
|
||||
);
|
||||
} catch (Throwable $exception) {
|
||||
$transaction->forceFill([
|
||||
'error_message' => 'Invoice email failed: '.$exception->getMessage(),
|
||||
])->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$transaction->forceFill([
|
||||
'invoice_email_sent_at' => now(),
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ class PaymentTransactionRecorder
|
||||
?int $creditsExpected = null,
|
||||
?int $creditsGranted = null,
|
||||
?CreditLedgerEntry $ledgerEntry = null,
|
||||
?string $invoiceUrl = null,
|
||||
?string $invoicePdfUrl = null,
|
||||
?string $errorMessage = null,
|
||||
?array $payload = null,
|
||||
): PaymentTransaction {
|
||||
@@ -47,6 +49,8 @@ class PaymentTransactionRecorder
|
||||
'currency' => $currency,
|
||||
'credits_expected' => $creditsExpected ?? $product?->credits,
|
||||
'credits_granted' => $creditsGranted ?? 0,
|
||||
'invoice_url' => $invoiceUrl,
|
||||
'invoice_pdf_url' => $invoicePdfUrl,
|
||||
'error_message' => $errorMessage,
|
||||
'payload' => $payload,
|
||||
'processed_at' => $status === PaymentTransactionStatus::PENDING ? null : now(),
|
||||
@@ -72,6 +76,8 @@ class PaymentTransactionRecorder
|
||||
?int $creditsExpected = null,
|
||||
?int $creditsGranted = null,
|
||||
?CreditLedgerEntry $ledgerEntry = null,
|
||||
?string $invoiceUrl = null,
|
||||
?string $invoicePdfUrl = null,
|
||||
?string $errorMessage = null,
|
||||
?array $payload = null,
|
||||
): PaymentTransaction {
|
||||
@@ -93,6 +99,8 @@ class PaymentTransactionRecorder
|
||||
'currency' => $currency,
|
||||
'credits_expected' => $creditsExpected ?? $product?->credits,
|
||||
'credits_granted' => $creditsGranted ?? 0,
|
||||
'invoice_url' => $invoiceUrl,
|
||||
'invoice_pdf_url' => $invoicePdfUrl,
|
||||
'error_message' => $errorMessage,
|
||||
'payload' => $payload,
|
||||
'processed_at' => now(),
|
||||
|
||||
Reference in New Issue
Block a user