|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* API for Billing |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/billing-hiapi |
|
6
|
|
|
* @package billing-hiapi |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\charge; |
|
12
|
|
|
|
|
13
|
|
|
use hiapi\db\CallExpression; |
|
14
|
|
|
use hiapi\db\HstoreExpression; |
|
15
|
|
|
use hiapi\components\ConnectionInterface; |
|
16
|
|
|
use hiapi\components\EntityManagerInterface; |
|
17
|
|
|
use hiapi\query\Specification; |
|
18
|
|
|
use hiapi\repositories\BaseRepository; |
|
19
|
|
|
use hiqdev\php\billing\charge\Charge; |
|
20
|
|
|
use hiqdev\php\billing\charge\ChargeFactoryInterface; |
|
21
|
|
|
use hiqdev\php\billing\sale\Sale; |
|
22
|
|
|
|
|
23
|
|
|
class ChargeRepository extends BaseRepository |
|
24
|
|
|
{ |
|
25
|
|
|
public $queryClass = ChargeQuery::class; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ChargeFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $factory; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
ConnectionInterface $db, |
|
34
|
|
|
EntityManagerInterface $em, |
|
35
|
|
|
ChargeFactoryInterface $factory, |
|
36
|
|
|
array $config = [] |
|
37
|
|
|
) { |
|
38
|
|
|
parent::__construct($config); |
|
39
|
|
|
|
|
40
|
|
|
$this->db = $db; |
|
41
|
|
|
$this->em = $em; |
|
|
|
|
|
|
42
|
|
|
$this->factory = $factory; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function save(Charge $charge) |
|
46
|
|
|
{ |
|
47
|
|
|
$action = $charge->getAction(); |
|
48
|
|
|
$sale = new Sale(null, $action->getTarget(), $action->getCustomer(), $charge->getPrice()->getPlan()); |
|
49
|
|
|
$action->setSale($sale); |
|
50
|
|
|
$this->em->save($action); |
|
|
|
|
|
|
51
|
|
|
$hstore = new HstoreExpression(array_filter([ |
|
52
|
|
|
'id' => $charge->getId(), |
|
53
|
|
|
'object_id' => $charge->getTarget()->getId(), |
|
54
|
|
|
'tariff_id' => $sale->getPlan()->getId(), |
|
55
|
|
|
'action_id' => $action->getId(), |
|
56
|
|
|
'type_id' => $action->getType()->getId(), |
|
57
|
|
|
'type' => $action->getType()->getName(), |
|
58
|
|
|
'buyer_id' => $action->getCustomer()->getId(), |
|
59
|
|
|
'buyer' => $action->getCustomer()->getLogin(), |
|
60
|
|
|
'currency' => $charge->getSum()->getCurrency()->getCode(), |
|
61
|
|
|
'sum' => $charge->getSum()->getAmount(), |
|
62
|
|
|
'quantity' => $charge->getUsage()->getQuantity(), |
|
63
|
|
|
'sale_id' => $sale ? $this->em->findId($sale) : null, |
|
|
|
|
|
|
64
|
|
|
'time' => $charge->getTime(), |
|
65
|
|
|
])); |
|
66
|
|
|
$call = new CallExpression('set_charge', [$hstore]); |
|
67
|
|
|
$command = $this->em->getConnection()->createSelect($call); |
|
|
|
|
|
|
68
|
|
|
$charge->setId($command->scalar()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.