1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Plane\Shop; |
4
|
|
|
|
5
|
|
|
use Plane\Shop\PriceFormat\PriceFormatInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Payment class |
9
|
|
|
* |
10
|
|
|
* @author Dariusz Korsak <[email protected]> |
11
|
|
|
* @package Plane\Shop |
12
|
|
|
*/ |
13
|
|
|
class Payment implements PaymentInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Fee is a fixed price |
17
|
|
|
*/ |
18
|
|
|
const FEE_FIXED = 'fixed'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Fee is calculated as a percentage of total price |
22
|
|
|
*/ |
23
|
|
|
const FEE_PERCENTAGE = 'percentage'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Payment id |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Payment name |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Payment description |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $description; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Payment fee |
45
|
|
|
* @var int|float |
46
|
|
|
*/ |
47
|
|
|
private $fee; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Payment fee type (fee_fixed|fee_percentage) |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $feeType = self::FEE_FIXED; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Price format object |
57
|
|
|
* @var \Plane\Shop\PriceFormat\PriceFormatInterface |
58
|
|
|
*/ |
59
|
|
|
private $priceFormat; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor |
63
|
|
|
* @param array $data |
64
|
|
|
*/ |
65
|
3 |
|
public function __construct(array $data) |
66
|
|
|
{ |
67
|
3 |
|
foreach ($data as $k => $v) { |
68
|
3 |
|
$this->$k = $v; |
69
|
3 |
|
} |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return id |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
1 |
|
public function getId() |
77
|
|
|
{ |
78
|
1 |
|
return $this->id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return name |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getName() |
86
|
|
|
{ |
87
|
1 |
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return description |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
1 |
|
public function getDescription() |
95
|
|
|
{ |
96
|
1 |
|
return $this->description; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set fee as fixed price |
101
|
|
|
*/ |
102
|
1 |
|
public function setFixed() |
103
|
|
|
{ |
104
|
1 |
|
$this->feeType = self::FEE_FIXED; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set fee as percentage of total price |
109
|
|
|
*/ |
110
|
1 |
|
public function setPercentage() |
111
|
|
|
{ |
112
|
1 |
|
$this->feeType = self::FEE_PERCENTAGE; |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return fee |
117
|
|
|
* @param float $totalPrice |
118
|
|
|
* @return float |
119
|
|
|
*/ |
120
|
3 |
|
public function getFee($totalPrice) |
121
|
|
|
{ |
122
|
3 |
|
if (!is_null($this->priceFormat)) { |
123
|
1 |
|
return $this->priceFormat->formatPrice($this->calculateFee($totalPrice)); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $this->calculateFee($totalPrice); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set price format object |
131
|
|
|
* @param \Plane\Shop\PriceFormat\PriceFormatInterface $priceFormat |
132
|
|
|
*/ |
133
|
1 |
|
public function setPriceFormat(PriceFormatInterface $priceFormat) |
134
|
|
|
{ |
135
|
1 |
|
$this->priceFormat = $priceFormat; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Calculate fee based on feeType |
140
|
|
|
* @param double $totalPrice |
141
|
|
|
* @return float |
142
|
|
|
*/ |
143
|
3 |
|
protected function calculateFee($totalPrice) |
144
|
|
|
{ |
145
|
3 |
|
if ($this->feeType == self::FEE_PERCENTAGE) { |
146
|
1 |
|
return (float) $totalPrice * $this->fee; |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
return (float) $this->fee; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|