|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\EveryPay\Messages; |
| 4 | + |
| 5 | +use Omnipay\EveryPay\Enums\PaymentState; |
| 6 | +use Omnipay\Common\Message\AbstractResponse; |
| 7 | +use Omnipay\Common\Message\RequestInterface; |
| 8 | + |
| 9 | +/** |
| 10 | + * Response |
| 11 | + */ |
| 12 | +class RefundResponse extends AbstractResponse |
| 13 | +{ |
| 14 | + public $failed = false; |
| 15 | + |
| 16 | + protected $message = null; |
| 17 | + protected $code = null; |
| 18 | + |
| 19 | + public function __construct(RequestInterface $request, $data) |
| 20 | + { |
| 21 | + parent::__construct($request, $data); |
| 22 | + |
| 23 | + $this->validateData(); |
| 24 | + } |
| 25 | + |
| 26 | + protected function validateData() |
| 27 | + { |
| 28 | + if (! $this->data) { |
| 29 | + $this->fail('Missing data!'); |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (isset($this->data['error'])) { |
| 35 | + $this->fail( |
| 36 | + $this->data['error']['message'], |
| 37 | + $this->data['error']['code'] ?? null |
| 38 | + ); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $this->message = 'Payment state - ' . $this->data['payment_state']; |
| 44 | + } |
| 45 | + |
| 46 | + protected function fail($message, $code = null) |
| 47 | + { |
| 48 | + $this->failed = true; |
| 49 | + $this->code = $code; |
| 50 | + $this->message = $message; |
| 51 | + } |
| 52 | + |
| 53 | + public function getMessage(): ?string |
| 54 | + { |
| 55 | + return $this->message; |
| 56 | + } |
| 57 | + |
| 58 | + public function getCode() |
| 59 | + { |
| 60 | + return $this->code; |
| 61 | + } |
| 62 | + |
| 63 | + protected function getPaymentState() |
| 64 | + { |
| 65 | + return $this->data['payment_state'] ?? null; |
| 66 | + } |
| 67 | + |
| 68 | + public function isSuccessful(): bool |
| 69 | + { |
| 70 | + if ($this->failed) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + return PaymentState::REFUNDED === $this->getPaymentState(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * This is the Payment Gateway’s reference to the transaction |
| 79 | + */ |
| 80 | + public function getTransactionReference() |
| 81 | + { |
| 82 | + return $this->data['payment_reference'] ?? null; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Initial amount used for the transaction. |
| 87 | + */ |
| 88 | + public function getInitialAmount() |
| 89 | + { |
| 90 | + return $this->data['initial_amount'] ?? null; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Payment standing amount. |
| 95 | + */ |
| 96 | + public function getStandingAmount() |
| 97 | + { |
| 98 | + return $this->data['standing_amount'] ?? null; |
| 99 | + } |
| 100 | +} |
0 commit comments