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 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 ?string $successUrl = null;
  20. public ?string $cancelUrl = null;
  21. public function name(): string
  22. {
  23. return 'fake';
  24. }
  25. public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
  26. {
  27. if ($this->failToStart) {
  28. throw new PaymentException('The provider is down.');
  29. }
  30. $id = 'cs_fake_'.$order->id;
  31. $this->started[] = $order;
  32. $this->successUrl = $successUrl;
  33. $this->cancelUrl = $cancelUrl;
  34. $this->payments[$id] = new PaymentStatus($id, paid: false);
  35. return new CheckoutLink($id, 'https://pay.test/'.$id);
  36. }
  37. public function checkPayment(string $checkoutId): PaymentStatus
  38. {
  39. return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.');
  40. }
  41. public function readWebhook(string $payload, string $signature): ?PaymentStatus
  42. {
  43. if ($signature !== 'valid') {
  44. throw new PaymentException('Bad signature.');
  45. }
  46. $data = json_decode($payload, true);
  47. return isset($data['checkout_id']) ? $this->checkPayment($data['checkout_id']) : null;
  48. }
  49. /**
  50. * Pretend the shopper paid on the payment page.
  51. */
  52. public function pay(Order $order, ?int $amount = null): void
  53. {
  54. $id = (string) $order->fresh()?->payment_checkout_id;
  55. $this->payments[$id] = new PaymentStatus($id, paid: true, reference: 'pi_fake_'.$order->id, amount: $amount ?? $order->total);
  56. }
  57. }