AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

FakeGateway.php 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. use App\Models\Store;
  9. /**
  10. * A payment provider for tests. Nothing leaves the machine.
  11. * Tests decide whether a payment page was paid by calling pay().
  12. */
  13. final class FakeGateway implements PaymentGateway
  14. {
  15. /** @var array<string, PaymentStatus> */
  16. public array $payments = [];
  17. /** @var list<Order> */
  18. public array $started = [];
  19. public bool $failToStart = false;
  20. /** Whether stores can take payments. Most tests do not care about connecting Stripe. */
  21. public bool $takingPayments = true;
  22. public ?string $successUrl = null;
  23. public ?string $cancelUrl = null;
  24. public function name(): string
  25. {
  26. return 'fake';
  27. }
  28. public function canTakePayments(Store $store): bool
  29. {
  30. return $this->takingPayments;
  31. }
  32. public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
  33. {
  34. if ($this->failToStart) {
  35. throw new PaymentException('The provider is down.');
  36. }
  37. $id = 'cs_fake_'.$order->id;
  38. $this->started[] = $order;
  39. $this->successUrl = $successUrl;
  40. $this->cancelUrl = $cancelUrl;
  41. $this->payments[$id] = new PaymentStatus($id, paid: false);
  42. return new CheckoutLink($id, 'https://pay.test/'.$id, 'acct_fake_'.$order->store_id);
  43. }
  44. public function checkPayment(Order $order): PaymentStatus
  45. {
  46. return $this->find((string) $order->payment_checkout_id);
  47. }
  48. public function readWebhook(string $payload, string $signature, bool $fromMerchants = false): ?PaymentStatus
  49. {
  50. if ($signature !== ($fromMerchants ? 'valid-connect' : 'valid')) {
  51. throw new PaymentException('Bad signature.');
  52. }
  53. $data = json_decode($payload, true);
  54. if (! isset($data['checkout_id'])) {
  55. return null;
  56. }
  57. $status = $this->find($data['checkout_id']);
  58. // Messages about merchants' accounts say which account the payment was made in.
  59. return new PaymentStatus($status->checkoutId, $status->paid, $status->reference, $status->amount, $data['account'] ?? null);
  60. }
  61. private function find(string $checkoutId): PaymentStatus
  62. {
  63. return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.');
  64. }
  65. /**
  66. * Pretend the shopper paid on the payment page.
  67. */
  68. public function pay(Order $order, ?int $amount = null): void
  69. {
  70. $id = (string) $order->fresh()?->payment_checkout_id;
  71. $this->payments[$id] = new PaymentStatus($id, paid: true, reference: 'pi_fake_'.$order->id, amount: $amount ?? $order->total);
  72. }
  73. }