AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FakeGateway.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 methods(Store $store): array
  33. {
  34. return ['card', 'fpx', 'grabpay'];
  35. }
  36. public function startCheckout(Order $order, string $successUrl, string $cancelUrl): CheckoutLink
  37. {
  38. if ($this->failToStart) {
  39. throw new PaymentException('The provider is down.');
  40. }
  41. $id = 'cs_fake_'.$order->id;
  42. $this->started[] = $order;
  43. $this->successUrl = $successUrl;
  44. $this->cancelUrl = $cancelUrl;
  45. $this->payments[$id] = new PaymentStatus($id, paid: false);
  46. return new CheckoutLink($id, 'https://pay.test/'.$id, 'acct_fake_'.$order->store_id);
  47. }
  48. public function checkPayment(Order $order): PaymentStatus
  49. {
  50. return $this->find((string) $order->payment_checkout_id);
  51. }
  52. public function readWebhook(string $payload, string $signature, bool $fromMerchants = false): ?PaymentStatus
  53. {
  54. if ($signature !== ($fromMerchants ? 'valid-connect' : 'valid')) {
  55. throw new PaymentException('Bad signature.');
  56. }
  57. $data = json_decode($payload, true);
  58. if (! isset($data['checkout_id'])) {
  59. return null;
  60. }
  61. $status = $this->find($data['checkout_id']);
  62. // Messages about merchants' accounts say which account the payment was made in.
  63. return new PaymentStatus($status->checkoutId, $status->paid, $status->reference, $status->amount, $data['account'] ?? null);
  64. }
  65. private function find(string $checkoutId): PaymentStatus
  66. {
  67. return $this->payments[$checkoutId] ?? throw new PaymentException('Unknown payment.');
  68. }
  69. /**
  70. * Pretend the shopper paid on the payment page.
  71. */
  72. public function pay(Order $order, ?int $amount = null): void
  73. {
  74. $id = (string) $order->fresh()?->payment_checkout_id;
  75. $this->payments[$id] = new PaymentStatus($id, paid: true, reference: 'pi_fake_'.$order->id, amount: $amount ?? $order->total);
  76. }
  77. }