|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ypmn; |
| 6 | + |
| 7 | +/** |
| 8 | + * Это класс для описания направления платежа по сбп |
| 9 | + **/ |
| 10 | +class PayoutMobileDestination implements DestinationInterface |
| 11 | +{ |
| 12 | + private const AVAILABLE_TYPES = [ |
| 13 | + 'sbp', |
| 14 | + ]; |
| 15 | + |
| 16 | + private string $type; |
| 17 | + private ?DetailsInterface $details = null; |
| 18 | + private ?Billing $recipient = null; |
| 19 | + |
| 20 | + /** |
| 21 | + * @throws PaymentException |
| 22 | + */ |
| 23 | + public function __construct(string $type = 'sbp') |
| 24 | + { |
| 25 | + $this->setType($type); |
| 26 | + } |
| 27 | + |
| 28 | + /** @inheritdoc */ |
| 29 | + public function getType(): string |
| 30 | + { |
| 31 | + return $this->type; |
| 32 | + } |
| 33 | + |
| 34 | + /** @inheritdoc */ |
| 35 | + public function setType(string $type): self |
| 36 | + { |
| 37 | + if (!in_array($type, self::AVAILABLE_TYPES)) { |
| 38 | + throw new PaymentException('Недопустимый тип выплаты'); |
| 39 | + } |
| 40 | + |
| 41 | + $this->type = $type; |
| 42 | + |
| 43 | + return $this; |
| 44 | + } |
| 45 | + |
| 46 | + /** @inheritdoc */ |
| 47 | + public function getDetails(): ?DetailsInterface |
| 48 | + { |
| 49 | + return $this->details; |
| 50 | + } |
| 51 | + |
| 52 | + /** @inheritdoc */ |
| 53 | + public function setDetails(?DetailsInterface $details): self |
| 54 | + { |
| 55 | + $this->details = $details; |
| 56 | + |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + /** @inheritdoc */ |
| 61 | + public function getRecipient(): ?Billing |
| 62 | + { |
| 63 | + return $this->recipient; |
| 64 | + } |
| 65 | + |
| 66 | + /** @inheritdoc */ |
| 67 | + public function setRecipient(?Billing $recipient): self |
| 68 | + { |
| 69 | + $this->recipient = $recipient; |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + /** @inheritdoc */ |
| 74 | + public function setPhoneNumber(string $phoneNumber): self |
| 75 | + { |
| 76 | + if ($this->getDetails() === null) { |
| 77 | + $this->setDetails(new PhoneDetails()); |
| 78 | + } |
| 79 | + |
| 80 | + $this->getDetails()->setNumber($phoneNumber); |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + /** @inheritdoc */ |
| 86 | + public function setBankInformation(int $bankId, string $bankName): self |
| 87 | + { |
| 88 | + if ($this->getDetails() === null) { |
| 89 | + $this->setDetails(new PhoneDetails()); |
| 90 | + } |
| 91 | + |
| 92 | + $this->getDetails()->setBankId($bankId); |
| 93 | + $this->getDetails()->setBankName($bankName); |
| 94 | + |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function arraySerialize() : array |
| 102 | + { |
| 103 | + $address = $this->getRecipient()->getAddressLine1() |
| 104 | + . ( $this->getRecipient()->getAddressLine2() ? '' . $this->getRecipient()->getAddressLine2() : null); |
| 105 | + |
| 106 | + return [ |
| 107 | + 'type' => $this->getType(), |
| 108 | + 'sbp' => [ |
| 109 | + 'phoneNumber' => $this->getDetails()->getNumber(), |
| 110 | + "bankId" => $this->getDetails()->getBankId(), |
| 111 | + "bankName" => $this->getDetails()->getBankName() |
| 112 | + ], |
| 113 | + 'recipient' => [ |
| 114 | + 'type' => $this->getRecipient()->getType(), |
| 115 | + 'email' => $this->getRecipient()->getEmail(), |
| 116 | + 'city' => $this->getRecipient()->getCity(), |
| 117 | + 'address' => $address, |
| 118 | + 'postalCode' => $this->getRecipient()->getZipCode(), |
| 119 | + 'countryCode' => $this->getRecipient()->getCountryCode(), |
| 120 | + 'firstName' => $this->getRecipient()->getFirstName(), |
| 121 | + 'lastName' => $this->getRecipient()->getLastName() |
| 122 | + ], |
| 123 | + ]; |
| 124 | + } |
| 125 | +} |
0 commit comments