Skip to content

Commit 5a3661c

Browse files
feat: add db logs for payments
1 parent e83b2e8 commit 5a3661c

File tree

5 files changed

+358
-0
lines changed

5 files changed

+358
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kanvas\Souk\Payments\Actions;
6+
7+
use Kanvas\Souk\Payments\Models\PaymentLogs;
8+
use Kanvas\Souk\Payments\Models\Payments;
9+
use Throwable;
10+
11+
class LogPaymentEventAction
12+
{
13+
public function execute(Payments $payment, string $event, array $context = []): void
14+
{
15+
try {
16+
PaymentLogs::create([
17+
'payments_id' => $payment->id,
18+
'apps_id' => $payment->apps_id,
19+
'companies_id' => $payment->companies_id,
20+
'users_id' => $payment->users_id,
21+
'status' => $event,
22+
'metadata' => $context,
23+
]);
24+
} catch (Throwable $e) {
25+
report($e);
26+
}
27+
}
28+
}

src/Domains/Souk/Payments/Models/PaymentLogs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
namespace Kanvas\Souk\Payments\Models;
66

7+
use Baka\Casts\Json;
78
use Kanvas\Souk\Models\BaseModel;
89

910
class PaymentLogs extends BaseModel
1011
{
1112
protected $table = 'payment_logs';
1213
protected $guarded = [];
14+
15+
protected $casts = [
16+
'metadata' => Json::class,
17+
];
1318
}

src/Domains/Souk/Payments/Models/Payments.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Relations\HasMany;
1010
use Kanvas\Payments\Models\PaymentMethods;
1111
use Kanvas\Souk\Models\BaseModel;
12+
use Kanvas\Souk\Payments\Actions\LogPaymentEventAction;
1213
use Kanvas\Souk\Payments\Enums\PaymentStatusEnum;
1314
use Kanvas\Workflow\Traits\CanUseWorkflow;
1415

@@ -66,6 +67,11 @@ public function addMetadata(array $metadata): void
6667
];
6768
}
6869

70+
public function addLog(string $event, array $context = []): void
71+
{
72+
app(LogPaymentEventAction::class)->execute($this, $event, $context);
73+
}
74+
6975
public function scopePending($query)
7076
{
7177
return $query->whereIn('status', [PaymentStatusEnum::PENDING->value, PaymentStatusEnum::PENDING_AUTHORIZATION->value]);

src/Domains/Souk/Payments/Providers/PortalPaymentProcessor.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ protected function setupService(Order $orderInput): array
135135

136136
public function startPaymentIntent(Payments $payment): array
137137
{
138+
$payment->addLog('payment_initiated', [
139+
'order_id' => $payment->order->id,
140+
'amount' => $payment->amount,
141+
]);
142+
138143
$merchantAuthentication = $this->setupMerchantAuthentication($payment, $payment->order);
139144
$payerAuthentication = $this->client->setupPayer(
140145
$payment->order->id,
@@ -399,6 +404,13 @@ public function processPayment(Payments $payment, ConsumerAuthentication $consum
399404
$transactionId = (string) $paymentResponse['data']['processorInformation']['transactionId'];
400405
$intentId = (string) $paymentResponse['data']['id'];
401406

407+
$payment->addLog('payment_authorized', [
408+
'transaction_id' => $transactionId,
409+
'intent_id' => $intentId,
410+
'order_id' => $order->id,
411+
'amount' => $order->getTotalAmount(),
412+
]);
413+
402414
$payment->status = PaymentStatusEnum::AUTHORIZED;
403415
$payment->addMetadata([
404416
'data' => [
@@ -427,6 +439,11 @@ public function processPayment(Payments $payment, ConsumerAuthentication $consum
427439

428440
private function processPaymentCall(Payments $payment, ConsumerAuthentication $consumerData, Order $order): array
429441
{
442+
$payment->addLog('payment_processing', [
443+
'order_id' => $order->id,
444+
'amount' => $order->getTotalAmount(),
445+
]);
446+
430447
$referenceId = $order->get('auth_session_id');
431448
$merchantAuthentication = $this->setupMerchantAuthentication($payment, $order, includeDetails: true);
432449
$paymentData = PaymentDetail::from([
@@ -465,6 +482,13 @@ private function processPaymentCall(Payments $payment, ConsumerAuthentication $c
465482
$errorMessage = $e->getMessage();
466483
$errorBody = $e->getErrorBody();
467484

485+
$payment->addLog('payment_error', [
486+
'error_type' => 'EchoPayException',
487+
'error_message' => $errorMessage,
488+
'error_body' => $errorBody,
489+
'order_id' => $order->id,
490+
]);
491+
468492
$payment->status = PaymentStatusEnum::FAILED->value;
469493
$order->updateQuietly([
470494
'payment_status' => PaymentStatusEnum::FAILED->value,
@@ -504,6 +528,12 @@ private function processPaymentCall(Payments $payment, ConsumerAuthentication $c
504528
$messageBody = [];
505529
}
506530

531+
$payment->addLog('payment_error', [
532+
'error_type' => get_class($e),
533+
'error_message' => $errorMessage,
534+
'order_id' => $order->id,
535+
]);
536+
507537
$payment->status = PaymentStatusEnum::FAILED->value;
508538
$order->updateQuietly([
509539
'payment_status' => PaymentStatusEnum::FAILED->value,
@@ -548,6 +578,12 @@ public function capturePayment(Payments $payment, Order $order, string $transact
548578
$merchantAuthentication
549579
);
550580

581+
$payment->addLog('payment_captured', [
582+
'order_id' => $order->id,
583+
'transaction_id' => $transactionId,
584+
'amount' => $order->getTotalAmount(),
585+
]);
586+
551587
$payment->status = PaymentStatusEnum::PAID;
552588
$order->updateQuietly([
553589
'payment_status' => PaymentStatusEnum::PAID->value,
@@ -569,6 +605,14 @@ public function capturePayment(Payments $payment, Order $order, string $transact
569605
} catch (EchoPayException $e) {
570606
report($e);
571607

608+
$payment->addLog('payment_error', [
609+
'error_type' => 'EchoPayException',
610+
'error_message' => $e->getMessage(),
611+
'error_body' => $e->getErrorBody(),
612+
'order_id' => $order->id,
613+
'context' => 'capture_payment',
614+
]);
615+
572616
return [
573617
'status' => 'error',
574618
'message' => $e->getMessage(),
@@ -577,6 +621,13 @@ public function capturePayment(Payments $payment, Order $order, string $transact
577621
} catch (Throwable $e) {
578622
report($e);
579623

624+
$payment->addLog('payment_error', [
625+
'error_type' => get_class($e),
626+
'error_message' => $e->getMessage(),
627+
'order_id' => $order->id,
628+
'context' => 'capture_payment',
629+
]);
630+
580631
return [
581632
'status' => 'error',
582633
'message' => $e->getMessage(),
@@ -601,6 +652,13 @@ public function reversePayment(Payments $payment, Order $order, string $transact
601652
$reason
602653
);
603654

655+
$payment->addLog('payment_reversed', [
656+
'transaction_id' => $transactionId,
657+
'reason' => $reason,
658+
'order_id' => $order->id,
659+
'amount' => $order->getTotalAmount(),
660+
]);
661+
604662
$payment->status = PaymentStatusEnum::REVERSED->value;
605663
$order->updateQuietly([
606664
'payment_status' => PaymentStatusEnum::REVERSED->value,
@@ -624,6 +682,16 @@ public function reversePayment(Payments $payment, Order $order, string $transact
624682
} catch (EchoPayException $e) {
625683
report($e);
626684

685+
$payment->addLog('payment_error', [
686+
'error_type' => 'EchoPayException',
687+
'error_message' => $e->getMessage(),
688+
'error_body' => $e->getErrorBody(),
689+
'transaction_id' => $transactionId,
690+
'reason' => $reason,
691+
'order_id' => $order->id,
692+
'context' => 'reverse_payment',
693+
]);
694+
627695
return [
628696
'status' => 'error',
629697
'message' => $e->getMessage(),
@@ -632,6 +700,15 @@ public function reversePayment(Payments $payment, Order $order, string $transact
632700
} catch (Throwable $e) {
633701
report($e);
634702

703+
$payment->addLog('payment_error', [
704+
'error_type' => get_class($e),
705+
'error_message' => $e->getMessage(),
706+
'transaction_id' => $transactionId,
707+
'reason' => $reason,
708+
'order_id' => $order->id,
709+
'context' => 'reverse_payment',
710+
]);
711+
635712
return [
636713
'status' => 'error',
637714
'message' => $e->getMessage(),

0 commit comments

Comments
 (0)