1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Rafał Muszyński <[email protected]> |
5
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.txt |
7
|
|
|
*/ |
8
|
|
|
namespace Newscoop\PaywallBundle\Services; |
9
|
|
|
|
10
|
|
|
use Newscoop\PaywallBundle\Entity\OrderInterface; |
11
|
|
|
use Newscoop\PaywallBundle\Adapter\GatewayAdapter; |
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
use Newscoop\PaywallBundle\Events\PaywallEvents; |
14
|
|
|
use Newscoop\EventDispatcher\Events\GenericEvent; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Purchase service. |
19
|
|
|
*/ |
20
|
|
|
class PurchaseService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var OrderService |
24
|
|
|
*/ |
25
|
|
|
protected $orderService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PaymentService |
29
|
|
|
*/ |
30
|
|
|
protected $paymentService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var GatewayAdapter |
34
|
|
|
*/ |
35
|
|
|
protected $adapter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EntityManager |
39
|
|
|
*/ |
40
|
|
|
protected $entityManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var EventDispatcherInterface |
44
|
|
|
*/ |
45
|
|
|
protected $dispatcher; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Construct. |
49
|
|
|
* |
50
|
|
|
* @param OrderService $orderService |
51
|
|
|
* @param PaymentService $paymentService |
52
|
|
|
* @param GatewayAdapter $adapter |
53
|
|
|
* @param EntityManager $entityManager |
54
|
|
|
* @param EventDispatcherInterface $dispatcher |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
OrderService $orderService, |
58
|
|
|
PaymentService $paymentService, |
59
|
|
|
GatewayAdapter $adapter, |
60
|
|
|
EntityManager $entityManager, |
61
|
|
|
EventDispatcherInterface $dispatcher |
62
|
|
|
) { |
63
|
|
|
$this->orderService = $orderService; |
64
|
|
|
$this->paymentService = $paymentService; |
65
|
|
|
$this->adapter = $adapter; |
66
|
|
|
$this->entityManager = $entityManager; |
67
|
|
|
$this->dispatcher = $dispatcher; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Starts the purchase process. |
72
|
|
|
* |
73
|
|
|
* @param array $items |
74
|
|
|
*/ |
75
|
|
|
public function startPurchase(array $items = array(), $currency = null) |
76
|
|
|
{ |
77
|
|
|
$order = $this->orderService->processAndCalculateOrderItems($items, $currency); |
78
|
|
|
if (!$order->getItems()->isEmpty()) { |
79
|
|
|
$response = $this->adapter->purchase($order); |
80
|
|
|
if (!$response) { |
81
|
|
|
$this->completePurchase($order); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $response; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Finishes purchase process. |
90
|
|
|
* |
91
|
|
|
* @param array $items |
92
|
|
|
* |
93
|
|
|
* @return OderInterface $order |
94
|
|
|
*/ |
95
|
|
|
public function finishPurchase(array $items = array()) |
96
|
|
|
{ |
97
|
|
|
$order = $this->orderService->processAndCalculateOrderItems($items); |
98
|
|
|
$response = $this->adapter->completePurchase($order); |
99
|
|
|
$this->completePurchase($order); |
100
|
|
|
|
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function completePurchase(OrderInterface $order) |
105
|
|
|
{ |
106
|
|
|
$this->paymentService->createPayment($order); |
107
|
|
|
$this->entityManager->persist($order); |
108
|
|
|
$this->activateOrderItems($order); |
109
|
|
|
$this->entityManager->flush(); |
110
|
|
|
$this->dispatcher->dispatch( |
111
|
|
|
PaywallEvents::ORDER_SUBSCRIPTION, |
112
|
|
|
new GenericEvent($order->getItems()->toArray()) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function activateOrderItems(OrderInterface $order) |
117
|
|
|
{ |
118
|
|
|
if ($this->adapter->isOfflineGateway()) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
foreach ($order->getItems() as $item) { |
123
|
|
|
$this->orderService->activateItem($item); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|