Add payments foundation (e-transfer/comp, Stripe config, receipts)
CI / Tests (PHP 8.1) (pull_request) Successful in 45s
CI / Tests (PHP 8.3) (pull_request) Successful in 50s
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 1m2s
CI / Tests (PHP 8.2) (pull_request) Successful in 1m0s
CI / PHPStan (pull_request) Successful in 1m4s
CI / Build Plugin Zip (pull_request) Has been skipped

Implements the payments foundation for #7. Without Stripe credentials
everything works on e-transfer (pending payment confirmed by a studio
admin); when Stripe keys are configured the default flips to credit card.
Per-student override (card/etransfer/comp) is set on the student detail.

- Schema: us_payments (amount DECIMAL dollars, method, status, receipt,
  stripe intent id).
- src/Payment/: Payment VO, PaymentRepository, StudioSettings (Stripe
  options + isStripeConfigured + settings page), BillingMethodResolver
  (per-student override; default card if configured else etransfer),
  ReceiptMailer, PaymentService (create at registration, link payment_id,
  comp->paid+confirm, markPaid->confirm+receipt), PaymentController
  (e-transfer confirmation queue), PaymentEndpoint (PATCH /payments/{id}).
- Booking + enrolment create the payment from the offering price; comp
  auto-confirms the lesson; setPaymentId on both repositories.
- Admin: Studio Settings + Payments menus (manage_billing); per-student
  billing method on the student detail page.
- Docs: payments.md + README updated.

Deferred to a follow-up: the live Stripe card charge (PaymentIntent +
Stripe.js Elements + webhook + stripe/stripe-php). Until then a card
payment is created pending and confirmed like an e-transfer.

Tests: tests/Unit/Payment/ (VO, repository, resolver, service, mailer).
composer test (147), cs, and PHPStan level 6 all pass.

Refs #7

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
2026-06-08 10:24:01 -03:00
co-authored by Claude Opus 4.8
parent 071ef7fc2a
commit 6c4097b385
27 changed files with 1201 additions and 12 deletions
+114
View File
@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Payment;
class PaymentRepository {
private string $table;
public function __construct( private \wpdb $db ) {
$this->table = $db->prefix . 'us_payments';
}
public function insert( Payment $payment ): int {
$this->db->insert(
$this->table,
[
'student_id' => $payment->studentId,
'instructor_id' => $payment->instructorId,
'registration_type' => $payment->registrationType,
'registration_id' => $payment->registrationId,
'amount' => $payment->amount,
'currency' => $payment->currency,
'method' => $payment->method,
'status' => $payment->status,
'stripe_payment_intent_id' => $payment->stripePaymentIntentId,
'receipt_number' => $payment->receiptNumber,
'receipt_sent_at' => $payment->receiptSentAt,
'paid_at' => $payment->paidAt,
'created_at' => current_time( 'mysql' ),
],
[ '%d', '%d', '%s', '%d', '%f', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ]
);
return $this->db->insert_id;
}
public function findById( int $id ): ?Payment {
$row = $this->db->get_row(
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
);
return $row ? Payment::fromRow( $row ) : null;
}
public function findByRegistration( string $registrationType, int $registrationId ): ?Payment {
$row = $this->db->get_row(
$this->db->prepare(
"SELECT * FROM {$this->table} WHERE registration_type = %s AND registration_id = %d ORDER BY id DESC LIMIT 1",
$registrationType,
$registrationId
)
);
return $row ? Payment::fromRow( $row ) : null;
}
/**
* Pending payments, newest first (studio-admin confirmation queue).
*
* @return list<Payment>
*/
public function findPending(): array {
$rows = $this->db->get_results(
$this->db->prepare(
"SELECT * FROM {$this->table} WHERE status = %s ORDER BY created_at DESC",
Payment::STATUS_PENDING
)
);
return array_map( Payment::fromRow( ... ), $rows ?? [] );
}
/**
* Mark a payment paid, stamping the paid time and receipt number.
*/
public function markPaid( int $id, string $receiptNumber ): bool {
return false !== $this->db->update(
$this->table,
[
'status' => Payment::STATUS_PAID,
'paid_at' => current_time( 'mysql' ),
'receipt_number' => $receiptNumber,
],
[ 'id' => $id ],
[ '%s', '%s', '%s' ],
[ '%d' ]
);
}
public function markReceiptSent( int $id ): bool {
return false !== $this->db->update(
$this->table,
[ 'receipt_sent_at' => current_time( 'mysql' ) ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
public function updateStatus( int $id, string $status ): bool {
if ( ! in_array( $status, Payment::VALID_STATUSES, true ) ) {
return false;
}
return (bool) $this->db->update(
$this->table,
[ 'status' => $status ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
}