1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\BillPay\Message\RequestData; |
4
|
|
|
|
5
|
|
|
use Omnipay\BillPay\Item; |
6
|
|
|
use Omnipay\BillPay\Message\AuthorizeRequest; |
7
|
|
|
use Omnipay\Common\Exception\InvalidRequestException; |
8
|
|
|
use Omnipay\Common\ItemBag; |
9
|
|
|
use Omnipay\Common\Message\AbstractRequest; |
10
|
|
|
use SimpleXMLElement; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TotalTrait |
14
|
|
|
* |
15
|
|
|
* @author Andreas Lange <[email protected]> |
16
|
|
|
* @copyright 2016, Connox GmbH |
17
|
|
|
* @license MIT |
18
|
|
|
*/ |
19
|
|
|
trait TotalTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Validates and returns the formated amount. |
23
|
|
|
* |
24
|
|
|
* @return string The amount formatted to the correct number of decimal places for the selected currency. |
25
|
|
|
* |
26
|
|
|
* @codeCoverageIgnore |
27
|
|
|
*/ |
28
|
|
|
abstract public function getAmount(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the payment currency code. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
* |
35
|
|
|
* @codeCoverageIgnore |
36
|
|
|
*/ |
37
|
|
|
abstract public function getCurrency(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A list of items in this order |
41
|
|
|
* |
42
|
|
|
* @return ItemBag|null A bag containing items in this order |
43
|
|
|
* |
44
|
|
|
* @codeCoverageIgnore |
45
|
|
|
*/ |
46
|
|
|
abstract public function getItems(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the net rebate amount for the order |
50
|
|
|
* |
51
|
|
|
* @return string|null |
52
|
|
|
*/ |
53
|
10 |
|
public function getRebate() |
54
|
|
|
{ |
55
|
10 |
|
return $this->getParameter('rebate'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the gross rebate amount for the order |
60
|
|
|
* |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
10 |
|
public function getRebateGross() |
64
|
|
|
{ |
65
|
10 |
|
return $this->getParameter('rebateGross'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the shipping method name |
70
|
|
|
* |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
9 |
|
public function getShippingName() |
74
|
|
|
{ |
75
|
9 |
|
return $this->getParameter('shippingName'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets the net amount of shipping cost for the order |
80
|
|
|
* |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
10 |
|
public function getShippingPrice() |
84
|
|
|
{ |
85
|
10 |
|
return $this->getParameter('shippingPrice'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the gross amount of shipping cost for the order |
90
|
|
|
* |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
10 |
|
public function getShippingPriceGross() |
94
|
|
|
{ |
95
|
10 |
|
return $this->getParameter('shippingPriceGross'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the transaction ID. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
* |
103
|
|
|
* @codeCoverageIgnore |
104
|
|
|
*/ |
105
|
|
|
abstract public function getTransactionId(); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets net rebate amount for the order |
109
|
|
|
* |
110
|
|
|
* @param float $rebate net rebate amount |
111
|
|
|
* |
112
|
|
|
* @return AuthorizeRequest |
113
|
|
|
*/ |
114
|
11 |
|
public function setRebate($rebate) |
115
|
|
|
{ |
116
|
11 |
|
return $this->setParameter('rebate', $rebate); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets gross rebate amount for the order |
121
|
|
|
* |
122
|
|
|
* @param string $rebateGross gross rebate amount |
123
|
|
|
* |
124
|
|
|
* @return AuthorizeRequest |
125
|
|
|
*/ |
126
|
11 |
|
public function setRebateGross($rebateGross) |
127
|
|
|
{ |
128
|
11 |
|
return $this->setParameter('rebateGross', $rebateGross); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Sets shipping method name (e.g. "Express") |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
* |
136
|
|
|
* @return AuthorizeRequest |
137
|
|
|
*/ |
138
|
11 |
|
public function setShippingName($name) |
139
|
|
|
{ |
140
|
11 |
|
return $this->setParameter('shippingName', $name); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets net amount of shipping cost for the order |
145
|
|
|
* |
146
|
|
|
* @param float $price net amount of shipping cost |
147
|
|
|
* |
148
|
|
|
* @return AuthorizeRequest |
149
|
|
|
*/ |
150
|
11 |
|
public function setShippingPrice($price) |
151
|
|
|
{ |
152
|
11 |
|
return $this->setParameter('shippingPrice', $price); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Sets gross amount of shipping cost for the order |
157
|
|
|
* |
158
|
|
|
* @param string $priceGross gross amount of shipping cost |
159
|
|
|
* |
160
|
|
|
* @return AuthorizeRequest |
161
|
|
|
*/ |
162
|
11 |
|
public function setShippingPriceGross($priceGross) |
163
|
|
|
{ |
164
|
11 |
|
return $this->setParameter('shippingPriceGross', $priceGross); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param SimpleXMLElement $data |
169
|
|
|
* |
170
|
|
|
* @throws InvalidRequestException |
171
|
|
|
*/ |
172
|
10 |
|
protected function appendTotal(SimpleXMLElement $data) |
173
|
|
|
{ |
174
|
10 |
|
$this->checkAmountDifference(); |
175
|
|
|
|
176
|
9 |
|
list($totalNet, $totalGross) = $this->calculateTotalAmounts(); |
177
|
|
|
|
178
|
9 |
|
$data->addChild('total'); |
179
|
9 |
|
$data->total[0]['shippingname'] = $this->getShippingName(); |
180
|
9 |
|
$data->total[0]['shippingprice'] = round(bcmul($this->getShippingPrice(), 100, 8)); |
181
|
9 |
|
$data->total[0]['shippingpricegross'] = round(bcmul($this->getShippingPriceGross(), 100, 8)); |
182
|
9 |
|
$data->total[0]['rebate'] = round(bcmul($this->getRebate(), 100, 8)); |
183
|
9 |
|
$data->total[0]['rebategross'] = round(bcmul($this->getRebateGross(), 100, 8)); |
184
|
9 |
|
$data->total[0]['carttotalprice'] = round(bcmul($totalNet, 100, 8)); |
185
|
9 |
|
$data->total[0]['carttotalpricegross'] = round(bcmul($totalGross, 100, 8)); |
186
|
9 |
|
$data->total[0]['currency'] = $this->getCurrency(); |
187
|
9 |
|
$data->total[0]['reference'] = $this->getTransactionId(); |
188
|
9 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @throws InvalidRequestException |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
10 |
|
protected function calculateTotalAmounts() |
196
|
|
|
{ |
197
|
10 |
|
$totalNet = 0.0; |
198
|
10 |
|
$totalGross = 0.0; |
199
|
|
|
|
200
|
10 |
|
foreach ($this->getItems()->all() as $pos => $item) { |
201
|
|
|
/* @var Item $item */ |
202
|
10 |
|
$totalNet = bcadd($totalNet, bcmul($item->getPriceNet(), $item->getQuantity(), 8), 8); |
203
|
10 |
|
$totalGross = bcadd($totalGross, bcmul($item->getPrice(), $item->getQuantity(), 8), 8); |
204
|
10 |
|
} |
205
|
|
|
|
206
|
|
|
// add shipping |
207
|
10 |
|
$totalNet = bcadd($totalNet, $this->getShippingPrice(), 8); |
208
|
10 |
|
$totalGross = bcadd($totalGross, $this->getShippingPriceGross(), 8); |
209
|
|
|
|
210
|
|
|
// remove rebates |
211
|
10 |
|
$totalNet = bcsub($totalNet, $this->getRebate(), 8); |
212
|
10 |
|
$totalGross = bcsub($totalGross, $this->getRebateGross(), 8); |
213
|
|
|
|
214
|
10 |
|
return [$totalNet, $totalGross]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get a single parameter. |
219
|
|
|
* |
220
|
|
|
* @param string $key The parameter key |
221
|
|
|
* |
222
|
|
|
* @return mixed |
223
|
|
|
* |
224
|
|
|
* @codeCoverageIgnore |
225
|
|
|
*/ |
226
|
|
|
abstract protected function getParameter($key); |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set a single parameter |
230
|
|
|
* |
231
|
|
|
* @param string $key The parameter key |
232
|
|
|
* @param mixed $value The value to set |
233
|
|
|
* |
234
|
|
|
* @return AbstractRequest Provides a fluent interface |
235
|
|
|
* |
236
|
|
|
* @codeCoverageIgnore |
237
|
|
|
*/ |
238
|
|
|
abstract protected function setParameter($key, $value); |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @throws InvalidRequestException |
242
|
|
|
*/ |
243
|
10 |
|
private function checkAmountDifference() |
244
|
|
|
{ |
245
|
10 |
|
$totalGross = $this->calculateTotalAmounts()[1]; |
246
|
|
|
|
247
|
10 |
|
if (bccomp($totalGross, $this->getAmount(), 8) !== 0) { |
248
|
1 |
|
throw new InvalidRequestException( |
249
|
1 |
|
sprintf( |
250
|
1 |
|
'Amount (%0.2f) differs from calculated amount (%0.2f) (items + shipping - rebate).', |
251
|
1 |
|
$totalGross, |
252
|
1 |
|
$this->getAmount() |
253
|
1 |
|
) |
254
|
1 |
|
); |
255
|
|
|
} |
256
|
9 |
|
} |
257
|
|
|
} |
258
|
|
|
|