1 | <?php |
||
2 | |||
3 | namespace Pagantis\OrdersApiClient\Method; |
||
4 | |||
5 | use Httpful\Http; |
||
6 | use Httpful\Mime; |
||
7 | use Httpful\Request; |
||
8 | use Httpful\Response; |
||
9 | use Pagantis\OrdersApiClient\Exception\ClientException; |
||
10 | use Pagantis\OrdersApiClient\Model\Order; |
||
11 | |||
12 | /** |
||
13 | * Class RefundOrderMethod |
||
14 | * |
||
15 | * @package Pagantis\OrdersApiClient\Method |
||
16 | */ |
||
17 | class RefundOrderMethod extends AbstractMethod |
||
18 | { |
||
19 | /** |
||
20 | * Get Order Endpoint |
||
21 | */ |
||
22 | const ENDPOINT = '/orders'; |
||
23 | |||
24 | const REFUND_ENDPOINT = 'refunds'; |
||
25 | |||
26 | /** |
||
27 | * @var string $orderId |
||
28 | */ |
||
29 | protected $orderId; |
||
30 | |||
31 | /** |
||
32 | * @var Order\Refund |
||
33 | */ |
||
34 | protected $refund; |
||
35 | |||
36 | /** |
||
37 | * @param string $orderId |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | public function setOrderId($orderId) |
||
42 | { |
||
43 | $this->orderId = $orderId; |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param Order\Refund $refund |
||
50 | * |
||
51 | * @return $this |
||
52 | */ |
||
53 | public function setRefund(Order\Refund $refund) |
||
54 | { |
||
55 | $this->refund = $refund; |
||
56 | |||
57 | return $this; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return $this|AbstractMethod |
||
62 | * @throws \Httpful\Exception\ConnectionErrorException |
||
63 | * @throws \Pagantis\OrdersApiClient\Exception\HttpException |
||
64 | * @throws ClientException |
||
65 | */ |
||
66 | public function call() |
||
67 | { |
||
68 | if ($this->refund instanceof Order\Refund && is_string($this->orderId)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
69 | $this->prepareRequest(); |
||
70 | return $this->setResponse($this->request->send()); |
||
71 | } |
||
72 | throw new ClientException('Please set Refund Object and OrderId'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return bool|Order\Refund |
||
77 | * |
||
78 | * @throws \Exception |
||
79 | */ |
||
80 | public function getRefund() |
||
81 | { |
||
82 | $response = $this->getResponse(); |
||
83 | if ($response instanceof Response) { |
||
0 ignored issues
–
show
|
|||
84 | $refund = new Order\Refund(); |
||
85 | $refund->import($this->getResponse()->body); |
||
86 | return $refund; |
||
87 | } |
||
88 | |||
89 | return false; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * prepareRequest |
||
94 | * |
||
95 | */ |
||
96 | protected function prepareRequest() |
||
97 | { |
||
98 | if (!$this->request instanceof Request) { |
||
0 ignored issues
–
show
|
|||
99 | $this->request = $this->getRequest() |
||
100 | ->method(Http::POST) |
||
101 | ->uri( |
||
102 | $this->apiConfiguration->getBaseUri() |
||
103 | . self::ENDPOINT |
||
104 | . self::SLASH |
||
105 | . $this->orderId |
||
106 | . self::SLASH |
||
107 | . self::REFUND_ENDPOINT |
||
108 | ) |
||
109 | ->sendsType(Mime::JSON) |
||
110 | ->body(json_encode($this->refund->export())) |
||
111 | ; |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 |