1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\cart; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\ResponseErrorException; |
14
|
|
|
use hiqdev\yii2\cart\ShoppingCart; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\base\BaseObject; |
17
|
|
|
|
18
|
|
|
class CartFinisher extends BaseObject |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ShoppingCart |
22
|
|
|
*/ |
23
|
|
|
public $cart; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var AbstractPurchase[] array of successful purchases |
27
|
|
|
*/ |
28
|
|
|
protected $_success = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ErrorPurchaseException[] array of failed purchases |
32
|
|
|
*/ |
33
|
|
|
protected $_error = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PendingPurchaseException[] array of purchases that are pending |
37
|
|
|
*/ |
38
|
|
|
protected $_pending = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Getter for array of successful purchases. |
42
|
|
|
* @return AbstractPurchase[] |
43
|
|
|
*/ |
44
|
|
|
public function getSuccess() |
45
|
|
|
{ |
46
|
|
|
return $this->_success; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Getter for array of failed purchases. |
51
|
|
|
* @return ErrorPurchaseException[] |
52
|
|
|
*/ |
53
|
|
|
public function getError() |
54
|
|
|
{ |
55
|
|
|
return $this->_error; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Getter for array of failed purchases. |
60
|
|
|
* @return PendingPurchaseException[] |
61
|
|
|
*/ |
62
|
|
|
public function getPending() |
63
|
|
|
{ |
64
|
|
|
return $this->_pending; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Runs the purchase. |
69
|
|
|
* Purchases the positions in the [[cart]]. |
70
|
|
|
*/ |
71
|
|
|
public function run() |
72
|
|
|
{ |
73
|
|
|
if ($this->cart->isEmpty) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->ensureCanBeFinished(); |
78
|
|
|
|
79
|
|
|
foreach ($this->cart->positions as $position) { |
80
|
|
|
$purchase = $position->getPurchaseModel(); |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
if ($purchase->execute()) { |
84
|
|
|
$this->_success[] = $purchase; |
85
|
|
|
$this->cart->remove($position); |
86
|
|
|
} else { |
87
|
|
|
$this->_error[] = new ErrorPurchaseException(reset(reset($purchase->getErrors())), $purchase); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} catch (PendingPurchaseException $e) { |
90
|
|
|
$this->_pending[] = $e; |
91
|
|
|
$this->cart->remove($position); |
92
|
|
|
} catch (ResponseErrorException $e) { |
93
|
|
|
$this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e); |
94
|
|
|
} catch (\hiqdev\hiart\Exception $e) { |
95
|
|
|
$this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function ensureCanBeFinished() |
101
|
|
|
{ |
102
|
|
|
/** @var PositionPurchasabilityValidatorInterface[] $validators */ |
103
|
|
|
$validators = []; |
104
|
|
|
|
105
|
|
|
foreach ($this->cart->positions as $position) { |
106
|
|
|
$purchase = $position->getPurchaseModel(); |
107
|
|
|
|
108
|
|
|
foreach ($purchase->getPurchasabilityRules() as $validator) { |
109
|
|
|
if (!isset($validators[$validator])) { |
110
|
|
|
$validators[$validator] = Yii::createObject($validator); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
foreach ($validators as $validator) { |
117
|
|
|
$validator->validate($this->cart->positions); |
118
|
|
|
} |
119
|
|
|
} catch (NotPurchasablePositionException $e) { |
120
|
|
|
$e->resolve(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|