1
|
|
|
<?php |
2
|
|
|
namespace PHPSC\PagSeguro\Purchases\Transactions; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use PHPSC\PagSeguro\Items\Item; |
6
|
|
|
use PHPSC\PagSeguro\Items\Items; |
7
|
|
|
use PHPSC\PagSeguro\Purchases\Decoder as BaseDecoder; |
8
|
|
|
use PHPSC\PagSeguro\Shipping\Shipping; |
9
|
|
|
use SimpleXMLElement; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Luís Otávio Cobucci Oblonczyk <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Decoder extends BaseDecoder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param SimpleXMLElement $obj |
18
|
|
|
* |
19
|
|
|
* @return Transaction |
20
|
|
|
*/ |
21
|
1 |
|
public function decode(SimpleXMLElement $obj) |
22
|
|
|
{ |
23
|
1 |
|
return new Transaction( |
24
|
1 |
|
$this->createDetails($obj), |
25
|
1 |
|
$this->createPayment($obj), |
26
|
1 |
|
(int) $obj->type, |
27
|
1 |
|
$this->createItems($obj->items), |
28
|
1 |
|
$this->createShipping($obj->shipping) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param SimpleXMLElement $obj |
34
|
|
|
* @return Payment |
35
|
|
|
*/ |
36
|
1 |
|
protected function createPayment(SimpleXMLElement $obj) |
37
|
|
|
{ |
38
|
1 |
|
return new Payment( |
39
|
1 |
|
new PaymentMethod( |
40
|
1 |
|
(int) $obj->paymentMethod->type, |
41
|
1 |
|
(int) $obj->paymentMethod->code |
42
|
|
|
), |
43
|
1 |
|
(float) $obj->grossAmount, |
44
|
1 |
|
(float) $obj->discountAmount, |
45
|
1 |
|
(float) $obj->feeAmount, |
46
|
1 |
|
(float) $obj->netAmount, |
47
|
1 |
|
(float) $obj->extraAmount, |
48
|
1 |
|
(int) $obj->installmentCount, |
49
|
1 |
|
isset($obj->escrowEndDate) ? new DateTime((string) $obj->escrowEndDate) : null |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param SimpleXMLElement $itemsNode |
55
|
|
|
* |
56
|
|
|
* @return Items |
57
|
|
|
*/ |
58
|
1 |
|
protected function createItems(SimpleXMLElement $itemsNode) |
59
|
|
|
{ |
60
|
1 |
|
$items = new Items(); |
61
|
|
|
|
62
|
1 |
|
foreach ($itemsNode->item as $item) { |
63
|
1 |
|
$items->add( |
64
|
1 |
|
new Item( |
65
|
1 |
|
(string) $item->id, |
66
|
1 |
|
(string) $item->description, |
67
|
1 |
|
(float) $item->amount, |
68
|
1 |
|
(int) $item->quantity, |
69
|
1 |
|
isset($item->shippingCost) ? (float) $item->shippingCost : null, |
70
|
1 |
|
isset($item->weight) ? (int) $item->weight : null |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $items; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param SimpleXMLElement $shipping |
80
|
|
|
* @return Shipping |
81
|
|
|
*/ |
82
|
1 |
|
protected function createShipping(SimpleXMLElement $shipping) |
83
|
|
|
{ |
84
|
1 |
|
return new Shipping( |
85
|
1 |
|
(int) $shipping->type, |
86
|
1 |
|
isset($shipping->address) ? $this->createAddress($shipping->address) : null, |
87
|
1 |
|
isset($shipping->cost) ? (float) $shipping->cost : null |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|