OrderPaymentData.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData;
  3. use App\Data\Pagarme\PagarmeData;
  4. final readonly class OrderPaymentData extends PagarmeData
  5. {
  6. /**
  7. * @param OrderSplitData[]|null $split
  8. */
  9. public function __construct(
  10. public string $paymentMethod,
  11. public ?OrderCreditCardData $creditCard = null, public ?OrderPixData $pix = null,
  12. public ?array $split = null,
  13. ) {
  14. self::requireIn($this->paymentMethod, ['credit_card', 'pix'], 'payments.payment_method');
  15. if ($this->paymentMethod === 'credit_card' && ! $this->creditCard) {
  16. throw new \InvalidArgumentException('payments.credit_card e obrigatorio para credit_card.');
  17. }
  18. if ($this->paymentMethod === 'pix' && ! $this->pix) {
  19. throw new \InvalidArgumentException('payments.pix e obrigatorio para pix.');
  20. }
  21. }
  22. /**
  23. * @param OrderSplitData[]|null $split
  24. */
  25. public static function creditCard(OrderCreditCardData $creditCard, ?array $split = null): self
  26. {
  27. return new self(
  28. paymentMethod: 'credit_card',
  29. creditCard: $creditCard,
  30. split: $split,
  31. );
  32. }
  33. /**
  34. * @param OrderSplitData[]|null $split
  35. */
  36. public static function pix(OrderPixData $pix, ?array $split = null): self
  37. {
  38. return new self(
  39. paymentMethod: 'pix',
  40. pix: $pix,
  41. split: $split,
  42. );
  43. }
  44. //
  45. public function toArray(): array
  46. {
  47. $payload = $this->filterFilledRecursive([
  48. 'payment_method' => $this->paymentMethod,
  49. 'split' => $this->split,
  50. ]);
  51. if ($this->creditCard) {
  52. $payload['credit_card'] = $this->creditCard->toArray();
  53. }
  54. if ($this->pix) {
  55. $payload['pix'] = $this->pix->toArray();
  56. }
  57. return $payload;
  58. }
  59. }