AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FakeGateway.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Tests\Support;
  3. use App\Commerce\Payments\CheckoutLink;
  4. use App\Commerce\Payments\PaymentException;
  5. use App\Commerce\Payments\PaymentGateway;
  6. use App\Commerce\Payments\PaymentStatus;
  7. use App\Models\Order;
  8. /**
  9. * A payment provider for tests. Nothing leaves the machine.
  10. * Tests decide whether a payment page was paid by calling pay().
  11. */
  12. final class FakeGateway implements PaymentGateway
  13. {
  14. /** @var array<string, PaymentStatus> */
  15. public array $payments = [];
  16. /** @var list<Order> */
  17. public array $started = [];
  18. public bool $failToStart = false;
  19. public function name(): string
  20. {
  21. return 'fake';
  22. }
  23. public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
  24. {
  25. if ($this->failToStart) {
  26. throw new PaymentException('The provider is down.');
  27. }
  28. $id = 'cs_fake_'.$order->id;
  29. $this->started[] = $order;
  30. $this->payments[$id] = new PaymentStatus($id, paid: false);
  31. return new CheckoutLink($id, 'https://pay.test/'.$id);
  32. }
  33. public function checkPayment(string $checkoutId): PaymentStatus
  34. {
  35. return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.');
  36. }
  37. public function readWebhook(string $payload, string $signature): ?PaymentStatus
  38. {
  39. if ($signature !== 'valid') {
  40. throw new PaymentException('Bad signature.');
  41. }
  42. $data = json_decode($payload, true);
  43. return isset($data['checkout_id']) ? $this->checkPayment($data['checkout_id']) : null;
  44. }
  45. /**
  46. * Pretend the shopper paid on the payment page.
  47. */
  48. public function pay(Order $order, ?int $amount = null): void
  49. {
  50. $id = (string) $order->fresh()?->payment_checkout_id;
  51. $this->payments[$id] = new PaymentStatus($id, paid: true, reference: 'pi_fake_'.$order->id, amount: $amount ?? $order->total);
  52. }
  53. }